}
private static void createMethod(final ClassWriter cw, final String className, final String referenceName, final String fieldName, final String typeName, final boolean bind) {
final org.objectweb.asm.Type type = org.objectweb.asm.Type.getType("L" + typeName.replace('.', '/') + ";");
final String methodName = (bind ? "" : "un") + "bind" + referenceName.substring(0, 1).toUpperCase() + referenceName.substring(1);
final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PROTECTED, methodName, "(" + type.toString() + ")V", null, null);
mv.visitVarInsn(Opcodes.ALOAD, 0);
if ( bind ) {
mv.visitVarInsn(type.getOpcode(Opcodes.ILOAD), 1);
mv.visitFieldInsn(Opcodes.PUTFIELD, className.replace('.', '/'), fieldName, type.toString());
} else {
mv.visitFieldInsn(Opcodes.GETFIELD, className.replace('.', '/'), fieldName, type.toString());
mv.visitVarInsn(Opcodes.ALOAD, 1);
final Label jmpLabel = new Label();
mv.visitJumpInsn(Opcodes.IF_ACMPNE, jmpLabel);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitInsn(Opcodes.ACONST_NULL);
mv.visitFieldInsn(Opcodes.PUTFIELD, className.replace('.', '/'), fieldName, type.toString());
mv.visitLabel(jmpLabel);
}
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(2, 2);
}