使用Java反射从getMethod获取NoSuchMethodException
我正在使用Java反射进行测试,并尝试根据参数的类型将重载方法应用于参数 然而,即使我试图获取的方法是公共的,我也没有任何方法例外。当我使用getDeclaredMethod时,此异常仍然出现 这是主要节目使用Java反射从getMethod获取NoSuchMethodException,java,get-method,Java,Get Method,我正在使用Java反射进行测试,并尝试根据参数的类型将重载方法应用于参数 然而,即使我试图获取的方法是公共的,我也没有任何方法例外。当我使用getDeclaredMethod时,此异常仍然出现 这是主要节目 public class Test { public static void main(Object... inputs){ InputManipulation test = new InputManipulation(); for (Objec
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);}
}
提前谢谢
我现在更改了测试类,如下所示。。但问题依然存在
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放入一个类数组中,但没有起到任何作用
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);
}
}
}
您的问题是由这个方法引起的代码>
您迭代两种类型的参数,并根据此参数调用方法“add”。当您尝试使用参数Integer调用方法时,您将传递给导致错误的方法字符串参数,您提供的初始代码似乎存在一些问题,正如其他人所建议的那样,使用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();
}
}
}
}
现在按如下方式更改您的测试类:
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();
}
}
}
}
import java.lang.reflect.InvocationTargetException;
导入java.lang.reflect.Method;
公开课考试{
公共静态void main(字符串[]args){
Test testExample=新测试();
testExample.testMethod(“String1”,1,“String2”);
}
公共void测试方法(对象…输入){
InputManipulation测试=新的InputManipulation();
用于(对象输入:输入){
类如何启动此应用程序?您的main()
方法没有Java正确的main()
方法签名,看这里你的代码是不可编译的,你怎么能得到一个异常?!OP实际上谈到了编译器错误:未处理的异常类型NoSuchMethodException
@Cherylyyang看看我关于修复的第一条评论。实际上,如果你有一个合适的IDE,它应该建议你如何修复它这不是专业的这里有个问题…阅读问题下面的注释,你也希望了解问题是什么问题是它抛出了nosuchmethodexception,这解决了问题它确实解决了编译错误,但它没有解决代码的实际问题,然后为该类获得适当的add
方法。获取选择正确的方法,使用给定的参数执行。您仍然没有阅读注释…现在删除if else
,因为它现在完全没有用。最后一步是正确设置答案格式并添加一些解释