简体   繁体   中英

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM