package Reflection;import java.lang.reflect.Field;
import java.lang.reflect.Method;public class Reflection 
{
public Object copy(Object object)throws Exception
{
Class<?> classtest = object.getClass();
Object objectCopy = classtest.
getConstructor(new Class[] {})
.newInstance(new Object[] {});
Field[] fields = classtest.getFields();
for(Field field:fields)
{
String name = field.getName();
String fristLetter = name.substring(0).toUpperCase();
String getMethodName = "get"+fristLetter+name.substring(1);
String setMethodName = "set"+fristLetter+name.substring(1);
Method getMethod = classtest.getMethod(getMethodName,new Class[]{});
Method setMethod = classtest.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 {

Student student = new Student("110102","xiaoli");
student.setAge(23);
Reflection test = new Reflection();
Student st =(Student)test.copy(student);
System.out.println(st.getId()+" : "+ 
st.getName()+" : "+st.getAge());



}}
class Student
{
String id;
String name;
int age;
public String getId() {
return id;
}
public void setId(String 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;
}
Student()
{}
Student(String id,String name)
{
this.id = id;
this.name = name;

}
}

解决方案 »

  1.   

    其一,你的Student的构造函数不是public的,用classtest.getConstructor(new Class[] {})是获取不到的。把Student的构造函数改成public的,或者调用classtest.getDeclaredConstructor(new Class[] {})其二,classtest.getFields()也是无法获取Student的Field的,因为其也不是public的,需要调用classtest.getDeclaredFields()其三,String fristLetter = name.substring(0).toUpperCase() 改为 String fristLetter = name.substring(0, 1).toUpperCase();其四,这只能算一个玩具式的拷贝工具更改后的代码:public class Reflection {
    public Object copy(Object object) throws Exception {
    Class<?> classtest = object.getClass();
    Object objectCopy = classtest.getDeclaredConstructor(new Class[] {})
    .newInstance(new Object[] {}); Field[] fields = classtest.getDeclaredFields();
    for (Field field : fields) {
    String name = field.getName();
    String fristLetter = name.substring(0, 1).toUpperCase();
    String getMethodName = "get" + fristLetter + name.substring(1);
    String setMethodName = "set" + fristLetter + name.substring(1);
    Method getMethod = classtest.getMethod(getMethodName,
    new Class[] {});
    Method setMethod = classtest.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 { Student student = new Student("110102", "xiaoli");
    student.setAge(23);
    Reflection test = new Reflection();
    Student st = (Student) test.copy(student);
    System.out.println(st.getId() + " : " + st.getName() + " : "
    + st.getAge()); }}class Student {
    String id;
    String name;
    int age; public String getId() {
    return id;
    } public void setId(String 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;
    } Student() {
    } Student(String id, String name) {
    this.id = id;
    this.name = name; }
    }
      

  2.   

    可不可以设置访问权限 
    setAccessible()
      

  3.   

    你都获取不到member的引用,你设置谁的accessable?