反射工具类访问方法和属性

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;

}


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值