package com.itany.util;
import java.lang.reflect.*;
/*
* 反射工具类
*/
public class ReflectUtil {
/*
* 通过反射,获取定义Class的父类参数的泛型的真实类型
*/
public static Class getSuperClassGenericType(Class clazz, int index)
throws Exception {
// 父类类型
Type type = clazz.getGenericSuperclass();
// 父类类型不带泛型
if (!(type instanceof ParameterizedType)) {
return Object.class;
}
// 父类类的参数类型
Type[] params = ((ParameterizedType) type).getActualTypeArguments();
// 下标不符合要求
if (index < 0 || index >= params.length) {
throw new Exception("此位置没有泛型");
}
return (Class) params[index];
}
/*
* 通过反射,获取定义Class的父类参数的泛型的真实类型
*/
public static Class getSuperClassGenericType(Class clazz) throws Exception {
return (Class) getSuperClassGenericType(clazz, 0);
}
/*
* 通过反射,获取对应方法[当前类+所有父类的 所有的方法]
*/
public static Method getDeclaredMethod(Object object, String methodName,
Class... paramaterTypes) {
for (Class clazz = object.getClass(); clazz != null; clazz = clazz
.getSuperclass()) {
try {
return clazz.getDeclaredMethod(methodName, paramaterTypes);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
return null;
}
/*
* 调用方法
*/
public static Object invokeMethod(Object object,String methodName,Object[]parameters,Class... paramaterTypes) throws NoSuchMethodException{
//得到对应方法
Method m = getDeclaredMethod(object,methodName,paramaterTypes);
if(m==null){
throw new NoSuchMethodException(methodName+"在"+object.getClass().getName()+"不存在");
}
//设置可访问,主要针对于私有方法,使其亦可访问
m.setAccessible(true);
try {
return m.invoke(object, parameters);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
/*
* 通过反射,获取对应属性[当前类+所有父类 所有属性]
*/
public static Field getDeclaredField(Object object,String fieldName){
for(Class clazz = object.getClass();clazz!=null;clazz=clazz.getSuperclass()){
try {
return clazz.getDeclaredField(fieldName);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
return null;
}
/*
* 给属性赋值
*/
public static void setFieldValue(Object object,String fieldName,Object value) throws NoSuchFieldException{
//得到属性
Field field = getDeclaredField(object,fieldName);
if(field==null){
throw new NoSuchFieldException(fieldName+"在"+object.getClass().getName()+"中不存在");
}
//设置属性的访问级别,主要针对于私有属性,使其亦可访问
field.setAccessible(true);
try {
field.set(object, value);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/*
* 获取属性的值
*/
public static Object getFieldValue(Object object,String fieldName) throws NoSuchFieldException{
//得到属性
Field field = getDeclaredField(object,fieldName);
if(field==null){
throw new NoSuchFieldException(fieldName+"在"+object.getClass().getName()+"中不存在");
}
//设置属性的访问级别,主要针对于私有属性,使其亦可访问
field.setAccessible(true);
try {
return field.get(object);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}