您的位置:首页 > 编程语言 > Java开发

Java中反射技术点记录

2014-06-23 13:00 330 查看
为了避免遗忘,将Reflect中常用方法和步骤记录下来。

测试所用的person类如下:

package neuq.test;

public class Person {
private String name;
private int age;

public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person(){
super();
}
public static void staticMethod(){
System.out.println("staticMethod run!!");
};

public void paramMethod(String name,int age){
System.out.println(name+"--------"+age);
}
public void noParamMethod(){
System.out.println("noParamMethod run!!!!");
}
}


2.JUnit测试代码如下:

package neuq.test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.junit.Test;

public class ReflectTest {

/*
* 测试拿到Class的三种方法
*
* @author:Dean
* @data:2014-6-23
*/
@Test
public void testGetClass() throws ClassNotFoundException{
//方法一:一般不用
Person p=new Person();
Class c1=p.getClass();
//方法二:很少用
Class c2=Person.class;
//方法三:常用
String className="neuq.test.Person";
Class c3=Class.forName(className);

System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
}

/*
* 测试通过指定的构造器实例化对象。
* @author:Dean
* @date:1994-6-23
*
*/

@Test
public void testGetSpecifiedConstructor() throws
ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException{
//通过类名拿到Class
String className="neuq.test.Person";
Class clazz=Class.forName(className);
//通过Class对象拿到Constructor,并制定参数类型。
Constructor constructor=clazz.getConstructor(String.class,int.class);
//通过Constructor实例化对象。
Person p=(Person) constructor.newInstance("Dean",20);

System.out.println(p);
}

/*
* 测试反射字段的方法&以及暴力访问
*
* @author:Dean
* @date:2014-6-23
*/
@Test
public void testGetField() throws Exception{
//首先拿到Class对象,并实例化
String className="neuq.test.Person";
Class clazz=Class.forName(className);
Constructor constructor=clazz.getConstructor(String.class,int.class);
Object obj=	constructor.newInstance("Dean",20);

/*
* 拿到字段,并设置值
* 私有属性只能根据getDeclaredXXX()拿到。否则会出NoSuchXXX异常
*/
Field field=clazz.getDeclaredField("name");
String value="Sam";

//暴力访问私有属性,设置可访问性。
field.setAccessible(true);
field.set(obj, value);
}

/*
*测试反射 静态方法 带参数的方法 不带参数的方法
* @ staticMethod()
* @	paramMethod()
* @	noParamMethod()
*
* @author:Dean
* @date:2014-6-23
*/
@Test
public void testGetMethod() throws Exception{
//先拿到Class的对象
String className="neuq.test.Person";
Class clazz=Class.forName(className);
Constructor constructor=clazz.getConstructor(String.class,int.class);
Object obj=constructor.newInstance("Dean",20);

//先测试静态方法,不需要实例化对象
Method method1=clazz.getMethod("staticMethod");
method1.invoke(null, null);

//测试无参数方法,需要实例化对象,不需要指定参数
Method method2=clazz.getMethod("noParamMethod");
method2.invoke(obj, null);

//测试带参数方法,需要实例化对象,需要指定参数
Method method3=clazz.getMethod("paramMethod", String.class,int.class);
method3.invoke(obj, "Sam",20);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: