Skip to content

Commit 9f53cce

Browse files
authored
Merge pull request #2973 from taoyect/xml_parsing_learning_code_optimize
Optimize the code of the xml configuration file parsing process
2 parents 6e84c85 + 7c4fbd0 commit 9f53cce

File tree

2 files changed

+126
-138
lines changed

2 files changed

+126
-138
lines changed

src/main/java/org/apache/ibatis/builder/BaseBuilder.java

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,56 +64,41 @@ protected Set<String> stringSetValueOf(String value, String defaultValue) {
6464
}
6565

6666
protected JdbcType resolveJdbcType(String alias) {
67-
if (alias == null) {
68-
return null;
69-
}
7067
try {
71-
return JdbcType.valueOf(alias);
68+
return alias == null ? null : JdbcType.valueOf(alias);
7269
} catch (IllegalArgumentException e) {
7370
throw new BuilderException("Error resolving JdbcType. Cause: " + e, e);
7471
}
7572
}
7673

7774
protected ResultSetType resolveResultSetType(String alias) {
78-
if (alias == null) {
79-
return null;
80-
}
8175
try {
82-
return ResultSetType.valueOf(alias);
76+
return alias == null ? null : ResultSetType.valueOf(alias);
8377
} catch (IllegalArgumentException e) {
8478
throw new BuilderException("Error resolving ResultSetType. Cause: " + e, e);
8579
}
8680
}
8781

8882
protected ParameterMode resolveParameterMode(String alias) {
89-
if (alias == null) {
90-
return null;
91-
}
9283
try {
93-
return ParameterMode.valueOf(alias);
84+
return alias == null ? null : ParameterMode.valueOf(alias);
9485
} catch (IllegalArgumentException e) {
9586
throw new BuilderException("Error resolving ParameterMode. Cause: " + e, e);
9687
}
9788
}
9889

9990
protected Object createInstance(String alias) {
10091
Class<?> clazz = resolveClass(alias);
101-
if (clazz == null) {
102-
return null;
103-
}
10492
try {
105-
return clazz.getDeclaredConstructor().newInstance();
93+
return clazz == null ? null : clazz.getDeclaredConstructor().newInstance();
10694
} catch (Exception e) {
10795
throw new BuilderException("Error creating instance. Cause: " + e, e);
10896
}
10997
}
11098

11199
protected <T> Class<? extends T> resolveClass(String alias) {
112-
if (alias == null) {
113-
return null;
114-
}
115100
try {
116-
return resolveAlias(alias);
101+
return alias == null ? null : resolveAlias(alias);
117102
} catch (Exception e) {
118103
throw new BuilderException("Error resolving class. Cause: " + e, e);
119104
}
@@ -139,11 +124,8 @@ protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, Class<? extends T
139124
}
140125
// javaType ignored for injected handlers see issue #746 for full detail
141126
TypeHandler<?> handler = typeHandlerRegistry.getMappingTypeHandler(typeHandlerType);
142-
if (handler == null) {
143-
// not in registry, create a new one
144-
handler = typeHandlerRegistry.getInstance(javaType, typeHandlerType);
145-
}
146-
return handler;
127+
// if handler not in registry, create a new one, otherwise return directly
128+
return handler == null ? typeHandlerRegistry.getInstance(javaType, typeHandlerType) : handler;
147129
}
148130

149131
protected <T> Class<? extends T> resolveAlias(String alias) {

src/main/java/org/apache/ibatis/builder/xml/XMLConfigBuilder.java

Lines changed: 119 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,15 @@ private Properties settingsAsProperties(XNode context) {
152152

153153
private void loadCustomVfs(Properties props) throws ClassNotFoundException {
154154
String value = props.getProperty("vfsImpl");
155-
if (value != null) {
156-
String[] clazzes = value.split(",");
157-
for (String clazz : clazzes) {
158-
if (!clazz.isEmpty()) {
159-
@SuppressWarnings("unchecked")
160-
Class<? extends VFS> vfsImpl = (Class<? extends VFS>) Resources.classForName(clazz);
161-
configuration.setVfsImpl(vfsImpl);
162-
}
155+
if (value == null) {
156+
return;
157+
}
158+
String[] clazzes = value.split(",");
159+
for (String clazz : clazzes) {
160+
if (!clazz.isEmpty()) {
161+
@SuppressWarnings("unchecked")
162+
Class<? extends VFS> vfsImpl = (Class<? extends VFS>) Resources.classForName(clazz);
163+
configuration.setVfsImpl(vfsImpl);
163164
}
164165
}
165166
}
@@ -169,33 +170,34 @@ private void loadCustomLogImpl(Properties props) {
169170
configuration.setLogImpl(logImpl);
170171
}
171172

172-
private void typeAliasesElement(XNode parent) {
173-
if (parent != null) {
174-
for (XNode child : parent.getChildren()) {
175-
if ("package".equals(child.getName())) {
176-
String typeAliasPackage = child.getStringAttribute("name");
177-
configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
178-
} else {
179-
String alias = child.getStringAttribute("alias");
180-
String type = child.getStringAttribute("type");
181-
try {
182-
Class<?> clazz = Resources.classForName(type);
183-
if (alias == null) {
184-
typeAliasRegistry.registerAlias(clazz);
185-
} else {
186-
typeAliasRegistry.registerAlias(alias, clazz);
187-
}
188-
} catch (ClassNotFoundException e) {
189-
throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e);
173+
private void typeAliasesElement(XNode context) {
174+
if (context == null) {
175+
return;
176+
}
177+
for (XNode child : context.getChildren()) {
178+
if ("package".equals(child.getName())) {
179+
String typeAliasPackage = child.getStringAttribute("name");
180+
configuration.getTypeAliasRegistry().registerAliases(typeAliasPackage);
181+
} else {
182+
String alias = child.getStringAttribute("alias");
183+
String type = child.getStringAttribute("type");
184+
try {
185+
Class<?> clazz = Resources.classForName(type);
186+
if (alias == null) {
187+
typeAliasRegistry.registerAlias(clazz);
188+
} else {
189+
typeAliasRegistry.registerAlias(alias, clazz);
190190
}
191+
} catch (ClassNotFoundException e) {
192+
throw new BuilderException("Error registering typeAlias for '" + alias + "'. Cause: " + e, e);
191193
}
192194
}
193195
}
194196
}
195197

196-
private void pluginElement(XNode parent) throws Exception {
197-
if (parent != null) {
198-
for (XNode child : parent.getChildren()) {
198+
private void pluginElement(XNode context) throws Exception {
199+
if (context != null) {
200+
for (XNode child : context.getChildren()) {
199201
String interceptor = child.getStringAttribute("interceptor");
200202
Properties properties = child.getChildrenAsProperties();
201203
Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).getDeclaredConstructor()
@@ -233,26 +235,27 @@ private void reflectorFactoryElement(XNode context) throws Exception {
233235
}
234236

235237
private void propertiesElement(XNode context) throws Exception {
236-
if (context != null) {
237-
Properties defaults = context.getChildrenAsProperties();
238-
String resource = context.getStringAttribute("resource");
239-
String url = context.getStringAttribute("url");
240-
if (resource != null && url != null) {
241-
throw new BuilderException(
242-
"The properties element cannot specify both a URL and a resource based property file reference. Please specify one or the other.");
243-
}
244-
if (resource != null) {
245-
defaults.putAll(Resources.getResourceAsProperties(resource));
246-
} else if (url != null) {
247-
defaults.putAll(Resources.getUrlAsProperties(url));
248-
}
249-
Properties vars = configuration.getVariables();
250-
if (vars != null) {
251-
defaults.putAll(vars);
252-
}
253-
parser.setVariables(defaults);
254-
configuration.setVariables(defaults);
238+
if (context == null) {
239+
return;
255240
}
241+
Properties defaults = context.getChildrenAsProperties();
242+
String resource = context.getStringAttribute("resource");
243+
String url = context.getStringAttribute("url");
244+
if (resource != null && url != null) {
245+
throw new BuilderException(
246+
"The properties element cannot specify both a URL and a resource based property file reference. Please specify one or the other.");
247+
}
248+
if (resource != null) {
249+
defaults.putAll(Resources.getResourceAsProperties(resource));
250+
} else if (url != null) {
251+
defaults.putAll(Resources.getUrlAsProperties(url));
252+
}
253+
Properties vars = configuration.getVariables();
254+
if (vars != null) {
255+
defaults.putAll(vars);
256+
}
257+
parser.setVariables(defaults);
258+
configuration.setVariables(defaults);
256259
}
257260

258261
private void settingsElement(Properties props) {
@@ -293,21 +296,22 @@ private void settingsElement(Properties props) {
293296
}
294297

295298
private void environmentsElement(XNode context) throws Exception {
296-
if (context != null) {
297-
if (environment == null) {
298-
environment = context.getStringAttribute("default");
299-
}
300-
for (XNode child : context.getChildren()) {
301-
String id = child.getStringAttribute("id");
302-
if (isSpecifiedEnvironment(id)) {
303-
TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
304-
DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
305-
DataSource dataSource = dsFactory.getDataSource();
306-
Environment.Builder environmentBuilder = new Environment.Builder(id).transactionFactory(txFactory)
307-
.dataSource(dataSource);
308-
configuration.setEnvironment(environmentBuilder.build());
309-
break;
310-
}
299+
if (context == null) {
300+
return;
301+
}
302+
if (environment == null) {
303+
environment = context.getStringAttribute("default");
304+
}
305+
for (XNode child : context.getChildren()) {
306+
String id = child.getStringAttribute("id");
307+
if (isSpecifiedEnvironment(id)) {
308+
TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
309+
DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
310+
DataSource dataSource = dsFactory.getDataSource();
311+
Environment.Builder environmentBuilder = new Environment.Builder(id).transactionFactory(txFactory)
312+
.dataSource(dataSource);
313+
configuration.setEnvironment(environmentBuilder.build());
314+
break;
311315
}
312316
}
313317
}
@@ -353,64 +357,66 @@ private DataSourceFactory dataSourceElement(XNode context) throws Exception {
353357
throw new BuilderException("Environment declaration requires a DataSourceFactory.");
354358
}
355359

356-
private void typeHandlerElement(XNode parent) {
357-
if (parent != null) {
358-
for (XNode child : parent.getChildren()) {
359-
if ("package".equals(child.getName())) {
360-
String typeHandlerPackage = child.getStringAttribute("name");
361-
typeHandlerRegistry.register(typeHandlerPackage);
362-
} else {
363-
String javaTypeName = child.getStringAttribute("javaType");
364-
String jdbcTypeName = child.getStringAttribute("jdbcType");
365-
String handlerTypeName = child.getStringAttribute("handler");
366-
Class<?> javaTypeClass = resolveClass(javaTypeName);
367-
JdbcType jdbcType = resolveJdbcType(jdbcTypeName);
368-
Class<?> typeHandlerClass = resolveClass(handlerTypeName);
369-
if (javaTypeClass != null) {
370-
if (jdbcType == null) {
371-
typeHandlerRegistry.register(javaTypeClass, typeHandlerClass);
372-
} else {
373-
typeHandlerRegistry.register(javaTypeClass, jdbcType, typeHandlerClass);
374-
}
360+
private void typeHandlerElement(XNode context) {
361+
if (context == null) {
362+
return;
363+
}
364+
for (XNode child : context.getChildren()) {
365+
if ("package".equals(child.getName())) {
366+
String typeHandlerPackage = child.getStringAttribute("name");
367+
typeHandlerRegistry.register(typeHandlerPackage);
368+
} else {
369+
String javaTypeName = child.getStringAttribute("javaType");
370+
String jdbcTypeName = child.getStringAttribute("jdbcType");
371+
String handlerTypeName = child.getStringAttribute("handler");
372+
Class<?> javaTypeClass = resolveClass(javaTypeName);
373+
JdbcType jdbcType = resolveJdbcType(jdbcTypeName);
374+
Class<?> typeHandlerClass = resolveClass(handlerTypeName);
375+
if (javaTypeClass != null) {
376+
if (jdbcType == null) {
377+
typeHandlerRegistry.register(javaTypeClass, typeHandlerClass);
375378
} else {
376-
typeHandlerRegistry.register(typeHandlerClass);
379+
typeHandlerRegistry.register(javaTypeClass, jdbcType, typeHandlerClass);
377380
}
381+
} else {
382+
typeHandlerRegistry.register(typeHandlerClass);
378383
}
379384
}
380385
}
381386
}
382387

383-
private void mapperElement(XNode parent) throws Exception {
384-
if (parent != null) {
385-
for (XNode child : parent.getChildren()) {
386-
if ("package".equals(child.getName())) {
387-
String mapperPackage = child.getStringAttribute("name");
388-
configuration.addMappers(mapperPackage);
389-
} else {
390-
String resource = child.getStringAttribute("resource");
391-
String url = child.getStringAttribute("url");
392-
String mapperClass = child.getStringAttribute("class");
393-
if (resource != null && url == null && mapperClass == null) {
394-
ErrorContext.instance().resource(resource);
395-
try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
396-
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource,
397-
configuration.getSqlFragments());
398-
mapperParser.parse();
399-
}
400-
} else if (resource == null && url != null && mapperClass == null) {
401-
ErrorContext.instance().resource(url);
402-
try (InputStream inputStream = Resources.getUrlAsStream(url)) {
403-
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url,
404-
configuration.getSqlFragments());
405-
mapperParser.parse();
406-
}
407-
} else if (resource == null && url == null && mapperClass != null) {
408-
Class<?> mapperInterface = Resources.classForName(mapperClass);
409-
configuration.addMapper(mapperInterface);
410-
} else {
411-
throw new BuilderException(
412-
"A mapper element may only specify a url, resource or class, but not more than one.");
388+
private void mapperElement(XNode context) throws Exception {
389+
if (context == null) {
390+
return;
391+
}
392+
for (XNode child : context.getChildren()) {
393+
if ("package".equals(child.getName())) {
394+
String mapperPackage = child.getStringAttribute("name");
395+
configuration.addMappers(mapperPackage);
396+
} else {
397+
String resource = child.getStringAttribute("resource");
398+
String url = child.getStringAttribute("url");
399+
String mapperClass = child.getStringAttribute("class");
400+
if (resource != null && url == null && mapperClass == null) {
401+
ErrorContext.instance().resource(resource);
402+
try (InputStream inputStream = Resources.getResourceAsStream(resource)) {
403+
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource,
404+
configuration.getSqlFragments());
405+
mapperParser.parse();
413406
}
407+
} else if (resource == null && url != null && mapperClass == null) {
408+
ErrorContext.instance().resource(url);
409+
try (InputStream inputStream = Resources.getUrlAsStream(url)) {
410+
XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url,
411+
configuration.getSqlFragments());
412+
mapperParser.parse();
413+
}
414+
} else if (resource == null && url == null && mapperClass != null) {
415+
Class<?> mapperInterface = Resources.classForName(mapperClass);
416+
configuration.addMapper(mapperInterface);
417+
} else {
418+
throw new BuilderException(
419+
"A mapper element may only specify a url, resource or class, but not more than one.");
414420
}
415421
}
416422
}

0 commit comments

Comments
 (0)