使用 Java 反射从 getMethod 获取 NoSuchMethodException
Getting NoSuchMethodException from getMethod using Java reflection
我正在使用 Java 反射进行测试,并尝试根据参数的类型将重载方法应用于参数..
但是,即使我尝试获取的方法是 public,我也有 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");
}
}
}
这里是自定义的InputManipulationclass
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);}
}
提前致谢!
我现在更改了测试 class 如下..但问题仍然存在。
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");
}
}
}
我也尝试将输入 Class 放入 Class 的数组中,正如另一个 post 所建议的,但它没有帮助..
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 种类型的参数,并根据此参数调用方法 'add'。当您尝试使用参数 Integer 调用方法时,您传递给导致错误的方法 String 参数
您提供的初始代码似乎存在一些问题,正如其他人所建议的那样,使用 IDE 会很快指出一些问题。但是,我已经接受了您的更新并修复了代码以在您提供的输入类型循环中调用正确的方法。
首先像这样更改您的 InputManipulation class:
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);
}
}
现在像这样改变你的测试class:
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();
}
}
}
}
我使用这些读数来帮助指导我的答案,但改变了我调用该方法的方式:
我正在使用 Java 反射进行测试,并尝试根据参数的类型将重载方法应用于参数..
但是,即使我尝试获取的方法是 public,我也有 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");
}
}
}
这里是自定义的InputManipulationclass
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);}
}
提前致谢!
我现在更改了测试 class 如下..但问题仍然存在。
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");
}
}
}
我也尝试将输入 Class 放入 Class 的数组中,正如另一个 post 所建议的,但它没有帮助..
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 种类型的参数,并根据此参数调用方法 'add'。当您尝试使用参数 Integer 调用方法时,您传递给导致错误的方法 String 参数
您提供的初始代码似乎存在一些问题,正如其他人所建议的那样,使用 IDE 会很快指出一些问题。但是,我已经接受了您的更新并修复了代码以在您提供的输入类型循环中调用正确的方法。
首先像这样更改您的 InputManipulation class:
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);
}
}
现在像这样改变你的测试class:
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();
}
}
}
}
我使用这些读数来帮助指导我的答案,但改变了我调用该方法的方式: