lundi 2 novembre 2015

Java Reflection: (Type) field.get(object) - unchecked cast

Following code retrieves the value of a field using the Reflection API. As you can see in the provided image, this generates an unchecked cast warning. The warning can be suppressed using @SuppressWarnings("unchecked"). I was wondering if there is an alternative to this though?

private KeyType getId(Field idField, ObjectType o) {
    KeyType id = null;
    try {
        idField.setAccessible(true);
        id = (KeyType) idField.get(o);
    } catch (IllegalAccessException ignored) {
        /* This never occurs since we have set the field accessible */
    }
    return id;
}

Unchecked cast warning





Aucun commentaire:

Enregistrer un commentaire