英文:
ASM BasicInterpreter IllegalStateException
问题
我在Stack Overflow上看到了这个问题,我正试图编译接受答案中的代码。
不幸的是,我在代码的这部分一直遇到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(); // 在这行
}
}
我尝试继承BasicInterpreter
,但是我仍然遇到相同的异常:
class BasicInterpreterLocal extends BasicInterpreter{} // 抛出IllegalStateException
我尝试过使用asm 7.、8.、9.0,但都没有起作用。
那么问题出在哪里呢?我找不到问题所在。
英文:
I saw this question here on SO and I'm trying to compile the code from the accepted answer.
Unfortunately, I'm keep getting an IllegalStateException at this part of the Code:
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;
}
};
With the stack trace:
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)
The exception is raised here in the BasicInterpreter class:
public BasicInterpreter() {
super(/* latest api = */ ASM8);
if (getClass() != BasicInterpreter.class) {
throw new IllegalStateException(); // at this line
}
}
I tried to inherit the BasicInterpreter but I'm keep getting the same exception.
class BasicInterpreterLocal extends BasicInterpreter{} // Throws an IllegalStateException
I tried it with asm 7.*, 8.**, 9.0, but nothing works.
So what is the problem?. I couldn't found it.
答案1
得分: 2
根据这个答案中的解释,子类应使用接受库版本号的构造函数,以确定ASM库的兼容性级别。似乎,代码最初使用的ASM库的版本5并没有检查这个要求。但是您正在使用一个更新的版本(显然是版本8)的库,它强制执行这个规则。
将代码更改为
BasicInterpreter basic = new BasicInterpreter(Opcodes.ASM5) { // <- 关键点在这里
@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())
// 这是考虑实际类型层次结构的地方,如果需要的话
return BasicValue.REFERENCE_VALUE;
return BasicValue.UNINITIALIZED_VALUE;
}
};
以修复此问题。我还将更新另一个答案。
英文:
As explained in this answer, subclasses should use the constructor accepting a library version number, to determine the compatibility level of the ASM library. It seems, version 5 of the ASM library, that the code was using initially, did not check this requirement. But you are using a newer version (8, apparently) of the library which enforces this rule.
Change the code to
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;
}
};
to fix this. I will also update the other answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论