Calling a method with an arg of Class<T> where T is a parameterized type
generics, java
Solution
The following syntax was suggested on the Mockito issues discussion board:
SomeWrapper<List<Foo>> wrapper = (SomeWrapper<List<Foo>>) (Object) method(List.class);
Problem
I'm attempting to call a constructor method that looks like: ``` public static SomeWrapper<T> method(Class<T> arg); ``` When T is an unparameterized type like String or Integer, calling is straightforward: ``` SomeWrapper<String> wrapper = method(String.class); ``` Things get tricky when T is a parameterized type like `List<String>`. The following is not valid: ``` SomeWrapper<List<String>> wrapper = method(List<String>.class); ``` About the only thing I could come up with is: ``` List<String> o = new ArrayList<String>(); Class<List<String>> c = (Class<List<String>>) o.getClass(); SomeWrapper<List<String>> wrapper = method(c); ``` Surely there is an easier way that doesn't require the construction of an additional object?