請看下面的示例,第一次調用getMethod()
會在Eclipse中產生警告。第二個不起作用,並以NoSuchMethodException
失敗。類型null的參數應明確地轉換爲類<?> []用於調用可變參量方法
The argument of type
null
should explicitly be cast toClass<?>[]
for the invocation of the varargs methodgetMethod(String, Class<?>...)
from typeClass<Example>
. It could alternatively be cast to Class for a varargs invocation.
我跟着警告,沒有任何工作了。
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);
}
}
第二個電話會產生這樣的錯誤:
Exception in thread "main" java.lang.NoSuchMethodException:
Example.exampleMethod(null)
at java.lang.Class.getMethod(Class.java:1605)
at Example.main(Example.java:12)
有人能解釋這種現象給我嗎?什麼是避免警告的正確方法?
鑄造'null'與未發現的'null'有什麼區別?我在JDK源代碼中看不到這一點。 – codevour 2011-04-08 07:14:55
請查看更新後的帖子。 – StKiller 2011-04-08 07:23:39