英文:
Jdk8 , using 'getDeclaredMethod' prompt 'NoSuchMethodException', but using 'getDeclaredMethods' is ok. Why?
问题
代码如下
```java
public static void main(String[] args) throws Exception {
Class<?> clazz = Class.forName(ThreadLocalRandom.class.getName());
Method advanceProbe = Arrays.stream(clazz.getDeclaredMethods()).filter(item -> item.getName().contains("advanceProbe")).findFirst().get();
System.out.println("advanceProbe = " + advanceProbe);
Method advanceProbe2 = clazz.getDeclaredMethod("advanceProbe");// line: 15
System.out.println("advanceProbe2 = " + advanceProbe2);
}
结果如下
advanceProbe = static final int java.util.concurrent.ThreadLocalRandom.advanceProbe(int)
Exception in thread "main" java.lang.NoSuchMethodException: java.util.concurrent.ThreadLocalRandom.advanceProbe()
at java.lang.Class.getDeclaredMethod(Class.java:2130)
at util.concurrent.Test.main(Test.java:15)
在Jdk8中,使用getDeclaredMethod
会提示NoSuchMethodException
,但使用getDeclaredMethods
是可以的。为什么?
<details>
<summary>英文:</summary>
[enter image description here](https://i.stack.imgur.com/Y3Rl3.png)
- code as follows
public static void main(String[] args) throws Exception {
Class<?> clazz = Class.forName(ThreadLocalRandom.class.getName());
Method advanceProbe = Arrays.stream(clazz.getDeclaredMethods()).filter(item -> item.getName().contains("advanceProbe")).findFirst().get();
System.out.println("advanceProbe = " + advanceProbe);
Method advanceProbe2 = clazz.getDeclaredMethod("advanceProbe");// line: 15
System.out.println("advanceProbe2 = " + advanceProbe2);
}
- result as follows
advanceProbe = static final int java.util.concurrent.ThreadLocalRandom.advanceProbe(int)
Exception in thread "main" java.lang.NoSuchMethodException: java.util.concurrent.ThreadLocalRandom.advanceProbe()
at java.lang.Class.getDeclaredMethod(Class.java:2130)
at util.concurrent.Test.main(Test.java:15)
Jdk8 , using 'getDeclaredMethod' prompt 'NoSuchMethodException', but using 'getDeclaredMethods' is ok. Why?
</details>
# 答案1
**得分**: 1
请参考[`getDeclaredMethod`][1]的文档。它期望找到您要查找的方法的参数类型,除了方法名称。如果您不包括任何参数类型,它将尝试查找具有该名称的无参数方法。
毕竟,否则`getDeclaredMethod`将如何处理具有相同名称的多个方法呢?您需要为它提供参数类型,以区分重载。
没有接受无参数的`advanceProbe`重载。这就是为什么没有找到方法的原因。
您要查找的方法接受一个`int`参数,所以您应该这样做:
```java
Method advanceProbe2 = clazz.getDeclaredMethod("advanceProbe", int.class);
英文:
Please see the documentation for getDeclaredMethod
. It expects the parameter types of the method that you are trying to find, in addition to the method name. If you don't include any parameter types, it will try to find a parameterless method with that name.
After all, how would getDeclaredMethod
handle multiple methods with the same name otherwise? You need to give it the parameter types for it to distinguish between overloads.
There is no overload of advanceProbe
that takes no parameters. That's why no method was found.
The method you are trying to find takes an int
parameter, so you should do:
Method advanceProbe2 = clazz.getDeclaredMethod("advanceProbe", int.class);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论