最后生成的实体打印出来是null,这是为啥?(代码如下:)import java.lang.reflect.Method;
import java.util.Date;public class ReflectTest {
private Integer id;
private String userName;
private String passWord;
private Date birchday; 

public static void main(String[] args) throws Exception {
ReflectTest rt = null;
String[] getM = new String[4];
String[] setM = new String[4];
String[] attributeNames = new String[]{"id","userName","passWord","birchday"};
for (int i = 0; i < 4; i++) {
String a = Character.toUpperCase(attributeNames[i].charAt(0)) + attributeNames[i].substring(1);
getM[i] = "get" + a;
setM[i] = "set" + a;
//System.out.println(methodNames[i]);
rt = new ReflectTest();
Method get = rt.getClass().getMethod(getM[i]);
Method set = rt.getClass().getMethod(setM[i],get.getReturnType());
//System.out.println(set.getName());
//System.out.println(get.getReturnType().getName()); 
if (get.getReturnType().getName().equals("java.lang.String")) {
set.invoke(rt, "my" + attributeNames[i]);
}
}
System.out.println(rt);
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
} public String getPassWord() {
return passWord;
} public void setPassWord(String passWord) {
this.passWord = passWord;
} public Date getBirchday() {
return birchday;
} public void setBirchday(Date birchday) {
this.birchday = birchday;
} @Override
public String toString() {
return "ReflexTest [birchday=" + birchday + ", id=" + id
+ ", passWord=" + passWord + ", userName=" + userName + "]";
}

}

解决方案 »

  1.   

    发个带格式的,这样看着多累啊
    提练一下问题,代码不用全发,像什么 set  get的拿掉就行。
      

  2.   

    你对反射部分的几个方法调用API不对,导致逻辑上根本没有按你的预期走比如说
    Method get = rt.getClass().getMethod(getM[i]);
                Method set = rt.getClass().getMethod(setM[i],get.getReturnType());
    给你贴一个可以跑的反射代码
    import java.lang.reflect.*;public class ReflectTester {

    public Object copy(Object object)throws Exception{
    Class classType=object.getClass();
    System.out.println("class: "+classType.getName());

    Object objectCopy=classType.getConstructor(new Class[]{}).
    newInstance(new Object[]{});//需要研究一下,这里先创建一个实例
    Field fields[]=classType.getDeclaredFields();
    for(int i=0;i<fields.length;i++){
    Field field=fields[i];
    String fieldName=field.getName();
    String firstLetter=fieldName.substring(0,1).toUpperCase();
    String getMethodName="get"+firstLetter+fieldName.substring(1);
    String setMethodName="set"+firstLetter+fieldName.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[]{});
    System.out.println(fieldName+":"+value);
    setMethod.invoke(objectCopy, new Object[]{value});
    }
    return objectCopy;
    } /**
     * @param args
     */
    public static void main(String[] args)throws Exception {
    // TODO Auto-generated method stub
    Customer customer=new Customer("Tom",21);
    customer.setId(new Long(1L));

    Customer customerCopy=(Customer)new ReflectTester().copy(customer);
    System.out.println("Copy information:"+customerCopy.getName()+" "+
    customer.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 int getAge() {
    return age;
    } public void setAge(int age) {
    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;
    }

    }
      

  3.   

    rt = new ReflectTest();
    为什么在循环里new ,外面去。
      

  4.   

    没有问题啊,类的属性初始值都是null的,除了null,你想得到什么,你初始化赋个值看看
      

  5.   

    帮楼主改了一下代码,楼主的代码是编译不通过的public class ReflectTest {
    private Integer id;
    private String userName;
    private String passWord;
    private Date birchday; public static void main(String[] args) throws Exception {
    ReflectTest rt = new ReflectTest();
    String[] getM = new String[4];
    String[] setM = new String[4];
    String[] attributeNames = new String[]{"id","userName","passWord","birchday"};
    for (int i = 0; i < 4; i++) {
    String a = Character.toUpperCase(attributeNames[i].charAt(0)) + attributeNames[i].substring(1);
    getM[i] = "get" + a;
    setM[i] = "set" + a;
    //System.out.println(methodNames[i]);
    Method get = rt.getClass().getMethod(getM[i],new Class[]{});
    Method set = rt.getClass().getMethod(setM[i],new Class[]{rt.getClass().getDeclaredField(attributeNames[i]).getType()});
    //System.out.println(set.getName());
    //System.out.println(get.getReturnType().getName()); 
    if (get.getReturnType().getName().equals("java.lang.String")) {
    System.out.println("set Method name: " + set.getName());
    set.invoke(rt, new String[]{"my" + attributeNames[i]});
    }
    }
    System.out.println(rt);
    }
    }这里省略了其他部分。另外,为什么楼主那里输出是空的,除了编译不通过的原因之外,楼主是在每次循环的时候重新初始化了一次这个rt,并且最后没有执行任何的set方法,所以输出出来是空的。如果解决了楼主的问题麻烦给分吧,谢谢啊!!!
      

  6.   

    其实就是new ReflectTest() 放在循环里面的问题,谢谢各位