.java使用未经检查或不安全的操作。注意:用-Xlint重新编译:不检查细节。

时间:2022-07-25 15:24:01

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.

我对反射一无所知,如果有人知道我如何修改这段代码来编译它,我将非常感激。

2 个解决方案

#1


5  

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 ?
    ...
  } 

#2


0  

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

它将显示必须通过用户定义或系统定义异常代码的所有异常。

#1


5  

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 ?
    ...
  } 

#2


0  

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

它将显示必须通过用户定义或系统定义异常代码的所有异常。