關於反射的問題,請高手幫忙!線上急等!

kingnacn發表於2007-03-08

簡要描述:invoke呼叫一個bean的get方法,返回的是空。確認傳給invoke的物件屬性有值。

以下是我的程式碼,請各位高手幫忙!

public TblAppLckehuinfo bean2bean(KehuinfoBean bean1,TblAppLckehuinfo bean2) throws SecurityException, NoSuchMethodException{

Class c1 = bean1.getClass();

Class c2 = bean2.getClass();

Field[] bean1fields = c1.getDeclaredFields();

Field[] bean2fields = c2.getDeclaredFields();

for(int i=0;i<bean1fields.length;i++){

Field field = bean1fields[0];

Class[] methodtypes = new Class[1];

String typename = field.getType().getName();

try {

methodtypes[0] = Class.forName(typename);

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Method method1 = c1.getDeclaredMethod(this.getGetterName(field.getName()),null);

Method method2 = c2.getDeclaredMethod(this.getSetterName(field.getName()),methodtypes);

try {

Object[] obj = new Object[0];

Object[] object = new Object[1];

object[0] = method1.invoke(bean1,obj);//這一句返回值是null,問題就在這裡!

method2.invoke(bean2,object);

} catch (IllegalArgumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InvocationTargetException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return pojo;

}

public String getGetterName(String fieldname){

String str = "";

str = "get"+fieldname.substring(0,1).toUpperCase()+fieldname.substring(1,fieldname.length());

return str;

}

public String getSetterName(String fieldname){

String str = "";

str = "set"+fieldname.substring(0,1).toUpperCase()+fieldname.substring(1,fieldname.length());

return str;

}

本人自己感覺沒有什麼問題,DEBUG跟蹤也都正常

相關文章