• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / generator / 1646

21 Apr 2025 10:17PM UTC coverage: 88.157% (-0.2%) from 88.328%
1646

push

github

hazendaz
[ci] Run auto formatting

2518 of 3412 branches covered (73.8%)

994 of 1117 new or added lines in 164 files covered. (88.99%)

23 existing lines in 12 files now uncovered.

10578 of 11999 relevant lines covered (88.16%)

0.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.46
/core/mybatis-generator-core/src/main/java/org/mybatis/generator/codegen/RootClassInfo.java
1
/*
2
 *    Copyright 2006-2025 the original author or authors.
3
 *
4
 *    Licensed under the Apache License, Version 2.0 (the "License");
5
 *    you may not use this file except in compliance with the License.
6
 *    You may obtain a copy of the License at
7
 *
8
 *       https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *    Unless required by applicable law or agreed to in writing, software
11
 *    distributed under the License is distributed on an "AS IS" BASIS,
12
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *    See the License for the specific language governing permissions and
14
 *    limitations under the License.
15
 */
16
package org.mybatis.generator.codegen;
17

18
import static org.mybatis.generator.internal.util.messages.Messages.getString;
19

20
import java.beans.BeanInfo;
21
import java.beans.Introspector;
22
import java.beans.PropertyDescriptor;
23
import java.util.Collections;
24
import java.util.HashMap;
25
import java.util.List;
26
import java.util.Map;
27

28
import org.mybatis.generator.api.IntrospectedColumn;
29
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
30
import org.mybatis.generator.internal.ObjectFactory;
31

32
/**
33
 * Holds information about a class (uses the JavaBeans Introspector to find properties).
34
 *
35
 * @author Jeff Butler
36
 */
37
public class RootClassInfo {
38

39
    private static final Map<String, RootClassInfo> rootClassInfoMap;
40

41
    static {
42
        rootClassInfoMap = Collections.synchronizedMap(new HashMap<>());
1✔
43
    }
1✔
44

45
    public static RootClassInfo getInstance(String className, List<String> warnings) {
46
        return rootClassInfoMap.computeIfAbsent(className, k -> new RootClassInfo(k, warnings));
1✔
47
    }
48

49
    /**
50
     * Clears the internal map containing root class info. This method should be called at the beginning of a generation
51
     * run to clear the cached root class info in case there has been a change. For example, when using the eclipse
52
     * launcher, the cache would be kept until eclipse was restarted.
53
     */
54
    public static void reset() {
55
        rootClassInfoMap.clear();
1✔
56
    }
1✔
57

58
    private PropertyDescriptor[] propertyDescriptors;
59
    private final String className;
60
    private final List<String> warnings;
61
    private boolean genericMode = false;
1✔
62

63
    private RootClassInfo(String className, List<String> warnings) {
64
        super();
1✔
65
        this.className = className;
1✔
66
        this.warnings = warnings;
1✔
67

68
        if (className == null) {
1✔
69
            return;
1✔
70
        }
71

72
        FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(className);
1✔
73
        String nameWithoutGenerics = fqjt.getFullyQualifiedNameWithoutTypeParameters();
1✔
74
        if (!nameWithoutGenerics.equals(className)) {
1!
75
            genericMode = true;
×
76
        }
77

78
        try {
79
            Class<?> clazz = ObjectFactory.externalClassForName(nameWithoutGenerics);
×
80
            BeanInfo bi = Introspector.getBeanInfo(clazz);
×
81
            propertyDescriptors = bi.getPropertyDescriptors();
×
82
        } catch (Exception e) {
1✔
83
            propertyDescriptors = null;
1✔
84
            warnings.add(getString("Warning.20", className)); //$NON-NLS-1$
1✔
85
        }
×
86
    }
1✔
87

88
    public boolean containsProperty(IntrospectedColumn introspectedColumn) {
89
        if (propertyDescriptors == null) {
1!
90
            return false;
1✔
91
        }
92

93
        boolean found = false;
×
94
        String propertyName = introspectedColumn.getJavaProperty();
×
NEW
95
        String propertyType = introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName();
×
96

97
        // get method names from class and check against this column definition.
98
        // better yet, have a map of method Names. check against it.
99
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
×
100
            if (hasProperty(propertyName, propertyType, propertyDescriptor)) {
×
101
                found = true;
×
102
                break;
×
103
            }
104
        }
105

106
        return found;
×
107
    }
108

109
    private boolean hasProperty(String propertyName, String propertyType, PropertyDescriptor propertyDescriptor) {
110
        return hasCorrectName(propertyName, propertyDescriptor)
×
111
                && isProperType(propertyName, propertyType, propertyDescriptor)
×
NEW
112
                && hasGetter(propertyName, propertyDescriptor) && hasSetter(propertyName, propertyDescriptor);
×
113
    }
114

115
    private boolean hasCorrectName(String propertyName, PropertyDescriptor propertyDescriptor) {
116
        return propertyDescriptor.getName().equals(propertyName);
×
117
    }
118

119
    private boolean isProperType(String propertyName, String propertyType, PropertyDescriptor propertyDescriptor) {
120
        String introspectedPropertyType = propertyDescriptor.getPropertyType().getName();
×
121
        if (genericMode && introspectedPropertyType.equals("java.lang.Object")) { //$NON-NLS-1$
×
122
            // OK - but add a warning
123
            warnings.add(getString("Warning.28", propertyName, className)); //$NON-NLS-1$
×
124
        } else if (!introspectedPropertyType.equals(propertyType)) {
×
125
            warnings.add(getString("Warning.21", propertyName, className, propertyType)); //$NON-NLS-1$
×
126
            return false;
×
127
        }
128

129
        return true;
×
130
    }
131

132
    private boolean hasGetter(String propertyName, PropertyDescriptor propertyDescriptor) {
133
        if (propertyDescriptor.getReadMethod() == null) {
×
134
            warnings.add(getString("Warning.22", propertyName, className)); //$NON-NLS-1$
×
135
            return false;
×
136
        }
137

138
        return true;
×
139
    }
140

141
    private boolean hasSetter(String propertyName, PropertyDescriptor propertyDescriptor) {
142
        if (propertyDescriptor.getWriteMethod() == null) {
×
143
            warnings.add(getString("Warning.23", propertyName, className)); //$NON-NLS-1$
×
144
            return false;
×
145
        }
146

147
        return true;
×
148
    }
149
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc