41
41
import java .util .Date ;
42
42
import java .util .EnumMap ;
43
43
import java .util .HashMap ;
44
- import java .util .HashSet ;
45
44
import java .util .Map ;
46
45
import java .util .Map .Entry ;
47
46
import java .util .Set ;
59
58
*/
60
59
public final class TypeHandlerRegistry {
61
60
62
- public static final ObjectTypeHandler OBJECT_TYPE_HANDLER = new ObjectTypeHandler ();
63
-
64
61
private final Map <JdbcType , TypeHandler <?>> jdbcTypeHandlerMap = new EnumMap <>(JdbcType .class );
65
62
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 <>();
67
64
@ Deprecated
68
65
private final Map <Class <?>, TypeHandler <?>> allTypeHandlersMap = new HashMap <>();
69
66
70
67
private static final Map <JdbcType , TypeHandler <?>> NULL_TYPE_HANDLER_MAP = Collections .emptyMap ();
71
68
69
+ @ SuppressWarnings ("rawtypes" )
72
70
private Class <? extends TypeHandler > defaultEnumTypeHandler = EnumTypeHandler .class ;
73
71
74
72
/**
@@ -191,7 +189,7 @@ public TypeHandlerRegistry(Configuration configuration) {
191
189
*
192
190
* @since 3.4.5
193
191
*/
194
- public void setDefaultEnumTypeHandler (Class <? extends TypeHandler > typeHandler ) {
192
+ public void setDefaultEnumTypeHandler (@ SuppressWarnings ( "rawtypes" ) Class <? extends TypeHandler > typeHandler ) {
195
193
this .defaultEnumTypeHandler = typeHandler ;
196
194
}
197
195
@@ -236,6 +234,7 @@ public TypeHandler<?> getTypeHandler(JdbcType jdbcType) {
236
234
return jdbcTypeHandlerMap .get (jdbcType );
237
235
}
238
236
237
+ @ SuppressWarnings ("unchecked" )
239
238
@ Deprecated
240
239
public <T > TypeHandler <T > getTypeHandler (TypeReference <T > javaTypeReference , JdbcType jdbcType ) {
241
240
return (TypeHandler <T >) getTypeHandler (javaTypeReference .getRawType (), jdbcType );
@@ -271,7 +270,7 @@ public TypeHandler<?> getTypeHandler(Type type, JdbcType jdbcType) {
271
270
if (handler == null ) {
272
271
handler = jdbcTypeHandlerMap .get (jdbcType );
273
272
}
274
- return handler != null ? handler : OBJECT_TYPE_HANDLER ;
273
+ return handler != null ? handler : ObjectTypeHandler . INSTANCE ;
275
274
}
276
275
277
276
if (jdbcHandlerMap != null ) {
@@ -290,13 +289,13 @@ public TypeHandler<?> getTypeHandler(Type type, JdbcType jdbcType) {
290
289
if (handler == null && type instanceof ParameterizedType ) {
291
290
handler = getTypeHandler ((Class <?>) ((ParameterizedType ) type ).getRawType (), jdbcType );
292
291
}
293
- // type drives generics here
294
292
return handler ;
295
293
}
296
294
297
295
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 ()) {
300
299
Type registeredType = entry .getKey ();
301
300
if (registeredType .equals (type )) {
302
301
candidate = entry .getValue ();
@@ -316,6 +315,7 @@ private TypeHandler<?> getSmartHandler(Type type, JdbcType jdbcType) {
316
315
}
317
316
}
318
317
}
318
+
319
319
if (candidate == null ) {
320
320
if (type instanceof Class ) {
321
321
Class <?> clazz = (Class <?>) type ;
@@ -331,12 +331,13 @@ private TypeHandler<?> getSmartHandler(Type type, JdbcType jdbcType) {
331
331
}
332
332
return null ;
333
333
}
334
+
334
335
try {
335
- TypeHandler <?> typeHandler = (TypeHandler <?>) candidate .getKey (). newInstance (type );
336
+ TypeHandler <?> typeHandler = (TypeHandler <?>) candidate .newInstance (type );
336
337
register (type , jdbcType , typeHandler );
337
338
return typeHandler ;
338
339
} 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 );
340
341
}
341
342
}
342
343
@@ -501,7 +502,6 @@ public void register(String javaTypeClassName, String typeHandlerClassName) thro
501
502
}
502
503
503
504
public void register (Type javaTypeClass , Class <?> typeHandlerClass ) {
504
- // TODO: change the argument type to Class<? extends TypeHandler<?>> ?
505
505
if (!TypeHandler .class .isAssignableFrom (typeHandlerClass )) {
506
506
throw new IllegalArgumentException (
507
507
String .format ("'%s' does not implement TypeHandler." , typeHandlerClass .getName ()));
@@ -524,26 +524,17 @@ public void register(Type javaTypeClass, Class<?> typeHandlerClass) {
524
524
// java type + jdbc type + handler type
525
525
526
526
public void register (Type javaTypeClass , JdbcType jdbcType , Class <?> typeHandlerClass ) {
527
- @ SuppressWarnings ("unchecked" )
528
- Class <? extends TypeHandler <?>> clazz = (Class <? extends TypeHandler <?>>) typeHandlerClass ;
529
527
for (Constructor <?> constructor : typeHandlerClass .getConstructors ()) {
530
528
if (constructor .getParameterCount () != 1 ) {
531
529
continue ;
532
530
}
533
531
Class <?> argType = constructor .getParameterTypes ()[0 ];
534
532
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 );
544
534
return ;
545
535
}
546
536
}
537
+ // It is not a smart handler
547
538
register (javaTypeClass , jdbcType , getInstance (javaTypeClass , typeHandlerClass ));
548
539
}
549
540
0 commit comments