I'm working on reflection in java. I know this is a common question and there are a lot of articles about it but I'm a bit confused and can't seem to find the right solution for what I'm trying to achieve.
I have :
public class ClassA {
private List<?> myList;
// getters, setters
}
"myList" will contain a list of objects (DTOs) :
List<?> myList; // could be List<DTO1> or List<DTO2> ...
I'm trying to get the type of the list from another class like this :
public class ClassB {
public void myMethod() {
// get type of ClassA.getMyList()
}
}
I can only get "ArrayList" when I try using stuff like getClass(), getType()... Trying to get the class of an element in the list results in "LinkedHashMap".
I really want to achieve this by using a wildcard type for the list.
So is there a way I can get the type of myList with minimal code ?
EDIT :
Thank you all for your answers.
Just one thing : myList contains elements of type LinkedHashMap, this wasn't very clear on my post.
It seems this really can't be done. Each LinkedHashMap represents an object but you can't know which one. They are just a list of keys + values.