Possible Duplicate:
Generic type of local variable at runtime
I'm new to Java generics, and coming from a .NET world, I'm used to being able to write a method like this:
public void genericMethod<T>(T genericObject)
{
if (genericObject is IList<String>)
{
//Do something...
}
}
The method accepts an object of a generic type, and checks whether that object implements a specific version of the generic interface IList<>
, in this case, IList<String>
.
Now, in Java, I'm able to do this:
public <T> void genericMethod(T genericObject)
{
if (genericObject instanceof Set<?>)
{
//Do something...
}
}
BUT
Java does not let me do if (genericObject instanceof Set<String>)
From what I know, because of type erasure, normally in Java this would be taken care of by a class object, and we would do something like the following:
public <T> void genericMethod(T genericObject)
{
Class<OurTestingType> testClass = OurTestingType.class;
if (genericObject.getClass() == testClass)
{
//Do something...
}
}
but since the type I'm checking for is a generic interface, you can't do this:
Class<Set<String>> testClass = Set<String>.class
So, how, in Java, do I check if a generic object implements the specific type of Set<String>
?