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

mybatis / generator / 2004

01 Feb 2026 02:52AM UTC coverage: 89.808% (-0.002%) from 89.81%
2004

push

github

web-flow
Merge pull request #1434 from jeffgbutler/sonar-nags

Fix a Few Sonar Nags

2269 of 3051 branches covered (74.37%)

24 of 26 new or added lines in 7 files covered. (92.31%)

11552 of 12863 relevant lines covered (89.81%)

0.9 hits per line

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

77.53
/core/mybatis-generator-core/src/main/java/org/mybatis/generator/internal/ObjectFactory.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.internal;
17

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

21
import java.net.URL;
22
import java.util.ArrayList;
23
import java.util.List;
24
import java.util.Optional;
25

26
import org.mybatis.generator.api.CommentGenerator;
27
import org.mybatis.generator.api.ConnectionFactory;
28
import org.mybatis.generator.api.IntrospectedColumn;
29
import org.mybatis.generator.api.JavaFormatter;
30
import org.mybatis.generator.api.JavaTypeResolver;
31
import org.mybatis.generator.api.KotlinFormatter;
32
import org.mybatis.generator.api.Plugin;
33
import org.mybatis.generator.api.XmlFormatter;
34
import org.mybatis.generator.config.CommentGeneratorConfiguration;
35
import org.mybatis.generator.config.ConnectionFactoryConfiguration;
36
import org.mybatis.generator.config.Context;
37
import org.mybatis.generator.config.Defaults;
38
import org.mybatis.generator.config.JavaTypeResolverConfiguration;
39
import org.mybatis.generator.config.PluginConfiguration;
40
import org.mybatis.generator.config.PropertyRegistry;
41
import org.mybatis.generator.exception.InternalException;
42

43
/**
44
 * This class creates the different objects needed by the generator.
45
 *
46
 * @author Jeff Butler
47
 */
48
public class ObjectFactory {
49

50
    private static final List<ClassLoader> externalClassLoaders;
51

52
    static {
53
        externalClassLoaders = new ArrayList<>();
1✔
54
    }
1✔
55

56
    /**
57
     * Utility class. No instances allowed.
58
     */
59
    private ObjectFactory() {
60
        super();
61
    }
62

63
    /**
64
     * Clears the class loaders.  This method should be called at the beginning of
65
     * a generation run so that and change to the classloading configuration
66
     * will be reflected.  For example, if the eclipse launcher changes configuration
67
     * it might not be updated if eclipse hasn't been restarted.
68
     *
69
     */
70
    public static void reset() {
71
        externalClassLoaders.clear();
1✔
72
    }
1✔
73

74
    /**
75
     * Adds a custom classloader to the collection of classloaders searched for "external" classes. These are classes
76
     * that do not depend on any of the generator's classes or interfaces. Examples are JDBC drivers, root classes, root
77
     * interfaces, etc.
78
     *
79
     * @param classLoader
80
     *            the class loader
81
     */
82
    public static synchronized void addExternalClassLoader(ClassLoader classLoader) {
83
        ObjectFactory.externalClassLoaders.add(classLoader);
×
84
    }
×
85

86
    /**
87
     * Returns a class loaded from the context classloader, or the classloader supplied by a client. This is
88
     * appropriate for JDBC drivers, model root classes, etc. It is not appropriate for any class that extends one of
89
     * the supplied classes or interfaces.
90
     *
91
     * @param type
92
     *            the type
93
     * @return the Class loaded from the external classloader
94
     * @throws ClassNotFoundException
95
     *             the class not found exception
96
     */
97
    @SuppressWarnings("unchecked")
98
    public static <T> Class<T> externalClassForName(String type) throws ClassNotFoundException {
99
        Class<T> clazz;
100

101
        for (ClassLoader classLoader : externalClassLoaders) {
1!
102
            try {
103
                clazz = (Class<T>) Class.forName(type, true, classLoader);
×
104
                return clazz;
×
105
            } catch (Exception e) {
×
106
                // ignore - fail safe below
107
            }
108
        }
×
109

110
        return internalClassForName(type);
1✔
111
    }
112

113
    public static <T> T createExternalObject(String type) {
114
        T answer;
115

116
        try {
NEW
117
            Class<T> clazz = externalClassForName(type);
×
118
            answer = clazz.getConstructor().newInstance();
×
119
        } catch (Exception e) {
×
120
            throw new InternalException(getString("RuntimeError.6", type), e); //$NON-NLS-1$
×
121
        }
×
122

123
        return answer;
×
124
    }
125

126
    @SuppressWarnings("unchecked")
127
    public static <T> Class<T> internalClassForName(String type) throws ClassNotFoundException {
128
        Class<?> clazz = null;
1✔
129

130
        try {
131
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
1✔
132
            clazz = Class.forName(type, true, cl);
1✔
133
        } catch (Exception e) {
1✔
134
            // ignore - failsafe below
135
        }
1✔
136

137
        if (clazz == null) {
1✔
138
            clazz = Class.forName(type, true, ObjectFactory.class.getClassLoader());
×
139
        }
140

141
        return (Class<T>) clazz;
1✔
142
    }
143

144
    public static Optional<URL> getResource(String resource) {
145
        URL url;
146

147
        for (ClassLoader classLoader : externalClassLoaders) {
1!
148
            url = classLoader.getResource(resource);
×
149
            if (url != null) {
×
150
                return Optional.of(url);
×
151
            }
152
        }
×
153

154
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
1✔
155
        url = cl.getResource(resource);
1✔
156

157
        if (url == null) {
1!
158
            url = ObjectFactory.class.getClassLoader().getResource(resource);
×
159
        }
160

161
        return Optional.ofNullable(url);
1✔
162
    }
163

164
    public static <T> T createInternalObject(String type) {
165
        T answer;
166

167
        try {
168
            Class<T> clazz = internalClassForName(type);
1✔
169
            answer = clazz.getConstructor().newInstance();
1✔
170
        } catch (Exception e) {
×
171
            throw new InternalException(getString("RuntimeError.6", type), e); //$NON-NLS-1$
×
172
        }
1✔
173

174
        return answer;
1✔
175
    }
176

177
    public static JavaTypeResolver createJavaTypeResolver(Context context, List<String> warnings) {
178
        String type = context.getJavaTypeResolverConfiguration()
1✔
179
                .map(JavaTypeResolverConfiguration::getImplementationType)
1✔
180
                .orElse(Defaults.DEFAULT_JAVA_TYPE_RESOLVER);
1✔
181

182
        JavaTypeResolver answer = createInternalObject(type);
1✔
183
        answer.setWarnings(warnings);
1✔
184

185
        context.getJavaTypeResolverConfiguration()
1✔
186
                .ifPresent(c -> answer.addConfigurationProperties(c.getProperties()));
1✔
187

188
        answer.setContext(context);
1✔
189

190
        return answer;
1✔
191
    }
192

193
    public static Plugin createPlugin(Context context, PluginConfiguration pluginConfiguration,
194
                                      CommentGenerator commentGenerator) {
195
        Plugin plugin = createInternalObject(pluginConfiguration.getConfigurationType().orElseThrow());
1✔
196
        plugin.setContext(context);
1✔
197
        plugin.setProperties(pluginConfiguration.getProperties());
1✔
198
        plugin.setCommentGenerator(commentGenerator);
1✔
199
        return plugin;
1✔
200
    }
201

202
    public static CommentGenerator createCommentGenerator(Context context) {
203
        CommentGenerator answer;
204

205
        String type = context.getCommentGeneratorConfiguration()
1✔
206
                .map(CommentGeneratorConfiguration::getImplementationType)
1✔
207
                .orElse(Defaults.DEFAULT_COMMENT_GENERATOR);
1✔
208

209
        answer = createInternalObject(type);
1✔
210

211
        context.getCommentGeneratorConfiguration()
1✔
212
                .ifPresent(c -> answer.addConfigurationProperties(c.getProperties()));
1✔
213

214
        return answer;
1✔
215
    }
216

217
    public static ConnectionFactory createConnectionFactory(ConnectionFactoryConfiguration config) {
218
        ConnectionFactory answer;
219

220
        String type = config.getImplementationType();
1✔
221

222
        answer = createInternalObject(type);
1✔
223
        answer.addConfigurationProperties(config.getProperties());
1✔
224

225
        return answer;
1✔
226
    }
227

228
    public static JavaFormatter createJavaFormatter(Context context) {
229
        String type = context.getProperty(PropertyRegistry.CONTEXT_JAVA_FORMATTER);
1✔
230
        if (!stringHasValue(type)) {
1!
231
            type = Defaults.DEFAULT_JAVA_FORMATTER;
1✔
232
        }
233

234
        JavaFormatter answer = createInternalObject(type);
1✔
235

236
        answer.setContext(context);
1✔
237

238
        return answer;
1✔
239
    }
240

241
    public static KotlinFormatter createKotlinFormatter(Context context) {
242
        String type = context.getProperty(PropertyRegistry.CONTEXT_KOTLIN_FORMATTER);
1✔
243
        if (!stringHasValue(type)) {
1!
244
            type = Defaults.DEFAULT_KOTLIN_FORMATTER;
1✔
245
        }
246

247
        KotlinFormatter answer = createInternalObject(type);
1✔
248

249
        answer.setContext(context);
1✔
250

251
        return answer;
1✔
252
    }
253

254
    public static XmlFormatter createXmlFormatter(Context context) {
255
        String type = context.getProperty(PropertyRegistry.CONTEXT_XML_FORMATTER);
1✔
256
        if (!stringHasValue(type)) {
1!
257
            type = Defaults.DEFAULT_XML_FORMATTER;
1✔
258
        }
259

260
        XmlFormatter answer = createInternalObject(type);
1✔
261

262
        answer.setContext(context);
1✔
263

264
        return answer;
1✔
265
    }
266

267
    public static IntrospectedColumn createIntrospectedColumn(Context context) {
268
        String type = context.getIntrospectedColumnImpl().orElse(IntrospectedColumn.class.getName());
1✔
269
        IntrospectedColumn answer = createInternalObject(type);
1✔
270
        answer.setContext(context);
1✔
271

272
        return answer;
1✔
273
    }
274
}
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