Java Reflect 2 Objects

Hi, I got the code below:

ModuleA.Student student 1 = null; ModuleB.Student student 2 = null; student2 = retrieveStudentFacade().findStudentbyName("John"); student1 = StudentSessionEJBBean.convert(student2,ModuleA.Student.Class); 

Now the problem is student1.getId (); return null, but should return a value to me. The following is the converter method, and someone will tell me using this method to reflect objects. It works well, as the error does not occur, just no return value?

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; } 
+1
source share
4 answers

In fact, really, you do not want to do this! Well, you can do it ... but you really really shouldn't do it.

Instead of using reflection, use a language and create such a constructor:

 package ModuleA; // should be all lower case by convention... public class Student { // pick differnt names for them is a good idea... 2 classes called Student is asking for trouble public Student(final ModualB.Student other) { // do the copying here like xxx = other.getXXX(); } } 

What you need to do in the code:

  • do not declare that the method "throws an exception", because then you need to have a "catch (Exception ex)", and this may make you hide errors in your code.

  • (guessing), at least in the catch block, execute "ex.printStackTrace ()" (or write it down) so you can see that something went wrong.

+2
source

Are you sure you are not swallowing any Exceptions?

I suggest you use setter / getter methods instead of directly accessing fields. You can retrieve methods similar to fields, and then call them on objects.

Although the code gets complicated, you should be able to achieve what you want.

Tools like bean copy utils also use getter / setter (so they only work on "beans" that have getter / seters that follow naming conventions).

+1
source

The reasons you are trying to do would be strange (share them), but there are better approaches than using reflection manually.

But you will need public setters / getters for the properties you want to copy (and you must have them anyway), and you will have to instantiate the target object.

+1
source

The getFields method returns only public fields (i.e., non-closed, protected or accessible packages). From JavaDoc:

Returns an array containing Field objects reflecting all available public fields of a class or interface

I assume that the id field you care about is confidential, so you should use the getDeclaredFields method instead.

Returns an array of Field objects that reflect all the fields declared by the class or interface represented by this Class object. This includes public, secure, default (batch) access, and private fields, but excludes inherited fields.

To reflect the (private) fields returned by this method, you also need to call field.setAccessible (true).

0
source

All Articles