Java-java.lang.NoSuchMethodException [英] Java - java.lang.NoSuchMethodException

查看:165
本文介绍了Java-java.lang.NoSuchMethodException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用此代码( Update 一次迭代后会出错,因为下一个方法将需要String.class.是否可以使用反射来解决此类问题?):

I try to use this code (Update m_set is used inside for loop which goes through few methods which use different type arguments. If I would add for example int.class in getMethod, I would get error after one iteration, because next method would require String.class. Is it possible to solve such problem using reflection?):

Method m_set = product.getClass().getMethod(method_name);
m_set.invoke(product, method_value);

我收到此错误:

 Exception in thread "main" java.lang.NoSuchMethodException: test.NormalChair.setHeight()
        at java.lang.Class.getMethod(Class.java:1655)
        at test.ProductTrader.create(ProductTrader.java:68)
        at test.Test.main(Test.java:32)

错误地表明它试图在使用此方法的类中查找方法.但是该方法在父类中,并且是公共方法.我知道是否要使用getDeclaredMethod,它会给出类似的错误,但是为什么使用getMethod会出现此错误?

In error it shows that it tries to find method in class that I use this method. But that method is in parent class and it is public method. I know if I would use getDeclaredMethod, it would give similar error, but why it gives this error using getMethod?

我的课堂有这个方法:

public abstract class AbstractChair {
    public String name;
    public int height;
    public AbstractChair() {
    }

    public AbstractChair(String name, int height){
        this.name = name;
        this.height = height;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }   
}

我尝试在以下方法上使用此方法的班级:

My class that I try to use this method on:

public class NormalChair extends AbstractChair {
    public NormalChair() {
        super();
    }

    public NormalChair(String name, int height) {
        super(name, height);
    }


    // Copy constructor
    public NormalChair(NormalChair chair) {
      this(chair.getName(), chair.getHeight());
    }

}

Update2

如果我做这样的事情:

if(method_name == "setHeight"){
  Method m_set = product.getClass().getMethod(method_name, int.class);
  m_set.invoke(product, method_value);
}
else if (method_name == "setName")
{
  Method m_set = product.getClass().getMethod(method_name, String.class);
  m_set.invoke(product, method_value);
}

然后错误消失.有人可以建议更通用的方法吗?

Then error disappears. Can someone suggest more universal approach?

推荐答案

似乎您忘记了传递方法需要的参数类型(请记住,方法可以重载不同的参数类型).看一下您的代码,这里没有setHeight()方法,但是有setHeight(int).您应该尝试类似

It seems that you forgot to pass type of arguments that your method needs (remember that methods can be overloaded with different arguments types). Take a look at your code, there is no setHeight() method there but setHeight(int). You should try something like

Method m_set = product.getClass().getMethod(method_name,method_value.getClass());
m_set.invoke(product, method_value);


由于原始类型可能会出现问题,因此可以使用方法.假设您要查找的类中只有一个具有相同名称的方法,您可以遍历所有公共方法,将其名称与要查找的方法进行比较,然后使用所需的参数调用它.像


Since you could have problems with primitive types you can use way around. Assuming that there is only one method with same name in your class you want to find you can iterate over all public methods, compare its name with method you are looking for, and then invoke it with arguments you want. Something like

Method[] methods = product.getClass().getMethods();
for (Method m : methods){
    System.out.println(m);
    if (m.getName().equals("setHeight")){
        m.invoke(product, method_value);
        break;
    }
}


另一种可能更好的方法是使用java.bean包中的类,例如PropertyDescriptor.通过此类,您可以找到特定属性的获取器和设置器.请注意,setHeight的属性为height,因此您需要像这样使用它


Another and probably better way would be using classes from java.bean package like PropertyDescriptor. Thanks to this class you can find getters and setters for specific property. Notice that property for setHeight is height so you would need to use it like

Method setter = new PropertyDescriptor("height", product.getClass()).getWriteMethod();
setter.invoke(product, method_value);

这篇关于Java-java.lang.NoSuchMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆