1.要想使用反射,首先需要获得待处理类或对象所对应的Class对象。
2.获取某个类或对象所对应的Class地下的常用三中方法
详见另一篇博文:《Java中Class类与反射机制的用法总结》
3.若通过类的不带参数的构造方法来生成对象,我们有两种方式:
3.1 首先获取待处理类的对应的Class对象,然后通过该Class对象的newInstance()方法直接生成类的实例对象。(此处的newInstance()方法是Class类的成员方法)
Class<?> classtype = object.getClass(); Object objectCopy = classtype.newInstance();
3.2 首先获取待处理类的对应的Class对象,然后通过该Class对象获得对应的Construtor对象,在通过该Construtor对象的newInstance()方法生成(此处的newInstance()方法是Construtor类的成员方法)
Class<?> classtype = object.getClass();
// 使用空构造方法生成示例对象
Constructor cons = classtype.getConstructor(new Class[]{});
Object objectCopy = cons.newInstance(new Object[]{});
//以上两行等价于这一行:Object objectCopy = classtype.newInstance();
//使用带参数的构造方法生成示例对象示例
//Constructor cons = classtype.getConstructor(new Class[]{String.class,int.class});
//Object obj = cons.newInstance(new Object[]{"hello",3});
4.若想通过带参数的构造方法生成实例,必须采用3.2的带参数格式的方法才可以。
5.ReflectTest类进一步演示了Reflection APIs的基本使用方法。
ReflectTest类有一个copy(Object object)方法,这个方法能够创建一个和参数object同样类型的对象,然后把object对象中的所有属性拷贝到新建的对象中,并将它返回。这个代码示例只能复制简单的JavaBean,假定JavaBean的每隔属性都有public类型的getXXX()和setXXX()方法。
代码示例:
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ReflectTest {
//该方法实现对Customer对象的拷贝操作
public Object copy(Object object)throws Exception{
Class<?> classtype = object.getClass();
//System.out.println(classtype.getName());
// 使用空构造方法生成示例对象
Constructor cons = classtype.getConstructor(new Class[]{});
Object objectCopy = cons.newInstance(new Object[]{});
//以上两行等价于这一行:Object objectCopy = classtype.newInstance();
//使用带参数的构造方法生成示例对象
//Constructor cons = classtype.getConstructor(new Class[]{String.class,int.class});
//Object obj = cons.newInstance(new Object[]{"hello",3});
Field[] fields = classtype.getDeclaredFields();
for(Field field:fields){
String name = field.getName();
//将属性的首字母转化为大写
String firstLetter = name.substring(0, 1).toUpperCase();
String getMethodName ="get"+firstLetter+name.substring(1);
String setMethodName = "set"+firstLetter+name.substring(1);
Method getMethod = classtype.getMethod(getMethodName, new Class[]{});
Method setMethod = classtype.getMethod(setMethodName,new Class[]{field.getType()});
Object value = getMethod.invoke(object, new Object[]{});
setMethod.invoke(objectCopy, new Object[]{value});
}
return objectCopy;
}
public static void main(String[] args)throws Exception{
Customer customer = new Customer("Tom",20);
customer.setId(1L);
ReflectTest test= new ReflectTest();
Customer customer2 = (Customer)test.copy(customer);
System.out.println(customer2.getId()+","+customer2.getName()+","+customer2.getAge());
}
}
class Customer{
private Long id;
private String name;
private int age;
public Customer(){
}
public Customer(String name, int age){
this.name = name;
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
运行结果:
1,Tom,20
本文参考:
第六十二讲 Class类、Method类及Field类的使用:http://www.iqiyi.com/w_19rr26kpe1.html
第六十三讲 反射机制大总结:http://www.iqiyi.com/w_19rr26m6gp.html