public class BeanGenerator {
public byte[] generate(PrintWriter printWriter) {
ClassWriter cw = new ClassWriter(0);
TraceClassVisitor tcv = new TraceClassVisitor(cw, printWriter);
CheckClassAdapter cv = new CheckClassAdapter(tcv);
cv.visit(V1_6, ACC_PUBLIC, "pkg/Bean", null, "java/lang/Object",
null);
cv.visitSource("Bean.java", null);
FieldVisitor fv = cv.visitField(ACC_PRIVATE, "f", "I", null, null);
if (fv != null) {
fv.visitEnd();
}
MethodVisitor mv;
mv = cv.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
if (mv != null) {
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>",
"()V");
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
mv = cv.visitMethod(ACC_PUBLIC, "getF", "()I", null, null);
if (mv != null) {
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "pkg/Bean", "f", "I");
mv.visitInsn(IRETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
mv = cv.visitMethod(ACC_PUBLIC, "setF", "(I)V", null, null);
if (mv != null) {
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ILOAD, 1);
mv.visitFieldInsn(PUTFIELD, "pkg/Bean", "f", "I");
mv.visitInsn(RETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
}
mv = cv.visitMethod(ACC_PUBLIC, "checkAndSetF", "(I)V", null, null);
if (mv != null) {
mv.visitCode();
mv.visitVarInsn(ILOAD, 1);
Label label = new Label();
mv.visitJumpInsn(IFLT, label);
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ILOAD, 1);
mv.visitFieldInsn(PUTFIELD, "pkg/Bean", "f", "I");
Label end = new Label();
mv.visitJumpInsn(GOTO, end);
mv.visitLabel(label);
mv.visitFrame(F_SAME, 0, null, 0, null);
mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL,
"java/lang/IllegalArgumentException", "<init>", "()V");
mv.visitInsn(ATHROW);
mv.visitLabel(end);
mv.visitFrame(F_SAME, 0, null, 0, null);
mv.visitInsn(RETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
}
cv.visitEnd();
return cw.toByteArray();
}