您的位置:首页 > 编程语言 > Java开发

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:
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: