I have done my research but I still can't call it if it is possible or not. This question will sound as a special case. However, I tried to generalize it as much as I could.
In Java, I know that it is possible to use reflection to expose object's fields (say x
). And I know that it is possible to go deeper and expose the fields of a the sub objects e.g. x
's fields. However, when it comes to generic objects this becomes tricky.
To better explain this, assume we have the following two simple classes:
package reflection;
import java.util.*;
public class Subject {
public List<SomeUserDefinedObj> myListOfObj =
new ArrayList<SomeUserDefinedObj>();
public SomeUserDefinedObj singleObj = new SomeUserDefinedObj();
public Subject(){
myListOfObj.add(new SomeUserDefinedObj());
}
}
and
package reflection;
public class SomeUserDefinedObj {
public int x;
}
And we have the following test class that will examine the Subject
through reflection
package reflection;
import java.lang.reflect.*;
public class Test {
public static void main(String[] args) {
Subject subject = new Subject();
Class<?> c = subject.getClass();
for (Field field:c.getDeclaredFields()){
System.out.println("Field in " + c.getName() + ": " + field.getName());
Class<?> nestedClass = field.getType();
for (Field nestedField:nestedClass.getDeclaredFields()){
System.out.println("\tField in " + nestedClass.getName() + ": " + nestedField.getName());
}
}
}
}
The output of this test would be:
Field in reflection.Subject: myListOfObj
Field in reflection.Subject: singleObj
Field in reflection.SomeUserDefinedObj: x
Now, as we can see the content of the single object is retrieved, but the list is as if it is empty. I'm aware that there are ways to get the type of the list (from which you can iterate over the objects within the list) using the instance of the object itself e.g. subject
from Test
like here. For example, I can do Object myInstance = field.get(subject);
and from their I will able to determine the type of the elements in the list and iterate over them (tested and works).
However, (and here where my question might be specific) I don't have access to the instance (due to working with legacy code which design is preventing such luxury). So, I'm trying to reach this goal with only using the reflected class object e.g. c
from Test
. I have found some resources that will help me determine the type of the elements in the list like in here and here (I tested them and they work). However, this (first) doesn't grantee the actual type since the type could be an abstract class, (second) I can't see any way of getting the list's elements information from there regardless of the type being abstract or not. As far as I understand, retrieving the type given this method only guarantee that it will return to you the type that initialized with the list but not the elements (and even the type could be inaccurate as explained).
Finally, the resources that I have read don't state firmly if it is possible or not to achieve such goal given all the circumstances I stated (working only with the reflected object not the actual instance of the object). So, the question become, is it possible to retrieve a Collection elements without having access to the actual instance using reflection?
Aucun commentaire:
Enregistrer un commentaire