Jdk8 , using 'getDeclaredMethod' prompt 'NoSuchMethodException', but using 'getDeclaredMethods' is ok. Why?

huangapple go评论62阅读模式
英文:

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 -&gt; item.getName().contains(&quot;advanceProbe&quot;)).findFirst().get();
    System.out.println(&quot;advanceProbe = &quot; + advanceProbe);

    Method advanceProbe2 = clazz.getDeclaredMethod(&quot;advanceProbe&quot;);// line: 15 
    System.out.println(&quot;advanceProbe2 = &quot; + 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 &#39;getDeclaredMethod&#39; prompt &#39;NoSuchMethodException&#39;, but using &#39;getDeclaredMethods&#39;  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(&quot;advanceProbe&quot;, int.class);

huangapple
  • 本文由 发表于 2023年7月6日 13:46:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定