Problem at JUnit test with generics

Posted by Tom Brito on Stack Overflow See other posts from Stack Overflow or by Tom Brito
Published on 2010-03-23T06:37:08Z Indexed on 2010/03/23 6:43 UTC
Read the original article Hit count: 266

Filed under:
|
|

In my utility method:

public static <T> T getField(Object obj, Class c, String fieldName) {
    try {
        Field field = c.getDeclaredField(fieldName);
        field.setAccessible(true);
        return (T) field.get(obj);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
        return null;
    }
}

The line

return (T) field.get(obj);

gives the warning "Type safety: Unchecked cast from Object to T"; but I cannot perform instanceof check against type parameter T, so what am I suppose to do here?

© Stack Overflow or respective owner

Related posts about java

Related posts about junit