Skip to content

Commit 3086caa

Browse files
committed
Enable adding type handlers and aliases with annotations (for MyBatis core && MyBatis-Spring). See http://code.google.com/p/mybatis/issues/detail?id=314
1 parent cf96d47 commit 3086caa

File tree

4 files changed

+68
-18
lines changed

4 files changed

+68
-18
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.apache.ibatis.type;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.TYPE)
10+
public @interface Alias {
11+
public String value();
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.apache.ibatis.type;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.TYPE)
10+
public @interface MappedJdbcTypes {
11+
public JdbcType[] value();
12+
}

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package org.apache.ibatis.type;
22

3-
import org.apache.ibatis.io.ResolverUtil;
4-
import org.apache.ibatis.io.Resources;
5-
63
import java.math.BigDecimal;
7-
import java.util.*;
84
import java.sql.ResultSet;
5+
import java.util.ArrayList;
6+
import java.util.Collection;
7+
import java.util.Date;
8+
import java.util.HashMap;
9+
import java.util.Iterator;
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.Set;
13+
14+
import org.apache.ibatis.io.ResolverUtil;
15+
import org.apache.ibatis.io.Resources;
916

1017
public class TypeAliasRegistry {
1118

@@ -96,13 +103,19 @@ public void registerAliases(String packageName, Class superType){
96103
Set<Class<? extends Class>> typeSet = resolverUtil.getClasses();
97104
for(Class type : typeSet){
98105
//Ignore inner classes
99-
if (!type.isAnonymousClass())
100-
registerAlias(type.getSimpleName(), type);
106+
if (!type.isAnonymousClass()) {
107+
registerAlias(type);
108+
}
101109
}
102110
}
103111

104112
public void registerAlias(Class type) {
105-
registerAlias(type.getSimpleName(), type.getName());
113+
String alias = type.getSimpleName();
114+
Alias aliasAnnotation = (Alias) type.getAnnotation(Alias.class);
115+
if (aliasAnnotation != null) {
116+
alias = aliasAnnotation.value();
117+
}
118+
registerAlias(alias, type);
106119
}
107120

108121
public void registerAlias(String alias, Class value) {

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,30 @@ public void register(JdbcType jdbcType, TypeHandler handler) {
144144
}
145145

146146
public void register(Class<?> type, TypeHandler handler) {
147-
register(type, null, handler);
147+
MappedJdbcTypes mappedJdbcTypes = (MappedJdbcTypes) handler.getClass().getAnnotation(MappedJdbcTypes.class);
148+
if (mappedJdbcTypes != null) {
149+
for (JdbcType handledJdbcType : mappedJdbcTypes.value()) {
150+
register(type, handledJdbcType, handler);
151+
}
152+
} else {
153+
register(type, null, handler);
154+
}
148155
}
149156

157+
public void register(TypeHandler handler) {
158+
boolean mappedTypeFound = false;
159+
MappedTypes mappedTypes = (MappedTypes) handler.getClass().getAnnotation(MappedTypes.class);
160+
if (mappedTypes != null) {
161+
for (Class<?> handledType : mappedTypes.value()) {
162+
register(handledType, handler);
163+
mappedTypeFound = true;
164+
}
165+
}
166+
if (!mappedTypeFound) {
167+
throw new RuntimeException("Unable to get mapped types, check @MappedTypes annotation for type handler " + handler);
168+
}
169+
}
170+
150171
public void register(Class<?> type, JdbcType jdbcType, TypeHandler handler) {
151172
Map<JdbcType, TypeHandler> map = TYPE_HANDLER_MAP.get(type);
152173
if (map == null) {
@@ -164,21 +185,13 @@ public void register(String packageName) {
164185
resolverUtil.find(new ResolverUtil.IsA(TypeHandler.class), packageName);
165186
Set<Class<? extends Class<?>>> handlerSet = resolverUtil.getClasses();
166187

167-
TypeHandler handler;
168188
for (Class<?> type : handlerSet) {
169-
@SuppressWarnings({"unchecked"})
170-
Annotation annotation = type.getAnnotation(MappedTypes.class);
171189
try {
172-
handler = (TypeHandler) type.getConstructor().newInstance();
190+
TypeHandler handler = (TypeHandler) type.getConstructor().newInstance();
191+
register(handler);
173192
} catch (Exception e) {
174193
throw new RuntimeException("Unable to find a usable constructor for " + type, e);
175194
}
176-
if (null != annotation) {
177-
MappedTypes mappedType = (MappedTypes) annotation;
178-
for (Class<?> handledType : mappedType.value()) {
179-
register(handledType, handler);
180-
}
181-
}
182195
}
183196
}
184197

0 commit comments

Comments
 (0)