How to create java.lang.Class with a type parameter that is also printed?

What to put in place ??? in the following code to make it work?

import java.util.List; public class Generic { private static class Foo<T> { Foo(Class<T> clazz) { assert clazz != null; } } public static void main(String[] args) { Class<List<String>> x = ???; Foo<List<String>> t = new Foo<List<String>>(x); } } 
+4
source share
2 answers

I would go for:

 @SuppressWarnings("unchecked") Class<List<String>> klass = (Class<List<String>>)((Class<?>) List.class); 

And you do not need a class instance for this type of cast.

+6
source

At run time, all List "classes" are equal to the type of the generic type

 Class<List<String>> x = (Class<List<String>>) (Class) List.class; 
+1
source

Source: https://habr.com/ru/post/1441835/


All Articles