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;
}
Aucun commentaire:
Enregistrer un commentaire