Skip to content

Commit c706462

Browse files
committed
Cleaned up a little bit
1 parent c97c5c5 commit c706462

File tree

7 files changed

+34
-68
lines changed

7 files changed

+34
-68
lines changed

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

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
import java.util.Map;
2828
import java.util.Set;
2929

30-
import org.apache.ibatis.io.Resources;
3130
import org.apache.ibatis.mapping.ResultMap;
3231
import org.apache.ibatis.session.Configuration;
3332
import org.apache.ibatis.type.JdbcType;
33+
import org.apache.ibatis.type.ObjectTypeHandler;
3434
import org.apache.ibatis.type.TypeHandler;
3535
import org.apache.ibatis.type.TypeHandlerRegistry;
3636

@@ -93,29 +93,18 @@ public JdbcType getJdbcType(String columnName) {
9393
* @return the type handler
9494
*/
9595
public TypeHandler<?> getTypeHandler(Class<?> propertyType, String columnName) {
96-
TypeHandler<?> handler = null;
97-
Map<Class<?>, TypeHandler<?>> columnHandlers = typeHandlerMap.get(columnName);
98-
if (columnHandlers == null) {
99-
columnHandlers = new HashMap<>();
100-
typeHandlerMap.put(columnName, columnHandlers);
101-
} else {
102-
handler = columnHandlers.get(propertyType);
103-
}
104-
if (handler == null) {
96+
Map<Class<?>, TypeHandler<?>> columnHandlers = typeHandlerMap.computeIfAbsent(columnName, k -> new HashMap<>());
97+
return columnHandlers.computeIfAbsent(propertyType, k -> {
98+
TypeHandler<?> handler = null;
10599
JdbcType jdbcType = getJdbcType(columnName);
106-
if (jdbcType != null && propertyType != null) {
107-
handler = typeHandlerRegistry.getTypeHandler(propertyType, jdbcType);
108-
} else if (propertyType != null) {
109-
handler = typeHandlerRegistry.getTypeHandler(propertyType);
110-
} else if (jdbcType != null) {
111-
handler = typeHandlerRegistry.getTypeHandler(jdbcType);
100+
if (jdbcType != null && k != null) {
101+
handler = typeHandlerRegistry.getTypeHandler(k, jdbcType);
112102
}
113-
if (handler == null) {
114-
handler = TypeHandlerRegistry.OBJECT_TYPE_HANDLER;
103+
if (handler == null && jdbcType != null) {
104+
handler = typeHandlerRegistry.getTypeHandler(jdbcType);
115105
}
116-
columnHandlers.put(propertyType, handler);
117-
}
118-
return handler;
106+
return handler == null ? ObjectTypeHandler.INSTANCE : handler;
107+
});
119108
}
120109

121110
private int getColumnIndex(String columnName) {
@@ -127,18 +116,6 @@ private int getColumnIndex(String columnName) {
127116
return -1;
128117
}
129118

130-
private Class<?> resolveClass(String className) {
131-
try {
132-
// #699 className could be null
133-
if (className != null) {
134-
return Resources.classForName(className);
135-
}
136-
} catch (ClassNotFoundException e) {
137-
// ignore
138-
}
139-
return null;
140-
}
141-
142119
private void loadMappedAndUnmappedColumnNames(ResultMap resultMap, String columnPrefix) throws SQLException {
143120
Set<String> mappedColumnNames = new HashSet<>();
144121
List<String> unmappedColumnNames = new ArrayList<>();

src/main/java/org/apache/ibatis/jdbc/Null.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2025 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.
@@ -70,9 +70,9 @@ public enum Null {
7070

7171
LONGVARBINARY(new BlobTypeHandler(), JdbcType.LONGVARBINARY),
7272

73-
OBJECT(new ObjectTypeHandler(), JdbcType.OTHER),
73+
OBJECT(ObjectTypeHandler.INSTANCE, JdbcType.OTHER),
7474

75-
OTHER(new ObjectTypeHandler(), JdbcType.OTHER),
75+
OTHER(ObjectTypeHandler.INSTANCE, JdbcType.OTHER),
7676

7777
TIMESTAMP(new DateTypeHandler(), JdbcType.TIMESTAMP),
7878

src/main/java/org/apache/ibatis/scripting/defaults/DefaultParameterHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public class DefaultParameterHandler implements ParameterHandler {
5757
private ParameterMetaData paramMetaData;
5858
private MetaObject paramMetaObject;
5959
private HashMap<Class<?>, MetaClass> metaClassCache = new HashMap<>();
60-
private static final TypeHandler<?> nullTypeHandler = new ObjectTypeHandler();
6160
private static final ParameterMetaData nullParameterMetaData = new ParameterMetaData() {
6261
// @formatter:off
6362
public <T> T unwrap(Class<T> iface) throws SQLException { return null; }
@@ -157,7 +156,7 @@ public void setParameters(PreparedStatement ps) {
157156
jdbcType = configuration.getJdbcTypeForNull();
158157
}
159158
if (typeHandler == null) {
160-
typeHandler = nullTypeHandler;
159+
typeHandler = ObjectTypeHandler.INSTANCE;
161160
}
162161
} else if (typeHandler == null) {
163162
if (propertyGenericType == null) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public Object getResult(CallableStatement cs, int columnIndex) throws SQLExcepti
6868
}
6969

7070
private ExecutorException exception() {
71-
// TODO:
7271
return new ExecutorException(
7372
"Multiple type-aware handlers are registered and being looked up without type; javaType=" + javaType
7473
+ ", jdbcType=" + jdbcType + ", type handlers="

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2023 the original author or authors.
2+
* Copyright 2009-2025 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,6 +24,7 @@
2424
* @author Clinton Begin
2525
*/
2626
public class ObjectTypeHandler extends BaseTypeHandler<Object> {
27+
public static final ObjectTypeHandler INSTANCE = new ObjectTypeHandler();
2728

2829
@Override
2930
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType)

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

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import java.util.Date;
4242
import java.util.EnumMap;
4343
import java.util.HashMap;
44-
import java.util.HashSet;
4544
import java.util.Map;
4645
import java.util.Map.Entry;
4746
import java.util.Set;
@@ -59,16 +58,15 @@
5958
*/
6059
public final class TypeHandlerRegistry {
6160

62-
public static final ObjectTypeHandler OBJECT_TYPE_HANDLER = new ObjectTypeHandler();
63-
6461
private final Map<JdbcType, TypeHandler<?>> jdbcTypeHandlerMap = new EnumMap<>(JdbcType.class);
6562
private final Map<Type, Map<JdbcType, TypeHandler<?>>> typeHandlerMap = new ConcurrentHashMap<>();
66-
private final Map<Type, Entry<Constructor<?>, Set<JdbcType>>> smartHandlers = new ConcurrentHashMap<>();
63+
private final ConcurrentHashMap<Type, Constructor<?>> smartHandlers = new ConcurrentHashMap<>();
6764
@Deprecated
6865
private final Map<Class<?>, TypeHandler<?>> allTypeHandlersMap = new HashMap<>();
6966

7067
private static final Map<JdbcType, TypeHandler<?>> NULL_TYPE_HANDLER_MAP = Collections.emptyMap();
7168

69+
@SuppressWarnings("rawtypes")
7270
private Class<? extends TypeHandler> defaultEnumTypeHandler = EnumTypeHandler.class;
7371

7472
/**
@@ -191,7 +189,7 @@ public TypeHandlerRegistry(Configuration configuration) {
191189
*
192190
* @since 3.4.5
193191
*/
194-
public void setDefaultEnumTypeHandler(Class<? extends TypeHandler> typeHandler) {
192+
public void setDefaultEnumTypeHandler(@SuppressWarnings("rawtypes") Class<? extends TypeHandler> typeHandler) {
195193
this.defaultEnumTypeHandler = typeHandler;
196194
}
197195

@@ -236,6 +234,7 @@ public TypeHandler<?> getTypeHandler(JdbcType jdbcType) {
236234
return jdbcTypeHandlerMap.get(jdbcType);
237235
}
238236

237+
@SuppressWarnings("unchecked")
239238
@Deprecated
240239
public <T> TypeHandler<T> getTypeHandler(TypeReference<T> javaTypeReference, JdbcType jdbcType) {
241240
return (TypeHandler<T>) getTypeHandler(javaTypeReference.getRawType(), jdbcType);
@@ -271,7 +270,7 @@ public TypeHandler<?> getTypeHandler(Type type, JdbcType jdbcType) {
271270
if (handler == null) {
272271
handler = jdbcTypeHandlerMap.get(jdbcType);
273272
}
274-
return handler != null ? handler : OBJECT_TYPE_HANDLER;
273+
return handler != null ? handler : ObjectTypeHandler.INSTANCE;
275274
}
276275

277276
if (jdbcHandlerMap != null) {
@@ -290,13 +289,13 @@ public TypeHandler<?> getTypeHandler(Type type, JdbcType jdbcType) {
290289
if (handler == null && type instanceof ParameterizedType) {
291290
handler = getTypeHandler((Class<?>) ((ParameterizedType) type).getRawType(), jdbcType);
292291
}
293-
// type drives generics here
294292
return handler;
295293
}
296294

297295
private TypeHandler<?> getSmartHandler(Type type, JdbcType jdbcType) {
298-
Entry<Constructor<?>, Set<JdbcType>> candidate = null;
299-
for (Entry<Type, Entry<Constructor<?>, Set<JdbcType>>> entry : smartHandlers.entrySet()) {
296+
Constructor<?> candidate = null;
297+
298+
for (Entry<Type, Constructor<?>> entry : smartHandlers.entrySet()) {
300299
Type registeredType = entry.getKey();
301300
if (registeredType.equals(type)) {
302301
candidate = entry.getValue();
@@ -316,6 +315,7 @@ private TypeHandler<?> getSmartHandler(Type type, JdbcType jdbcType) {
316315
}
317316
}
318317
}
318+
319319
if (candidate == null) {
320320
if (type instanceof Class) {
321321
Class<?> clazz = (Class<?>) type;
@@ -331,12 +331,13 @@ private TypeHandler<?> getSmartHandler(Type type, JdbcType jdbcType) {
331331
}
332332
return null;
333333
}
334+
334335
try {
335-
TypeHandler<?> typeHandler = (TypeHandler<?>) candidate.getKey().newInstance(type);
336+
TypeHandler<?> typeHandler = (TypeHandler<?>) candidate.newInstance(type);
336337
register(type, jdbcType, typeHandler);
337338
return typeHandler;
338339
} catch (ReflectiveOperationException e) {
339-
throw new TypeException("Failed to invoke constructor " + candidate.getKey().toString(), e);
340+
throw new TypeException("Failed to invoke constructor " + candidate.toString(), e);
340341
}
341342
}
342343

@@ -501,7 +502,6 @@ public void register(String javaTypeClassName, String typeHandlerClassName) thro
501502
}
502503

503504
public void register(Type javaTypeClass, Class<?> typeHandlerClass) {
504-
// TODO: change the argument type to Class<? extends TypeHandler<?>> ?
505505
if (!TypeHandler.class.isAssignableFrom(typeHandlerClass)) {
506506
throw new IllegalArgumentException(
507507
String.format("'%s' does not implement TypeHandler.", typeHandlerClass.getName()));
@@ -524,26 +524,17 @@ public void register(Type javaTypeClass, Class<?> typeHandlerClass) {
524524
// java type + jdbc type + handler type
525525

526526
public void register(Type javaTypeClass, JdbcType jdbcType, Class<?> typeHandlerClass) {
527-
@SuppressWarnings("unchecked")
528-
Class<? extends TypeHandler<?>> clazz = (Class<? extends TypeHandler<?>>) typeHandlerClass;
529527
for (Constructor<?> constructor : typeHandlerClass.getConstructors()) {
530528
if (constructor.getParameterCount() != 1) {
531529
continue;
532530
}
533531
Class<?> argType = constructor.getParameterTypes()[0];
534532
if (Type.class.equals(argType) || Class.class.equals(argType)) {
535-
smartHandlers.computeIfAbsent(javaTypeClass, k -> {
536-
for (Entry<Constructor<?>, Set<JdbcType>> entry : smartHandlers.values()) {
537-
if (entry.getKey().equals(constructor)) {
538-
return entry;
539-
}
540-
}
541-
// Might have to Collections.synchronizedSet(new HashSet<>())
542-
return Map.entry(constructor, new HashSet<>());
543-
}).getValue().add(jdbcType);
533+
smartHandlers.computeIfAbsent(javaTypeClass, k -> constructor);
544534
return;
545535
}
546536
}
537+
// It is not a smart handler
547538
register(javaTypeClass, jdbcType, getInstance(javaTypeClass, typeHandlerClass));
548539
}
549540

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
@Deprecated
3434
public class UnknownTypeHandler extends BaseTypeHandler<Object> {
3535

36-
private static final ObjectTypeHandler OBJECT_TYPE_HANDLER = new ObjectTypeHandler();
3736
// TODO Rename to 'configuration' after removing the 'configuration' property(deprecated property) on parent class
3837
private final Configuration config;
3938
private final Supplier<TypeHandlerRegistry> typeHandlerRegistrySupplier;
@@ -82,7 +81,7 @@ public Object getNullableResult(ResultSet rs, String columnName) throws SQLExcep
8281
public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
8382
TypeHandler<?> handler = resolveTypeHandler(rs.getMetaData(), columnIndex);
8483
if (handler == null || handler instanceof UnknownTypeHandler) {
85-
handler = OBJECT_TYPE_HANDLER;
84+
handler = ObjectTypeHandler.INSTANCE;
8685
}
8786
return handler.getResult(rs, columnIndex);
8887
}
@@ -95,12 +94,12 @@ public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQ
9594
private TypeHandler<?> resolveTypeHandler(Object parameter, JdbcType jdbcType) {
9695
TypeHandler<?> handler;
9796
if (parameter == null) {
98-
handler = OBJECT_TYPE_HANDLER;
97+
handler = ObjectTypeHandler.INSTANCE;
9998
} else {
10099
handler = typeHandlerRegistrySupplier.get().getTypeHandler(parameter.getClass(), jdbcType);
101100
// check if handler is null (issue #270)
102101
if (handler == null || handler instanceof UnknownTypeHandler) {
103-
handler = OBJECT_TYPE_HANDLER;
102+
handler = ObjectTypeHandler.INSTANCE;
104103
}
105104
}
106105
return handler;
@@ -123,7 +122,7 @@ private TypeHandler<?> resolveTypeHandler(ResultSet rs, String column) {
123122
handler = resolveTypeHandler(rsmd, columnIndex);
124123
}
125124
if (handler == null || handler instanceof UnknownTypeHandler) {
126-
handler = OBJECT_TYPE_HANDLER;
125+
handler = ObjectTypeHandler.INSTANCE;
127126
}
128127
return handler;
129128
} catch (SQLException e) {

0 commit comments

Comments
 (0)