次のコードは、Reflection API を使用してフィールドの値を取得します。提供された画像でわかるように、これにより未チェックのキャスト警告が生成されます。@SuppressWarnings("unchecked")を使用して警告を抑制することができます。これに代わるものがあるかどうか疑問に思っていましたか?
更新: KeyType はジェネリックです。だからKeyType.class.cast(object); 型消去のために機能しません。
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;
}
解決策: SuppressWarnings注釈がここに行く方法のようです.. お時間をいただきありがとうございます。