lundi 13 mars 2017

new of ParameterizedType [duplicate]

This question already has an answer here:

If we have class as shown below:

Class ClassA {

}

To create an instance of classA using reflection, we either get reference to the default constructor and invoke it or call Class.forName(".ClassA").newInstance()

The question is how do I achive the same thing for a class that takes generic type.

Say I have a class

public class ClassA<T> implements IClassA<String, T> {

    public ClassA(Class<T> clazz) {
        super(clazz, clazz.getSimpleName());
    }
}

If we want to create an instance of type ClassA we do the following:

ClassA object = new ClassA<>(ClassB.class);

How do I achieve the same thing using reflection.

Thanks for your help in advance...

-Chandra





Aucun commentaire:

Enregistrer un commentaire