Regression in 6.1-beta: GeneratorAdapter fails to generate methods that return void and arrays
With ASM 6.0 following code
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.GeneratorAdapter;
import org.objectweb.asm.commons.Method;
import org.objectweb.asm.util.Textifier;
import org.objectweb.asm.util.TraceClassVisitor;
import java.io.PrintWriter;
public class Example {
public static void main(String[] args) {
ClassWriter cw = new ClassWriter(Opcodes.ASM6);
cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Example", null, "java/lang/Object", null);
{
Method m = Method.getMethod("void <init> ()");
GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, m, null, null, cw);
mg.loadThis();
mg.invokeConstructor(Type.getType(Object.class), m);
mg.returnValue();
mg.endMethod();
}
{
Method m = Method.getMethod("[I fun ()");
GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, m, null, null, cw);
mg.push(0);
mg.newArray(Type.INT_TYPE);
mg.returnValue();
mg.endMethod();
}
cw.visitEnd();
TraceClassVisitor traceClassVisitor = new TraceClassVisitor(null, new Textifier(), new PrintWriter(System.out));
new ClassReader(cw.toByteArray()).accept(traceClassVisitor, 0);
}
}
produces
// class version 49.0 (49)
// access flags 0x1
public class Example {
// access flags 0x1
public <init>()V
ALOAD 0
INVOKESPECIAL java/lang/Object.<init> ()V
RETURN
MAXSTACK = 0
MAXLOCALS = 0
// access flags 0x1
public fun()Ljava/lang/[I;
ICONST_0
NEWARRAY T_INT
ARETURN
MAXSTACK = 0
MAXLOCALS = 0
}
While with ASM 6.1-beta generation of first method fails with
java.lang.AssertionError
at org.objectweb.asm.Type.getOpcode(Type.java:829)
at org.objectweb.asm.commons.GeneratorAdapter.returnValue(GeneratorAdapter.java:1097)
and second fails with
java.lang.UnsupportedOperationException
at org.objectweb.asm.Type.getOpcode(Type.java:827)
at org.objectweb.asm.commons.GeneratorAdapter.returnValue(GeneratorAdapter.java:1097)
Seems that this regression is consequence of changes in org.objectweb.asm.Type.getOpcode
in 4f0da9db and 6db046e2, reduced test for its change of behavior:
assertEquals(Opcodes.RETURN, Type.VOID_TYPE.getOpcode(Opcodes.IRETURN));
assertEquals(Opcodes.ARETURN, Type.getType(int[].class).getOpcode(Opcodes.IRETURN));
Edited by Evgeny Mandrikov