* "address") { this.address = (String)value; } else if (attribute ==
* "city") { this.city = (String)city; } }
*/
public void addPersistenceGetSet(ClassDetails classDetails) {
// create the _persistence_get() method
MethodVisitor cv_get = cv.visitMethod(ACC_PUBLIC, "_persistence_get", "(Ljava/lang/String;)Ljava/lang/Object;", null, null);
Label label = null;
for (AttributeDetails attributeDetails : classDetails.getAttributesMap().values()) {
if (!attributeDetails.isAttributeOnSuperClass() && !attributeDetails.isVirtualProperty()) {
if (label != null) {
cv_get.visitLabel(label);
}
// else if (attribute == "address")
cv_get.visitVarInsn(ALOAD, 1);
cv_get.visitLdcInsn(attributeDetails.getAttributeName().intern());
label = new Label();
cv_get.visitJumpInsn(IF_ACMPNE, label);
// return this.address
cv_get.visitVarInsn(ALOAD, 0);
cv_get.visitFieldInsn(GETFIELD, classDetails.getClassName(), attributeDetails.getAttributeName(), attributeDetails.getReferenceClassType().getDescriptor());
// if this is a primitive, get the wrapper class
String wrapper = ClassWeaver.wrapperFor(attributeDetails.getReferenceClassType().getSort());
if (wrapper != null) {
// Call valueOf on the wrapper (more optimal than
// constructor).
cv_get.visitMethodInsn(INVOKESTATIC, wrapper, "valueOf", "(" + attributeDetails.getReferenceClassType().getDescriptor() + ")L" + wrapper + ";");
}
cv_get.visitInsn(ARETURN);
}
}
if (label != null) {
cv_get.visitLabel(label);
}
// call super, or return null
if (classDetails.getSuperClassDetails() == null) {
// return null;
cv_get.visitInsn(ACONST_NULL);
} else {
cv_get.visitVarInsn(ALOAD, 0);
cv_get.visitVarInsn(ALOAD, 1);
cv_get.visitMethodInsn(INVOKESPECIAL, classDetails.getSuperClassName(), "_persistence_get", "(Ljava/lang/String;)Ljava/lang/Object;");
}
cv_get.visitInsn(ARETURN);
cv_get.visitMaxs(0, 0);
// create the _persistence_set() method
MethodVisitor cv_set = cv.visitMethod(ACC_PUBLIC, "_persistence_set", "(Ljava/lang/String;Ljava/lang/Object;)V", null, null);
label = null;
for (AttributeDetails attribute : classDetails.getAttributesMap().values()) {
if (!attribute.isAttributeOnSuperClass() && !attribute.isVirtualProperty()) {
if (label != null) {
cv_set.visitLabel(label);
}
// else if (attribute == "address")
cv_set.visitVarInsn(ALOAD, 1);
cv_set.visitLdcInsn(attribute.getAttributeName().intern());
label = new Label();
cv_set.visitJumpInsn(IF_ACMPNE, label);
// this.address = (String)value;
cv_set.visitVarInsn(ALOAD, 0);
cv_set.visitVarInsn(ALOAD, 2);
String wrapper = wrapperFor(attribute.getReferenceClassType().getSort());
if (wrapper == null) {
wrapper = attribute.getReferenceClassName().replace('.', '/');
}
cv_set.visitTypeInsn(CHECKCAST, wrapper);
// Unwrap any primitive wrapper to its value.
unwrapPrimitive(attribute, cv_set);
cv_set.visitFieldInsn(PUTFIELD, classDetails.getClassName(), attribute.getAttributeName(), attribute.getReferenceClassType().getDescriptor());
// return;
cv_set.visitInsn(RETURN);
}
}
if (label != null) {
cv_set.visitLabel(label);
}
// call super, or return null
if (classDetails.getSuperClassDetails() != null) {
cv_set.visitVarInsn(ALOAD, 0);
cv_set.visitVarInsn(ALOAD, 1);
cv_set.visitVarInsn(ALOAD, 2);
cv_set.visitMethodInsn(INVOKESPECIAL, classDetails.getSuperClassName(), "_persistence_set", "(Ljava/lang/String;Ljava/lang/Object;)V");
}
cv_set.visitInsn(RETURN);
cv_set.visitMaxs(0, 0);
}