• 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

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

25
import org.mybatis.generator.api.CommentGenerator;
26
import org.mybatis.generator.api.ConnectionFactory;
27
import org.mybatis.generator.api.FullyQualifiedTable;
28
import org.mybatis.generator.api.IntrospectedColumn;
29
import org.mybatis.generator.api.IntrospectedTable;
30
import org.mybatis.generator.api.JavaFormatter;
31
import org.mybatis.generator.api.JavaTypeResolver;
32
import org.mybatis.generator.api.KotlinFormatter;
33
import org.mybatis.generator.api.Plugin;
34
import org.mybatis.generator.api.XmlFormatter;
35
import org.mybatis.generator.api.dom.DefaultJavaFormatter;
36
import org.mybatis.generator.api.dom.DefaultKotlinFormatter;
37
import org.mybatis.generator.api.dom.DefaultXmlFormatter;
38
import org.mybatis.generator.codegen.mybatis3.IntrospectedTableMyBatis3Impl;
39
import org.mybatis.generator.codegen.mybatis3.IntrospectedTableMyBatis3SimpleImpl;
40
import org.mybatis.generator.config.CommentGeneratorConfiguration;
41
import org.mybatis.generator.config.ConnectionFactoryConfiguration;
42
import org.mybatis.generator.config.Context;
43
import org.mybatis.generator.config.JavaTypeResolverConfiguration;
44
import org.mybatis.generator.config.PluginConfiguration;
45
import org.mybatis.generator.config.PropertyRegistry;
46
import org.mybatis.generator.config.TableConfiguration;
47
import org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl;
48
import org.mybatis.generator.runtime.dynamic.sql.IntrospectedTableMyBatis3DynamicSqlImpl;
49
import org.mybatis.generator.runtime.kotlin.IntrospectedTableKotlinImpl;
50

51
/**
52
 * This class creates the different objects needed by the generator.
53
 *
54
 * @author Jeff Butler
55
 */
56
public class ObjectFactory {
57

58
    private static final List<ClassLoader> externalClassLoaders;
59

60
    static {
61
        externalClassLoaders = new ArrayList<>();
1✔
62
    }
1✔
63

64
    /**
65
     * Utility class. No instances allowed.
66
     */
67
    private ObjectFactory() {
68
        super();
69
    }
70

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

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

92
    /**
93
     * Returns a class loaded from the context classloader, or the classloader supplied by a client. This is appropriate
94
     * for JDBC drivers, model root classes, etc. It is not appropriate for any class that extends one of the supplied
95
     * classes or interfaces.
96
     *
97
     * @param type
98
     *            the type
99
     *
100
     * @return the Class loaded from the external classloader
101
     *
102
     * @throws ClassNotFoundException
103
     *             the class not found exception
104
     */
105
    public static Class<?> externalClassForName(String type) throws ClassNotFoundException {
106

107
        Class<?> clazz;
108

109
        for (ClassLoader classLoader : externalClassLoaders) {
1!
110
            try {
111
                clazz = Class.forName(type, true, classLoader);
×
112
                return clazz;
×
113
            } catch (Exception e) {
×
114
                // ignore - fail safe below
115
            }
116
        }
×
117

118
        return internalClassForName(type);
1✔
119
    }
120

121
    public static Object createExternalObject(String type) {
122
        Object answer;
123

124
        try {
125
            Class<?> clazz = externalClassForName(type);
×
126
            answer = clazz.getConstructor().newInstance();
×
127
        } catch (Exception e) {
×
NEW
128
            throw new RuntimeException(getString("RuntimeError.6", type), e); //$NON-NLS-1$
×
UNCOV
129
        }
×
130

131
        return answer;
×
132
    }
133

134
    public static Class<?> internalClassForName(String type) throws ClassNotFoundException {
135
        Class<?> clazz = null;
1✔
136

137
        try {
138
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
1✔
139
            clazz = Class.forName(type, true, cl);
1✔
140
        } catch (Exception e) {
1✔
141
            // ignore - failsafe below
142
        }
1✔
143

144
        if (clazz == null) {
1✔
145
            clazz = Class.forName(type, true, ObjectFactory.class.getClassLoader());
×
146
        }
147

148
        return clazz;
1✔
149
    }
150

151
    public static URL getResource(String resource) {
152
        URL url;
153

154
        for (ClassLoader classLoader : externalClassLoaders) {
1!
155
            url = classLoader.getResource(resource);
×
156
            if (url != null) {
×
157
                return url;
×
158
            }
159
        }
×
160

161
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
1✔
162
        url = cl.getResource(resource);
1✔
163

164
        if (url == null) {
1!
165
            url = ObjectFactory.class.getClassLoader().getResource(resource);
×
166
        }
167

168
        return url;
1✔
169
    }
170

171
    public static Object createInternalObject(String type) {
172
        Object answer;
173

174
        try {
175
            Class<?> clazz = internalClassForName(type);
1✔
176

177
            answer = clazz.getConstructor().newInstance();
1✔
178
        } catch (Exception e) {
×
NEW
179
            throw new RuntimeException(getString("RuntimeError.6", type), e); //$NON-NLS-1$
×
180

181
        }
1✔
182

183
        return answer;
1✔
184
    }
185

186
    public static JavaTypeResolver createJavaTypeResolver(Context context, List<String> warnings) {
187
        JavaTypeResolverConfiguration config = context.getJavaTypeResolverConfiguration();
1✔
188
        String type;
189

190
        if (config != null && config.getConfigurationType() != null) {
1!
191
            type = config.getConfigurationType();
×
192
            if ("DEFAULT".equalsIgnoreCase(type)) { //$NON-NLS-1$
×
193
                type = JavaTypeResolverDefaultImpl.class.getName();
×
194
            }
195
        } else {
196
            type = JavaTypeResolverDefaultImpl.class.getName();
1✔
197
        }
198

199
        JavaTypeResolver answer = (JavaTypeResolver) createInternalObject(type);
1✔
200
        answer.setWarnings(warnings);
1✔
201

202
        if (config != null) {
1✔
203
            answer.addConfigurationProperties(config.getProperties());
1✔
204
        }
205

206
        answer.setContext(context);
1✔
207

208
        return answer;
1✔
209
    }
210

211
    public static Plugin createPlugin(Context context, PluginConfiguration pluginConfiguration) {
212
        Plugin plugin = (Plugin) createInternalObject(pluginConfiguration.getConfigurationType());
1✔
213
        plugin.setContext(context);
1✔
214
        plugin.setProperties(pluginConfiguration.getProperties());
1✔
215
        return plugin;
1✔
216
    }
217

218
    public static CommentGenerator createCommentGenerator(Context context) {
219

220
        CommentGeneratorConfiguration config = context.getCommentGeneratorConfiguration();
1✔
221
        CommentGenerator answer;
222

223
        String type;
224
        if (config == null || config.getConfigurationType() == null) {
1!
225
            type = DefaultCommentGenerator.class.getName();
1✔
226
        } else {
227
            type = config.getConfigurationType();
×
228
        }
229

230
        answer = (CommentGenerator) createInternalObject(type);
1✔
231

232
        if (config != null) {
1✔
233
            answer.addConfigurationProperties(config.getProperties());
1✔
234
        }
235

236
        return answer;
1✔
237
    }
238

239
    public static ConnectionFactory createConnectionFactory(Context context) {
240

241
        ConnectionFactoryConfiguration config = context.getConnectionFactoryConfiguration();
1✔
242
        ConnectionFactory answer;
243

244
        String type;
245
        if (config == null || config.getConfigurationType() == null) {
1!
246
            type = JDBCConnectionFactory.class.getName();
1✔
247
        } else {
248
            type = config.getConfigurationType();
×
249
        }
250

251
        answer = (ConnectionFactory) createInternalObject(type);
1✔
252

253
        if (config != null) {
1!
254
            answer.addConfigurationProperties(config.getProperties());
1✔
255
        }
256

257
        return answer;
1✔
258
    }
259

260
    public static JavaFormatter createJavaFormatter(Context context) {
261
        String type = context.getProperty(PropertyRegistry.CONTEXT_JAVA_FORMATTER);
1✔
262
        if (!stringHasValue(type)) {
1!
263
            type = DefaultJavaFormatter.class.getName();
1✔
264
        }
265

266
        JavaFormatter answer = (JavaFormatter) createInternalObject(type);
1✔
267

268
        answer.setContext(context);
1✔
269

270
        return answer;
1✔
271
    }
272

273
    public static KotlinFormatter createKotlinFormatter(Context context) {
274
        String type = context.getProperty(PropertyRegistry.CONTEXT_KOTLIN_FORMATTER);
1✔
275
        if (!stringHasValue(type)) {
1!
276
            type = DefaultKotlinFormatter.class.getName();
1✔
277
        }
278

279
        KotlinFormatter answer = (KotlinFormatter) createInternalObject(type);
1✔
280

281
        answer.setContext(context);
1✔
282

283
        return answer;
1✔
284
    }
285

286
    public static XmlFormatter createXmlFormatter(Context context) {
287
        String type = context.getProperty(PropertyRegistry.CONTEXT_XML_FORMATTER);
1✔
288
        if (!stringHasValue(type)) {
1!
289
            type = DefaultXmlFormatter.class.getName();
1✔
290
        }
291

292
        XmlFormatter answer = (XmlFormatter) createInternalObject(type);
1✔
293

294
        answer.setContext(context);
1✔
295

296
        return answer;
1✔
297
    }
298

299
    public static IntrospectedTable createIntrospectedTable(TableConfiguration tableConfiguration,
300
            FullyQualifiedTable table, Context context) {
301

302
        IntrospectedTable answer = createIntrospectedTableForValidation(context);
1✔
303
        answer.setFullyQualifiedTable(table);
1✔
304
        answer.setTableConfiguration(tableConfiguration);
1✔
305

306
        return answer;
1✔
307
    }
308

309
    /**
310
     * Creates an introspected table implementation that is only usable for validation .
311
     *
312
     * @param context
313
     *            the context
314
     *
315
     * @return the introspected table
316
     */
317
    public static IntrospectedTable createIntrospectedTableForValidation(Context context) {
318
        String type = context.getTargetRuntime();
1✔
319
        if (!stringHasValue(type)) {
1✔
320
            type = IntrospectedTableMyBatis3DynamicSqlImpl.class.getName();
1✔
321
        } else if ("MyBatis3".equalsIgnoreCase(type)) { //$NON-NLS-1$
1✔
322
            type = IntrospectedTableMyBatis3Impl.class.getName();
1✔
323
        } else if ("MyBatis3Simple".equalsIgnoreCase(type)) { //$NON-NLS-1$
1✔
324
            type = IntrospectedTableMyBatis3SimpleImpl.class.getName();
1✔
325
        } else if ("MyBatis3DynamicSql".equalsIgnoreCase(type)) { //$NON-NLS-1$
1✔
326
            type = IntrospectedTableMyBatis3DynamicSqlImpl.class.getName();
1✔
327
        } else if ("MyBatis3Kotlin".equalsIgnoreCase(type)) { //$NON-NLS-1$
1!
328
            type = IntrospectedTableKotlinImpl.class.getName();
1✔
329
        }
330

331
        IntrospectedTable answer = (IntrospectedTable) createInternalObject(type);
1✔
332
        answer.setContext(context);
1✔
333

334
        return answer;
1✔
335
    }
336

337
    public static IntrospectedColumn createIntrospectedColumn(Context context) {
338
        String type = context.getIntrospectedColumnImpl();
1✔
339
        if (!stringHasValue(type)) {
1!
340
            type = IntrospectedColumn.class.getName();
1✔
341
        }
342

343
        IntrospectedColumn answer = (IntrospectedColumn) createInternalObject(type);
1✔
344
        answer.setContext(context);
1✔
345

346
        return answer;
1✔
347
    }
348
}
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

© 2025 Coveralls, Inc