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
}
No. Due to Java's Type Erasure, all generics are stripped during the compile process.
You can however use instanceOf or getClass on elements in the list to see if they match a specific type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With