反射代码

这么写代码,一定会被打死
除非是写框架

public class ReflectionTest {

    private String aPrivateField;
    public Long aPublicField;


    public void print () {
        System.out.println("what's wrong?");
    }
}

直接给实例了,具体看注释

public class Test {

    // 反射调用public方法
    public static <T,R> R invoke (T t, String methodName, Class<?> [] parameterTypes, Object [] params)
        throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        Class clazz = t.getClass();
        Method method = clazz.getMethod(methodName, parameterTypes);
        Object result = method.invoke(t, params);
        return (R) result;
    }

    //设置对象的可以被访问的变量,例如public,内部类访问外部类等
    public static <T> void setField (T t, String fieldName, Object fieldValue)
        throws NoSuchFieldException, IllegalAccessException {
        Class clazz = t.getClass();
        Field field = clazz.getField(fieldName);
        field.set(t, fieldValue);
    }

    //对于不可访问的对象,getField方法是无无法获取的。要用getDeclaredField。如果要修改,需要将是否可访问设置为true setAccessible(true);
    public static <T> void forceSetField (T t, String fieldName, Object fieldValue)
        throws NoSuchFieldException, IllegalAccessException {
        Class clazz = t.getClass();
        Field field = clazz.getDeclaredField(fieldName);
        field.setAccessible(true);
        field.set(t, fieldValue);
    }

    //读取对象中的成员变量
    public static <T,R> R getField (T t, String fieldName)
        throws NoSuchFieldException, IllegalAccessException {
        Class clazz = t.getClass();
        Field field = clazz.getField(fieldName);
        return (R) field.get(t);
    }
    //对于不可访问的对象,setField方法是没有操作权限的。如果要修改,需要将是否可访问设置为true setAccessible(true);
    public static <T,R> R forceGetField (T t, String fieldName)
        throws NoSuchFieldException, IllegalAccessException {
        Class clazz = t.getClass();
        Field field = clazz.getDeclaredField(fieldName);
        field.setAccessible(true);
        return (R) field.get(t);
    }

    public static void main(String[] args)
        throws IllegalAccessException, UnsupportedEncodingException, NoSuchMethodException, InvocationTargetException,
        InstantiationException, NoSuchFieldException {

        ReflectionTest reflectionTest = ReflectionTest.class.newInstance();

        forceSetField(reflectionTest, "aPrivateField", "66666");
        System.out.println("aPrivateField的值为 :" + forceGetField(reflectionTest, "aPrivateField"));

        try {
            setField(reflectionTest, "aPrivateField", "66666");
            System.out.println("aPrivateField的值为 :" + getField(reflectionTest, "aPrivateField"));
        } catch (Exception e) {
            System.out.println(e.getClass().getName());
        }

        setField(reflectionTest, "aPublicField", 123456L);
        System.out.println("aPrivateField的值为 :" + getField(reflectionTest, "aPublicField"));



    }
 }

执行结果:
aPrivateField的值为 :66666
java.lang.NoSuchFieldException
aPrivateField的值为 :123456

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值