转载自:http://jackleliu.spaces.live.com/blog/cns!426307897fa1054e!125.entry
10月25日
java的reflect机制(反射)
为了学习reflect,特意写了两个类,我觉得反射最重要的就是通过方法名调用方法.
一个被调用的类,和一个 利用reflect的调用类.
PlainJavaClass.java:
package reflect;
public class PlainJavaClass {
public int a = 2;
public int b = 3;
public PlainJavaClass()
{
}
public PlainJavaClass(int a,int b)
{
this.a = a;
this.b = b;
}
public int add()
{
return a + b;
}
public int plus(int a, int b)
{
return a-b;
}
}
ReflectJavaClass.java:
package reflect;
import java.lang.reflect.*;
public class ReflectJavaClass {
public void invokePlain() {
Class Plain = null;
Method add = null;
Method plus = null;
Class[] plusPara = {int.class,int.class};
Object[] transPlusPara = {new Integer(50),new Integer(20)};
Object addReturnObj = null;
Object plusReturnObj = null;
try {
Plain = Class.forName("reflect.PlainJavaClass");
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
try {
add = Plain.getDeclaredMethod("add",null);
plus = Plain.getDeclaredMethod("plus",plusPara);
} catch (SecurityException e) {
e.printStackTrace();
return;
} catch (NoSuchMethodException e) {
e.printStackTrace();
return;
}
try {
addReturnObj = add.invoke(new PlainJavaClass(5,2),null);
plusReturnObj = plus.invoke(new PlainJavaClass(),transPlusPara);
} catch (IllegalArgumentException e) {
e.printStackTrace();
return;
} catch (IllegalAccessException e) {
e.printStackTrace();
return;
} catch (InvocationTargetException e) {
e.printStackTrace();
return;
}
Integer addReturnValue = (Integer)addReturnObj;
Integer plusReturnValue = (Integer)plusReturnObj;
System.out.println("return value :"+addReturnValue);
System.out.println("return value :"+plusReturnValue);
}
public static void main(String[] args)
{
new ReflectJavaClass(). invokePlain();
}
}
可以看出是第2个类来调用第一个类.
要利用java的reflect首先要取得PlainJavaClass对应的Class对象:Plain = Class.forName("reflect.PlainJavaClass"),注意其中的reflect.PlainJavaClass类名称中包名reflect不能缺少.
得到Plain之后就可以得到Plain中定义的某个方法,add=Plain.getDeclaredMethod("add",null);或者plus = Plain.getDeclaredMethod("plus",plusPara);其中第一个参数是方法名(String),第2个是参数的类型数组(Class[]).
得到了Method对象就可以调用了.addReturnObj = add.invoke(new PlainJavaClass(5,2),null);或者plusReturnObj = plus.invoke(new PlainJavaClass(),transPlusPara); ,invoke第一个参数是方法所在对象(Object),第2个参数是方法传进去的参数值(Objec[]).
方法调用之后可以得到相应的返回值:
Integer addReturnValue = (Integer)addReturnObj;
Integer plusReturnValue = (Integer)plusReturnObj;
System.out.println("return value :"+addReturnValue);
System.out.println("return value :"+plusReturnValue);
运行结果如下:
return value :7
return value :30
10月25日
java的reflect机制(反射)
为了学习reflect,特意写了两个类,我觉得反射最重要的就是通过方法名调用方法.
一个被调用的类,和一个 利用reflect的调用类.
PlainJavaClass.java:
package reflect;
public class PlainJavaClass {
public int a = 2;
public int b = 3;
public PlainJavaClass()
{
}
public PlainJavaClass(int a,int b)
{
this.a = a;
this.b = b;
}
public int add()
{
return a + b;
}
public int plus(int a, int b)
{
return a-b;
}
}
ReflectJavaClass.java:
package reflect;
import java.lang.reflect.*;
public class ReflectJavaClass {
public void invokePlain() {
Class Plain = null;
Method add = null;
Method plus = null;
Class[] plusPara = {int.class,int.class};
Object[] transPlusPara = {new Integer(50),new Integer(20)};
Object addReturnObj = null;
Object plusReturnObj = null;
try {
Plain = Class.forName("reflect.PlainJavaClass");
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
try {
add = Plain.getDeclaredMethod("add",null);
plus = Plain.getDeclaredMethod("plus",plusPara);
} catch (SecurityException e) {
e.printStackTrace();
return;
} catch (NoSuchMethodException e) {
e.printStackTrace();
return;
}
try {
addReturnObj = add.invoke(new PlainJavaClass(5,2),null);
plusReturnObj = plus.invoke(new PlainJavaClass(),transPlusPara);
} catch (IllegalArgumentException e) {
e.printStackTrace();
return;
} catch (IllegalAccessException e) {
e.printStackTrace();
return;
} catch (InvocationTargetException e) {
e.printStackTrace();
return;
}
Integer addReturnValue = (Integer)addReturnObj;
Integer plusReturnValue = (Integer)plusReturnObj;
System.out.println("return value :"+addReturnValue);
System.out.println("return value :"+plusReturnValue);
}
public static void main(String[] args)
{
new ReflectJavaClass(). invokePlain();
}
}
可以看出是第2个类来调用第一个类.
要利用java的reflect首先要取得PlainJavaClass对应的Class对象:Plain = Class.forName("reflect.PlainJavaClass"),注意其中的reflect.PlainJavaClass类名称中包名reflect不能缺少.
得到Plain之后就可以得到Plain中定义的某个方法,add=Plain.getDeclaredMethod("add",null);或者plus = Plain.getDeclaredMethod("plus",plusPara);其中第一个参数是方法名(String),第2个是参数的类型数组(Class[]).
得到了Method对象就可以调用了.addReturnObj = add.invoke(new PlainJavaClass(5,2),null);或者plusReturnObj = plus.invoke(new PlainJavaClass(),transPlusPara); ,invoke第一个参数是方法所在对象(Object),第2个参数是方法传进去的参数值(Objec[]).
方法调用之后可以得到相应的返回值:
Integer addReturnValue = (Integer)addReturnObj;
Integer plusReturnValue = (Integer)plusReturnObj;
System.out.println("return value :"+addReturnValue);
System.out.println("return value :"+plusReturnValue);
运行结果如下:
return value :7
return value :30