vendredi 16 novembre 2018

Java : How to get type of List?>

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 ?





Aucun commentaire:

Enregistrer un commentaire