Skip to content

Commit f3bd219

Browse files
committed
Allow registering a type handler for a common interface of enums.
1 parent 4b465eb commit f3bd219

File tree

2 files changed

+60
-15
lines changed

2 files changed

+60
-15
lines changed

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

Lines changed: 22 additions & 10 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.
@@ -221,20 +221,32 @@ private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMap(Type type) {
221221
}
222222
if (jdbcHandlerMap == null && type instanceof Class) {
223223
Class<?> clazz = (Class<?>) type;
224-
jdbcHandlerMap = getJdbcHandlerMapForSuperclass(clazz);
225-
if (jdbcHandlerMap != null) {
226-
TYPE_HANDLER_MAP.put(type, jdbcHandlerMap);
227-
} else if (clazz.isEnum()) {
228-
register(clazz, new EnumTypeHandler(clazz));
229-
return TYPE_HANDLER_MAP.get(clazz);
224+
if (clazz.isEnum()) {
225+
jdbcHandlerMap = getJdbcHandlerMapForEnumInterfaces(clazz);
226+
if (jdbcHandlerMap == null) {
227+
register(clazz, new EnumTypeHandler(clazz));
228+
return TYPE_HANDLER_MAP.get(clazz);
229+
}
230+
} else {
231+
jdbcHandlerMap = getJdbcHandlerMapForSuperclass(clazz);
230232
}
231233
}
232-
if (jdbcHandlerMap == null) {
233-
TYPE_HANDLER_MAP.put(type, NULL_TYPE_HANDLER_MAP);
234-
}
234+
TYPE_HANDLER_MAP.put(type, jdbcHandlerMap == null ? NULL_TYPE_HANDLER_MAP : jdbcHandlerMap);
235235
return jdbcHandlerMap;
236236
}
237237

238+
private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMapForEnumInterfaces(Class<?> clazz) {
239+
for (Class<?> iface : clazz.getInterfaces()) {
240+
Map<JdbcType, TypeHandler<?>> jdbcHandlerMap = TYPE_HANDLER_MAP.get(iface);
241+
if (jdbcHandlerMap != null) {
242+
return jdbcHandlerMap;
243+
} else {
244+
return getJdbcHandlerMapForEnumInterfaces(iface);
245+
}
246+
}
247+
return null;
248+
}
249+
238250
private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMapForSuperclass(Class<?> clazz) {
239251
Class<?> superclass = clazz.getSuperclass();
240252
if (superclass == null || Object.class.equals(superclass)) {

src/test/java/org/apache/ibatis/type/TypeHandlerRegistryTest.java

Lines changed: 38 additions & 5 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.
@@ -15,10 +15,7 @@
1515
*/
1616
package org.apache.ibatis.type;
1717

18-
import static org.junit.Assert.assertEquals;
19-
import static org.junit.Assert.assertFalse;
20-
import static org.junit.Assert.assertSame;
21-
import static org.junit.Assert.assertTrue;
18+
import static org.junit.Assert.*;
2219

2320
import java.net.URI;
2421
import java.sql.CallableStatement;
@@ -159,4 +156,40 @@ class MyDate2 extends MyDate1 {
159156
}
160157
assertEquals(DateTypeHandler.class, typeHandlerRegistry.getTypeHandler(MyDate2.class).getClass());
161158
}
159+
160+
interface SomeInterface {
161+
}
162+
163+
enum SomeEnum implements SomeInterface {
164+
}
165+
166+
class SomeClass implements SomeInterface {
167+
}
168+
169+
@MappedTypes(SomeInterface.class)
170+
public static class SomeInterfaceTypeHandler<E extends Enum<E> & SomeInterface> extends BaseTypeHandler<E> {
171+
@Override
172+
public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType)
173+
throws SQLException {
174+
}
175+
@Override
176+
public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
177+
return null;
178+
}
179+
@Override
180+
public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
181+
return null;
182+
}
183+
@Override
184+
public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
185+
return null;
186+
}
187+
}
188+
189+
@Test
190+
public void demoTypeHandlerForSuperInterface() {
191+
typeHandlerRegistry.register(SomeInterfaceTypeHandler.class);
192+
assertNull("Registering interface works only for enums.", typeHandlerRegistry.getTypeHandler(SomeClass.class));
193+
assertSame(SomeInterfaceTypeHandler.class, typeHandlerRegistry.getTypeHandler(SomeEnum.class).getClass());
194+
}
162195
}

0 commit comments

Comments
 (0)