JAVA反射获取T泛型子类构造方法
2016-04-26 00:00
423 查看
/*jadclipse*/// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov. package com.pyh.dao.core; import java.lang.reflect.*; import java.util.*; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.beanutils.converters.DateConverter; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.Assert; public class TmReflectionUtils { public TmReflectionUtils() { } public static Object getFieldValue(Object object, String fieldName) { Field field = getDeclaredField(object, fieldName); if (field == null) throw new IllegalArgumentException((new StringBuilder( "Could not find field [")).append(fieldName) .append("] on target [").append(object).append("]") .toString()); makeAccessible(field); Object result = null; try { result = field.get(object); } catch (IllegalAccessException e) { logger.error("不可能抛出的异常{}", e.getMessage()); } return result; } public static void setFieldValue(Object object, String fieldName, Object value) { Field field = getDeclaredField(object, fieldName); if (field == null) throw new IllegalArgumentException((new StringBuilder( "Could not find field [")).append(fieldName) .append("] on target [").append(object).append("]") .toString()); makeAccessible(field); try { field.set(object, value); } catch (IllegalAccessException e) { logger.error("不可能抛出的异常:{}", e.getMessage()); } } public static Object invokeMethod(Object object, String methodName, Class parameterTypes[], Object parameters[]) { Method method = getDeclaredMethod(object, methodName, parameterTypes); if (method == null) throw new IllegalArgumentException((new StringBuilder( "Could not find method [")).append(methodName) .append("] on target [").append(object).append("]") .toString()); method.setAccessible(true); try { return method.invoke(object, parameters); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } } protected static Field getDeclaredField(Object object, String fieldName) { Assert.notNull(object, "object不能为空"); Assert.hasText(fieldName, "fieldName"); for (Class superClass = object.getClass(); superClass != java / lang / Object;) try { return superClass.getDeclaredField(fieldName); } catch (NoSuchFieldException nosuchfieldexception) { superClass = superClass.getSuperclass(); } return null; } protected static void makeAccessible(Field field) { if (!Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) field.setAccessible(true); } protected static Method getDeclaredMethod(Object object, String methodName, Class parameterTypes[]) { Assert.notNull(object, "object不能为空"); for (Class superClass = object.getClass(); superClass != java / lang / Object;) try { return superClass.getDeclaredMethod(methodName, parameterTypes); } catch (NoSuchMethodException nosuchmethodexception) { superClass = superClass.getSuperclass(); } return null; } public static Class getSuperClassGenricType(Class clazz) { return getSuperClassGenricType(clazz, 0); } public static Class getSuperClassGenricType(Class clazz, int index) { Type genType = clazz.getGenericSuperclass(); if (!(genType instanceof ParameterizedType)) { logger.warn((new StringBuilder( String.valueOf(clazz.getSimpleName()))).append( "'s superclass not ParameterizedType").toString()); return java / lang / Object; } Type params[] = ((ParameterizedType) genType).getActualTypeArguments(); if (index >= params.length || index < 0) { logger.warn((new StringBuilder("Index: ")).append(index) .append(", Size of ").append(clazz.getSimpleName()) .append("'s Parameterized Type: ").append(params.length) .toString()); return java / lang / Object; } if (!(params[index] instanceof Class)) { logger.warn((new StringBuilder( String.valueOf(clazz.getSimpleName()))) .append(" not set the actual class on superclass generic parameter") .toString()); return java / lang / Object; } else { return (Class) params[index]; } } public static List convertElementPropertyToList(Collection collection, String propertyName) { List list = new ArrayList(); try { Object obj; for (Iterator iterator = collection.iterator(); iterator.hasNext(); list .add(PropertyUtils.getProperty(obj, propertyName))) obj = iterator.next(); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } return list; } public static String convertElementPropertyToString(Collection collection, String propertyName, String separator) { List list = convertElementPropertyToList(collection, propertyName); return StringUtils.join(list, separator); } public static Object convertValue(Object value, Class toType) { try { DateConverter dc = new DateConverter(); dc.setUseLocaleFormat(true); dc.setPatterns(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" }); ConvertUtils.register(dc, java / util / Date); return ConvertUtils.convert(value, toType); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } } public static RuntimeException convertReflectionExceptionToUnchecked( Exception e) { if ((e instanceof IllegalAccessException) || (e instanceof IllegalArgumentException) || (e instanceof NoSuchMethodException)) return new IllegalArgumentException("Reflection Exception.", e); if (e instanceof InvocationTargetException) return new RuntimeException("Reflection Exception.", ((InvocationTargetException) e).getTargetException()); else return new RuntimeException("Unexpected Exception.", e); } public static Object convertStringToObject(String value, Class toType) { try { return ConvertUtils.convert(value, toType); } catch (Exception e) { throw convertReflectionExceptionToUnchecked(e); } } private static Logger logger = LoggerFactory.getLogger(com / pyh / dao / core / TmReflectionUtils); } /* DECOMPILATION REPORT Decompiled from: E:\JavaIDE\WorkSpace\xywpyh\WebRoot\WEB-INF\lib\spring-jsonutil1.0.jar Total time: 40 ms Jad reported messages/errors: Exit status: 0 Caught exceptions: */
相关文章推荐
- Java中的日志——Java.util.logging、log4j、commons-logging
- Java事件处理机制
- java 添加事件4种方法
- java 布局
- Java jdbc 数据库操作
- struts2导出数据到excel并下载
- java线程安全问题之静态变量、实例变量、局部变量
- JAVA 实现支付宝接口
- Eclipse导入System Lib方法
- 20145222黄亚奇《Java程序设计》实验三实验报告
- RxJava 学习之(一)
- 关于CKEditor4.5.6的使用,自定义toolbar配置,上传图片案例(SpringMVC+MyBatis案例),自定义行高,去编辑器的中内容,将编辑器中内容设置到指定的位置等
- 关于CKEditor4.5.6的使用,自定义toolbar配置,上传图片案例(SpringMVC+MyBatis案例),自定义行高,去编辑器的中内容,将编辑器中内容设置到指定的位置等
- Java虚拟机 类加载过程与机制
- java内部类的几点体会
- 【Java】JDK类 CountDownLatch
- ACM中Java的应用
- ACM中Java的应用
- java中获取资源文件
- Java中Scanner对象