【问题标题】:Getting NoSuchMethodException from getMethod using Java reflection使用 Java 反射从 getMethod 获取 NoSuchMethodException
【发布时间】:2018-11-18 12:01:08
【问题描述】:

我正在使用 Java 反射进行测试,并尝试根据参数类型将重载方法应用于参数..

但是,即使我尝试获取的方法是公开的,我也有 NoSuchMethodException。在我使用 getDeclaredMethod 时仍然出现此异常。

这是主程序

public class Test {

    public static void main(Object... inputs){

        InputManipulation test = new InputManipulation();

        for (Object input: inputs){
            Class ownerClass = test.getClass();
            Class inputClass = input.getClass();
            Method method = ownerClass.getDeclaredMethod("add", inputClass);
            method.invoke(test, "Testing reflection");
        }

    }
}

这是自定义的 InputManipulation 类

public class InputManipulation {

    Integer total;

    public InputManipulation(){this.total = 0;}

    public void add(Integer num){this.total += num;}
    public void add(String str){System.out.println(str);}
}

提前致谢!

我现在改了Test类如下..但问题依旧存在。

public class Test {

    public static void main(String[] args){
        Test testExample = new Test();
        testExample.testMethod("String1", 1, "String2");
    }

    public void testMethod(Object... inputs){
        InputManipulation test = new InputManipulation();
        for (Object input: inputs){
            Class ownerClass = test.getClass();
            Class inputClass = input.getClass();
            Method method = ownerClass.getDeclaredMethod("add", inputClass);
            method.invoke(test, "Testing reflection");
        }

    }
}

我还尝试按照另一篇文章的建议将 inputClass 放入 Class 数组中,但没有帮助..

【问题讨论】:

  • 如何启动这个应用程序?
  • 您的main() 方法没有Java 正确的main() 方法签名,请看这里protechtraining.com/content/…
  • 你的代码不可编译,你怎么会出现异常?!
  • OP 其实讲的是编译器错误:Unhandled exception type NoSuchMethodException
  • @CherylYang 看看我对修复的第一条评论。实际上,如果你有一个合适的 IDE,它应该会建议你如何修复它

标签: java get-method


【解决方案1】:

您提供的初始代码似乎存在一些问题,正如其他人所建议的那样,使用 IDE 会很快指出一些问题。但是,我已经接受了您的更新并修复了代码以在您提供的输入类型的循环中调用正确的方法。

首先像这样改变你的 InputManipulation 类:

public class InputManipulation {

    Integer total;

    public InputManipulation() {
        this.total = 0;
    }

    public void add(Integer num) {
        this.total += num;
        System.out.println(this.total);
    }

    public void add(String str) {
        System.out.println(str);
    }

}

现在像这样改变你的测试类:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {

    public static void main(String[] args) {
         Test testExample = new Test();
            testExample.testMethod("String1", 1, "String2");
    }

     public void testMethod(Object... inputs){
            InputManipulation test = new InputManipulation();
            for (Object input: inputs){

                Class<? extends Object> ownerClass = test.getClass();
                Class<? extends Object> inputClass = input.getClass();
                //Method method;    //not needed

                try {
                    ownerClass.getDeclaredMethod("add", inputClass).invoke(test, input);

                } catch (NoSuchMethodException | SecurityException | 
                        IllegalAccessException | IllegalArgumentException |
                        InvocationTargetException e) {

                    e.printStackTrace();
                }

            }

      }
}

我使用这些读数来帮助指导我的答案,但改变了我调用该方法的方式:

http://tutorials.jenkov.com/java-reflection/methods.html

【讨论】:

    【解决方案2】:
    public class Test {
    
    public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
        Test testExample = new Test();
        testExample.testMethod("String1", 1, "String2");
    }
    
    public void testMethod(Object... inputs) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
        InputManipulation test = new InputManipulation();
        for (Object input: inputs){
            Class ownerClass = test.getClass();
            Class inputClass = input.getClass();
            Method method = ownerClass.getDeclaredMethod("add", inputClass);
            method.invoke(test, input);
        }
    }
    }
    

    你的问题是由这个method.invoke(test, "Testing reflection");引起的 您遍历 2 种类型的参数,并依赖于您调用方法“添加”的此参数。当您尝试使用参数 Integer 调用方法时,您传递给导致错误的方法 String 参数

    【讨论】:

    • 这不是问题所在...阅读问题下的 cmets,希望您也能理解问题所在
    • 问题是它抛出了nosuchmethodexception,这就解决了问题
    • 确实解决了编译错误,但并没有解决代码的实际问题
    • 然后为该类获取正确的add 方法。获得正确的方法后,使用给定的参数执行它。你还没有读过cmets...
    • 现在删除if-else,因为它现在完全没用了。最后一步是正确格式化答案并添加一些解释
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    相关资源
    最近更新 更多