11
Java Reflectionについてa question last nightを投稿し、今朝コンパイラの警告を発見しています。Java Reflectionコールを適切にチェックする方法はありますか?
C:\javasandbox\reflection>javac ReflectionTest.java
Note: ReflectionTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:\javasandbox\reflection>javac -Xlint:unchecked ReflectionTest.java
ReflectionTest.java:17: warning: [unchecked] unchecked call to
getDeclaredMethod(java.lang.String,java.lang.Class<?>...) as a member of the raw
type java.lang.Class
myMethod = myTarget.getDeclaredMethod("getValue");
^
ReflectionTest.java:22: warning: [unchecked] unchecked call to
getDeclaredMethod(java.lang.String,java.lang.Class<?>...) as a member of the raw
type java.lang.Class
myMethod = myTarget.getDeclaredMethod("setValue", params);
^
2 warnings
これらの返されたメソッドを確認する適切な方法はありますか? (つまり、これらの警告を取り除くための適切な方法はありますか?)
ソースコード:
import java.lang.reflect.*;
class Target {
String value;
public Target() { this.value = new String("."); }
public void setValue(String value) { this.value = value; }
public String getValue() { return this.value; }
}
class ReflectionTest {
public static void main(String args[]) {
try {
Class myTarget = Class.forName("Target");
Method myMethod;
myMethod = myTarget.getDeclaredMethod("getValue");
System.out.println("Method Name: " + myMethod.toString());
Class params[] = new Class[1];
params[0] = String.class;
myMethod = myTarget.getDeclaredMethod("setValue", params);
System.out.println("Method Name: " + myMethod.toString());
} catch (Exception e) {
System.out.println("ERROR");
}
}
}
私は現在のJDK7コンパイラがその行でrawtypes警告を出し、変数が使用されている未チェックの遅延警告も出すと信じています。 –
最初のバージョンを意味しますか?おそらく第2版はまだ大丈夫です。 –
はい、現在のJDK8コンパイラは 'Class'の最初の行で' raw types'エラーを返しますが、 'Class >'という2行目では満足しています。 –