Search in sources :

Example 41 with Label

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

the class PrimaryKeyGenerator method addMethodToString.

/**
 * Method to add a toString() method.
 * @param cw The ClassWriter to use
 */
protected void addMethodToString(ClassWriter cw) {
    if (DataNucleusEnhancer.LOGGER.isDebugEnabled()) {
        DataNucleusEnhancer.LOGGER.debug(Localiser.msg("005019", "String " + pkClassName + ".toString()"));
    }
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null);
    mv.visitCode();
    Label startLabel = new Label();
    mv.visitLabel(startLabel);
    // "StringBuilder str = new StringBuilder();"
    mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder");
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V");
    mv.visitVarInsn(Opcodes.ASTORE, 1);
    Label l1 = new Label();
    mv.visitLabel(l1);
    // "str.append(field1).append(":").append(field2) ..." etc
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    int[] pkPositions = cmd.getPKMemberPositions();
    for (int i = 0; i < pkPositions.length; i++) {
        AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtRelativePosition(pkPositions[i]);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, className_ASM, mmd.getName(), EnhanceUtils.getTypeDescriptorForType(mmd.getTypeName()));
        // Use most representive form of "StringBuilder.append()"
        if (mmd.getType() == int.class || mmd.getType() == long.class || mmd.getType() == float.class || mmd.getType() == double.class) {
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(" + EnhanceUtils.getTypeDescriptorForType(mmd.getTypeName()) + ")Ljava/lang/StringBuilder;");
        } else if (mmd.getType() == char.class) {
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(" + EnhanceUtils.getTypeDescriptorForType(mmd.getTypeName()) + ")Ljava/lang/StringBuilder;");
        } else if (mmd.getType() == String.class) {
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
        } else if (Date.class.isAssignableFrom(mmd.getType())) {
            // Use the long value of the date (millisecs)
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, mmd.getTypeName().replace('.', '/'), "getTime", "()J");
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(J)Ljava/lang/StringBuilder;");
        } else if (Calendar.class.isAssignableFrom(mmd.getType())) {
            // Use the long value of the Calendar (millisecs)
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/Calendar", "getTime", "()Ljava/util/Date;");
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/Date", "getTime", "()J");
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(J)Ljava/lang/StringBuilder;");
        } else if (mmd.getType() == TimeZone.class) {
            // Use the ID of the TimeZone
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/TimeZone", "getID", "()Ljava/lang/String;");
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
        } else {
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, mmd.getTypeName().replace('.', '/'), "toString", "()Ljava/lang/String;");
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
        }
        if (i < (pkPositions.length - 1)) {
            // Add separator ({stringSeparator})
            mv.visitLdcInsn(stringSeparator);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
        }
    }
    mv.visitInsn(Opcodes.POP);
    // "return str.toString();"
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;");
    mv.visitInsn(Opcodes.ARETURN);
    Label endLabel = new Label();
    mv.visitLabel(endLabel);
    mv.visitLocalVariable("this", className_DescName, null, startLabel, endLabel, 0);
    mv.visitLocalVariable("str", "Ljava/lang/StringBuilder;", null, l1, endLabel, 1);
    mv.visitMaxs(pkPositions.length, 2);
    mv.visitEnd();
}
Also used : TimeZone(java.util.TimeZone) Label(org.datanucleus.enhancer.asm.Label) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) Date(java.util.Date) MethodVisitor(org.datanucleus.enhancer.asm.MethodVisitor)

Example 42 with Label

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

the class DefaultConstructor method execute.

/**
 * Method to add the contents of the class method.
 */
public void execute() {
    visitor.visitCode();
    Label l0 = new Label();
    visitor.visitLabel(l0);
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    Class superclass = enhancer.getClassBeingEnhanced().getSuperclass();
    visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclass.getName().replace('.', '/'), "<init>", "()V");
    visitor.visitInsn(Opcodes.RETURN);
    Label l1 = new Label();
    visitor.visitLabel(l1);
    visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, l0, l1, 0);
    visitor.visitMaxs(1, 1);
    visitor.visitEnd();
}
Also used : Label(org.datanucleus.enhancer.asm.Label)

Example 43 with Label

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

the class GetNormal method execute.

/**
 * Method to add the contents of the class method.
 */
public void execute() {
    visitor.visitCode();
    String fieldTypeDesc = Type.getDescriptor(fmd.getType());
    Label startLabel = new Label();
    visitor.visitLabel(startLabel);
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), fmd.getName(), fieldTypeDesc);
    EnhanceUtils.addReturnForType(visitor, (Class) returnType);
    Label endLabel = new Label();
    visitor.visitLabel(endLabel);
    visitor.visitLocalVariable(argNames[0], getClassEnhancer().getClassDescriptor(), null, startLabel, endLabel, 0);
    visitor.visitMaxs(1, 1);
    visitor.visitEnd();
}
Also used : Label(org.datanucleus.enhancer.asm.Label)

Example 44 with Label

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

the class GetObjectId method execute.

/**
 * Method to add the contents of the class method.
 */
public void execute() {
    visitor.visitCode();
    Label l0 = new Label();
    visitor.visitLabel(l0);
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), getNamer().getStateManagerFieldName(), getNamer().getStateManagerDescriptor());
    Label l1 = new Label();
    visitor.visitJumpInsn(Opcodes.IFNULL, l1);
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), getNamer().getStateManagerFieldName(), getNamer().getStateManagerDescriptor());
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    visitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, getNamer().getStateManagerAsmClassName(), "getObjectId", "(" + getNamer().getPersistableDescriptor() + ")" + EnhanceUtils.CD_Object);
    visitor.visitInsn(Opcodes.ARETURN);
    visitor.visitLabel(l1);
    visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    if (!enhancer.getClassMetaData().isDetachable()) {
        visitor.visitInsn(Opcodes.ACONST_NULL);
        visitor.visitInsn(Opcodes.ARETURN);
        Label l3 = new Label();
        visitor.visitLabel(l3);
        visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, l0, l3, 0);
        visitor.visitMaxs(2, 1);
    } else {
        visitor.visitVarInsn(Opcodes.ALOAD, 0);
        visitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getClassEnhancer().getASMClassName(), getNamer().getIsDetachedMethodName(), "()Z");
        Label l3 = new Label();
        visitor.visitJumpInsn(Opcodes.IFNE, l3);
        visitor.visitInsn(Opcodes.ACONST_NULL);
        visitor.visitInsn(Opcodes.ARETURN);
        visitor.visitLabel(l3);
        visitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        visitor.visitVarInsn(Opcodes.ALOAD, 0);
        visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), getNamer().getDetachedStateFieldName(), "[" + EnhanceUtils.CD_Object);
        visitor.visitInsn(Opcodes.ICONST_0);
        visitor.visitInsn(Opcodes.AALOAD);
        visitor.visitInsn(Opcodes.ARETURN);
        Label l4 = new Label();
        visitor.visitLabel(l4);
        visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, l0, l4, 0);
        visitor.visitMaxs(2, 1);
    }
    visitor.visitEnd();
}
Also used : Label(org.datanucleus.enhancer.asm.Label)

Example 45 with Label

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

the class GetStateManager method execute.

/**
 * Method to add the contents of the class method.
 */
public void execute() {
    visitor.visitCode();
    Label l0 = new Label();
    visitor.visitLabel(l0);
    visitor.visitVarInsn(Opcodes.ALOAD, 0);
    visitor.visitFieldInsn(Opcodes.GETFIELD, getClassEnhancer().getASMClassName(), getNamer().getStateManagerFieldName(), getNamer().getStateManagerDescriptor());
    visitor.visitInsn(Opcodes.ARETURN);
    Label l1 = new Label();
    visitor.visitLabel(l1);
    visitor.visitLocalVariable("this", getClassEnhancer().getClassDescriptor(), null, l0, l1, 0);
    visitor.visitMaxs(1, 1);
    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