mudoot
mudoot

Reputation: 51

How can I get the reflect constructor with Generics in Java?

now exists a class below:

class A{
    private A(HashMap map){

    }
}

how can I get the constructor that the parameters are generics with reflection?

EDIT : Question edited.

Upvotes: 0

Views: 513

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533880

You can't have templates in Java. You can have Generics and you can get that information from the Constructor.

Constructor aConstructor = A.class.getConstructors()[0];
Class[] parameterTypes = aConstructor.getParameterTypes();
System.out.println(Arrays.toString(parameterTypes)); // prints [java.util.HashMap]

BTW: Is there any reason it has to be a HashMap and not a Map?

Upvotes: 1

Related Questions