2

我收到此错误:

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);
    }
}
4

2 回答 2

2

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

于 2012-07-23T17:53:48.957 回答
1

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

FooBar 类中对超级方法的常量池引用指向了错误的类(即 F 而不是 Foo)。因此,VerifyError 与相应的消息“Bad method call”一起被抛出。

于 2012-07-26T03:21:43.533 回答