汇编语言 BasicInterpreter 抛出非法状态异常。

3

我在SO上看到了这个问题,现在我正在尝试编译被接受的答案中的代码。不幸的是,在代码的这部分我一直收到IllegalStateException:

BasicInterpreter basic = new BasicInterpreter() {
        @Override public BasicValue newValue(Type type) {
            return type!=null && (type.getSort()==Type.OBJECT || type.getSort()==Type.ARRAY)?
                    new BasicValue(type): super.newValue(type);
        }
        @Override public BasicValue merge(BasicValue a, BasicValue b) {
            if(a.equals(b)) return a;
            if(a.isReference() && b.isReference())
                // this is the place to consider the actual type hierarchy if you want
                return BasicValue.REFERENCE_VALUE;
            return BasicValue.UNINITIALIZED_VALUE;
        }
    };

使用堆栈跟踪:
Exception in thread "main" java.lang.IllegalStateException
    at org.objectweb.asm.tree.analysis.BasicInterpreter.<init>(BasicInterpreter.java:66)
    at ConstantTracker$1.<init>(ConstantTracker.java:48)
    at ConstantTracker.<init>(ConstantTracker.java:48)
    at HelloWorld.analyze(HelloWorld.java:37)
    at HelloWorld.main(HelloWorld.java:28)

异常在BasicInterpreter类中被触发:
public BasicInterpreter() {
    super(/* latest api = */ ASM8);
    if (getClass() != BasicInterpreter.class) {
      throw new IllegalStateException(); // at this line
    }
  }

我尝试继承BasicInterpreter,但是我一直收到相同的异常。
class BasicInterpreterLocal extends BasicInterpreter{} // Throws an IllegalStateException

我尝试过使用asm 7.*,8.**,9.0,但都没有成功。

那么问题出在哪里呢?我找不到它。

1个回答

2

这个答案所解释的那样,子类应该使用接受库版本号的构造函数来确定ASM库的兼容性级别。似乎最初使用的ASM库版本5没有检查这个要求。但是您正在使用新版本(显然是8)的库,它强制执行此规则。

将代码更改为

BasicInterpreter basic = new BasicInterpreter(Opcodes.ASM5) { // <- the crucial point
    @Override public BasicValue newValue(Type type) {
        return type!=null && (type.getSort()==Type.OBJECT || type.getSort()==Type.ARRAY)?
               new BasicValue(type): super.newValue(type);
    }
    @Override public BasicValue merge(BasicValue a, BasicValue b) {
        if(a.equals(b)) return a;
        if(a.isReference() && b.isReference())
            // this is the place to consider the actual type hierarchy if you want
            return BasicValue.REFERENCE_VALUE;
        return BasicValue.UNINITIALIZED_VALUE;
    }
};

为了解决这个问题,我还将更新其他答案。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接