Skip to content

Commit

Permalink
Merge branch 'master' of https://git.oschina.net/free/Mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
abel533 committed Aug 8, 2017
2 parents b21992b + 1bc8a96 commit 35a2f1d
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/main/java/tk/mybatis/mapper/entity/EntityTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.TypeException;
import org.apache.ibatis.type.TypeHandler;

import tk.mybatis.mapper.MapperException;
import tk.mybatis.mapper.util.StringUtil;

import javax.persistence.Table;

import java.lang.reflect.Constructor;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -210,7 +215,7 @@ public ResultMap getResultMap(Configuration configuration) {
}
if (entityColumn.getTypeHandler() != null) {
try {
builder.typeHandler(entityColumn.getTypeHandler().newInstance());
builder.typeHandler(getInstance(entityColumn.getJavaType(),entityColumn.getTypeHandler()));
} catch (Exception e) {
throw new MapperException(e);
}
Expand Down Expand Up @@ -240,4 +245,30 @@ public void initPropertyMap() {
public Map<String, EntityColumn> getPropertyMap() {
return propertyMap;
}

/**
* 实例化TypeHandler
* @param javaTypeClass
* @param typeHandlerClass
* @return
*/
@SuppressWarnings("unchecked")
public <T> TypeHandler<T> getInstance(Class<?> javaTypeClass, Class<?> typeHandlerClass) {
if (javaTypeClass != null) {
try {
Constructor<?> c = typeHandlerClass.getConstructor(Class.class);
return (TypeHandler<T>) c.newInstance(javaTypeClass);
} catch (NoSuchMethodException ignored) {
// ignored
} catch (Exception e) {
throw new TypeException("Failed invoking constructor for handler " + typeHandlerClass, e);
}
}
try {
Constructor<?> c = typeHandlerClass.getConstructor();
return (TypeHandler<T>) c.newInstance();
} catch (Exception e) {
throw new TypeException("Unable to find a usable constructor for " + typeHandlerClass, e);
}
}
}

0 comments on commit 35a2f1d

Please sign in to comment.