generate(dir, "pkg/Wide.class", dump());
}
public byte[] dump() {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
MethodVisitor mv;
cw.visit(V1_2, ACC_PUBLIC, "pkg/Wide", null, "java/lang/Object", null);
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
for (int i = 0; i < 256; ++i) {
mv.visitLdcInsn(Integer.toString(i)); // wide form
mv.visitInsn(POP);
}
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
mv = cw.visitMethod(ACC_PUBLIC, "wideLocals", "(I)I", null, null);
mv.visitCode();
Label l0 = new Label();
Label l1 = new Label();
mv.visitJumpInsn(GOTO, l1); // will give GOTO_W
mv.visitLabel(l0);
mv.visitIincInsn(300, 1); // covers 'update maxlocals' in MethodWriter
mv.visitVarInsn(ILOAD, 300); // will give wide form
mv.visitJumpInsn(IFEQ, l1); // will give long forward jump
// many NOPs will be introduced here by the method resizing test
mv.visitVarInsn(ILOAD, 300); // will give wide form
mv.visitInsn(IRETURN);
mv.visitLabel(l1);
for (int i = 1; i < 300; ++i) {
mv.visitVarInsn(ILOAD, i);
if (i <= 5) {
mv.visitInsn(ICONST_0 + i);
} else if (i <= Byte.MAX_VALUE) {
mv.visitIntInsn(BIPUSH, i);
} else {
mv.visitIntInsn(SIPUSH, i);
}
mv.visitInsn(IADD);
mv.visitVarInsn(ISTORE, i + 1);
}
mv.visitInsn(ICONST_0);
mv.visitJumpInsn(IFEQ, l0); // will give long backward jump
mv.visitJumpInsn(GOTO, l0); // will give long backward goto
mv.visitMaxs(0, 0);
mv.visitEnd();
cw.visitEnd();
return cw.toByteArray();
}