Is there a way in Java to reflect a generic type of a local variable? I know you can do that with a field – Get generic type of java.util.List. Any idea how to solve, for instance:
public void foo(List<String> s){
//reflect s somehow to get String
}
Or even more general:
public void foo<T>(List<T> s){
//reflect s somehow to get T
}
Here is nice tutorial that shows how and when you can read generic types using reflection. For example to get String from your firs
foomethodyou can use this code
Output: parameterArgClass = class java.lang.String
It was possible because your explicitly declared in source code that List can contains only Strings. However in case
T can be anything so because of type erasure it is impossible to retrieve info about precise T class.