ASM BasicInterpreter IllegalStateException
Tôi đã thấy câu hỏi này ở đây trên SO và tôi đang cố gắng biên dịch mã từ câu trả lời được chấp nhận. Thật không may, tôi tiếp tục nhận được IllegalStateException ở phần này của Mã:
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;
}
};
Với dấu vết ngăn xếp:
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)
Ngoại lệ được nêu ra ở đây trong lớp BasicInterpreter :
public BasicInterpreter() {
super(/* latest api = */ ASM8);
if (getClass() != BasicInterpreter.class) {
throw new IllegalStateException(); // at this line
}
}
Tôi đã cố gắng kế thừa BasicInterpreter nhưng tôi vẫn nhận được cùng một ngoại lệ.
class BasicInterpreterLocal extends BasicInterpreter{} // Throws an IllegalStateException
Tôi đã thử nó với asm 7. *, 8. **, 9.0, nhưng không có gì hoạt động.
Vì vậy, vấn đề là gì?. Tôi không thể tìm thấy nó.
Trả lời
Như đã giải thích trong câu trả lời này , các lớp con nên sử dụng hàm tạo chấp nhận số phiên bản thư viện, để xác định mức độ tương thích của thư viện ASM. Có vẻ như, phiên bản 5 của thư viện ASM, mà mã được sử dụng ban đầu, đã không kiểm tra yêu cầu này. Nhưng bạn đang sử dụng phiên bản mới hơn (có vẻ như là 8) của thư viện thực thi quy tắc này.
Thay đổi mã thành
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;
}
};
để sửa lỗi này. Tôi cũng sẽ cập nhật câu trả lời khác.