Reputation: 758
I don't understand why I'm getting a warning with the following code:
public static boolean isAssignableFrom(Class clazz, Object o) {
return clazz.isAssignableFrom(o.getClass());
}
Unchecked call to
isAssignableFrom(Class<?>)
as a member of raw typejava.lang.Class
When I use the isInstance
method instead (which provides identical results from what I understand), I don't get a warning:
public static boolean isAssignableFrom(Class clazz, Object o) {
return clazz.isInstance(o);
}
Upvotes: 5
Views: 1342
Reputation: 201409
Because Class
is a generic type, and you aren't telling Java that the Object
must be an instance of the class. Change
public static boolean isAssignableFrom(Class clazz, Object o)
to something like
public static <C> boolean isAssignableFrom(Class<C> clazz, C o)
Upvotes: 3