【问题标题】:Java Error: The method getMethod(String, Class<boolean>) is undefined for type ClassJava 错误:方法 getMethod(String, Class<boolean>) 未为 Class 类型定义
【发布时间】:2012-06-22 18:31:27
【问题描述】:

我试图将一个方法作为参数传递给另一个类中的方法。该方法在第一个类中定义,另一个类的方法是静态的。看了会更容易理解:

设置

public class MyClass extends ParentClass {
    public MyClass() {
        super(new ClickHandler() {
            public void onClick(ClickEvent event) {
                try {
                    OtherClass.responseMethod(MyClass.class.getMethod("myMethod",Boolean.class));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void myMethod(Boolean success) {
        if(success.booleanValue()) {
            //do stuff
        }
    }
}

但是,当我尝试构建时,我收到以下错误:

错误

The method getMethod(String, Class<boolean>) is undefined for the type Class<MyClass>

问题不是没有找到myMethod,而是没有找到Class&lt;MyClass&gt;.getMethod,我也不知道为什么。

更新

我们已经重新编写了这部分代码,并且没有使用“getMethodorgetDeclaredMethod”。由于 npe 发现我正在做的事情存在一些问题,并付出了很多努力来寻找答案,所以我接受了这个答案。

【问题讨论】:

  • 使用Boolean 而不是boolean
  • 您发布的代码无法编译,因为您将代码(try / OtherClass.responseMethod.... 东西)直接放在匿名类主体中。
  • 为了记录,在我的代码中我没有忘记onClick方法,只是写在这里。现在已经固定在这里了。仍然出现同样的错误。
  • 告诉我们你用什么编译它?因为现在它可以很好地与 Eclipse 和 JDK 1.6.0_26 一起编译。
  • 真的吗?它在 ant 构建/编译中失败

标签: java class methods compiler-errors undefined


【解决方案1】:

更新 2

编译时错误表明您正在使用 Java 1.4 编译该类。 现在,在 Java 1.4 中,将数组参数定义为 Type... 是非法的,您必须将它们定义为 Type[],这就是为 Class 定义 getMethod 的方式:

Method getMethod(String name, Class[] parameterTypes)

因此,您不能使用简化的 1.5 语法并编写:

MyClass.class.getMethod("myMethod",boolean.class));

你需要做的是:

MyClass.class.getMethod("myMethod",new Class[] {boolean.class}));

更新 1

您发布的代码由于其他原因无法编译:

super(new ClickHandler() {

    // This is anonymous class body 
    // You cannot place code directly here. Embed it in anonymous block, 
    // or a method.

    try {
        OtherClass.responseMethod(
            MyClass.class.getMethod("myMethod",boolean.class));
    } catch (Exception e) {
        e.printStackTrace();
    }
});

你应该做的是创建一个接受MethodClickHander构造函数,像这样

public ClickHandler(Method method) {

    try {
        OtherClass.responseMethod(method);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

然后,在MyClass 构造函数中像这样调用它:

public MyClass() {
    super(new ClickHandler(MyClass.class.getMethod("myMethod",boolean.class)));
}

原答案

更多内容,来自Class#getMethod(String, Class...)的JavaDoc

返回一个 Method 对象,该对象反映此 Class 对象表示的类或接口的指定公共成员方法。

你的方法是private,而不是public

如果您想访问私有方法,您应该使用Class#getDeclaredMethod(String, Class...) 并通过调用setAccessible(true) 使其可访问。

【讨论】:

  • 我尝试将我的方法更改为 public 和 protected,但都不起作用。这就是为什么我说问题不在于它没有找到我的方法,它没有找到getMethod。我想保密,所以我可能会使用getDeclaredMethod,但这不是我现在要问的问题。
  • 糟糕,我在这里写错了。该 try/catch 实际上位于新 ClickHandler 内的 onClick 方法定义中。我的错。
  • 天啊...那么解决这个问题。并发布实际的损坏代码,而不是为 stackoverflow 损坏的损坏代码。
  • 我们已经修改了这部分代码,我的问题不再相关。我将无法找出我的问题是什么,但既然你付出了这么多努力来回答,你会得到接受的答案。
【解决方案2】:

问题是您的代码无法编译

new ClickHandler() {
   // not in a method !!
        try {
            OtherClass.responseMethod(MyClass.class.getMethod("myMethod",boolean.class));
        } catch (Exception e) {
            e.printStackTrace();
        }

我假设 ClickHandler 有一个您应该定义的方法,并且您需要将此代码移至该方法。在任何情况下,您都不能将此代码放在方法或初始化程序块之外。


来自getMethod

返回一个 Method 对象,该对象反映此 Class 对象表示的类或接口的指定公共成员方法。

你的方法是private,而不是public

你可以使用getDeclaredMethod。

您遇到的另一个问题是此方法需要一个实例,而您似乎没有存储该实例。

【讨论】:

  • RE:私人与公共,请参阅我对另一个答案的评论。 RE:例如,你是说我宁愿说Class&lt;MyClass&gt; myClass = MyClass.class;" and then call getMethod`?
  • 您可以这样做,但 getMethod 仍然只会为您提供公共方法。按照我的建议尝试 getDeclaredMethod。
  • 对,我知道getDeclaredMethod,但错误不是找不到方法myMethod,而是根本没有定义s that getMethod`。我尝试做Class&lt;MyClass&gt; myClass = MyClass.class;,但仍然出现同样的错误。
  • 我没有意识到问题是代码甚至无法编译;)
  • 哈哈,是的,我想我应该说得更大一些。编译时错误,而不是运行时错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-23
  • 2011-07-24
  • 2020-01-17
  • 2013-06-13
  • 1970-01-01
  • 2015-07-28
相关资源
最近更新 更多