lundi 26 octobre 2015

Java 8: create new instance of class with generics using reflection

I have the following method:

public Comparator<T> getComparator()  throws ReflectiveOperationException {
    String className = "some.ClassName";
    Class<Comparator<T>> c = Class.forName(className); // (1)
    return (Comparator<T>) c. newInstance();
}

In the line (1) I get this error:

Type mismatch: cannot convert from Class <capture#1-of ?> to Class<Comparator<T>>

What's wrong in this code and how should I make an instance of Comparator<T>?





Aucun commentaire:

Enregistrer un commentaire