vendredi 23 décembre 2016

how to cast generic class in run-time, java8

everyone.

I am trying to cast generic class in run-time, java8.
I want to cast object to generic class ( such like List< String >, ArrayList< Integer >).
However, String or Integer is not determined in my situation, compile-time.
But I know the name of the classes( "String" or "Integer" ).
So, I think I can get Class by using reflection.
But I don't know how to do it. Is it possible? and What should I do?

List<String> list = new ArrayList<>();
list.add("hoge");

Object obj = (Object) list;
String className = "String";

List<String> list2 = /* do something to get this */
// (List<String>)list is NG. obj and className can be used only.





Aucun commentaire:

Enregistrer un commentaire