Search in sources :

Example 11 with MethodVisitor

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

the class Cmp2Generator method createOpenEJB_removeCmr.

// private void createPrintln(MethodVisitor mv, String message) {
// mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
// mv.visitLdcInsn(message);
// mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
// }
// private void createPrintField(MethodVisitor mv, String fieldName, String descriptor) {
// mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
// mv.visitLdcInsn(fieldName + "=");
// mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "print", "(Ljava/lang/String;)V");
// 
// mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
// mv.visitVarInsn(ALOAD, 0);
// mv.visitFieldInsn(GETFIELD, implClassName, fieldName, descriptor);
// mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/Object;)V");
// }
/**
 * Emit the remove logic for an individual CMR field.
 * Like the addCmr logic, each field is guarded by an
 * if test on the property name.
 *
 * @param mv
 * @param cmrField
 */
private void createOpenEJB_removeCmr(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 valued CMR field.  Remove the object from the collection.
    if (cmrField.getCmrStyle() != CmrStyle.SINGLE) {
        // ${cmrField.name}.remove(arg2)
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, implClassName, cmrField.getName(), cmrField.getDescriptor());
        mv.visitVarInsn(ALOAD, 2);
        mv.visitMethodInsn(INVOKEINTERFACE, cmrField.getCmrStyle().getCollectionType().getInternalName(), "remove", "(Ljava/lang/Object;)Z", true);
        mv.visitInsn(POP);
        // return;
        mv.visitInsn(RETURN);
    } else {
        // single valued, so just null out the field.
        // this.${cmrField.name} = null;
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(ACONST_NULL);
        mv.visitFieldInsn(PUTFIELD, implClassName, cmrField.getName(), cmrField.getDescriptor());
        // return;
        mv.visitInsn(RETURN);
    }
    // end of if statement
    mv.visitLabel(end);
}
Also used : Label(org.apache.xbean.asm5.Label)

Example 12 with MethodVisitor

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

the class Cmp2Generator method createSimplePrimaryKeyGetter.

/**
 * Create a simple internal method for obtaining the
 * primary key.  There are 2 possibilities for handling
 * the primary key here:
 * <p/>
 * 1)  There is a defined primary key field.  The
 * contents of that field are returned.
 * <p/>
 * 2)  The primary key is provided by the container.
 * This is a long value stored in a private, generated
 * field.  This field is returned as a generated
 * wrappered Long.
 * <p/>
 * 3)  A primary key class has been provided.  An instance
 * of this class is instantiated, and code is generated
 * that will copy all of the CMP fields from the EJB
 * into the primary key instance.
 */
private void createSimplePrimaryKeyGetter() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "OpenEJB_getPrimaryKey", "()Ljava/lang/Object;", null, null);
    mv.visitCode();
    // the primary key is identifed as a field.  We just return that value directly.
    if (pkField != null) {
        // push the pk field
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, implClassName, pkField.getName(), pkField.getDescriptor());
        // return the pk field (from the stack)
        mv.visitInsn(pkField.getType().getOpcode(IRETURN));
    } else if (Object.class.equals(primKeyClass)) {
        // this is a container-generated primary key.  It's a long value stored in
        // a generated field.  We return that value, wrappered in a Long instance.
        // push the pk field
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, implClassName, UNKNOWN_PK_NAME, UNKNOWN_PK_TYPE.getDescriptor());
        // return the pk field (from the stack)
        mv.visitInsn(UNKNOWN_PK_TYPE.getOpcode(IRETURN));
    } else {
        // We have a primary key class defined.  For every field that matches one of the
        // defined CMP fields, we generate code to copy that value into the corresponding
        // field of the primary key class.
        final String pkImplName = primKeyClass.getName().replace('.', '/');
        // new Pk();
        mv.visitTypeInsn(NEW, pkImplName);
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, pkImplName, "<init>", "()V", false);
        mv.visitVarInsn(ASTORE, 1);
        mv.visitVarInsn(ALOAD, 1);
        // copy each field from the ejb to the pk class
        for (final Field field : primKeyClass.getFields()) {
            final CmpField cmpField = cmpFields.get(field.getName());
            // only process the cmp fields
            if (cmpField == null) {
                continue;
            }
            // check again since generated code is so hard to debug
            if (!cmpField.getType().getClassName().equals(field.getType().getName())) {
                throw new IllegalArgumentException("Primary key " + cmpField.getName() + " is type " + cmpField.getType().getClassName() + " but CMP field is type " + field.getType().getName());
            }
            // push the value from the cmp bean
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, implClassName, cmpField.getName(), cmpField.getDescriptor());
            // set matching field in the pk class to the value on the stack
            mv.visitFieldInsn(PUTFIELD, pkImplName, cmpField.getName(), cmpField.getDescriptor());
            mv.visitVarInsn(ALOAD, 1);
        }
        // return the Pk();
        mv.visitInsn(ARETURN);
    }
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Field(java.lang.reflect.Field) MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Example 13 with MethodVisitor

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

the class Cmp2Generator method createSetEntityContext.

public void createSetEntityContext() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "setEntityContext", "(Ljavax/ejb/EntityContext;)V", null, null);
    mv.visitCode();
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 2);
    mv.visitEnd();
}
Also used : MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Example 14 with MethodVisitor

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

the class Cmp2Generator method createSetter.

/**
 * Generate a concrete setter field for a CMP field.
 * At this point, we're just generating a simple
 * accessor for the field, given the type.  The
 * JPA engine when it makes this implementation class
 * a managed class define whatever additional logic
 * might be required.
 *
 * @param cmpField The CMP field backing this setter method.
 */
private void createSetter(final CmpField cmpField) {
    final String methodName = setterName(cmpField.getName());
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, methodName, "(" + cmpField.getDescriptor() + ")V", null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(cmpField.getType().getOpcode(ILOAD), 1);
    mv.visitFieldInsn(PUTFIELD, implClassName, cmpField.getName(), cmpField.getDescriptor());
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Example 15 with MethodVisitor

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

the class MakeTxLookup method createNewHibernateStrategy.

private static void createNewHibernateStrategy(final File basedir, final String target, final String abstractJtaPlatformPackage) throws Exception {
    final ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, target.replace('.', '/'), null, abstractJtaPlatformPackage + "/AbstractJtaPlatform", null);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, abstractJtaPlatformPackage + "/AbstractJtaPlatform", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "locateTransactionManager", "()Ljavax/transaction/TransactionManager;", null, null);
        mv.visitCode();
        mv.visitMethodInsn(INVOKESTATIC, "org/apache/openejb/OpenEJB", "getTransactionManager", "()Ljavax/transaction/TransactionManager;", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "locateUserTransaction", "()Ljavax/transaction/UserTransaction;", null, null);
        mv.visitCode();
        final Label l0 = new Label();
        final Label l1 = new Label();
        final Label l2 = new Label();
        mv.visitTryCatchBlock(l0, l1, l2, "javax/naming/NamingException");
        mv.visitLabel(l0);
        mv.visitMethodInsn(INVOKESTATIC, "org/apache/openejb/loader/SystemInstance", "get", "()Lorg/apache/openejb/loader/SystemInstance;", false);
        mv.visitLdcInsn(Type.getType("Lorg/apache/openejb/spi/ContainerSystem;"));
        mv.visitMethodInsn(INVOKEVIRTUAL, "org/apache/openejb/loader/SystemInstance", "getComponent", "(Ljava/lang/Class;)Ljava/lang/Object;", false);
        mv.visitTypeInsn(CHECKCAST, "org/apache/openejb/spi/ContainerSystem");
        mv.visitMethodInsn(INVOKEINTERFACE, "org/apache/openejb/spi/ContainerSystem", "getJNDIContext", "()Ljavax/naming/Context;", true);
        mv.visitLdcInsn("comp/UserTransaction");
        mv.visitMethodInsn(INVOKEINTERFACE, "javax/naming/Context", "lookup", "(Ljava/lang/String;)Ljava/lang/Object;", true);
        mv.visitTypeInsn(CHECKCAST, "javax/transaction/UserTransaction");
        mv.visitLabel(l1);
        mv.visitInsn(ARETURN);
        mv.visitLabel(l2);
        mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "javax/naming/NamingException" });
        mv.visitVarInsn(ASTORE, 1);
        mv.visitInsn(ACONST_NULL);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
    cw.visitEnd();
    write(basedir, cw, target.replace('.', '/'));
}
Also used : Label(org.apache.xbean.asm5.Label) ClassWriter(org.apache.xbean.asm5.ClassWriter) MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Aggregations

MethodVisitor (org.apache.xbean.asm5.MethodVisitor)28 Label (org.apache.xbean.asm5.Label)7 MethodVisitor (jodd.asm5.MethodVisitor)6 ClassWriter (org.apache.xbean.asm5.ClassWriter)6 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 EmptyClassVisitor (jodd.asm.EmptyClassVisitor)2 EmptyMethodVisitor (jodd.asm.EmptyMethodVisitor)2 Type (org.apache.xbean.asm5.Type)2 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 MethodAdapter (jodd.asm.MethodAdapter)1 FieldVisitor (jodd.asm5.FieldVisitor)1 Label (jodd.asm5.Label)1 ProxettaException (jodd.proxetta.ProxettaException)1 ProxyGenerationException (org.apache.openejb.util.proxy.ProxyGenerationException)1