ASM BasicInterpreter IllegalStateException

Oct 21 2020

Widziałem to pytanie tutaj na SO i próbuję skompilować kod z zaakceptowanej odpowiedzi. Niestety w tej części Kodeksu ciągle otrzymuję wyjątek 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;
        }
    };

Ze śladem stosu:

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)

Wyjątek jest tutaj zgłaszany w klasie BasicInterpreter :

public BasicInterpreter() {
    super(/* latest api = */ ASM8);
    if (getClass() != BasicInterpreter.class) {
      throw new IllegalStateException(); // at this line
    }
  }

Próbowałem odziedziczyć BasicInterpreter, ale wciąż otrzymuję ten sam wyjątek.

class BasicInterpreterLocal extends BasicInterpreter{} // Throws an IllegalStateException

Wypróbowałem to z asm 7. *, 8. **, 9.0, ale nic nie działa.

Więc jaki jest problem?. Nie mogłem tego znaleźć.

Odpowiedzi

2 Holger Oct 21 2020 at 22:37

Jak wyjaśniono w tej odpowiedzi , podklasy powinny używać konstruktora akceptującego numer wersji biblioteki, aby określić poziom zgodności biblioteki ASM. Wygląda na to, że wersja 5 biblioteki ASM, której kod używał początkowo, nie sprawdzała tego wymagania. Ale używasz nowszej wersji (podobno 8) biblioteki, która wymusza tę regułę.

Zmień kod na

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

aby to naprawić. Zaktualizuję też drugą odpowiedź.