經過Java反射調用方法

這是個測試用的例子,經過反射調用對象的方法。java

 
 
TestRef.java
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

/**
* Created by IntelliJ IDEA.
* File: TestRef.java
* User: leizhimin
* Date: 2008-1-28 14:48:44
*/
public class TestRef {

    public staticvoid main(String args[]) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Foo foo = new Foo("這個一個Foo對象!");
        Class clazz = foo.getClass();
        Method m1 = clazz.getDeclaredMethod("outInfo");
        Method m2 = clazz.getDeclaredMethod("setMsg", String.class);
        Method m3 = clazz.getDeclaredMethod("getMsg");
        m1.invoke(foo);
        m2.invoke(foo, "從新設置msg信息!");
        String msg = (String) m3.invoke(foo);
        System.out.println(msg);
    }
}

class Foo {
    private String msg;

    public Foo(String msg) {
        this.msg = msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }

    public void outInfo() {
        System.out.println("這是測試Java反射的測試類");
    }
}

控制檯輸出結果:jvm

這是測試Java反射的測試類
從新設置msg信息!

Process finished with exit code 0 

JAVA反射使用手測試

        本篇文章爲在工做中使用JAVA反射的經驗總結,也能夠說是一些小技巧,之後學會新的小技巧,會不斷更新。本文不許備討論JAVA反射的機制,網上有不少,你們隨便google一下就能夠了。this

        在開始以前,我先定義一個測試類Student,代碼以下:google

package chb.test.reflect;  
  
public class Student {  
    private int age;  
    private String name;  
    public int getAge() {  
        return age;  
    }  
    public void setAge(int age) {  
        this.age = age;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
      
    public static void hi(int age,String name){  
        System.out.println("你們好,我叫"+name+",今年"+age+"歲");  
    }  
}

1、JAVA反射的常規使用步驟spa

    反射調用通常分爲3個步驟:code

  • 獲得要調用類的class
  • 獲得要調用的類中的方法(Method)
  • 方法調用(invoke)

     代碼示例:對象

Class cls = Class.forName("chb.test.reflect.Student");  
Method m = cls.getDeclaredMethod("hi",new Class[]{int.class,String.class});  
m.invoke(cls.newInstance(),20,"chb");

2、方法調用中的參數類型blog

        在方法調用中,參數類型必須正確,這裏須要注意的是不能使用包裝類替換基本類型,好比不能使用Integer.class代替int.class。get

       如我要調用Student的setAge方法,下面的調用是正確的:

Class cls = Class.forName("chb.test.reflect.Student");  
Method setMethod = cls.getDeclaredMethod("setAge",int.class);  
setMethod.invoke(cls.newInstance(), 15);

      而若是咱們用Integer.class替代int.class就會出錯,如:

Class cls = Class.forName("chb.test.reflect.Student");  
Method setMethod = cls.getDeclaredMethod("setAge",Integer.class);  
setMethod.invoke(cls.newInstance(), 15);

      jvm會報出以下異常:

java.lang.NoSuchMethodException: chb.test.reflect.Student.setAge(java.lang.Integer)  
    at java.lang.Class.getDeclaredMethod(Unknown Source)  
    at chb.test.reflect.TestClass.testReflect(TestClass.java:23)

3、static方法的反射調用

 

       static方法調用時,沒必要獲得對象示例,以下:

Class cls = Class.forName("chb.test.reflect.Student");  
Method staticMethod = cls.getDeclaredMethod("hi",int.class,String.class);  
staticMethod.invoke(cls,20,"chb");//這裏不須要newInstance  
//staticMethod.invoke(cls.newInstance(),20,"chb");

4、private的成員變量賦值

    若是直接經過反射給類的private成員變量賦值,是不容許的,這時咱們能夠經過setAccessible方法解決。代碼示例:

Class cls = Class.forName("chb.test.reflect.Student");  
Object student = cls.newInstance();//獲得一個實例  
Field field = cls.getDeclaredField("age");  
field.set(student, 10);  
System.out.println(field.get(student));

     運行如上代碼,系統會報出以下異常:

    java.lang.IllegalAccessException: Class chb.test.reflect.TestClass can not access a member of class chb.test.reflect.Student with modifiers "private"  
        at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)  
        at java.lang.reflect.Field.doSecurityCheck(Unknown Source)  
        at java.lang.reflect.Field.getFieldAccessor(Unknown Source)  
        at java.lang.reflect.Field.set(Unknown Source)  
        at chb.test.reflect.TestClass.testReflect(TestClass.java:20)

   解決方法:

Class cls = Class.forName("chb.test.reflect.Student");  
Object student = cls.newInstance();  
Field field = cls.getDeclaredField("age");  
field.setAccessible(true);//設置容許訪問  
field.set(student, 10);  
System.out.println(field.get(student));

其實,在某些場合下(類中有get,set方法),能夠先反射調用set方法,再反射調用get方法達到如上效果,代碼示例:

Class cls = Class.forName("chb.test.reflect.Student");  
Object student = cls.newInstance();  
  
Method setMethod = cls.getDeclaredMethod("setAge",Integer.class);  
setMethod.invoke(student, 15);//調用set方法  
              
Method getMethod = cls.getDeclaredMethod("getAge");  
System.out.println(getMethod.invoke(student));
相關文章
相關標籤/搜索