, - 1.6 (50), - 1.7 (51), . ( ASM5) :
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import static org.objectweb.asm.Opcodes.*;
public class LookupTest {
public static void main(String[] args) throws InstantiationException,
IllegalAccessException, ClassNotFoundException {
new ClassLoader() {
@Override
protected Class<?> findClass(String name)
throws ClassNotFoundException {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
| ClassWriter.COMPUTE_FRAMES);
cw.visit(V1_6, ACC_PUBLIC | ACC_SUPER, name, null,
"java/lang/Object", null);
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V",
null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>",
"()V", false);
Label target = new Label();
mv.visitLabel(target);
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
"Ljava/io/PrintStream;");
mv.visitLdcInsn("Hello");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream",
"println", "(Ljava/lang/String;)V", false);
mv.visitInsn(ICONST_0);
mv.visitLookupSwitchInsn(target, new int[0], new Label[0]);
mv.visitMaxs(-1, -1);
mv.visitEnd();
cw.visitEnd();
byte[] bytes = cw.toByteArray();
return defineClass(name, bytes, 0, bytes.length);
}
}.loadClass("LookupGotoTest").newInstance();
}
}
, V1_6
V1_7
, :
Exception in thread "main" java.lang.VerifyError: Bad instruction
Exception Details:
Location:
LookupGotoTest.<init>()V @13: lookupswitch
Reason:
Error exists in the bytecode
Bytecode:
0x0000000: 2ab7 0008 b200 0e12 10b6 0016 03ab 0000
0x0000010: ffff fff7 0000 0000
Stackmap Table:
full_frame(@4,{Object[#2]},{})
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2658)
at java.lang.Class.getConstructor0(Class.java:3062)
at java.lang.Class.newInstance(Class.java:403)
at LookupTest.main(LookupTest.java:46)
, goto, 1,7 -:
Label target2 = new Label();
mv.visitInsn(ICONST_0);
mv.visitLookupSwitchInsn(target2, new int[0], new Label[0]);
mv.visitLabel(target2);
mv.visitJumpInsn(GOTO, target);
- : Java Java 1.6 StackMapTable Type Inference, 1.7 Type Checking, , lookupswitch.
, 1.7+, ASM StackMapTable.
@Holger @apangin, , , ASM, , case mv.visitLookupSwitchInsn(target, new int[]{1}, new Label[]{target});
. , : , , -.