mercredi 13 février 2019

How to use reflection in order to execute a generic constructor

Let's say I have String type = "Integer" for example, and I have on the other side a public class MyType<T> with a constructor public MyType(){} How can I use the java reflection newInstance method in order to be able to do something along the lines:

public static MyType<?> create(String type){

    return /*new MyType<Integer>() if type was "Integer", etc*/;

}





Aucun commentaire:

Enregistrer un commentaire