vendredi 24 avril 2020

Access list of methods by the generic type parameter T without any instance of the class

private Map<String, Service> serviceMap = new HashMap();

public <T extends Service> T getService() {
    return (T) this.serviceMap.get(T.class.getTypeName()); // <-- is not possible
}

I want to access the definition of a class using reflection without having any instance of that class by just using T, but it is not possible with T.class, so I ask if there is any other solution with up to Java 14 to get access without passing a Class<T> as parameter to getService(). You see in the code what I'm trying to achieve.





Aucun commentaire:

Enregistrer un commentaire