Skip to content

Commit 2da2275

Browse files
committed
Merge branch 'master' into merge-typehandlers-jsr310
# Conflicts: # src/test/java/org/apache/ibatis/type/usesjava8/Jsr310TypeHandlerRegistryTest.java
2 parents da6dca2 + 151e90e commit 2da2275

File tree

64 files changed

+1199
-738
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1199
-738
lines changed

pom.xml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
<dependency>
153153
<groupId>org.javassist</groupId>
154154
<artifactId>javassist</artifactId>
155-
<version>3.21.0-GA</version>
155+
<version>3.22.0-CR1</version>
156156
<scope>compile</scope>
157157
<optional>true</optional>
158158
</dependency>
@@ -204,7 +204,7 @@
204204
<dependency>
205205
<groupId>org.hsqldb</groupId>
206206
<artifactId>hsqldb</artifactId>
207-
<version>2.3.4</version>
207+
<version>2.3.4</version> <!-- Version 2.4.0 required jdk8 -->
208208
<scope>test</scope>
209209
</dependency>
210210
<dependency>
@@ -216,7 +216,7 @@
216216
<dependency>
217217
<groupId>org.mockito</groupId>
218218
<artifactId>mockito-core</artifactId>
219-
<version>2.7.19</version>
219+
<version>2.7.22</version>
220220
<scope>test</scope>
221221
</dependency>
222222
<!-- Do not go to 2.x until we are on jdk7 -->
@@ -245,6 +245,18 @@
245245
<version>9.1-901-1.jdbc4</version>
246246
<scope>test</scope>
247247
</dependency>
248+
<dependency>
249+
<groupId>org.assertj</groupId>
250+
<artifactId>assertj-core</artifactId>
251+
<version>1.7.1</version> <!-- Stay on 1.7.1 to support Java 6 -->
252+
<scope>test</scope>
253+
</dependency>
254+
<dependency>
255+
<groupId>eu.codearte.catch-exception</groupId>
256+
<artifactId>catch-exception</artifactId>
257+
<version>1.4.4</version>
258+
<scope>test</scope>
259+
</dependency>
248260
</dependencies>
249261

250262
<build>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2016 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,6 +45,7 @@
4545
import org.apache.ibatis.session.LocalCacheScope;
4646
import org.apache.ibatis.transaction.TransactionFactory;
4747
import org.apache.ibatis.type.JdbcType;
48+
import org.apache.ibatis.type.TypeHandler;
4849

4950
/**
5051
* @author Clinton Begin
@@ -254,6 +255,9 @@ private void settingsElement(Properties props) throws Exception {
254255
configuration.setLazyLoadTriggerMethods(stringSetValueOf(props.getProperty("lazyLoadTriggerMethods"), "equals,clone,hashCode,toString"));
255256
configuration.setSafeResultHandlerEnabled(booleanValueOf(props.getProperty("safeResultHandlerEnabled"), true));
256257
configuration.setDefaultScriptingLanguage(resolveClass(props.getProperty("defaultScriptingLanguage")));
258+
@SuppressWarnings("unchecked")
259+
Class<? extends TypeHandler> typeHandler = (Class<? extends TypeHandler>)resolveClass(props.getProperty("defaultEnumTypeHandler"));
260+
configuration.setDefaultEnumTypeHandler(typeHandler);
257261
configuration.setCallSettersOnNulls(booleanValueOf(props.getProperty("callSettersOnNulls"), false));
258262
configuration.setUseActualParamName(booleanValueOf(props.getProperty("useActualParamName"), true));
259263
configuration.setReturnInstanceForEmptyRow(booleanValueOf(props.getProperty("returnInstanceForEmptyRow"), false));

src/main/java/org/apache/ibatis/cache/CacheKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class CacheKey implements Cloneable, Serializable {
3737
private int hashcode;
3838
private long checksum;
3939
private int count;
40-
private List<Object> updateList;
40+
private transient List<Object> updateList;
4141

4242
public CacheKey() {
4343
this.hashcode = DEFAULT_HASHCODE;

src/main/java/org/apache/ibatis/executor/loader/ResultLoaderMap.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public boolean load(String property) throws SQLException {
8484
return false;
8585
}
8686

87+
public void remove(String property) {
88+
loaderMap.remove(property.toUpperCase(Locale.ENGLISH));
89+
}
90+
8791
public void loadAll() throws SQLException {
8892
final Set<String> methodNameSet = loaderMap.keySet();
8993
String[] methodNames = methodNameSet.toArray(new String[methodNameSet.size()]);

src/main/java/org/apache/ibatis/executor/loader/cglib/CglibProxyFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2016 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -147,6 +147,9 @@ public Object intercept(Object enhanced, Method method, Object[] args, MethodPro
147147
if (lazyLoader.size() > 0 && !FINALIZE_METHOD.equals(methodName)) {
148148
if (aggressive || lazyLoadTriggerMethods.contains(methodName)) {
149149
lazyLoader.loadAll();
150+
} else if (PropertyNamer.isSetter(methodName)) {
151+
final String property = PropertyNamer.methodToProperty(methodName);
152+
lazyLoader.remove(property);
150153
} else if (PropertyNamer.isGetter(methodName)) {
151154
final String property = PropertyNamer.methodToProperty(methodName);
152155
if (lazyLoader.hasLoader(property)) {

src/main/java/org/apache/ibatis/executor/loader/javassist/JavassistProxyFactory.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2016 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -149,6 +149,9 @@ public Object invoke(Object enhanced, Method method, Method methodProxy, Object[
149149
if (lazyLoader.size() > 0 && !FINALIZE_METHOD.equals(methodName)) {
150150
if (aggressive || lazyLoadTriggerMethods.contains(methodName)) {
151151
lazyLoader.loadAll();
152+
} else if (PropertyNamer.isSetter(methodName)) {
153+
final String property = PropertyNamer.methodToProperty(methodName);
154+
lazyLoader.remove(property);
152155
} else if (PropertyNamer.isGetter(methodName)) {
153156
final String property = PropertyNamer.methodToProperty(methodName);
154157
if (lazyLoader.hasLoader(property)) {

src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ private List<UnMappedColumnAutoMapping> createAutomaticMappings(ResultSetWrapper
507507
private boolean applyAutomaticMappings(ResultSetWrapper rsw, ResultMap resultMap, MetaObject metaObject, String columnPrefix) throws SQLException {
508508
List<UnMappedColumnAutoMapping> autoMapping = createAutomaticMappings(rsw, resultMap, metaObject, columnPrefix);
509509
boolean foundValues = false;
510-
if (autoMapping.size() > 0) {
510+
if (!autoMapping.isEmpty()) {
511511
for (UnMappedColumnAutoMapping mapping : autoMapping) {
512512
final Object value = mapping.typeHandler.getResult(rsw.getResultSet(), mapping.column);
513513
if (value != null) {
@@ -1012,7 +1012,7 @@ private CacheKey createRowKey(ResultMap resultMap, ResultSetWrapper rsw, String
10121012
final CacheKey cacheKey = new CacheKey();
10131013
cacheKey.update(resultMap.getId());
10141014
List<ResultMapping> resultMappings = getResultMappingsForRowKey(resultMap);
1015-
if (resultMappings.size() == 0) {
1015+
if (resultMappings.isEmpty()) {
10161016
if (Map.class.isAssignableFrom(resultMap.getType())) {
10171017
createRowKeyForMap(rsw, cacheKey);
10181018
} else {
@@ -1043,7 +1043,7 @@ private CacheKey combineKeys(CacheKey rowKey, CacheKey parentRowKey) {
10431043

10441044
private List<ResultMapping> getResultMappingsForRowKey(ResultMap resultMap) {
10451045
List<ResultMapping> resultMappings = resultMap.getIdResultMappings();
1046-
if (resultMappings.size() == 0) {
1046+
if (resultMappings.isEmpty()) {
10471047
resultMappings = resultMap.getPropertyResultMappings();
10481048
}
10491049
return resultMappings;

src/main/java/org/apache/ibatis/io/ExternalResources.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2016 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,12 +24,17 @@
2424
import java.nio.channels.FileChannel;
2525
import java.util.Properties;
2626

27+
import org.apache.ibatis.logging.Log;
28+
import org.apache.ibatis.logging.LogFactory;
29+
2730
/**
2831
* @author Clinton Begin
2932
*/
3033
@Deprecated
3134
public class ExternalResources {
3235

36+
private static final Log log = LogFactory.getLog(ExternalResources.class);
37+
3338
private ExternalResources() {
3439
// do nothing
3540
}
@@ -72,7 +77,7 @@ public static String getConfiguredTemplate(String templatePath, String templateP
7277
} catch (FileNotFoundException e) {
7378
throw e;
7479
} catch (Exception e) {
75-
e.printStackTrace();
80+
log.error("", e);
7681
}
7782

7883
return templateName;

src/main/java/org/apache/ibatis/session/Configuration.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2016 the original author or authors.
2+
* Copyright 2009-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -88,6 +88,7 @@
8888
import org.apache.ibatis.transaction.managed.ManagedTransactionFactory;
8989
import org.apache.ibatis.type.JdbcType;
9090
import org.apache.ibatis.type.TypeAliasRegistry;
91+
import org.apache.ibatis.type.TypeHandler;
9192
import org.apache.ibatis.type.TypeHandlerRegistry;
9293

9394
/**
@@ -461,6 +462,18 @@ public TypeHandlerRegistry getTypeHandlerRegistry() {
461462
return typeHandlerRegistry;
462463
}
463464

465+
/**
466+
* Set a default {@link TypeHandler} class for {@link Enum}.
467+
* A default {@link TypeHandler} is {@link org.apache.ibatis.type.EnumTypeHandler}.
468+
* @param typeHandler a type handler class for {@link Enum}
469+
* @since 3.4.5
470+
*/
471+
public void setDefaultEnumTypeHandler(Class<? extends TypeHandler> typeHandler) {
472+
if (typeHandler != null) {
473+
getTypeHandlerRegistry().setDefaultEnumTypeHandler(typeHandler);
474+
}
475+
}
476+
464477
public TypeAliasRegistry getTypeAliasRegistry() {
465478
return typeAliasRegistry;
466479
}

src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.EnumMap;
2929
import java.util.HashMap;
3030
import java.util.Map;
31+
import java.util.Map.Entry;
3132
import java.util.Set;
3233
import java.util.concurrent.ConcurrentHashMap;
3334

@@ -48,6 +49,8 @@ public final class TypeHandlerRegistry {
4849

4950
private static final Map<JdbcType, TypeHandler<?>> NULL_TYPE_HANDLER_MAP = new HashMap<JdbcType, TypeHandler<?>>();
5051

52+
private Class<? extends TypeHandler> defaultEnumTypeHandler = EnumTypeHandler.class;
53+
5154
public TypeHandlerRegistry() {
5255
register(Boolean.class, new BooleanTypeHandler());
5356
register(boolean.class, new BooleanTypeHandler());
@@ -140,6 +143,16 @@ public TypeHandlerRegistry() {
140143
register(char.class, new CharacterTypeHandler());
141144
}
142145

146+
/**
147+
* Set a default {@link TypeHandler} class for {@link Enum}.
148+
* A default {@link TypeHandler} is {@link org.apache.ibatis.type.EnumTypeHandler}.
149+
* @param typeHandler a type handler class for {@link Enum}
150+
* @since 3.4.5
151+
*/
152+
public void setDefaultEnumTypeHandler(Class<? extends TypeHandler> typeHandler) {
153+
this.defaultEnumTypeHandler = typeHandler;
154+
}
155+
143156
public boolean hasTypeHandler(Class<?> javaType) {
144157
return hasTypeHandler(javaType, null);
145158
}
@@ -207,9 +220,9 @@ private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMap(Type type) {
207220
if (jdbcHandlerMap == null && type instanceof Class) {
208221
Class<?> clazz = (Class<?>) type;
209222
if (clazz.isEnum()) {
210-
jdbcHandlerMap = getJdbcHandlerMapForEnumInterfaces(clazz);
223+
jdbcHandlerMap = getJdbcHandlerMapForEnumInterfaces(clazz, clazz);
211224
if (jdbcHandlerMap == null) {
212-
register(clazz, new EnumTypeHandler(clazz));
225+
register(clazz, getInstance(clazz, defaultEnumTypeHandler));
213226
return TYPE_HANDLER_MAP.get(clazz);
214227
}
215228
} else {
@@ -220,14 +233,20 @@ private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMap(Type type) {
220233
return jdbcHandlerMap;
221234
}
222235

223-
private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMapForEnumInterfaces(Class<?> clazz) {
236+
private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMapForEnumInterfaces(Class<?> clazz, Class<?> enumClazz) {
224237
for (Class<?> iface : clazz.getInterfaces()) {
225238
Map<JdbcType, TypeHandler<?>> jdbcHandlerMap = TYPE_HANDLER_MAP.get(iface);
226239
if (jdbcHandlerMap == null) {
227-
jdbcHandlerMap = getJdbcHandlerMapForEnumInterfaces(iface);
240+
jdbcHandlerMap = getJdbcHandlerMapForEnumInterfaces(iface, enumClazz);
228241
}
229242
if (jdbcHandlerMap != null) {
230-
return jdbcHandlerMap;
243+
// Found a type handler regsiterd to a super interface
244+
HashMap<JdbcType, TypeHandler<?>> newMap = new HashMap<JdbcType, TypeHandler<?>>();
245+
for (Entry<JdbcType, TypeHandler<?>> entry : jdbcHandlerMap.entrySet()) {
246+
// Create a type handler instance with enum type as a constructor arg
247+
newMap.put(entry.getKey(), getInstance(enumClazz, entry.getValue().getClass()));
248+
}
249+
return newMap;
231250
}
232251
}
233252
return null;

0 commit comments

Comments
 (0)