关于反射的问题,请高手帮忙!在线急等!

07-03-08 kingnacn
简要描述: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跟踪也都正常

banq
2007-03-09 17:23
>method1.invoke(bean1,obj);//

使用eclipse断点跟踪,可以看出bean1 和obj是否是你所要的对象,或者将这俩个类型输出。

猜你喜欢