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

mybatis / generator / 1942

12 Jan 2026 05:01PM UTC coverage: 88.75% (+0.4%) from 88.365%
1942

push

github

web-flow
Merge pull request #1412 from jeffgbutler/jspecify

Adopt JSpecify

2331 of 3162 branches covered (73.72%)

1800 of 1949 new or added lines in 202 files covered. (92.36%)

18 existing lines in 10 files now uncovered.

11384 of 12827 relevant lines covered (88.75%)

0.89 hits per line

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

34.62
/core/mybatis-generator-core/src/main/java/org/mybatis/generator/codegen/RootClassInfo.java
1
/*
2
 *    Copyright 2006-2026 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
51
     * a generation run to clear the cached root class info in case there has been a change.
52
     * For example, when using the eclipse launcher, the cache would be kept until eclipse
53
     * was restarted.
54
     *
55
     */
56
    public static void reset() {
57
        rootClassInfoMap.clear();
1✔
58
    }
1✔
59

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

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

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

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

86
    public boolean containsProperty(IntrospectedColumn introspectedColumn) {
87
        if (propertyDescriptors.length == 0) {
1!
88
            return false;
1✔
89
        }
90

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

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

105
        return found;
×
106
    }
107

108
    private boolean hasProperty(String propertyName, String propertyType, PropertyDescriptor propertyDescriptor) {
109
        return hasCorrectName(propertyName, propertyDescriptor)
×
110
                && isProperType(propertyName, propertyType, propertyDescriptor)
×
111
                && hasGetter(propertyName, propertyDescriptor)
×
112
                && 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