ASM BasicInterpreter IllegalStateException
Ho visto questa domanda qui su SO e sto cercando di compilare il codice dalla risposta accettata. Sfortunatamente, continuo a ricevere un'eccezione IllegalStateException in questa parte del codice:
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;
}
};
Con lo 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)
L'eccezione viene sollevata qui nella classe BasicInterpreter :
public BasicInterpreter() {
super(/* latest api = */ ASM8);
if (getClass() != BasicInterpreter.class) {
throw new IllegalStateException(); // at this line
}
}
Ho provato a ereditare BasicInterpreter ma continuo a ricevere la stessa eccezione.
class BasicInterpreterLocal extends BasicInterpreter{} // Throws an IllegalStateException
L'ho provato con asm 7. *, 8. **, 9.0, ma non funziona niente.
Quindi qual'è il problema?. Non l'ho trovato.
Risposte
Come spiegato in questa risposta , le sottoclassi dovrebbero utilizzare il costruttore che accetta un numero di versione della libreria, per determinare il livello di compatibilità della libreria ASM. Sembra che la versione 5 della libreria ASM, che il codice utilizzava inizialmente, non abbia verificato questo requisito. Ma stai usando una versione più recente (8, apparentemente) della libreria che applica questa regola.
Cambia il codice in
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;
}
};
per risolvere questo problema. Aggiornerò anche l'altra risposta.