Search in sources :

Example 46 with Label

use of org.datanucleus.enhancer.asm.Label in project datanucleus-core by datanucleus.

the class CopyField method execute.

/**
 * Method to add the contents of the class method.
 */
public void execute() {
    AbstractMemberMetaData[] fields = enhancer.getClassMetaData().getManagedMembers();
    String pcSuperclassName = enhancer.getClassMetaData().getPersistableSuperclass();
    visitor.visitCode();
    Label startLabel = new Label();
    visitor.visitLabel(startLabel);
    if (pcSuperclassName != null) {
        Class supercls = enhancer.getClassLoaderResolver().classForName(pcSuperclassName);
        String superclsDescriptor = Type.getDescriptor(supercls);
        if (fields.length > 0) {
            visitor.visitVarInsn(Opcodes.ILOAD, 2);
            visitor.visitFieldInsn(Opcodes.GETSTATIC, getClassEnhancer().getASMClassName(), getNamer().getInheritedFieldCountFieldName(), "I");
            visitor.visitInsn(Opcodes.ISUB);
            Label[] fieldOptions = new Label[fields.length];
            for (int i = 0; i < fields.length; i++) {
                fieldOptions[i] = new Label();
            }
            Label defaultLabel = new Label();
            Label endSwitchLabel = new Label();
            // switch:
            visitor.visitTableSwitchInsn(0, fields.length - 1, defaultLabel, fieldOptions);
            for (int i = 0; i < fields.length; i++) {
                visitor.visitLabel(fieldOptions[i]);
                visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                visitor.visitVarInsn(Opcodes.ALOAD, 0);
                visitor.visitVarInsn(Opcodes.ALOAD, 1);
                if (fields[i] instanceof PropertyMetaData) {
                    // Persistent property so use dnSetXXX(obj.dnGetXXX())
                    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(), getNamer().getGetMethodPrefixMethodName() + fields[i].getName(), "()" + Type.getDescriptor(fields[i].getType()));
                    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(), getNamer().getSetMethodPrefixMethodName() + fields[i].getName(), "(" + Type.getDescriptor(fields[i].getType()) + ")V");
                } else {
                    // Persistent field so use xxx = obj.xxx
                    visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), fields[i].getName(), Type.getDescriptor(fields[i].getType()));
                    visitor.visitFieldInsn(Opcodes.PUTFIELD, getClassEnhancer().getASMClassName(), fields[i].getName(), Type.getDescriptor(fields[i].getType()));
                }
                visitor.visitJumpInsn(Opcodes.GOTO, endSwitchLabel);
            }
            // default :
            visitor.visitLabel(defaultLabel);
            visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
            // super.dnCopyField(obj, index);
            visitor.visitVarInsn(Opcodes.ALOAD, 0);
            visitor.visitVarInsn(Opcodes.ALOAD, 1);
            visitor.visitVarInsn(Opcodes.ILOAD, 2);
            visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, pcSuperclassName.replace('.', '/'), getNamer().getCopyFieldMethodName(), "(" + superclsDescriptor + "I)V");
            // End of switch
            visitor.visitLabel(endSwitchLabel);
            visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
            visitor.visitInsn(Opcodes.RETURN);
        } else {
            // super.dnCopyField(obj, index);
            visitor.visitVarInsn(Opcodes.ALOAD, 0);
            visitor.visitVarInsn(Opcodes.ALOAD, 1);
            visitor.visitVarInsn(Opcodes.ILOAD, 2);
            visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, pcSuperclassName.replace('.', '/'), getNamer().getCopyFieldMethodName(), "(" + superclsDescriptor + "I)V");
            visitor.visitInsn(Opcodes.RETURN);
        }
    } else {
        if (fields.length > 0) {
            visitor.visitVarInsn(Opcodes.ILOAD, 2);
            Label[] fieldOptions = new Label[fields.length];
            for (int i = 0; i < fields.length; i++) {
                fieldOptions[i] = new Label();
            }
            Label defaultLabel = new Label();
            Label endSwitchLabel = new Label();
            // switch:
            visitor.visitTableSwitchInsn(0, fields.length - 1, defaultLabel, fieldOptions);
            for (int i = 0; i < fields.length; i++) {
                visitor.visitLabel(fieldOptions[i]);
                visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                visitor.visitVarInsn(Opcodes.ALOAD, 0);
                visitor.visitVarInsn(Opcodes.ALOAD, 1);
                if (fields[i] instanceof PropertyMetaData) {
                    // Persistent property so use dnSetXXX(obj.dnGetXXX())
                    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(), getNamer().getGetMethodPrefixMethodName() + fields[i].getName(), "()" + Type.getDescriptor(fields[i].getType()));
                    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(), getNamer().getSetMethodPrefixMethodName() + fields[i].getName(), "(" + Type.getDescriptor(fields[i].getType()) + ")V");
                } else {
                    // Persistent field so use xxx = obj.xxx
                    visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), fields[i].getName(), Type.getDescriptor(fields[i].getType()));
                    visitor.visitFieldInsn(Opcodes.PUTFIELD, getClassEnhancer().getASMClassName(), fields[i].getName(), Type.getDescriptor(fields[i].getType()));
                }
                visitor.visitJumpInsn(Opcodes.GOTO, endSwitchLabel);
            }
            // default:
            visitor.visitLabel(defaultLabel);
            visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
            // throw new IllegalArgumentException("out of field index :" + index);
            visitor.visitTypeInsn(Opcodes.NEW, "java/lang/IllegalArgumentException");
            visitor.visitInsn(Opcodes.DUP);
            visitor.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuffer");
            visitor.visitInsn(Opcodes.DUP);
            visitor.visitLdcInsn("out of field index :");
            visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V");
            visitor.visitVarInsn(Opcodes.ILOAD, 2);
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(I)Ljava/lang/StringBuffer;");
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;");
            visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V");
            visitor.visitInsn(Opcodes.ATHROW);
            // End of switch
            visitor.visitLabel(endSwitchLabel);
            visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
            visitor.visitInsn(Opcodes.RETURN);
        } else {
            // throw new IllegalArgumentException("out of field index :" + index);
            visitor.visitTypeInsn(Opcodes.NEW, "java/lang/IllegalArgumentException");
            visitor.visitInsn(Opcodes.DUP);
            visitor.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuffer");
            visitor.visitInsn(Opcodes.DUP);
            visitor.visitLdcInsn("out of field index :");
            visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V");
            visitor.visitVarInsn(Opcodes.ILOAD, 2);
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(I)Ljava/lang/StringBuffer;");
            visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;");
            visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V");
            visitor.visitInsn(Opcodes.ATHROW);
        }
    }
    // Set parameter names
    Label endLabel = new Label();
    visitor.visitLabel(endLabel);
    visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
    visitor.visitLocalVariable(argNames[0], getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 1);
    visitor.visitLocalVariable(argNames[1], "I", null, startLabel, endLabel, 2);
    if (pcSuperclassName != null) {
        visitor.visitMaxs(3, 3);
    } else {
        visitor.visitMaxs(5, 3);
    }
    visitor.visitEnd();
}
Also used : Label(org.datanucleus.enhancer.asm.Label) PropertyMetaData(org.datanucleus.metadata.PropertyMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData)

Example 47 with Label

use of org.datanucleus.enhancer.asm.Label in project datanucleus-core by datanucleus.

the class IsDetached method execute.

/**
 * Method to add the contents of the class method.
 */
public void execute() {
    visitor.visitCode();
    Label startLabel = new Label();
    visitor.visitLabel(startLabel);
    if (getClassEnhancer().getClassMetaData().isDetachable()) {
        visitor.visitVarInsn(Opcodes.ALOAD, 0);
        visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), getNamer().getStateManagerFieldName(), "L" + getNamer().getStateManagerAsmClassName() + ";");
        Label l1 = new Label();
        visitor.visitJumpInsn(Opcodes.IFNONNULL, l1);
        visitor.visitVarInsn(Opcodes.ALOAD, 0);
        visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), getNamer().getDetachedStateFieldName(), "[Ljava/lang/Object;");
        visitor.visitJumpInsn(Opcodes.IFNULL, l1);
        visitor.visitInsn(Opcodes.ICONST_1);
        visitor.visitInsn(Opcodes.IRETURN);
        visitor.visitLabel(l1);
        visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    }
    visitor.visitInsn(Opcodes.ICONST_0);
    visitor.visitInsn(Opcodes.IRETURN);
    Label endLabel = new Label();
    visitor.visitLabel(endLabel);
    visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
    visitor.visitMaxs(1, 1);
    visitor.visitEnd();
}
Also used : Label(org.datanucleus.enhancer.asm.Label)

Example 48 with Label

use of org.datanucleus.enhancer.asm.Label in project datanucleus-core by datanucleus.

the class LoadClass method execute.

/**
 * Method to add the contents of the class method.
 */
public void execute() {
    visitor.visitCode();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    visitor.visitTryCatchBlock(l0, l1, l2, "java/lang/ClassNotFoundException");
    visitor.visitLabel(l0);
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;");
    visitor.visitLabel(l1);
    visitor.visitInsn(Opcodes.ARETURN);
    visitor.visitLabel(l2);
    visitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/ClassNotFoundException" });
    visitor.visitVarInsn(Opcodes.ASTORE, 1);
    Label l3 = new Label();
    visitor.visitLabel(l3);
    visitor.visitTypeInsn(Opcodes.NEW, "java/lang/NoClassDefFoundError");
    visitor.visitInsn(Opcodes.DUP);
    visitor.visitVarInsn(Opcodes.ALOAD, 1);
    visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/ClassNotFoundException", "getMessage", "()Ljava/lang/String;");
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/NoClassDefFoundError", "<init>", "(Ljava/lang/String;)V");
    visitor.visitInsn(Opcodes.ATHROW);
    Label l4 = new Label();
    visitor.visitLabel(l4);
    visitor.visitLocalVariable(argNames[0], "Ljava/lang/String;", null, l0, l4, 0);
    visitor.visitLocalVariable("e", "Ljava/lang/ClassNotFoundException;", null, l3, l4, 1);
    visitor.visitMaxs(3, 2);
    visitor.visitEnd();
}
Also used : Label(org.datanucleus.enhancer.asm.Label)

Aggregations

Label (org.datanucleus.enhancer.asm.Label)48 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)13 MethodVisitor (org.datanucleus.enhancer.asm.MethodVisitor)9 PropertyMetaData (org.datanucleus.metadata.PropertyMetaData)8 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)7 ClassMetaData (org.datanucleus.metadata.ClassMetaData)6 Date (java.util.Date)2 TimeZone (java.util.TimeZone)2 InterfaceMetaData (org.datanucleus.metadata.InterfaceMetaData)2 ClassWriter (org.datanucleus.enhancer.asm.ClassWriter)1 FieldVisitor (org.datanucleus.enhancer.asm.FieldVisitor)1