Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting an unchecked warning?

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 type java.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);
}
like image 404
Revolutionair Avatar asked Oct 31 '22 01:10

Revolutionair


1 Answers

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)
like image 60
Elliott Frisch Avatar answered Nov 04 '22 07:11

Elliott Frisch