this.name = name;
this.fields = fields;
}
public void generate(ClassVisitor cv) {
MethodVisitor mv;
cv.visit(V1_1, ACC_PUBLIC, name, null, "java/lang/Object", null);
// generate default constructor
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();
}
Iterator<Map.Entry<String, Type>> i = fields.entrySet().iterator();
while (i.hasNext()) {
Map.Entry<String, Type> e = i.next();
String fName = e.getKey();
String fDesc = e.getValue().getDescriptor();
Type fType = e.getValue();
String mName;
String mDesc;
// generate field
cv.visitField(ACC_PRIVATE, fName, fDesc, null, null).visitEnd();
// generate getter
mName = "get" + up(fName);
mDesc = "()" + fDesc;
mv = cv.visitMethod(ACC_PUBLIC, mName, mDesc, null, null);
if (mv != null) {
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, name, fName, fDesc);
mv.visitInsn(fType.getOpcode(IRETURN));
mv.visitMaxs(fType.getSize(), 1);
mv.visitEnd();
}
// generate setter
mName = "set" + up(fName);
mDesc = "(" + fDesc + ")V";
mv = cv.visitMethod(ACC_PUBLIC, mName, mDesc, null, null);
if (mv != null) {
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(fType.getOpcode(ILOAD), 1);
mv.visitFieldInsn(PUTFIELD, name, fName, fDesc);
mv.visitInsn(RETURN);
mv.visitMaxs(1 + fType.getSize(), 1 + fType.getSize());
mv.visitEnd();
}
}
cv.visitEnd();
}