嗨,我得到了下面的代码:的Java 2个反映对象
ModuleA.Student student 1 = null;
ModuleB.Student student 2 = null;
student2 = retrieveStudentFacade().findStudentbyName("John");
student1 = StudentSessionEJBBean.convert(student2,ModuleA.Student.Class);
现在的问题student1.getId();返回null,但应该返回一个值。下面是转换器方法,有人指导我使用这种方法来反映对象。它工作得很好,因为没有错误发生只是没有价值回报?
UPDATE
public static <A,B> B convert(A instance, Class<B> targetClass) throws Exception {
B target = (B) targetClass.newInstance();
for (Field targetField: targetClass.getDeclaredFields()) {
Field field = instance.getClass().getDeclaredField(targetField.getName());
field.setAccessible(true);
targetField.set(target, field.get(instance));
}
return target;
}
嗨我的ModuleB.Student的属性是私人的ModuleA.Student实际上是一个代理被保护 – user236501 2009-12-23 03:04:02