Search in sources :

Example 1 with ACC_SUPER

use of org.apache.xbean.asm9.Opcodes.ACC_SUPER in project tomee by apache.

the class Cmp1Generator method generate.

/**
 * Generate the class for implementing CMP 1 level of
 * persistence.
 *
 * @return The generated byte-array containing the class data.
 */
public byte[] generate() {
    // We're creating a superclass for the implementation.  We force this to implement
    // EntityBean to allow POJOs to be used as the bean class.
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, implClassName, null, beanClassName, new String[] { "javax/ejb/EntityBean" });
    // if we have an unknown pk, we need to add a field for the pk
    if (unknownPk) {
        // public Long OpenEJB_pk;
        final FieldVisitor fv = cw.visitField(ACC_PUBLIC, "OpenEJB_pk", "Ljava/lang/Long;", null, null);
        fv.visitEnd();
    }
    // there's not much to generate here.  We create a default constructor, then generate the
    // post create methods.  A lot of the work is done by having mapped superclass information that
    // we pass to the JPA engine.
    createConstructor();
    postCreateGenerator.generate();
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : FieldVisitor(org.apache.xbean.asm9.FieldVisitor)

Example 2 with ACC_SUPER

use of org.apache.xbean.asm9.Opcodes.ACC_SUPER in project tomee by apache.

the class ValidationGenerator method generate.

public byte[] generate() throws ProxyGenerationException {
    generatedMethods.clear();
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    final String generatedClassName = getName().replace('.', '/');
    cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, generatedClassName, null, "java/lang/Object", null);
    {
        // public constructor
        final MethodVisitor 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();
    }
    generateMethods(cw);
    /**
     * Read all parent classes and copy the public methods we need
     * into our new class.
     */
    Class current = clazz;
    while (current != null && !current.equals(Object.class)) {
        try {
            final ClassReader classReader = new ClassReader(DynamicSubclass.readClassFile(current));
            classReader.accept(new CopyMethodAnnotations(), ClassReader.SKIP_CODE);
        } catch (final IOException e) {
            throw new ProxyGenerationException(e);
        }
        current = current.getSuperclass();
    }
    return cw.toByteArray();
}
Also used : ProxyGenerationException(org.apache.openejb.util.proxy.ProxyGenerationException) ClassReader(org.apache.xbean.asm9.ClassReader) IOException(java.io.IOException) ClassWriter(org.apache.xbean.asm9.ClassWriter) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 3 with ACC_SUPER

use of org.apache.xbean.asm9.Opcodes.ACC_SUPER in project tomee by apache.

the class ServiceClasspathTest method subclass.

public static File subclass(final Class<?> parent, final String subclassName) throws Exception {
    final String subclassNameInternal = subclassName.replace('.', '/');
    final byte[] bytes;
    {
        final ClassWriter cw = new ClassWriter(0);
        final String parentClassNameInternal = parent.getName().replace('.', '/');
        cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + ACC_SUPER, subclassNameInternal, null, parentClassNameInternal, null);
        final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitMethodInsn(INVOKESPECIAL, parentClassNameInternal, "<init>", "()V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
        cw.visitEnd();
        bytes = cw.toByteArray();
    }
    return Archive.archive().add(subclassNameInternal + ".class", bytes).asJar();
}
Also used : ClassWriter(org.apache.xbean.asm9.ClassWriter) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 4 with ACC_SUPER

use of org.apache.xbean.asm9.Opcodes.ACC_SUPER in project tomee by apache.

the class LocalBeanProxyFactory method generateProxy.

public static byte[] generateProxy(final Class<?> classToProxy, final String proxyName, final Class<?>... interfaces) throws ProxyGenerationException {
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    final String proxyClassFileName = proxyName.replace('.', '/');
    final String classFileName = classToProxy.getName().replace('.', '/');
    // push class signature
    final String[] interfaceNames = new String[interfaces.length];
    for (int i = 0; i < interfaces.length; i++) {
        final Class<?> anInterface = interfaces[i];
        interfaceNames[i] = anInterface.getName().replace('.', '/');
    }
    cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, proxyClassFileName, null, classFileName, interfaceNames);
    cw.visitSource(classFileName + ".java", null);
    cw.visitAnnotation("L" + Proxy.class.getName().replace('.', '/') + ";", true).visitEnd();
    // push InvocationHandler fields
    cw.visitField(ACC_FINAL + ACC_PRIVATE, BUSSINESS_HANDLER_NAME, "Ljava/lang/reflect/InvocationHandler;", null, null).visitEnd();
    cw.visitField(ACC_FINAL + ACC_PRIVATE, NON_BUSINESS_HANDLER_NAME, "Ljava/lang/reflect/InvocationHandler;", null, null).visitEnd();
    final Map<String, List<Method>> methodMap = new HashMap<>();
    getNonPrivateMethods(classToProxy, methodMap);
    for (final Class<?> anInterface : interfaces) {
        getNonPrivateMethods(anInterface, methodMap);
    }
    // Iterate over the public methods
    for (final Map.Entry<String, List<Method>> entry : methodMap.entrySet()) {
        for (final Method method : entry.getValue()) {
            final String name = method.getName();
            if (Modifier.isPublic(method.getModifiers()) || method.getParameterTypes().length == 0 && ("finalize".equals(name) || "clone".equals(name))) {
                // forward invocations of any public methods or
                // finalize/clone methods to businessHandler
                processMethod(cw, method, proxyClassFileName, BUSSINESS_HANDLER_NAME);
            } else {
                // forward invocations of any other methods to nonBusinessHandler
                processMethod(cw, method, proxyClassFileName, NON_BUSINESS_HANDLER_NAME);
            }
        }
    }
    return cw.toByteArray();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Method(java.lang.reflect.Method) HashMap(java.util.HashMap) Map(java.util.Map) ClassWriter(org.apache.xbean.asm9.ClassWriter)

Example 5 with ACC_SUPER

use of org.apache.xbean.asm9.Opcodes.ACC_SUPER 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.asm9.Label) ClassWriter(org.apache.xbean.asm9.ClassWriter) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Aggregations

ClassWriter (org.apache.xbean.asm9.ClassWriter)12 MethodVisitor (org.apache.xbean.asm9.MethodVisitor)10 Method (java.lang.reflect.Method)4 ZipEntry (java.util.zip.ZipEntry)4 AnnotationVisitor (org.apache.xbean.asm9.AnnotationVisitor)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ClassReader (org.apache.xbean.asm9.ClassReader)2 FieldVisitor (org.apache.xbean.asm9.FieldVisitor)2 Label (org.apache.xbean.asm9.Label)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ObjectStreamException (java.io.ObjectStreamException)1 Serializable (java.io.Serializable)1 Annotation (java.lang.annotation.Annotation)1 Constructor (java.lang.reflect.Constructor)1