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?
Update: KeyType is a generic. So KeyType.class.cast(object); wouldn't work due to type erasure.
private K getId(Field idField, V o) {
K id = null;
try {
idField.setAccessible(true);
id = (K) idField.get(o);
} catch (IllegalAccessException ignored) {
/* This never occurs since we have set the field accessible */
}
return id;
}
Solution: Seems like the SuppressWarnings annotation IS the way to go here.. thanks for your time guys.