Search in sources :

Example 26 with MethodVisitor

use of jodd.asm5.MethodVisitor in project tomee by apache.

the class Cmp2Generator method createOpenEJB_deleted.

private void createOpenEJB_deleted() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "OpenEJB_deleted", "()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);
    // deleted = true;
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(ICONST_1);
    mv.visitFieldInsn(PUTFIELD, implClassName, DELETED, "Z");
    for (final CmrField cmrField : cmrFields) {
        // ${cmrField.accessor}.delete(${cmrField.name});
        createOpenEJB_deleted(mv, cmrField);
    }
    // return;
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Label(org.apache.xbean.asm5.Label) MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Example 27 with MethodVisitor

use of jodd.asm5.MethodVisitor in project tomee by apache.

the class PostCreateGenerator method createEjbPostCreate.

/**
 * Generate an ejbPostCreatexxxx method corresponding
 * to an ejbCreatexxxx method definition.  These provided
 * methods are just empty stubs.
 *
 * @param ejbPostCreateName The name we're creating under.
 * @param ejbCreate         The matching ejbCreate method.  The post create method
 *                          will match this one in terms of method signature.
 */
public void createEjbPostCreate(final String ejbPostCreateName, final Method ejbCreate) {
    final String methodDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getArgumentTypes(ejbCreate));
    final MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, ejbPostCreateName, methodDescriptor, null, null);
    mv.visitCode();
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(0, ejbCreate.getParameterTypes().length + 1);
    mv.visitEnd();
}
Also used : MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Example 28 with MethodVisitor

use of jodd.asm5.MethodVisitor in project tomee by apache.

the class MakeTxLookup method createTopLinkStrategy.

private static void createTopLinkStrategy(final File baseDir) throws Exception {
    final String factory = TOPLINK_FACTORY;
    final String classFilePath = factory.replace('.', '/');
    final String sourceFileName = factory.substring(factory.lastIndexOf('.') + 1, factory.length()) + ".java";
    final ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, classFilePath, null, "oracle/toplink/essentials/transaction/JTATransactionController", null);
    cw.visitSource(sourceFileName, null);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "oracle/toplink/essentials/transaction/JTATransactionController", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PROTECTED, "acquireTransactionManager", "()Ljavax/transaction/TransactionManager;", null, new String[] { "java/lang/Exception" });
        mv.visitCode();
        mv.visitMethodInsn(INVOKESTATIC, "org/apache/openejb/OpenEJB", "getTransactionManager", "()Ljavax/transaction/TransactionManager;", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    cw.visitEnd();
    write(baseDir, cw, classFilePath);
}
Also used : ClassWriter(org.apache.xbean.asm5.ClassWriter) MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Example 29 with MethodVisitor

use of jodd.asm5.MethodVisitor in project tomee by apache.

the class MakeTxLookup method createHibernteStrategy.

private static void createHibernteStrategy(final File baseDir) throws Exception {
    final String factory = HIBERNATE_FACTORY;
    final String classFilePath = factory.replace('.', '/');
    final String sourceFileName = factory.substring(factory.lastIndexOf('.') + 1, factory.length()) + ".java";
    final ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, classFilePath, null, "java/lang/Object", new String[] { "org/hibernate/transaction/TransactionManagerLookup" });
    cw.visitSource(sourceFileName, null);
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "getTransactionManager", "(Ljava/util/Properties;)Ljavax/transaction/TransactionManager;", null, new String[] { "org/hibernate/HibernateException" });
        mv.visitCode();
        mv.visitMethodInsn(INVOKESTATIC, "org/apache/openejb/OpenEJB", "getTransactionManager", "()Ljavax/transaction/TransactionManager;", false);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 2);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "getUserTransactionName", "()Ljava/lang/String;", null, null);
        mv.visitCode();
        mv.visitLdcInsn("java:comp/UserTransaction");
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "getTransactionIdentifier", "(Ljavax/transaction/Transaction;)Ljava/lang/Object;", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 1);
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 2);
        mv.visitEnd();
    }
    cw.visitEnd();
    write(baseDir, cw, classFilePath);
}
Also used : ClassWriter(org.apache.xbean.asm5.ClassWriter) MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Example 30 with MethodVisitor

use of jodd.asm5.MethodVisitor in project tomee by apache.

the class DynamicSubclass method generateBytes.

private static byte[] generateBytes(final Class<?> classToProxy, final boolean proxyNonAbstractMethods) throws ProxyGenerationException {
    final Map<String, MethodVisitor> visitors = new HashMap<String, MethodVisitor>();
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    final String proxyClassFileName = getSubclassName(classToProxy).replace('.', '/');
    final String classFileName = classToProxy.getName().replace('.', '/');
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, proxyClassFileName, null, classFileName, null);
    cw.visitSource(classFileName + ".java", null);
    // push InvocationHandler field
    cw.visitField(ACC_FINAL + ACC_PRIVATE, "this$handler", "Ljava/lang/reflect/InvocationHandler;", null, null).visitEnd();
    for (final Constructor<?> constructor : classToProxy.getConstructors()) {
        if (!Modifier.isPublic(constructor.getModifiers())) {
            continue;
        }
        final MethodVisitor mv = visitConstructor(cw, proxyClassFileName, classFileName, constructor);
        visitors.put("<init>" + Type.getConstructorDescriptor(constructor), mv);
    }
    final Map<String, List<Method>> methodMap = new HashMap<>();
    getNonPrivateMethods(classToProxy, methodMap);
    // Iterate over the public methods
    for (final Map.Entry<String, List<Method>> entry : methodMap.entrySet()) {
        for (final Method method : entry.getValue()) {
            if (Modifier.isAbstract(method.getModifiers()) || (proxyNonAbstractMethods && Modifier.isPublic(method.getModifiers()))) {
                final MethodVisitor visitor = LocalBeanProxyFactory.visit(cw, method, proxyClassFileName, "this$handler");
                visitors.put(method.getName() + Type.getMethodDescriptor(method), visitor);
            }
        }
    }
    copyClassAnnotations(classToProxy, cw);
    copyMethodAnnotations(classToProxy, visitors);
    // This should never be reached, but just in case
    for (final MethodVisitor visitor : visitors.values()) {
        visitor.visitEnd();
    }
    return cw.toByteArray();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Method(java.lang.reflect.Method) HashMap(java.util.HashMap) Map(java.util.Map) ClassWriter(org.apache.xbean.asm5.ClassWriter) MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Aggregations

MethodVisitor (org.apache.xbean.asm5.MethodVisitor)28 MethodVisitor (jodd.asm5.MethodVisitor)6 ClassWriter (org.apache.xbean.asm5.ClassWriter)6 Label (org.apache.xbean.asm5.Label)5 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 Label (jodd.asm5.Label)2 Field (java.lang.reflect.Field)1 MethodAdapter (jodd.asm.MethodAdapter)1 FieldVisitor (jodd.asm5.FieldVisitor)1 Type (jodd.asm5.Type)1 ProxettaException (jodd.proxetta.ProxettaException)1 Type (org.apache.xbean.asm5.Type)1