Reputation: 45
Maybe this is not a big problem, but i could not figure it out. Is there a way to check if a class is defined as a generic type. I don't want to define an instance of this class, i just want somehow to know, if this class is defined as a normal class or as generic type class.
This would be the first Class, which is of type Generic
public class MyGenericClass<T, G> {
public void someMethod(T firstParam, G secondParam){
// do something
}
}
and this would be the second and normal Class
public class AnotherClass {
public void someMethod(){
// do something
}
}
I want a way to find out this result
isGenericType(MyGenericClass.class) --> true
isGenericType(AnotherClass.class) --> false
Upvotes: 3
Views: 1055
Reputation: 59114
From the Class<?> object
you can check cls.getTypeParameters()
.
From a generic class you get a nonempty array of TypeVariable
.
From a non-generic class you get an empty array.
E.g.
java.util.List.class.getTypeParameters()
TypeVariable[1] { E }
String.class.getTypeParameters()
TypeVariable[0] {}
Upvotes: 3