Java:当方法明确存在时,NoSuchMethodException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3200301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:20:37  来源:igfitidea点击:

Java: NoSuchMethodException when method clearly exists

javareflection

提问by Brian S

On my current project, I've felt the need to create a sort of simulated callback system in Java using reflection. However, I'm having issues getting my reflection to actually function. The code at fault follows:

在我当前的项目中,我觉得需要使用反射在 Java 中创建一种模拟回调系统。但是,我在使反射实际起作用时遇到了问题。出错的代码如下:

public Callback(Object parentObj, String methodName, Class<?>...parameters)
{
    if(parentObj == null)
        throw new IllegalArgumentException("parentObj cannot be null", new NullPointerException());

    Class<?> clazz = parentObj.getClass();

    // Trace debugging, see output
    for(Method m : clazz.getDeclaredMethods())
        if(m.getName().equals("myMethod")) System.out.println (m);

    try { this.method = clazz.getMethod(methodName, parameters); }
    catch(NoSuchMethodException nsme) { nsme.printStackTrace(); } // Exception caught
    catch(SecurityException se) { se.printStackTrace(); }

    this.parentObj = parentObj;
    this.parameters = parameters;
}

When I construct the Callbackobject, I'm using syntax like this:

当我构造Callback对象时,我使用的是这样的语法:

new Callback(this, "myMethod", boolean.class)

When I try to create my pseudo-callback, it hits the NoSuchMethodExceptioncatch block. I've included some trace debugging above to show the output of one of my methods failing. The output:

当我尝试创建我的伪回调时,它遇到了NoSuchMethodExceptioncatch 块。我在上面包含了一些跟踪调试以显示我的一种方法失败的输出。输出:

private void my.package.MyClass.myMethod(boolean)
java.lang.NoSuchMethodException: my.package.MyClass.myMethod(boolean)
    at java.lang.Class.getMethod(Class.java:1605)
    at my.package.other.Callback.<init>(Callback.java:63)

I couldn't figure the problem out, so I started hunting, to little avail. The best I could find was mention of versioning conflict between the compiled JAR and the runtime. However, MyJar.jar/META-INF/MANIFEST.MFcontains Created-By: 1.6.0_02 (Sun Microsystems Inc.). My IDE is running C:\Program Files\Java\jdk1.6.0_02\bin\javac.exeto compile my project. I'm using C:\Program Files\Java\jdk1.6.0_02\bin\java.exeto run my JAR.

我无法弄清楚问题所在,所以我开始寻找,但收效甚微。我能找到的最好的是提到编译的 JAR 和运行时之间的版本冲突。但是,MyJar.jar/META-INF/MANIFEST.MF包含Created-By: 1.6.0_02 (Sun Microsystems Inc.). 我的 IDE 正在运行C:\Program Files\Java\jdk1.6.0_02\bin\javac.exe以编译我的项目。我C:\Program Files\Java\jdk1.6.0_02\bin\java.exe用来运行我的 JAR。

I'm at a loss why Class.getMethodis claiming the method doesn't exist, but Class.getMethodsseems to have no problem finding it. Help? :(

我不知道为什么Class.getMethod声称该方法不存在,但Class.getMethods找到它似乎没有问题。帮助?:(

采纳答案by ZZ Coder

Your method is private but getMethod()only returns public method.

您的方法是私有的,但getMethod()只返回公共方法。

You need to use getDeclaredMethod().

您需要使用getDeclaredMethod().

回答by Laurence Gonsalves

The versioning issue that can cause NoSuchMethodException isn't a difference between the compiler versions. It's a difference in the version of (in your case) MyClassat compile time versus runtime.

可能导致 NoSuchMethodException 的版本问题不是编译器版本之间的差异。这是MyClass编译时(在您的情况下)与运行时的版本不同。

Since you're using reflection you issue might have nothing to do with versioning, though. Certainly that would not explain different behavior between getMethodand getDeclaredMethods, because you're running them against the same Class instance, hence a version difference isn't really possible.

但是,由于您使用的是反射,因此您的问题可能与版本控制无关。当然,这不能解释getMethodand之间的不同行为getDeclaredMethods,因为您是针对同一个 Class 实例运行它们,因此版本差异实际上是不可能的。

Are you sure that the parameters match your actual method?

您确定这些参数与您的实际方法相符吗?

回答by Adam Crume

The Javadoc for getMethodisn't explicit, but it looks like it might throw a NoSuchMethodException for methods that aren't public, and your method is private.

getMethod的 Javadoc不是明确的,但看起来它可能会为非公共方法抛出 NoSuchMethodException ,并且您的方法是私有的。

回答by Thorbj?rn Ravn Andersen

You need the parameter list to be absolutely correct for the method you want for the call to succeed.

您需要参数列表对于您希望调用成功的方法是绝对正确的。

I've found that tiny steps are important when doing reflection because the compiler doesn't help. Write a small snippet which actually invokes exactly the method you want to in thisparticular case, and then when that works, generalize it into the framework here. I would focus on the parameters passed.

我发现在进行反射时微小的步骤很重要,因为编译器没有帮助。编写一个小片段,在这种特殊情况下实际调用您想要的方法,然后当它起作用时,将其概括到此处的框架中。我会专注于传递的参数。