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

mybatis / generator / 2202

05 May 2026 07:27PM UTC coverage: 91.746% (+0.04%) from 91.703%
2202

push

github

web-flow
Merge pull request #1508 from mybatis/renovate/com.github.javaparser-javaparser-core-3.x

Update dependency com.github.javaparser:javaparser-core to v3.28.1

2454 of 3154 branches covered (77.81%)

12038 of 13121 relevant lines covered (91.75%)

0.92 hits per line

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

77.27
/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.stringValueOrElse;
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.Indenter;
29
import org.mybatis.generator.api.IntrospectedColumn;
30
import org.mybatis.generator.api.JavaFormatter;
31
import org.mybatis.generator.api.JavaTypeResolver;
32
import org.mybatis.generator.api.KnownRuntime;
33
import org.mybatis.generator.api.KotlinFormatter;
34
import org.mybatis.generator.api.Plugin;
35
import org.mybatis.generator.api.XmlFormatter;
36
import org.mybatis.generator.config.CommentGeneratorConfiguration;
37
import org.mybatis.generator.config.ConnectionFactoryConfiguration;
38
import org.mybatis.generator.config.Context;
39
import org.mybatis.generator.config.Defaults;
40
import org.mybatis.generator.config.JavaTypeResolverConfiguration;
41
import org.mybatis.generator.config.PluginConfiguration;
42
import org.mybatis.generator.config.PropertyRegistry;
43
import org.mybatis.generator.exception.InternalException;
44

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

52
    private static final List<ClassLoader> externalClassLoaders;
53

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

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

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

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

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

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

112
        return internalClassForName(type);
1✔
113
    }
114

115
    public static <T> T createExternalObject(String type) {
116
        T answer;
117

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

125
        return answer;
×
126
    }
127

128
    @SuppressWarnings("unchecked")
129
    public static <T> Class<T> internalClassForName(String type) throws ClassNotFoundException {
130
        Class<?> clazz;
131
        try {
132
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
1✔
133
            clazz = Class.forName(type, true, cl);
1✔
134
        } catch (Exception e) {
1✔
135
            // ignore - failsafe below
136
            clazz = null;
1✔
137
        }
1✔
138

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

143
        return (Class<T>) clazz;
1✔
144
    }
145

146
    public static Optional<URL> getResource(String resource) {
147
        URL url;
148

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

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

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

163
        return Optional.ofNullable(url);
1✔
164
    }
165

166
    public static <T> T createInternalObject(String type) {
167
        T answer;
168

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

176
        return answer;
1✔
177
    }
178

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

184
        JavaTypeResolver answer = createInternalObject(type);
1✔
185
        answer.setWarnings(warnings);
1✔
186

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

190
        answer.setContext(context);
1✔
191

192
        return answer;
1✔
193
    }
194

195
    public static Plugin createPlugin(Context context, PluginConfiguration pluginConfiguration,
196
                                      CommentGenerator commentGenerator, KnownRuntime knownRuntime,
197
                                      Indenter indenter) {
198
        Plugin plugin = createInternalObject(pluginConfiguration.getConfigurationType().orElseThrow());
1✔
199
        plugin.setContext(context);
1✔
200
        plugin.setProperties(pluginConfiguration.getProperties());
1✔
201
        plugin.setCommentGenerator(commentGenerator);
1✔
202
        plugin.setKnownRuntime(knownRuntime);
1✔
203
        plugin.setIndenter(indenter);
1✔
204
        return plugin;
1✔
205
    }
206

207
    public static CommentGenerator createCommentGenerator(Context context, Indenter indenter) {
208
        CommentGenerator answer;
209

210
        String type = context.getCommentGeneratorConfiguration()
1✔
211
                .map(CommentGeneratorConfiguration::getImplementationType)
1✔
212
                .orElse(Defaults.DEFAULT_COMMENT_GENERATOR);
1✔
213

214
        answer = createInternalObject(type);
1✔
215
        answer.setIndenter(indenter);
1✔
216

217
        context.getCommentGeneratorConfiguration()
1✔
218
                .ifPresent(c -> answer.addConfigurationProperties(c.getProperties()));
1✔
219

220
        return answer;
1✔
221
    }
222

223
    public static ConnectionFactory createConnectionFactory(ConnectionFactoryConfiguration config) {
224
        ConnectionFactory answer;
225

226
        String type = config.getImplementationType();
1✔
227

228
        answer = createInternalObject(type);
1✔
229
        answer.addConfigurationProperties(config.getProperties());
1✔
230

231
        return answer;
1✔
232
    }
233

234
    public static JavaFormatter createJavaFormatter(Context context, Indenter indenter) {
235
        String type = stringValueOrElse(context.getProperty(PropertyRegistry.CONTEXT_JAVA_FORMATTER),
1✔
236
                Defaults.DEFAULT_JAVA_FORMATTER);
237
        JavaFormatter answer = createInternalObject(type);
1✔
238

239
        answer.setContext(context);
1✔
240
        answer.setIndenter(indenter);
1✔
241

242
        return answer;
1✔
243
    }
244

245
    public static KotlinFormatter createKotlinFormatter(Context context) {
246
        String type = stringValueOrElse(context.getProperty(PropertyRegistry.CONTEXT_KOTLIN_FORMATTER),
1✔
247
                Defaults.DEFAULT_KOTLIN_FORMATTER);
248
        KotlinFormatter answer = createInternalObject(type);
1✔
249

250
        answer.setContext(context);
1✔
251

252
        return answer;
1✔
253
    }
254

255
    public static XmlFormatter createXmlFormatter(Context context, Indenter indenter) {
256
        String type = stringValueOrElse(context.getProperty(PropertyRegistry.CONTEXT_XML_FORMATTER),
1✔
257
                Defaults.DEFAULT_XML_FORMATTER);
258
        XmlFormatter answer = createInternalObject(type);
1✔
259

260
        answer.setContext(context);
1✔
261
        answer.setIndenter(indenter);
1✔
262

263
        return answer;
1✔
264
    }
265

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

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