Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2016 the original author or authors.
* Copyright 2009-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -221,20 +221,32 @@ private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMap(Type type) {
}
if (jdbcHandlerMap == null && type instanceof Class) {
Class<?> clazz = (Class<?>) type;
jdbcHandlerMap = getJdbcHandlerMapForSuperclass(clazz);
if (jdbcHandlerMap != null) {
TYPE_HANDLER_MAP.put(type, jdbcHandlerMap);
} else if (clazz.isEnum()) {
register(clazz, new EnumTypeHandler(clazz));
return TYPE_HANDLER_MAP.get(clazz);
if (clazz.isEnum()) {
jdbcHandlerMap = getJdbcHandlerMapForEnumInterfaces(clazz);
if (jdbcHandlerMap == null) {
register(clazz, new EnumTypeHandler(clazz));
return TYPE_HANDLER_MAP.get(clazz);
}
} else {
jdbcHandlerMap = getJdbcHandlerMapForSuperclass(clazz);
}
}
if (jdbcHandlerMap == null) {
TYPE_HANDLER_MAP.put(type, NULL_TYPE_HANDLER_MAP);
}
TYPE_HANDLER_MAP.put(type, jdbcHandlerMap == null ? NULL_TYPE_HANDLER_MAP : jdbcHandlerMap);
return jdbcHandlerMap;
}

private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMapForEnumInterfaces(Class<?> clazz) {
for (Class<?> iface : clazz.getInterfaces()) {
Map<JdbcType, TypeHandler<?>> jdbcHandlerMap = TYPE_HANDLER_MAP.get(iface);
if (jdbcHandlerMap != null) {
return jdbcHandlerMap;
} else {
return getJdbcHandlerMapForEnumInterfaces(iface);
}
}
return null;
}

private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMapForSuperclass(Class<?> clazz) {
Class<?> superclass = clazz.getSuperclass();
if (superclass == null || Object.class.equals(superclass)) {
Expand Down
43 changes: 38 additions & 5 deletions src/test/java/org/apache/ibatis/type/TypeHandlerRegistryTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2016 the original author or authors.
* Copyright 2009-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,10 +15,7 @@
*/
package org.apache.ibatis.type;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

import java.net.URI;
import java.sql.CallableStatement;
Expand Down Expand Up @@ -159,4 +156,40 @@ class MyDate2 extends MyDate1 {
}
assertEquals(DateTypeHandler.class, typeHandlerRegistry.getTypeHandler(MyDate2.class).getClass());
}

interface SomeInterface {
}

enum SomeEnum implements SomeInterface {
}

class SomeClass implements SomeInterface {
}

@MappedTypes(SomeInterface.class)
public static class SomeInterfaceTypeHandler<E extends Enum<E> & SomeInterface> extends BaseTypeHandler<E> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType)
throws SQLException {
}
@Override
public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
return null;
}
@Override
public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return null;
}
@Override
public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return null;
}
}

@Test
public void demoTypeHandlerForSuperInterface() {
typeHandlerRegistry.register(SomeInterfaceTypeHandler.class);
assertNull("Registering interface works only for enums.", typeHandlerRegistry.getTypeHandler(SomeClass.class));
assertSame(SomeInterfaceTypeHandler.class, typeHandlerRegistry.getTypeHandler(SomeEnum.class).getClass());
}
}