Search in sources :

Example 16 with Type

use of org.apache.xbean.asm5.Type in project tomee by apache.

the class Cmp2Generator method createOpenEJB_removeCmr.

private void createOpenEJB_removeCmr() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "OpenEJB_removeCmr", "(Ljava/lang/String;Ljava/lang/Object;)V", null, null);
    mv.visitCode();
    // if (deleted) return;
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, implClassName, DELETED, "Z");
    final Label notDeleted = new Label();
    mv.visitJumpInsn(IFEQ, notDeleted);
    mv.visitInsn(RETURN);
    mv.visitLabel(notDeleted);
    for (final CmrField cmrField : cmrFields) {
        // if ("${cmrField.name}".equals(name)) {
        // ${cmrField.name}.remove(value);
        // return;
        // }
        // 
        // OR
        // 
        // if ("${cmrField.name}".equals(name)) {
        // ${cmrField.name} = null;
        // return;
        // }
        createOpenEJB_removeCmr(mv, cmrField);
    }
    // throw new IllegalArgumentException("Unknown cmr field " + name + " on entity bean of type " + getClass().getName());
    mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
    mv.visitInsn(DUP);
    mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
    mv.visitLdcInsn("Unknown cmr field ");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitLdcInsn(" on entity bean of type ");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V", false);
    mv.visitInsn(ATHROW);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Label(org.apache.xbean.asm5.Label) MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Example 17 with Type

use of org.apache.xbean.asm5.Type in project tomee by apache.

the class Cmp2Generator method createOpenEJB_addCmr.

/**
 * Generate the OpenEJB_addCmr logic for an individual
 * CMR field.  Each CMR field has a test against the
 * property name, which is passed to the wrappering
 * addCmr method.  This results in a series of
 * if blocks for each defined CMD property.
 *
 * @param mv       The method we're generating within.
 * @param cmrField The CMR field definition.
 */
private void createOpenEJB_addCmr(final MethodVisitor mv, final CmrField cmrField) {
    // if (${cmrField.name}.equals(arg1))
    mv.visitLdcInsn(cmrField.getName());
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    // if not equal jump to end
    final Label end = new Label();
    mv.visitJumpInsn(IFEQ, end);
    // collection already anchored in the CMR field.
    if (cmrField.getCmrStyle() != CmrStyle.SINGLE) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, implClassName, cmrField.getName(), cmrField.getDescriptor());
        mv.visitVarInsn(ASTORE, 3);
        mv.visitVarInsn(ALOAD, 3);
        final Label fieldNotNull = new Label();
        mv.visitJumpInsn(IFNONNULL, fieldNotNull);
        // lazy creation of the collection type if not already created.
        mv.visitTypeInsn(NEW, cmrField.getInitialValueType().getInternalName());
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, cmrField.getInitialValueType().getInternalName(), "<init>", "()V", false);
        mv.visitVarInsn(ASTORE, 3);
        mv.visitLabel(fieldNotNull);
        // ${cmrField.name}.add(arg2)
        mv.visitVarInsn(ALOAD, 3);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEINTERFACE, cmrField.getCmrStyle().getCollectionType().getInternalName(), "add", "(Ljava/lang/Object;)Z", true);
        mv.visitInsn(POP);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 3);
        // unconditionally set the CMR field to the collection.  This is either the
        // original one on entry, or a new one for first access.
        mv.visitFieldInsn(PUTFIELD, implClassName, cmrField.getName(), cmrField.getDescriptor());
        // return null;
        mv.visitInsn(ACONST_NULL);
        mv.visitInsn(ARETURN);
    } else {
        // push: this.${cmrField.name};
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, implClassName, cmrField.getName(), cmrField.getDescriptor());
        // this.${cmrField.name} = (${cmrField.type}) bean;
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 2);
        mv.visitTypeInsn(CHECKCAST, cmrField.getType().getInternalName());
        mv.visitFieldInsn(PUTFIELD, implClassName, cmrField.getName(), cmrField.getDescriptor());
        // return pushed value above
        mv.visitInsn(ARETURN);
    }
    // end of if statement
    mv.visitLabel(end);
}
Also used : Label(org.apache.xbean.asm5.Label)

Example 18 with Type

use of org.apache.xbean.asm5.Type in project tomee by apache.

the class Cmp2Generator method createSelectMethod.

/**
 * Generate a concrete implementation of an abstract
 * ejbSelectxxxx method.
 *
 * @param selectMethod The abstract definition for the method we're generating.
 */
private void createSelectMethod(final Method selectMethod) {
    final Class<?> returnType = selectMethod.getReturnType();
    final Method executeMethod = EjbSelect.getSelectMethod(returnType);
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, selectMethod.getName(), Type.getMethodDescriptor(selectMethod), null, getExceptionTypes(selectMethod));
    mv.visitCode();
    // push deploymentInfo
    mv.visitFieldInsn(GETSTATIC, implClassName, "deploymentInfo", "Ljava/lang/Object;");
    // push method signature
    mv.visitLdcInsn(getSelectMethodSignature(selectMethod));
    // one of the primitives.
    if (!returnType.isPrimitive()) {
        mv.visitLdcInsn(returnType.getName());
    }
    // new Object[]
    mv.visitIntInsn(BIPUSH, selectMethod.getParameterTypes().length);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    // object[i] = arg${i}
    int i = 0;
    for (final Class<?> parameterType : selectMethod.getParameterTypes()) {
        // push arguement i on stack
        mv.visitInsn(DUP);
        bipush(mv, i);
        mv.visitVarInsn(Type.getType(parameterType).getOpcode(ILOAD), i + 1);
        // convert argument on stack to an Object
        Convert.toObjectFrom(mv, parameterType);
        // store it into the array
        mv.visitInsn(AASTORE);
        if (long.class.equals(parameterType) || double.class.equals(parameterType)) {
            // longs and doubles are double wide
            i = i + 2;
        } else {
            i++;
        }
    }
    // EjbSelect.execute_xxxx(deploymentInfo, signature, [returnType.] args[]);
    mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(executeMethod.getDeclaringClass()), executeMethod.getName(), Type.getMethodDescriptor(executeMethod), false);
    // needs to match the type returned from the method call
    if (!Void.TYPE.equals(returnType)) {
        // stack already
        if (!returnType.isPrimitive()) {
            // convert return type
            Convert.fromObjectTo(mv, returnType);
        }
        // And generate the appropriate return for the type
        mv.visitInsn(Type.getReturnType(selectMethod).getOpcode(IRETURN));
    } else {
        // void return is just a RETURN.
        mv.visitInsn(RETURN);
    }
    // close method
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Method(java.lang.reflect.Method) MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Example 19 with Type

use of org.apache.xbean.asm5.Type in project tomee by apache.

the class Cmp2Generator method createOpenEJB_addCmr.

private void createOpenEJB_addCmr() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "OpenEJB_addCmr", "(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;", null, null);
    mv.visitCode();
    // if (deleted) return null;
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, implClassName, DELETED, "Z");
    final Label notDeleted = new Label();
    mv.visitJumpInsn(IFEQ, notDeleted);
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ARETURN);
    mv.visitLabel(notDeleted);
    for (final CmrField cmrField : cmrFields) {
        // if ("${cmrField.name}".equals(name)) {
        // ${cmrField.name}.add((${cmrField.type})value);
        // return null;
        // }
        // 
        // OR
        // 
        // if ("${cmrField.name}".equals(name)) {
        // Object oldValue = ${cmrField.name};
        // ${cmrField.name} = (${cmrField.type}) bean;
        // return oldValue;
        // }
        createOpenEJB_addCmr(mv, cmrField);
    }
    // throw new IllegalArgumentException("Unknown cmr field " + name + " on entity bean of type " + getClass().getName());
    mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
    mv.visitInsn(DUP);
    mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false);
    mv.visitLdcInsn("Unknown cmr field ");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitLdcInsn(" on entity bean of type ");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V", false);
    mv.visitInsn(ATHROW);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Label(org.apache.xbean.asm5.Label) MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Example 20 with Type

use of org.apache.xbean.asm5.Type in project tomee by apache.

the class Cmp2Generator method initCmrFields.

/**
 * Initialize the CMR fields associated with a CMR
 * definition.  This initializes two fields per CMR
 * defined field:  1)  The CMR field itself (which might
 * be initialized to an instance of a defined type) and 2)
 * the appropriate CMD accessor that handles the
 * different types of relationship.
 *
 * @param mv       The method context we're initializing in.
 * @param cmrField The CMR field to process.
 */
private void initCmrFields(final MethodVisitor mv, final CmrField cmrField) {
    // this.${cmrField.name} = new ${cmrField.initialValueType}();
    final Type initialValueType = cmrField.getInitialValueType();
    if (initialValueType != null) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, initialValueType.getInternalName());
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, initialValueType.getInternalName(), "<init>", "()V", false);
        mv.visitFieldInsn(PUTFIELD, implClassName, cmrField.getName(), cmrField.getDescriptor());
    }
    // this.${cmrField.name}Cmr = new ${cmrField.accessorType}<${cmrField.type}, ${cmrField.proxyType}>(this,
    // ${cmrField.name},
    // ${cmrField.type},
    // ${cmrField.relatedName});
    mv.visitVarInsn(ALOAD, 0);
    mv.visitTypeInsn(NEW, cmrField.getAccessorInternalName());
    mv.visitInsn(DUP);
    // arg0: EntityBean source = this
    mv.visitVarInsn(ALOAD, 0);
    // arg1: String sourceProperty - "b"
    mv.visitLdcInsn(cmrField.getName());
    // arg2: Class<Bean> relatedType = BBean_BBean
    mv.visitLdcInsn(cmrField.getType());
    // arg3: String relatedProperty
    if (cmrField.getRelatedName() != null) {
        mv.visitLdcInsn(cmrField.getRelatedName());
    } else {
        mv.visitInsn(ACONST_NULL);
    }
    // invoke
    mv.visitMethodInsn(INVOKESPECIAL, cmrField.getAccessorInternalName(), "<init>", "(Ljavax/ejb/EntityBean;Ljava/lang/String;Ljava/lang/Class;Ljava/lang/String;)V", false);
    // bCmr = result
    mv.visitFieldInsn(PUTFIELD, implClassName, cmrField.getName() + "Cmr", cmrField.getAccessorDescriptor());
}
Also used : Type(org.apache.xbean.asm5.Type)

Aggregations

MethodVisitor (org.apache.xbean.asm5.MethodVisitor)9 Label (org.apache.xbean.asm5.Label)4 Type (org.apache.xbean.asm5.Type)4 FieldInsnNode (org.apache.xbean.asm5.tree.FieldInsnNode)4 InsnNode (org.apache.xbean.asm5.tree.InsnNode)4 IntInsnNode (org.apache.xbean.asm5.tree.IntInsnNode)4 JumpInsnNode (org.apache.xbean.asm5.tree.JumpInsnNode)4 LdcInsnNode (org.apache.xbean.asm5.tree.LdcInsnNode)4 MethodInsnNode (org.apache.xbean.asm5.tree.MethodInsnNode)4 MethodNode (org.apache.xbean.asm5.tree.MethodNode)4 TypeInsnNode (org.apache.xbean.asm5.tree.TypeInsnNode)4 VarInsnNode (org.apache.xbean.asm5.tree.VarInsnNode)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 MatchEntry (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry)3 MatchEntryBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder)3 Icmpv4TypeCaseBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv4TypeCaseBuilder)3 Icmpv6TypeCaseBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv6TypeCaseBuilder)3 TcpSrcCaseBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.TcpSrcCaseBuilder)3 Icmpv4TypeBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv4.type._case.Icmpv4TypeBuilder)3