类型为null的参数应显式转换为Class<?> []以调用varargs方法 [英] The argument of type null should explicitly be cast to Class<?>[] for the invocation of the varargs method

查看:146
本文介绍了类型为null的参数应显式转换为Class<?> []以调用varargs方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看下面的示例,第一次调用 getMethod()在Eclipse中生成一个警告。第二个不起作用,并以 NoSuchMethodException 失败。

Please have a look at the following example, the first call to getMethod() produces a Warning in Eclipse. The second one doesn't work and fails with a NoSuchMethodException.


参数类型 null 应显式转换为 Class<?> [] 以调用varargs方法 getMethod(String,Class<?> ...) from type Class< Example> 。也可以将其强制转换为类进行varargs调用。

The argument of type null should explicitly be cast to Class<?>[] for the invocation of the varargs method getMethod(String, Class<?>...) from type Class<Example>. It could alternatively be cast to Class for a varargs invocation.

我按下了警告,没有任何工作了。

I followed the warning and nothing worked anymore.

import java.lang.reflect.Method;


public class Example
{
  public void exampleMethod() { }

  public static void main(String[] args) throws Throwable
  {
   Method defaultNull = Example.class.getMethod("exampleMethod", null);         
   Method castedNull = Example.class.getMethod("exampleMethod", (Class<?>) null);
 }
}

第二次调用产生此错误:

The second call produces this error:

Exception in thread "main" java.lang.NoSuchMethodException: 
    Example.exampleMethod(null)
        at java.lang.Class.getMethod(Class.java:1605)
        at Example.main(Example.java:12)

有人可以向我解释这种行为吗?什么是避免警告的正确方法?

Can someone explain this behaviour to me? What's the correct way to avoid the warning?

推荐答案

getMethod 方法是一个VarArg参数。
正确用法是:
如果反射方法没有参数,则不应指定第二个参数。
如果反射方法有参数,那么应该以下一种方式指定每个参数:

The second parameter to the getMethod method is a VarArg argument. The correct use is : If reflected method has no parameter, then no second parameter should be specified. If the reflected method has parameter, so each parameter should be specified in the next way:

import java.lang.reflect.Method;


public class Example {

    public void exampleMethodNoParam() {
        System.out.println("No params");
    }

    public void exampleMethodWithParam(String arg) {
        System.out.println(arg);
    }

    public static void main(String[] args) throws Throwable {
        Example example = new Example();
        Method noParam = Example.class.getMethod("exampleMethodNoParam");
        Method stringParam = Example.class.getMethod("exampleMethodWithParam", String.class);
        noParam.invoke(example);
        stringParam.invoke(example, "test");
        //output 
        //No params
        //test
    }
}

更新

因此,在您的情况下,当您指定 null时编译器不知道您指定了什么类型。当您尝试将 null 强制转换为未知但仍为类的类时,您会收到异常,因为没有

So, in your case, when you specify null the compiler doesn't know what type do you specify. When you try to cast the null to a Class which is unknown but anyway is a class, you get an exception because there is no

public void exampleMethod(Class<?> object){}

exampleMethod的签名。

signature of exampleMethod.

这篇关于类型为null的参数应显式转换为Class&lt;?&gt; []以调用varargs方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆