1、相关接口
package org.liufei.jweb.reflection.type;
/**
*
* @author 刘飞
*
* @param <T>
*/
public interface TypeHandler {
public Object getValue(Object value);
}
2、转换器工具
package org.liufei.jweb.reflection.type;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.liufei.jweb.reflection.type.http.HttpServletRequestTypeHandler;
import org.liufei.jweb.reflection.type.http.HttpServletResponseTypeHandler;
import org.liufei.jweb.reflection.type.http.HttpSessionTypeHandler;
import org.liufei.jweb.reflection.type.http.ServletContextTypeHandler;
import org.liufei.jweb.util.Assert;
public class JavaTypeHandlers {
private static transient final Logger logger = Logger
.getLogger(JavaTypeHandlers.class);
private static final Map<Class<?>, TypeHandler> TYPE_HANDLERS = new HashMap<Class<?>, TypeHandler>();
private static final Class<?>[] HTTP_SERVLET_API_CLASSES = new Class<?>[]{
ServletContext.class,
ServletRequest.class,
ServletResponse.class,
HttpServletRequest.class,
HttpServletResponse.class,
HttpSession.class
} ;
static {
try {
TYPE_HANDLERS.put(int.class, IntegerHandler.class
.newInstance());
TYPE_HANDLERS.put(Integer.class, IntegerHandler.class
.newInstance());
TYPE_HANDLERS.put(boolean.class, BooleanHandler.class
.newInstance());
TYPE_HANDLERS.put(Boolean.class, BooleanHandler.class
.newInstance());
TYPE_HANDLERS.put(long.class, LongHandler.class
.newInstance());
TYPE_HANDLERS.put(Long.class, LongHandler.class
.newInstance());
TYPE_HANDLERS.put(float.class, FloatHandler.class
.newInstance());
TYPE_HANDLERS.put(Float.class, FloatHandler.class
.newInstance());
TYPE_HANDLERS.put(double.class, DoubleHandler.class
.newInstance());
TYPE_HANDLERS.put(Double.class, DoubleHandler.class
.newInstance());
TYPE_HANDLERS.put(char.class, CharacterHandler.class
.newInstance());
TYPE_HANDLERS.put(Character.class,
CharacterHandler.class.newInstance());
TYPE_HANDLERS.put(short.class, ShortHandler.class
.newInstance());
TYPE_HANDLERS.put(Short.class, ShortHandler.class
.newInstance());
TYPE_HANDLERS.put(byte.class, ByteHandler.class
.newInstance());
TYPE_HANDLERS.put(Byte.class, ByteHandler.class
.newInstance());
TYPE_HANDLERS.put(String.class, StringHandler.class
.newInstance());
} catch (InstantiationException e) {
logger.error("init TypeHandler failed.", e);
throw new RuntimeException("init TypeHandler failed.", e) ;
} catch (IllegalAccessException e) {
logger.error("init TypeHandler failed.", e);
throw new RuntimeException("init TypeHandler failed.", e) ;
}
}
public TypeHandler getUnsupportedDataHandler(Class<?> fieldType) {
return new UnsupportedDataHandler(fieldType);
}
/**
* 扩展用户自定义的类型注入插件。
* @param typeHandlerMap
*/
public static void register(Map<Class<?>, TypeHandler> typeHandlerMap) {
Assert.notNull(typeHandlerMap) ;
if(typeHandlerMap == null) {
return ;
}
logger.debug("\n\tregister TypeHandler map : " + typeHandlerMap + "\n") ;
if(typeHandlerMap.size() > 0) {
TYPE_HANDLERS.putAll(typeHandlerMap) ;
}
}
/**
* 扩展用户自定义的类型注入插件。
* @param clazz
* @param typeHandler
*/
public static void register(Class<?> clazz, TypeHandler typeHandler) {
Assert.notNull(clazz) ;
Assert.notNull(typeHandler) ;
if(clazz == null || typeHandler == null) {
return ;
}
logger.debug("\n\tregister TypeHandler " + clazz + " = " + typeHandler.getClass() + "\n") ;
TYPE_HANDLERS.put(clazz, typeHandler) ;
}
/**
* 扩展用户自定义的类型注入插件。
* @param classes 一般是该类的父类和接口组成的数组。
* @param typeHandler
*/
public static void register(Class<?>[] classes, TypeHandler typeHandler) {
Assert.notNull(classes) ;
Assert.notNull(typeHandler) ;
Assert.notEmpty(classes) ;
if(classes == null || typeHandler == null){
return ;
}
logger.debug("\n\tregister multiple classes ( size = " + classes.length + " ) TypeHandler " + typeHandler.getClass() + " begin...\n") ;
if(classes.length > 0) {
int i = 1 ;
for (Class<?> clazz : classes) {
logger.debug("\n\t[" + i + "] register TypeHandler " + clazz + " = " + typeHandler.getClass() + "\n") ;
TYPE_HANDLERS.put(clazz, typeHandler) ;
i++ ;
}
}
logger.debug("\n\tregister multiple classes ( size = " + classes.length + " ) TypeHandler " + typeHandler.getClass() + " end...\n") ;
}
/**
* 卸载类型转换插件
* @param typeHandlerMap
*/
public static void unInstall(Map<Class<?>, TypeHandler> typeHandlerMap) {
Assert.notNull(typeHandlerMap) ;
if(typeHandlerMap == null) {
return ;
}
logger.debug("\n\tunInstall TypeHandler map " + typeHandlerMap + "\n") ;
if(typeHandlerMap.size() > 0) {
for (Object key : typeHandlerMap.keySet()) {
typeHandlerMap.remove(key) ;
}
}
}
/**
* 卸载类型转换插件
* @param classes
*/
public static void unInstall(Class<?>... classes) {
Assert.notNull(classes) ;
if(classes == null) {
return ;
}
if(classes.length > 0) {
for (Class<?> clazz : classes) {
logger.debug("\n\tunInstall TypeHandler for class " + clazz + "\n") ;
TYPE_HANDLERS.remove(clazz) ;
}
}
}
/**
* 注册Servlet API相关对象。
* @param request
* @param response
* @param sessionCreate 由 HttpServletRequest 获取 HttpSession 时用到
* {@link javax.servlet.http.HttpServletRequest#getSession(boolean)}
*
*/
public static void httpServletRegister(HttpServletRequest request,
HttpServletResponse response, boolean sessionCreate) {
httpServletRegister(request, response, request.getSession(sessionCreate), request.getSession(sessionCreate).getServletContext()) ;
}
/**
* 注册Servlet API相关对象。
* @param request
* @param response
* @param session
* @param servletContext
*/
public static void httpServletRegister(HttpServletRequest request,
HttpServletResponse response, HttpSession session, ServletContext servletContext) {
Assert.notNull(request) ;
Assert.notNull(response) ;
Assert.notNull(session) ;
Assert.notNull(servletContext) ;
logger.debug(
"\n\tregister httpServlet api args TypeHandler [ " +
HttpServletRequest.class +
", " +
HttpServletResponse.class +
", " +
ServletRequest.class +
", " +
ServletResponse.class +
", " +
HttpSession.class +
", " +
ServletContext.class +
" ].\n"
) ;
register(HttpServletRequest.class, new HttpServletRequestTypeHandler(request)) ;
register(HttpServletResponse.class, new HttpServletResponseTypeHandler(response)) ;
register(ServletRequest.class, new HttpServletRequestTypeHandler(request)) ;
register(ServletResponse.class, new HttpServletResponseTypeHandler(response)) ;
register(HttpSession.class, new HttpSessionTypeHandler(session)) ;
register(ServletContext.class, new ServletContextTypeHandler(servletContext)) ;
}
public static void removeHttpServletRegister() {
logger.debug(
"\n\tunInstall httpServlet api args TypeHandler [ " +
HttpServletRequest.class +
", " +
HttpServletResponse.class +
", " +
ServletRequest.class +
", " +
ServletResponse.class +
", " +
HttpSession.class +
", " +
ServletContext.class +
" ].\n"
) ;
unInstall(HTTP_SERVLET_API_CLASSES) ;
}
/**
*
* @param value
* 需要转化的值
* @param clazz
* 要转化的类型
* @return
*/
public static Object convertValueToType(Object value, Class<?> clazz) {
logger.debug("\n\tconvert value " + value + "( type : " + value.getClass().getName() + ")" + " to type " + clazz.getName() + "\n") ;
TypeHandler typeHandler = null ;
typeHandler = TYPE_HANDLERS.get(clazz);
if(typeHandler != null) {
return typeHandler.getValue(value);
}
else if(typeHandler == null) {//接口、父类
Class<?>[] clazzs = clazz.getInterfaces() ;
for (Class<?> clazzi : clazzs) {
typeHandler = TYPE_HANDLERS.get(clazzi);
if(typeHandler != null) {
return typeHandler.getValue(value);
}
}
Class<?> superclazz = clazz.getSuperclass() ;
typeHandler = TYPE_HANDLERS.get(superclazz);
if(typeHandler != null) {
return typeHandler.getValue(value);
}
}
if (typeHandler == null) {
logger.error("\n\tunknown data type :" + clazz + "\n") ;
throw new RuntimeException("unknown data type :" + clazz);
}
return null ;
}
public static class IntegerHandler implements TypeHandler {
public Integer getValue(Object value) {
return Integer.valueOf(value.toString());
}
}
public static class StringHandler implements TypeHandler {
public String getValue(Object value) {
return value.toString();
}
}
public static class LongHandler implements TypeHandler {
public Long getValue(Object value) {
return Long.valueOf(value.toString());
}
}
public static class FloatHandler implements TypeHandler {
public Float getValue(Object value) {
return Float.valueOf(value.toString());
}
}
public static class DoubleHandler implements TypeHandler {
public Double getValue(Object value) {
return Double.valueOf(value.toString());
}
}
public static class BooleanHandler implements TypeHandler {
public Boolean getValue(Object value) {
if (value.toString().length() > 1) {
return Boolean.valueOf(value.toString());
} else if (value.toString().length() <= 1) {
char c = value.toString().charAt(0);
if (c == '1' || c == 'y' || c == 'Y' || c == 't' || c == 'T')
return Boolean.TRUE;
else
return Boolean.FALSE;
} else {
return Boolean.FALSE;
}
}
}
public static class CharacterHandler implements TypeHandler {
public Character getValue(Object value) {
return Character.valueOf(value.toString().charAt(0));
}
}
public static class ShortHandler implements TypeHandler {
public Short getValue(Object value) {
return Short.valueOf(value.toString());
}
}
public static class ByteHandler implements TypeHandler {
public Byte getValue(Object value) {
return Byte.valueOf(value.toString());
}
}
public static class UnsupportedDataHandler implements TypeHandler {
private Class<?> fieldType;
public UnsupportedDataHandler(Class<?> fieldType) {
this.fieldType = fieldType;
}
public Object getValue(Object fieldValue) {
throw new RuntimeException("unknown data type :" + fieldType);
}
}
}