方法?</init>中的<init>方法调用错误

时间:2012-07-23 14:55:12

标签: java bytecode

我收到此错误:

java.lang.VerifyError: Bad <init> method call in method FooBar.<init>(I)V at offset 2
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2404)
    at java.lang.Class.getConstructor0(Class.java:2714)
    at java.lang.Class.getDeclaredConstructor(Class.java:2002)

尝试访问我使用ASM 4.0修改的类的构造函数时(使用jdk7)。

我检查了类的初始化方法的字节码,它如下:

aload_0
iload_1
invokespecial com/foo/F/<init>(I)V
return

反编译字节码会产生:

import com.foo.Foo;

public class FooBar extends Foo
{
  public FooBar(int i)
  {
    super(i);
  }
}

我完全不知道为什么我收到这个错误。我不知道我是否提供了足够的信息;如果我可以添加更多信息,请告诉我。

编辑:这是访问构造函数的代码:

Class fooBarClass = /* define class from class file bytes */;
Constructor fooBarConstructor = fooBarClass.getDeclaredConstructor(int.class);

EDIT2:这是Foo类的代码:

public class Foo extends F {

    public Foo(int i) {
        super(i);
    }
}

2 个答案:

答案 0 :(得分:2)

尝试反编译Foo类并注意正确的构造函数。我的赌注是构造函数Foo(int)不存在。

答案 1 :(得分:0)

抛出VerifyError是因为在类FooBar的构造函数中调用的方法实际上是F类的方法,而不是类Foo。

对类FooBar中的super方法的常量池引用是错误的类(即F而不是Foo)。因此,抛出VerifyError并显示相应的消息“Bad method call”。

相关问题