.java 使用未经检查或不安全的操作。注意:使用 -Xlint:unchecked 重新编译以了解详细信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20307801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details
提问by user2125844
My teacher gave us some sample code to help to show how reflection in Java works however, I am getting some errors:
我的老师给了我们一些示例代码来帮助展示 Java 中的反射是如何工作的,但是,我遇到了一些错误:
Note: DynamicMethodInvocation.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Here is the code:
这是代码:
import java.lang.reflect.*;
import java.lang.Class;
import static java.lang.System.out;
import static java.lang.System.err;
public class DynamicMethodInvocation {
public void work(int i, String s) {
out.printf("Called: i=%d, s=%s%n\n", i, s);
}
public static void main(String[] args) {
DynamicMethodInvocation x = new DynamicMethodInvocation();
Class clX = x.getClass();
out.println("class of x: " + clX + '\n');
// To find a method, need array of matching Class types.
Class[] argTypes = { int.class, String.class };
// Find a Method object for the given method.
Method toInvoke = null;
try {
toInvoke = clX.getMethod("work", argTypes);
out.println("method found: " + toInvoke + '\n');
} catch (NoSuchMethodException e) {
err.println(e);
}
// To invoke the method, need the invocation arguments, as an Object array
Object[] theArgs = { 42, "Chocolate Chips" };
// The last step: invoke the method.
try {
toInvoke.invoke(x, theArgs);
} catch (IllegalAccessException e) {
err.println(e);
} catch (InvocationTargetException e) {
err.println(e);
}
}
}
I know nothing about reflection and if anyone knows how I can modify this code to get this to compile it would be very appreciated.
我对反射一无所知,如果有人知道我如何修改此代码以使其编译,将不胜感激。
采纳答案by Vineet Kosaraju
There is no compile error, this is just a warning. You can ignore this and the class will still work properly.
没有编译错误,这只是一个警告。您可以忽略这一点,该类仍将正常工作。
If you want to ignore these warnings, you can add the following above your method:
如果要忽略这些警告,可以在方法上方添加以下内容:
@SuppressWarnings("unchecked")
Alternatively, you can fix this by changing the main method to:
或者,您可以通过将 main 方法更改为:
public static void main(String[] args) {
DynamicMethodInvocation x = new DynamicMethodInvocation();
Class<?> clX = x.getClass(); // added the generic ?
...
}
回答by Akib Bagwan
Note: Recompile with -Xlint:unchecked for details.
javac -Xlint:unchecked filename.java
it will show the unchecked all exceptions that must catch through the user define or system define exception code
它将显示未经检查的所有必须通过用户定义或系统定义异常代码捕获的异常