How to check the type of a generic parameter in Java (without using reflection if possible) ?
The target is something like C# allows to do:
public <T> void doStaff() {
if(T is Type1) {
}
if(T is Type2) {
}
}
How to write a method like that in Java ?
Generic information in Java is erased at runtime. You can never do something like “T instanceof”. However, you can pass something into the method, and do an instanceof a reference, like so:
To make it clearer, T is a generic parameter, that goes away at runtime. t is a reference that you can work on at runtime.