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

mybatis / mybatis-3 / 4017

02 Dec 2026 01:21PM UTC coverage: 87.564% (+0.1%) from 87.454%
4017

Pull #3809

github

web-flow
Merge de6182685 into 19b82e6ae
Pull Request #3809: Fix JDBC array cleanup when binding or extraction fails

3907 of 4711 branches covered (82.93%)

22 of 22 new or added lines in 1 file covered. (100.0%)

152 existing lines in 10 files now uncovered.

10034 of 11459 relevant lines covered (87.56%)

0.88 hits per line

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

93.89
/src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java
1
/*
2
 *    Copyright 2009-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.apache.ibatis.type;
17

18
import java.io.InputStream;
19
import java.io.Reader;
20
import java.lang.reflect.Constructor;
21
import java.lang.reflect.Modifier;
22
import java.lang.reflect.ParameterizedType;
23
import java.lang.reflect.Type;
24
import java.math.BigDecimal;
25
import java.math.BigInteger;
26
import java.sql.Time;
27
import java.sql.Timestamp;
28
import java.time.Instant;
29
import java.time.LocalDate;
30
import java.time.LocalDateTime;
31
import java.time.LocalTime;
32
import java.time.Month;
33
import java.time.OffsetDateTime;
34
import java.time.OffsetTime;
35
import java.time.Year;
36
import java.time.YearMonth;
37
import java.time.ZonedDateTime;
38
import java.time.chrono.JapaneseDate;
39
import java.util.Arrays;
40
import java.util.Collection;
41
import java.util.Collections;
42
import java.util.Date;
43
import java.util.EnumMap;
44
import java.util.HashMap;
45
import java.util.Map;
46
import java.util.Map.Entry;
47
import java.util.Set;
48
import java.util.concurrent.ConcurrentHashMap;
49

50
import org.apache.ibatis.binding.MapperMethod.ParamMap;
51
import org.apache.ibatis.io.ResolverUtil;
52
import org.apache.ibatis.io.Resources;
53
import org.apache.ibatis.reflection.TypeParameterResolver;
54
import org.apache.ibatis.session.Configuration;
55
import org.gaul.modernizer_maven_annotations.SuppressModernizer;
56

57
/**
58
 * @author Clinton Begin
59
 * @author Kazuki Shimizu
60
 */
61
public final class TypeHandlerRegistry {
62

63
  private final Map<JdbcType, TypeHandler<?>> jdbcTypeHandlerMap = new EnumMap<>(JdbcType.class);
1 ✔
64
  private final Map<Type, Map<JdbcType, TypeHandler<?>>> typeHandlerMap = new ConcurrentHashMap<>();
1 ✔
65
  private final ConcurrentHashMap<Type, Constructor<?>> smartHandlers = new ConcurrentHashMap<>();
1 ✔
66
  private final Map<Class<?>, TypeHandler<?>> allTypeHandlersMap = new HashMap<>();
1 ✔
67

68
  private static final Map<JdbcType, TypeHandler<?>> NULL_TYPE_HANDLER_MAP = createNullTypeHandlerMap();
1 ✔
69

70
  // Map.of() does not permit null keys; this map is intentionally queried with null JdbcType
71
  @SuppressModernizer
72
  private static Map<JdbcType, TypeHandler<?>> createNullTypeHandlerMap() {
73
    return Collections.emptyMap();
1 ✔
74
  }
75

76
  @SuppressWarnings("rawtypes")
1 ✔
77
  private Class<? extends TypeHandler> defaultEnumTypeHandler = EnumTypeHandler.class;
78

79
  /**
80
   * The default constructor.
81
   */
82
  public TypeHandlerRegistry() {
83
    this(new Configuration());
1 ✔
84
  }
1 ✔
85

86
  /**
87
   * The constructor that pass the MyBatis configuration.
88
   *
89
   * @param configuration
90
   *          a MyBatis configuration
91
   *
92
   * @since 3.5.4
93
   */
94
  public TypeHandlerRegistry(Configuration configuration) {
1 ✔
95
    // If a handler is registered against null JDBC type, it is the default handler for the Java type. Users can
96
    // override the default handler (e.g. `register(boolean.class, null, new YNBooleanTypeHandler())` or register a
97
    // custom handler for a specific Java-JDBC type combination (e.g. `register(boolean.class, JdbcType.CHAR, new
98
    // YNBooleanTypeHandler())`).
99
    register(new Type[] { Boolean.class, boolean.class }, new JdbcType[] { null }, BooleanTypeHandler.INSTANCE);
1 ✔
100
    register(new Type[] { Byte.class, byte.class }, new JdbcType[] { null }, ByteTypeHandler.INSTANCE);
1 ✔
101
    register(new Type[] { Short.class, short.class }, new JdbcType[] { null }, ShortTypeHandler.INSTANCE);
1 ✔
102
    register(new Type[] { Integer.class, int.class }, new JdbcType[] { null }, IntegerTypeHandler.INSTANCE);
1 ✔
103
    register(new Type[] { Long.class, long.class }, new JdbcType[] { null }, LongTypeHandler.INSTANCE);
1 ✔
104
    register(new Type[] { Float.class, float.class }, new JdbcType[] { null }, FloatTypeHandler.INSTANCE);
1 ✔
105
    register(new Type[] { Double.class, double.class }, new JdbcType[] { null }, DoubleTypeHandler.INSTANCE);
1 ✔
106
    register(new Type[] { Character.class, char.class }, new JdbcType[] { null }, new CharacterTypeHandler());
1 ✔
107
    register((Type) String.class, null, StringTypeHandler.INSTANCE);
1 ✔
108
    register((Type) Reader.class, null, new ClobReaderTypeHandler());
1 ✔
109
    register((Type) BigInteger.class, null, new BigIntegerTypeHandler());
1 ✔
110
    register((Type) BigDecimal.class, null, BigDecimalTypeHandler.INSTANCE);
1 ✔
111
    register((Type) InputStream.class, null, new BlobInputStreamTypeHandler());
1 ✔
112
    register((Type) Byte[].class, null, new ByteObjectArrayTypeHandler());
1 ✔
113
    register((Type) byte[].class, null, ByteArrayTypeHandler.INSTANCE);
1 ✔
114
    register((Type) Date.class, null, DateTypeHandler.INSTANCE);
1 ✔
115
    register((Type) java.sql.Date.class, null, new SqlDateTypeHandler());
1 ✔
116
    register((Type) Time.class, null, new SqlTimeTypeHandler());
1 ✔
117
    register((Type) Timestamp.class, null, new SqlTimestampTypeHandler());
1 ✔
118
    register((Type) Instant.class, null, new InstantTypeHandler());
1 ✔
119
    register((Type) LocalDateTime.class, null, new LocalDateTimeTypeHandler());
1 ✔
120
    register((Type) LocalDate.class, null, new LocalDateTypeHandler());
1 ✔
121
    register((Type) LocalTime.class, null, new LocalTimeTypeHandler());
1 ✔
122
    register((Type) OffsetDateTime.class, null, new OffsetDateTimeTypeHandler());
1 ✔
123
    register((Type) OffsetTime.class, null, new OffsetTimeTypeHandler());
1 ✔
124
    register((Type) ZonedDateTime.class, null, new ZonedDateTimeTypeHandler());
1 ✔
125
    register((Type) Month.class, null, new MonthTypeHandler());
1 ✔
126
    register((Type) Year.class, null, new YearTypeHandler());
1 ✔
127
    register((Type) YearMonth.class, null, new YearMonthTypeHandler());
1 ✔
128
    register((Type) JapaneseDate.class, null, new JapaneseDateTypeHandler());
1 ✔
129

130
    // These type handlers are used only for specific combinations of Java type and JDBC type.
131
    register((Type) String.class, JdbcType.CLOB, ClobTypeHandler.INSTANCE);
1 ✔
132
    register((Type) String.class, JdbcType.NCLOB, NClobTypeHandler.INSTANCE);
1 ✔
133
    register(new Type[] { String.class }, new JdbcType[] { JdbcType.NCHAR, JdbcType.NVARCHAR, JdbcType.LONGNVARCHAR },
1 ✔
134
        NStringTypeHandler.INSTANCE);
135
    register(new Type[] { Byte[].class }, new JdbcType[] { JdbcType.BLOB, JdbcType.LONGVARBINARY },
1 ✔
136
        new BlobByteObjectArrayTypeHandler());
137
    register(new Type[] { byte[].class }, new JdbcType[] { JdbcType.BLOB, JdbcType.LONGVARBINARY },
1 ✔
138
        BlobTypeHandler.INSTANCE);
139
    register((Type) Date.class, JdbcType.DATE, DateOnlyTypeHandler.INSTANCE);
1 ✔
140
    register((Type) Date.class, JdbcType.TIME, TimeOnlyTypeHandler.INSTANCE);
1 ✔
141
    register((Type) String.class, JdbcType.SQLXML, new SqlxmlTypeHandler());
1 ✔
142

143
    // Type handlers in the `jdbcTypeHandlerMap` are used when Java type is unknown or
144
    // as a last resort when no matching handler is found for the target Java type.
145
    // It is also used in some internal purposes like creating cache keys.
146
    // Although it is possible for users to override these mappings via register(JdbcType, TypeHandler),
147
    // it might have unexpected side-effect.
148
    // To configure type handlers for mapping to Map, for example, it is recommended to call the 3-args
149
    // version of register method. e.g. register(Object.class, JdbcType.DATE, new DateTypeHandler())
150
    jdbcTypeHandlerMap.put(JdbcType.BOOLEAN, BooleanTypeHandler.INSTANCE);
1 ✔
151
    jdbcTypeHandlerMap.put(JdbcType.BIT, BooleanTypeHandler.INSTANCE);
1 ✔
152
    jdbcTypeHandlerMap.put(JdbcType.TINYINT, ByteTypeHandler.INSTANCE);
1 ✔
153
    jdbcTypeHandlerMap.put(JdbcType.SMALLINT, ShortTypeHandler.INSTANCE);
1 ✔
154
    jdbcTypeHandlerMap.put(JdbcType.INTEGER, IntegerTypeHandler.INSTANCE);
1 ✔
155
    jdbcTypeHandlerMap.put(JdbcType.BIGINT, LongTypeHandler.INSTANCE);
1 ✔
156
    jdbcTypeHandlerMap.put(JdbcType.REAL, FloatTypeHandler.INSTANCE); // As per JDBC spec
1 ✔
157
    jdbcTypeHandlerMap.put(JdbcType.FLOAT, DoubleTypeHandler.INSTANCE); // As per JDBC spec
1 ✔
158
    jdbcTypeHandlerMap.put(JdbcType.DOUBLE, DoubleTypeHandler.INSTANCE);
1 ✔
159
    jdbcTypeHandlerMap.put(JdbcType.DECIMAL, BigDecimalTypeHandler.INSTANCE);
1 ✔
160
    jdbcTypeHandlerMap.put(JdbcType.NUMERIC, BigDecimalTypeHandler.INSTANCE);
1 ✔
161
    jdbcTypeHandlerMap.put(JdbcType.CHAR, StringTypeHandler.INSTANCE);
1 ✔
162
    jdbcTypeHandlerMap.put(JdbcType.VARCHAR, StringTypeHandler.INSTANCE);
1 ✔
163
    jdbcTypeHandlerMap.put(JdbcType.LONGVARCHAR, StringTypeHandler.INSTANCE);
1 ✔
164
    jdbcTypeHandlerMap.put(JdbcType.CLOB, ClobTypeHandler.INSTANCE);
1 ✔
165
    jdbcTypeHandlerMap.put(JdbcType.NVARCHAR, NStringTypeHandler.INSTANCE);
1 ✔
166
    jdbcTypeHandlerMap.put(JdbcType.NCHAR, NStringTypeHandler.INSTANCE);
1 ✔
167
    jdbcTypeHandlerMap.put(JdbcType.LONGNVARCHAR, NStringTypeHandler.INSTANCE);
1 ✔
168
    jdbcTypeHandlerMap.put(JdbcType.NCLOB, NClobTypeHandler.INSTANCE);
1 ✔
169
    jdbcTypeHandlerMap.put(JdbcType.ARRAY, new ArrayTypeHandler());
1 ✔
170
    jdbcTypeHandlerMap.put(JdbcType.BINARY, ByteArrayTypeHandler.INSTANCE);
1 ✔
171
    jdbcTypeHandlerMap.put(JdbcType.VARBINARY, ByteArrayTypeHandler.INSTANCE);
1 ✔
172
    jdbcTypeHandlerMap.put(JdbcType.LONGVARBINARY, ByteArrayTypeHandler.INSTANCE);
1 ✔
173
    jdbcTypeHandlerMap.put(JdbcType.BLOB, BlobTypeHandler.INSTANCE);
1 ✔
174
    jdbcTypeHandlerMap.put(JdbcType.TIMESTAMP, DateTypeHandler.INSTANCE);
1 ✔
175
    jdbcTypeHandlerMap.put(JdbcType.DATE, DateOnlyTypeHandler.INSTANCE);
1 ✔
176
    jdbcTypeHandlerMap.put(JdbcType.TIME, TimeOnlyTypeHandler.INSTANCE);
1 ✔
177
  }
1 ✔
178

179
  /**
180
   * Set a default {@link TypeHandler} class for {@link Enum}. A default {@link TypeHandler} is
181
   * {@link org.apache.ibatis.type.EnumTypeHandler}.
182
   *
183
   * @param typeHandler
184
   *          a type handler class for {@link Enum}
185
   *
186
   * @since 3.4.5
187
   */
188
  public void setDefaultEnumTypeHandler(@SuppressWarnings("rawtypes") Class<? extends TypeHandler> typeHandler) {
189
    this.defaultEnumTypeHandler = typeHandler;
×
190
  }
×
191

192
  public boolean hasTypeHandler(Type javaType) {
193
    return hasTypeHandler(javaType, null);
1 ✔
194
  }
195

196
  @Deprecated(since = "3.6.0", forRemoval = true)
197
  public boolean hasTypeHandler(TypeReference<?> javaTypeReference) {
198
    return hasTypeHandler(javaTypeReference, null);
×
199
  }
200

201
  public boolean hasTypeHandler(Type javaType, JdbcType jdbcType) {
202
    return javaType != null && getTypeHandler(javaType, jdbcType) != null;
1 !
203
  }
204

205
  @Deprecated(since = "3.6.0", forRemoval = true)
206
  public boolean hasTypeHandler(TypeReference<?> javaTypeReference, JdbcType jdbcType) {
207
    return javaTypeReference != null && getTypeHandler(javaTypeReference, jdbcType) != null;
×
208
  }
209

210
  @Deprecated(since = "3.6.0", forRemoval = true)
211
  public TypeHandler<?> getMappingTypeHandler(Class<? extends TypeHandler<?>> handlerType) {
212
    return allTypeHandlersMap.get(handlerType);
1 ✔
213
  }
214

215
  public TypeHandler<?> getTypeHandler(Type type) {
216
    return getTypeHandler(type, null);
1 ✔
217
  }
218

219
  @Deprecated(since = "3.6.0", forRemoval = true)
220
  public <T> TypeHandler<T> getTypeHandler(TypeReference<T> javaTypeReference) {
221
    return getTypeHandler(javaTypeReference, null);
1 ✔
222
  }
223

224
  public TypeHandler<?> getTypeHandler(JdbcType jdbcType) {
225
    return jdbcTypeHandlerMap.get(jdbcType);
1 ✔
226
  }
227

228
  @SuppressWarnings("unchecked")
229
  @Deprecated(since = "3.6.0", forRemoval = true)
230
  public <T> TypeHandler<T> getTypeHandler(TypeReference<T> javaTypeReference, JdbcType jdbcType) {
231
    return (TypeHandler<T>) getTypeHandler(javaTypeReference.getRawType(), jdbcType);
1 ✔
232
  }
233

234
  public TypeHandler<?> getTypeHandler(Type type, JdbcType jdbcType, Class<? extends TypeHandler<?>> typeHandlerClass) {
235
    TypeHandler<?> typeHandler = getTypeHandler(type, jdbcType);
1 ✔
236
    if (typeHandler != null && (typeHandlerClass == null || typeHandler.getClass().equals(typeHandlerClass))) {
1 ✔
237
      return typeHandler;
1 ✔
238
    }
239
    if (typeHandlerClass == null) {
1 ✔
240
      typeHandler = getSmartHandler(type, jdbcType);
1 ✔
241
    } else {
242
      typeHandler = getMappingTypeHandler(typeHandlerClass);
1 ✔
243
      if (typeHandler == null) {
1 ✔
244
        typeHandler = getInstance(type, typeHandlerClass);
1 ✔
245
      }
246
    }
247
    return typeHandler;
1 ✔
248
  }
249

250
  public TypeHandler<?> getTypeHandler(Type type, JdbcType jdbcType) {
251
    if (ParamMap.class.equals(type)) {
1 ✔
252
      return null;
1 ✔
253
    } else if (type == null) {
1 ✔
254
      return getTypeHandler(jdbcType);
1 ✔
255
    }
256

257
    TypeHandler<?> handler = null;
1 ✔
258
    Map<JdbcType, TypeHandler<?>> jdbcHandlerMap = getJdbcHandlerMap(type);
1 ✔
259

260
    if (Object.class.equals(type)) {
1 ✔
261
      if (jdbcHandlerMap != null) {
1 ✔
262
        handler = jdbcHandlerMap.get(jdbcType);
1 ✔
263
      }
264
      return handler;
1 ✔
265
    }
266

267
    if (jdbcHandlerMap != null) {
1 ✔
268
      handler = jdbcHandlerMap.get(jdbcType);
1 ✔
269
      if (handler == null) {
1 ✔
270
        handler = jdbcHandlerMap.get(null);
1 ✔
271
      }
272
      if (handler == null) {
1 ✔
273
        // #591
274
        handler = pickSoleHandler(jdbcHandlerMap);
1 ✔
275
      }
276
    }
277
    if (handler == null) {
1 ✔
278
      handler = getSmartHandler(type, jdbcType);
1 ✔
279
    }
280
    if (handler == null && type instanceof ParameterizedType) {
1 ✔
281
      handler = getTypeHandler((Class<?>) ((ParameterizedType) type).getRawType(), jdbcType);
1 ✔
282
    }
283
    return handler;
1 ✔
284
  }
285

286
  private TypeHandler<?> getSmartHandler(Type type, JdbcType jdbcType) {
287
    Constructor<?> candidate = null;
1 ✔
288

289
    for (Entry<Type, Constructor<?>> entry : smartHandlers.entrySet()) {
1 ✔
290
      Type registeredType = entry.getKey();
1 ✔
291
      if (registeredType.equals(type)) {
1 ✔
292
        candidate = entry.getValue();
1 ✔
293
        break;
1 ✔
294
      }
295
      if (registeredType instanceof Class) {
1 ✔
296
        if (type instanceof Class && ((Class<?>) registeredType).isAssignableFrom((Class<?>) type)) {
1 !
297
          candidate = entry.getValue();
1 ✔
298
        }
299
      } else if (registeredType instanceof ParameterizedType) {
1 !
300
        Class<?> registeredClass = (Class<?>) ((ParameterizedType) registeredType).getRawType();
1 ✔
301
        if (type instanceof ParameterizedType) {
1 ✔
302
          Class<?> clazz = (Class<?>) ((ParameterizedType) type).getRawType();
1 ✔
303
          if (registeredClass.isAssignableFrom(clazz)) {
1 ✔
304
            candidate = entry.getValue();
1 ✔
305
          }
306
        }
307
      }
308
    }
1 ✔
309

310
    if (candidate == null) {
1 ✔
311
      if (type instanceof Class) {
1 ✔
312
        Class<?> clazz = (Class<?>) type;
1 ✔
313
        if (Enum.class.isAssignableFrom(clazz)) {
1 ✔
314
          Class<?> enumClass = (clazz.isAnonymousClass() || !clazz.isEnum()) ? clazz.getSuperclass() : clazz;
1 !
315
          TypeHandler<?> enumHandler = getInstance(enumClass, defaultEnumTypeHandler);
1 ✔
316
          register(new Type[] { enumClass }, new JdbcType[] { jdbcType }, enumHandler);
1 ✔
317
          return enumHandler;
1 ✔
318
        }
319
      }
320
      return null;
1 ✔
321
    }
322

323
    try {
324
      TypeHandler<?> typeHandler = (TypeHandler<?>) candidate.newInstance(type);
1 ✔
325
      register(type, jdbcType, typeHandler);
1 ✔
326
      return typeHandler;
1 ✔
327
    } catch (ReflectiveOperationException e) {
×
328
      throw new TypeException("Failed to invoke constructor " + candidate.toString(), e);
×
329
    }
330
  }
331

332
  private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMap(Type type) {
333
    Map<JdbcType, TypeHandler<?>> jdbcHandlerMap = typeHandlerMap.get(type);
1 ✔
334
    if (jdbcHandlerMap != null) {
1 ✔
335
      return NULL_TYPE_HANDLER_MAP.equals(jdbcHandlerMap) ? null : jdbcHandlerMap;
1 ✔
336
    }
337
    if (type instanceof Class) {
1 ✔
338
      Class<?> clazz = (Class<?>) type;
1 ✔
339
      if (!Enum.class.isAssignableFrom(clazz)) {
1 ✔
340
        jdbcHandlerMap = getJdbcHandlerMapForSuperclass(clazz);
1 ✔
341
      }
342
    }
343
    typeHandlerMap.put(type, jdbcHandlerMap == null ? NULL_TYPE_HANDLER_MAP : jdbcHandlerMap);
1 ✔
344
    return jdbcHandlerMap;
1 ✔
345
  }
346

347
  private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMapForSuperclass(Class<?> clazz) {
348
    Class<?> superclass = clazz.getSuperclass();
1 ✔
349
    if (superclass == null || Object.class.equals(superclass)) {
1 ✔
350
      return null;
1 ✔
351
    }
352
    Map<JdbcType, TypeHandler<?>> jdbcHandlerMap = typeHandlerMap.get(superclass);
1 ✔
353
    if (jdbcHandlerMap != null) {
1 ✔
354
      return jdbcHandlerMap;
1 ✔
355
    }
356
    return getJdbcHandlerMapForSuperclass(superclass);
1 ✔
357
  }
358

359
  private TypeHandler<?> pickSoleHandler(Map<JdbcType, TypeHandler<?>> jdbcHandlerMap) {
360
    TypeHandler<?> soleHandler = null;
1 ✔
361
    for (TypeHandler<?> handler : jdbcHandlerMap.values()) {
1 ✔
362
      if (soleHandler == null) {
1 !
363
        soleHandler = handler;
1 ✔
364
      } else if (!handler.getClass().equals(soleHandler.getClass())) {
×
365
        // More than one type handlers registered.
366
        return null;
×
367
      }
368
    }
1 ✔
369
    return soleHandler;
1 ✔
370
  }
371

372
  public void register(JdbcType mappedJdbcType, TypeHandler<?> handler) {
373
    jdbcTypeHandlerMap.put(mappedJdbcType, handler);
×
374
  }
×
375

376
  //
377
  // REGISTER INSTANCE
378
  //
379

380
  // Only handler
381

382
  public <T> void register(TypeHandler<T> handler) {
383
    register(mappedJavaTypes(handler.getClass()), mappedJdbcTypes(handler.getClass()), handler);
1 ✔
384
  }
1 ✔
385

386
  // java type + handler
387

388
  public void register(Class<?> mappedJavaType, TypeHandler<?> handler) {
389
    register((Type) mappedJavaType, handler);
1 ✔
390
  }
1 ✔
391

392
  private void register(Type mappedJavaType, TypeHandler<?> handler) {
393
    register(new Type[] { mappedJavaType }, mappedJdbcTypes(handler.getClass()), handler);
1 ✔
394
  }
1 ✔
395

396
  @Deprecated(since = "3.6.0", forRemoval = true)
397
  public <T> void register(TypeReference<T> javaTypeReference, TypeHandler<? extends T> handler) {
398
    register(javaTypeReference.getRawType(), handler);
1 ✔
399
  }
1 ✔
400

401
  // java type + jdbc type + handler
402

403
  /**
404
   * @deprecated Use {@link #register(Type, JdbcType, TypeHandler)} instead.
405
   */
406
  @Deprecated(since = "3.6.1", forRemoval = true)
407
  public void register(Class<?> mappedJavaType, JdbcType mappedJdbcType, TypeHandler<?> handler) {
408
    register((Type) mappedJavaType, mappedJdbcType, handler);
1 ✔
409
  }
1 ✔
410

411
  public void register(Type mappedJavaType, JdbcType mappedJdbcType, TypeHandler<?> handler) {
412
    register(new Type[] { mappedJavaType }, new JdbcType[] { mappedJdbcType }, handler);
1 ✔
413
  }
1 ✔
414

415
  private void register(Type[] mappedJavaTypes, JdbcType[] mappedJdbcTypes, TypeHandler<?> handler) {
416
    for (Type javaType : mappedJavaTypes) {
1 ✔
417
      if (javaType == null) {
1 ✔
418
        continue;
1 ✔
419
      }
420

421
      typeHandlerMap.compute(javaType, (k, v) -> {
1 ✔
422
        Map<JdbcType, TypeHandler<?>> map = (v == null || v == NULL_TYPE_HANDLER_MAP ? new HashMap<>() : v);
1 ✔
423
        for (JdbcType jdbcType : mappedJdbcTypes) {
1 ✔
424
          map.put(jdbcType, handler);
1 ✔
425
        }
426
        return map;
1 ✔
427
      });
428

429
      if (javaType instanceof ParameterizedType) {
1 ✔
430
        // MEMO: add annotation to skip this?
431
        Type rawType = ((ParameterizedType) javaType).getRawType();
1 ✔
432
        typeHandlerMap.compute(rawType, (k, v) -> {
1 ✔
433
          Map<JdbcType, TypeHandler<?>> map = (v == null || v == NULL_TYPE_HANDLER_MAP ? new HashMap<>() : v);
1 !
434
          for (JdbcType jdbcType : mappedJdbcTypes) {
1 ✔
435
            map.merge(jdbcType, handler, (handler1, handler2) -> handler1.equals(handler2) ? handler1
1 !
436
                : new ConflictedTypeHandler((Class<?>) rawType, jdbcType, handler1, handler2));
1 ✔
437
          }
438
          return map;
1 ✔
439
        });
440
      }
441
    }
442

443
    allTypeHandlersMap.put(handler.getClass(), handler);
1 ✔
444
  }
1 ✔
445

446
  //
447
  // REGISTER CLASS
448
  //
449

450
  // Only handler type
451

452
  public void register(Class<?> handlerClass) {
453
    register(mappedJavaTypes(handlerClass), mappedJdbcTypes(handlerClass), handlerClass);
1 ✔
454
  }
1 ✔
455

456
  // java type + handler type
457

458
  @Deprecated(since = "3.6.0", forRemoval = true)
459
  public void register(String javaTypeClassName, String typeHandlerClassName) throws ClassNotFoundException {
UNCOV
460
    register(Resources.classForName(javaTypeClassName), Resources.classForName(typeHandlerClassName));
×
UNCOV
461
  }
×
462

463
  public void register(Type mappedJavaType, Class<?> handlerClass) {
464
    register(new Type[] { mappedJavaType }, mappedJdbcTypes(handlerClass), handlerClass);
1 ✔
465
  }
1 ✔
466

467
  // java type + jdbc type + handler type
468

469
  public void register(Type mappedJavaType, JdbcType mappedJdbcType, Class<?> handlerClass) {
470
    register(new Type[] { mappedJavaType }, new JdbcType[] { mappedJdbcType }, handlerClass);
1 ✔
471
  }
1 ✔
472

473
  private void register(Type[] mappedJavaTypes, JdbcType[] mappedJdbcTypes, Class<?> handlerClass) {
474
    if (!TypeHandler.class.isAssignableFrom(handlerClass)) {
1 !
UNCOV
475
      throw new IllegalArgumentException(String.format("'%s' does not implement TypeHandler.", handlerClass.getName()));
×
476
    }
477
    for (Constructor<?> constructor : handlerClass.getConstructors()) {
1 ✔
478
      if (constructor.getParameterCount() != 1) {
1 ✔
479
        continue;
1 ✔
480
      }
481
      Class<?> argType = constructor.getParameterTypes()[0];
1 ✔
482
      if (Type.class.equals(argType) || Class.class.equals(argType)) {
1 !
483
        for (Type javaType : mappedJavaTypes) {
1 ✔
484
          smartHandlers.computeIfAbsent(javaType, k -> constructor);
1 ✔
485
        }
486
        return;
1 ✔
487
      }
488
    }
489
    // It is not a smart handler
490
    register(mappedJavaTypes, mappedJdbcTypes, getInstance(null, handlerClass));
1 ✔
491
  }
1 ✔
492

493
  private Type[] mappedJavaTypes(Class<?> clazz) {
494
    MappedTypes mappedTypesAnno = clazz.getAnnotation(MappedTypes.class);
1 ✔
495
    if (mappedTypesAnno != null) {
1 ✔
496
      return mappedTypesAnno.value();
1 ✔
497
    }
498
    return TypeParameterResolver.resolveClassTypeParams(TypeHandler.class, clazz);
1 ✔
499
  }
500

501
  private JdbcType[] mappedJdbcTypes(Class<?> clazz) {
502
    MappedJdbcTypes mappedJdbcTypesAnno = clazz.getAnnotation(MappedJdbcTypes.class);
1 ✔
503
    if (mappedJdbcTypesAnno != null) {
1 ✔
504
      JdbcType[] jdbcTypes = mappedJdbcTypesAnno.value();
1 ✔
505
      if (mappedJdbcTypesAnno.includeNullJdbcType()) {
1 ✔
506
        int newLength = jdbcTypes.length + 1;
1 ✔
507
        jdbcTypes = Arrays.copyOf(jdbcTypes, newLength);
1 ✔
508
        jdbcTypes[newLength - 1] = null;
1 ✔
509
      }
510
      return jdbcTypes;
1 ✔
511
    }
512
    return new JdbcType[] { null };
1 ✔
513
  }
514

515
  // Construct a handler (used also from Builders)
516

517
  @SuppressWarnings("unchecked")
518
  public <T> TypeHandler<T> getInstance(Type javaType, Class<?> handlerClass) {
519
    try {
520
      if (javaType != null) {
1 ✔
521
        // Try constructor with Type argument
522
        try {
523
          return (TypeHandler<T>) handlerClass.getConstructor(Type.class).newInstance(javaType);
1 ✔
524
        } catch (NoSuchMethodException ignored) {
1 ✔
525
        }
526
        // Try constructor with Class argument
527
        Class<?> classArg = rawClass(javaType);
1 ✔
528
        if (classArg != null) {
1 !
529
          try {
530
            return (TypeHandler<T>) handlerClass.getConstructor(Class.class).newInstance(classArg);
1 ✔
531
          } catch (NoSuchMethodException ignored) {
1 ✔
532
          }
533
        }
534
      }
535
      // Try no-arg constructor
536
      try {
537
        return (TypeHandler<T>) handlerClass.getConstructor().newInstance();
1 ✔
538
      } catch (NoSuchMethodException e) {
1 ✔
539
        throw new TypeException("Unable to find a usable constructor for " + handlerClass, e);
1 ✔
540
      }
UNCOV
541
    } catch (ReflectiveOperationException e) {
×
UNCOV
542
      throw new TypeException("Failed to invoke constructor for handler " + handlerClass, e);
×
543
    }
544
  }
545

546
  private static Class<?> rawClass(Type javaType) {
547
    if (javaType instanceof Class) {
1 ✔
548
      return (Class<?>) javaType;
1 ✔
549
    }
550
    if (javaType instanceof ParameterizedType) {
1 !
551
      return (Class<?>) ((ParameterizedType) javaType).getRawType();
1 ✔
552
    }
UNCOV
553
    return null;
×
554
  }
555

556
  // scan
557

558
  public void register(String packageName) {
559
    ResolverUtil<Class<?>> resolverUtil = new ResolverUtil<>();
1 ✔
560
    resolverUtil.find(new ResolverUtil.IsA(TypeHandler.class), packageName);
1 ✔
561
    Set<Class<? extends Class<?>>> handlerSet = resolverUtil.getClasses();
1 ✔
562
    for (Class<?> type : handlerSet) {
1 ✔
563
      // Ignore inner classes and interfaces (including package-info.java) and abstract classes
564
      if (!type.isAnonymousClass() && !type.isInterface() && !Modifier.isAbstract(type.getModifiers())) {
1 !
565
        register(type);
1 ✔
566
      }
567
    }
1 ✔
568
  }
1 ✔
569

570
  // get information
571

572
  /**
573
   * Gets the type handlers. Used by mybatis-guice.
574
   *
575
   * @return the type handlers
576
   *
577
   * @since 3.2.2
578
   */
579
  public Collection<TypeHandler<?>> getTypeHandlers() {
580
    return Collections.unmodifiableCollection(allTypeHandlersMap.values());
1 ✔
581
  }
582

583
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc