Java 反射使用

我的博客原文地址

Field

  • getDeclaringClass():返回表示声明由Field对象表示的字段的类或接口的Class对象。
  • getType():返回一个Class对象,用于标识Field对象所表示的字段的声明类型。

反射

TestA

public class TestA {
    private TestB mTestB;
    private boolean mTestBoolean = false;
    public TestA(){
        mTestB = new TestB();
    }

    public void getTestBoolean(){
        Log.e("Test","mTestBoolean = "+mTestBoolean);
    }

    private void testPrivateMethod(){
        Log.e("Test","testPrivateMethod!");
    }

    private String testPrivateMethodWithArgs(String arg1,int arg2){
        Log.e("Test","testPrivateMethodWithArgs! arg1 = "+arg1+",arg2 = "+arg2);
        return "return "+arg1;
    }
}

TestB

public class TestB {
    public TestB(){

    }

    private void functionA(){
        Log.e("Test","functionA()");
    }
}

OutterClass

public class OuterClass {
    private InnerClass mInnerClass;
    public OuterClass(){
        mInnerClass = new InnerClass();
        mInnerClass.printInt();
    }

    private class InnerClass{
        private int mInt = 1;
        private String mStr = "Test";
        public void printInt(){
            Log.d("Test","mInt = "+mInt);
        }
    }
}

获取属性值

    public void getFiled(){
        TestA a = new TestA();

        try {
            //或者a.getClass().getDeclaredField("mTestBoolean");
            Field field = TestA.class.getDeclaredField("mTestBoolean");
            field.setAccessible(true);
            Object value = field.get(a);
            if(value != null && value instanceof Boolean){
                Log.e("Test"," value = "+value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getFiled1(){
        TestA a = new TestA();

        try {
            Class classObj = Class.forName("com.example.heqiang.testsomething.TestA");
            Field field = classObj.getDeclaredField("mTestBoolean");
            field.setAccessible(true);
            Object value = field.get(a);
            if(value != null && value instanceof Boolean){
                Log.e("Test"," value = "+value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

修改属性值

    public void setField(){
        TestA a = new TestA();
        a.getTestBoolean();

        try {
            Field field = TestA.class.getDeclaredField("mTestBoolean");
            field.setAccessible(true);
            field.set(a,true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        a.getTestBoolean();
    }

调用方法

    public void invokeMethod(){
        TestA a = new TestA();
        try {
            Method method = TestA.class.getDeclaredMethod("testPrivateMethod");
            method.setAccessible(true);
            method.invoke(a);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

调用带参数方法并获取返回值

    public void invokeMethodWithArgs(){
        TestA a = new TestA();
        Object result = null;
        try {
            Method method = TestA.class.getDeclaredMethod("testPrivateMethodWithArgs",String.class,int class);
            method.setAccessible(true);
            result = method.invoke(a,"Test",1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(result != null && result instanceof String){
            Log.e("Test"," result = "+result);
        }
    }

调用私有属性的方法

    public void invokePirvateFieldMethod(){
        TestA a = new TestA();
        Method functionA = null;
        try {
            Field field = TestA.class.getDeclaredField("mTestB");
            field.setAccessible(true);
            Object testB = field.get(a);
            if(testB != null){
                Class testBClass = testB.getClass();
                functionA = testBClass.getDeclaredMethod("functionA");
                functionA.setAccessible(true);
                if(functionA != null){
                    functionA.invoke(testB);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

内部类

    public void testReflectInnerClass(){
        OuterClass outerClass = new OuterClass();
        try{
            Field outerInnerClassFiled =Class.forName("com.example.heqiang.testsomething.OuterClass").getDeclaredField("mInnerClass");
            outerInnerClassFiled.setAccessible(true);
            Class innerClass = Class.forName("com.example.heqiang.testsomething.OuterClass$InnerClass");
            Object outerInnerClassObject = outerInnerClassFiled.get(outerClass);
            if (outerInnerClassObject.getClass().getName().equals(innerClass.getName())){
                Field innerIntFiled =innerClass.getDeclaredField("mInt");
                innerIntFiled.setAccessible(true);
                Field innerStrFiled =innerClass.getDeclaredField("mStr");
                innerStrFiled.setAccessible(true);
                Method innerFucMethod =innerClass.getDeclaredMethod("printInt");
                innerFucMethod.setAccessible(true);

                innerFucMethod.invoke(outerInnerClassObject);
                innerIntFiled.set(outerInnerClassObject, 8);
                innerFucMethod.invoke(outerInnerClassObject);

                Object innerStrObject = innerStrFiled.get(outerInnerClassObject);
                Log.e("Test","mStr length = "+((String)innerStrObject).length());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

获取方法的类型和参数

public class ReflectTest {
    public static void testFunc(String grg0,int arg1){

    }

    public final int testFunc1(){
        return 0;
    }
}
    public void testRefect(){
        Method[] methods = ReflectTest.class.getMethods();
        for (Method method : methods) {
            Log.d("Test","---------------------->");
            String methodName = method.getName();
            Log.d("Test","methodName = "+methodName);
            int modifiers = method.getModifiers();
            if((modifiers & Modifier.PUBLIC) != 0)
                Log.d("Test","public");
            if((modifiers & Modifier.PRIVATE) != 0)
                Log.d("Test","private");
            if((modifiers & Modifier.STATIC) != 0)
                Log.d("Test","static");
            if((modifiers & Modifier.FINAL) != 0)
                Log.d("Test","final");
            Class<?>[] parameterTypes = method.getParameterTypes();
            Log.d("Test","parameter num = "+parameterTypes.length);
            for(Class c : parameterTypes){
                Log.d("Test","parameter name = "+c.getName());
            }
            Log.d("Test","<---------------------->");
        }
    }

结果:

D/Test    (23196): ---------------------->
D/Test    (23196): methodName = equals
D/Test    (23196): public
D/Test    (23196): parameter num = 1
D/Test    (23196): parameter name = java.lang.Object
D/Test    (23196): <---------------------->
D/Test    (23196): ---------------------->
D/Test    (23196): methodName = getClass
D/Test    (23196): public
D/Test    (23196): final
D/Test    (23196): parameter num = 0
D/Test    (23196): <---------------------->
D/Test    (23196): ---------------------->
D/Test    (23196): methodName = hashCode
D/Test    (23196): public
D/Test    (23196): parameter num = 0
D/Test    (23196): <---------------------->
D/Test    (23196): ---------------------->
D/Test    (23196): methodName = notify
D/Test    (23196): public
D/Test    (23196): final
D/Test    (23196): parameter num = 0
D/Test    (23196): <---------------------->
D/Test    (23196): ---------------------->
D/Test    (23196): methodName = notifyAll
D/Test    (23196): public
D/Test    (23196): final
D/Test    (23196): parameter num = 0
D/Test    (23196): <---------------------->
D/Test    (23196): ---------------------->
D/Test    (23196): methodName = testFunc
D/Test    (23196): public
D/Test    (23196): static
D/Test    (23196): parameter num = 2
D/Test    (23196): parameter name = java.lang.String
D/Test    (23196): parameter name = int
D/Test    (23196): <---------------------->
D/Test    (23196): ---------------------->
D/Test    (23196): methodName = testFunc1
D/Test    (23196): public
D/Test    (23196): final
D/Test    (23196): parameter num = 0
D/Test    (23196): <---------------------->
D/Test    (23196): ---------------------->
D/Test    (23196): methodName = toString
D/Test    (23196): public
D/Test    (23196): parameter num = 0
D/Test    (23196): <---------------------->
D/Test    (23196): ---------------------->
D/Test    (23196): methodName = wait
D/Test    (23196): public
D/Test    (23196): final
D/Test    (23196): parameter num = 0
D/Test    (23196): <---------------------->
D/Test    (23196): ---------------------->
D/Test    (23196): methodName = wait
D/Test    (23196): public
D/Test    (23196): final
D/Test    (23196): parameter num = 1
D/Test    (23196): parameter name = long
D/Test    (23196): <---------------------->
D/Test    (23196): ---------------------->
D/Test    (23196): methodName = wait
D/Test    (23196): public
D/Test    (23196): final
D/Test    (23196): parameter num = 2
D/Test    (23196): parameter name = long
D/Test    (23196): parameter name = int
D/Test    (23196): <---------------------->

http://www.yiibai.com/javareflect/
http://blog.csdn.net/u011240877/article/details/54604146

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寒江蓑笠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值