java.lang.Class および java.lang.reflect を含む)java.lang.Class の次のメソッドがジェネリクス化されました: getInterfaces()、getClasses()、getConstructors()、getMethod(String, Class...)、getConstructor(Class...)、getDeclaredClasses()、 getDeclaredConstructors()、getDeclaredMethod(String, Class...)、および getDeclaredConstructor(Class...)。その結果、これらのメソッドを使用するコードは、コンパイル中に警告が出力されるようになりました。getDeclaredMethod() を呼び出す次のコードを考えます。
import java.lang.reflect.Method;
public class Warning {
void m() {
try {
Warning warn = new Warning();
Class c = warn.getClass();
Method m = c.getDeclaredMethod("m");
} catch (NoSuchMethodException x) {
x.printStackTrace();
}
}
}
$ javac Warning.java
Note: Warning.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
$ javac -Xlint:unchecked Warning.java
Warning.java:8: warning: [unchecked] unchecked call to getDeclaredMethod(java.lang.String,java.lang.Class<?>...) as a member of the raw type java.lang.Class
Method m = c.getDeclaredMethod("m");
^
1 warning
警告を回避するには、適切なジェネリック型を含むように c の宣言を変更するようにしてください。この場合、宣言は次のようにするとよいでしょう。
Class<?> c = warn.getClass();
Method.toString() および Constructor.toString() が、修飾子のセットを正しく表示するようになりました。Array.newInstance(Class, int...) の最後のパラメータは、可変長引数です。java.lang.Class および java.lang.reflect での変更は次のとおりです。