1、案例介绍
Student类,有属性id,name,age,对应的get、set方法和构造方法,现产生一个Student对象。通过反射复制此Student对象。
2、案例设计
通过反射机制来获取类的属性和方法。通过反射来生成对象,并通过反射来调用其set方法来对属性进行赋值以达到复制对象的目的。最后对复制成功的对象信息进行打印。
3、方案实施
(1)创建Student类。
(2)创建一个Student对象。
(3)获取Student对象所属类型的Class对象。
(4)通过调用class.newInstance方法来构建一个目标对象。
(5)获取class对象中的get和set方法。
(6)调用源对象的get方法获取属性值。
(7)调用目标对象的set方法来设置属性值。
(8)打印目标对象的数据。
4、案例实现
package com.iotek.reflect;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.activation.FileDataSource;
import org.omg.CORBA.portable.ValueBase;
public class ReflectionExampleDemo {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException{
        Student student = new Student(1, "EchoDemo", 22);
        Student student2 = (Student) ObjectCopyUtil.copyObj(student);
        System.out.println("复制对象成功");
        System.out.println(student2.toString());
    }
}
/**
 * 这是一个拷贝对象的工具类,内部提供了一个拷贝对象的方法,接收源对象。
 * @author 紫苏半夏
 *
 */
class ObjectCopyUtil{
    public static Object copyObj(Object obj) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException{
        //获取student对象所对应类型的Class对象(也就是Student类所对应的对象)
        Class<?> classType = obj.getClass();
        //通过class对象的newInstance方法来构建一个目标对象
        Object objCopy = classType.newInstance();
        //获取Class对象的get和set方法
        for(Field field : classType.getDeclaredFields()){
            /*//得到属性所对应的get和set方法
            String getMethodName = "get"+field.getName().substring(0,1).toUpperCase()+field.getName().substring(1);
            String setMethodName = "set"+field.getName().substring(0,1).toUpperCase()+field.getName().substring(1);
            //调用源对象的get方法获取属性值
            Method getMethod = classType.getDeclaredMethod(getMethodName, new Class[]{});
            Object value = getMethod.invoke(obj, new Object[]{});
            //调用源对象的set方法给属性赋值
            Method setMethod = classType.getDeclaredMethod(setMethodName, new Class[]{field.getType()});
            setMethod.invoke(obj, new Object[]{value});*/
            //直接获取源对象的值
            field.setAccessible(true);
            Object value = field.get(obj);
            field.set(objCopy, value);
        }
        return objCopy;
    }
}
//声明一个学生类
class Student{
    private int id;
    private String name;
    private int age;
    public Student(){
    }
    public Student(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public int getId() {
        return id;
    }
    public void setId(int 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;
    }
}