Search in sources :

Example 1 with FieldVisitor

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

the class PrimaryKeyGenerator method addFields.

/**
 * Method to add fields to match the PK fields of the persistable class
 * @param cw The ClassWriter to use
 */
protected void addFields(ClassWriter cw) {
    int[] pkPositions = cmd.getPKMemberPositions();
    for (int i = 0; i < pkPositions.length; i++) {
        AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtRelativePosition(pkPositions[i]);
        String fieldTypeName = getTypeNameForField(mmd);
        if (DataNucleusEnhancer.LOGGER.isDebugEnabled()) {
            DataNucleusEnhancer.LOGGER.debug(Localiser.msg("005021", fieldTypeName + " " + pkClassName + " " + mmd.getName()));
        }
        FieldVisitor fv = cw.visitField(Opcodes.ACC_PUBLIC, mmd.getName(), EnhanceUtils.getTypeDescriptorForType(fieldTypeName), null, null);
        fv.visitEnd();
    }
}
Also used : AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) FieldVisitor(org.datanucleus.enhancer.asm.FieldVisitor)

Example 2 with FieldVisitor

use of org.datanucleus.enhancer.asm.FieldVisitor in project tests by datanucleus.

the class DynamicEnhanceSchemaToolTest method createClass.

private static byte[] createClass(String className) throws Exception {
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    FieldVisitor fv;
    String classNameASM = className.replace('.', '/');
    // TODO Use getAsmVersionForJRE instead of V1_6 (requires proper stack map)
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, classNameASM, null, "java/lang/Object", new String[] {});
    fv = cw.visitField(Opcodes.ACC_PRIVATE, "name", "Ljava/lang/String;", null, null);
    fv.visitEnd();
    // Default Constructor
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(Opcodes.RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + classNameASM + ";", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    // String getName()
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getName", "()Ljava/lang/String;", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, classNameASM, "name", "Ljava/lang/String;");
        mv.visitInsn(Opcodes.ARETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + classNameASM + ";", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    // void setName(String)
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "setName", "(Ljava/lang/String;)V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitFieldInsn(Opcodes.PUTFIELD, classNameASM, "name", "Ljava/lang/String;");
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitInsn(Opcodes.RETURN);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLocalVariable("this", "L" + classNameASM + ";", null, l0, l2, 0);
        mv.visitLocalVariable("s", "Ljava/lang/String;", null, l0, l2, 1);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    // Object getProperty(String)
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getProperty", "(Ljava/lang/String;)Ljava/lang/Object;", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitVarInsn(Opcodes.ASTORE, 2);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitLdcInsn("name");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
        Label l2 = new Label();
        mv.visitJumpInsn(Opcodes.IFEQ, l2);
        Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.GETFIELD, classNameASM, "name", "Ljava/lang/String;");
        mv.visitVarInsn(Opcodes.ASTORE, 2);
        mv.visitLabel(l2);
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        mv.visitInsn(Opcodes.ARETURN);
        Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitLocalVariable("this", "L" + classNameASM + ";", null, l0, l4, 0);
        mv.visitLocalVariable("propertyName", "Ljava/lang/String;", null, l0, l4, 1);
        mv.visitLocalVariable("o", "Ljava/lang/Object;", null, l1, l4, 2);
        mv.visitMaxs(2, 3);
        mv.visitEnd();
    }
    // void setProperty(String, Object)
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "setProperty", "(Ljava/lang/String;Ljava/lang/Object;)V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitLdcInsn("name");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
        Label l1 = new Label();
        mv.visitJumpInsn(Opcodes.IFEQ, l1);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/String");
        mv.visitFieldInsn(Opcodes.PUTFIELD, classNameASM, "name", "Ljava/lang/String;");
        mv.visitLabel(l1);
        mv.visitInsn(Opcodes.RETURN);
        Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLocalVariable("this", "L" + classNameASM + ";", null, l0, l3, 0);
        mv.visitLocalVariable("propertyName", "Ljava/lang/String;", null, l0, l3, 1);
        mv.visitLocalVariable("value", "Ljava/lang/Object;", null, l0, l3, 2);
        mv.visitMaxs(2, 3);
        mv.visitEnd();
    }
    return cw.toByteArray();
}
Also used : Label(org.datanucleus.enhancer.asm.Label) FieldVisitor(org.datanucleus.enhancer.asm.FieldVisitor) ClassWriter(org.datanucleus.enhancer.asm.ClassWriter) MethodVisitor(org.datanucleus.enhancer.asm.MethodVisitor)

Aggregations

FieldVisitor (org.datanucleus.enhancer.asm.FieldVisitor)2 ClassWriter (org.datanucleus.enhancer.asm.ClassWriter)1 Label (org.datanucleus.enhancer.asm.Label)1 MethodVisitor (org.datanucleus.enhancer.asm.MethodVisitor)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1