Search in sources :

Example 1 with RETURN

use of org.apache.xbean.asm9.Opcodes.RETURN in project component-runtime by Talend.

the class PluginGenerator method createChainPlugin.

public File createChainPlugin(final File dir, final String plugin) {
    final File target = new File(dir, plugin);
    try (final JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(target))) {
        final String packageName = toPackage(target.getParentFile().getParentFile().getName()).replace(".", "/");
        final String sourcePackage = "org/talend/test";
        final String fromPack = sourcePackage.replace('/', '.');
        final String toPack = packageName.replace('.', '/');
        final File root = new File(jarLocation(getClass()), sourcePackage);
        ofNullable(root.listFiles()).map(Stream::of).orElseGet(Stream::empty).filter(c -> c.getName().endsWith(".class")).forEach(clazz -> {
            try (final InputStream is = new FileInputStream(clazz)) {
                final ClassReader reader = new ClassReader(is);
                final ClassWriter writer = new ClassWriter(COMPUTE_FRAMES);
                reader.accept(new ClassRemapper(writer, new Remapper() {

                    @Override
                    public String map(final String key) {
                        return key.replace(sourcePackage, toPack).replace(fromPack, packageName);
                    }
                }), EXPAND_FRAMES);
                outputStream.putNextEntry(new JarEntry(toPack + '/' + clazz.getName()));
                outputStream.write(writer.toByteArray());
            } catch (final IOException e) {
                fail(e.getMessage());
            }
        });
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
    return target;
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Action(org.talend.sdk.component.api.service.Action) ClassReader(org.apache.xbean.asm6.ClassReader) V1_8(org.apache.xbean.asm6.Opcodes.V1_8) Type(org.apache.xbean.asm6.Type) JarEntry(java.util.jar.JarEntry) Processor(org.talend.sdk.component.api.processor.Processor) ACC_PUBLIC(org.apache.xbean.asm6.Opcodes.ACC_PUBLIC) ClassRemapper(org.apache.xbean.asm6.commons.ClassRemapper) ElementListener(org.talend.sdk.component.api.processor.ElementListener) ALOAD(org.apache.xbean.asm6.Opcodes.ALOAD) EXPAND_FRAMES(org.apache.xbean.asm6.ClassReader.EXPAND_FRAMES) ACC_SUPER(org.apache.xbean.asm6.Opcodes.ACC_SUPER) JarOutputStream(java.util.jar.JarOutputStream) ZipEntry(java.util.zip.ZipEntry) Remapper(org.apache.xbean.asm6.commons.Remapper) COMPUTE_FRAMES(org.apache.xbean.asm6.ClassWriter.COMPUTE_FRAMES) INVOKESPECIAL(org.apache.xbean.asm6.Opcodes.INVOKESPECIAL) AnnotationVisitor(org.apache.xbean.asm6.AnnotationVisitor) Optional.ofNullable(java.util.Optional.ofNullable) NEW(org.apache.xbean.asm6.Opcodes.NEW) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) RETURN(org.apache.xbean.asm6.Opcodes.RETURN) MethodVisitor(org.apache.xbean.asm6.MethodVisitor) Collectors.joining(java.util.stream.Collectors.joining) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) Stream(java.util.stream.Stream) DUP(org.apache.xbean.asm6.Opcodes.DUP) JarLocation.jarLocation(org.apache.ziplock.JarLocation.jarLocation) ClassWriter(org.apache.xbean.asm6.ClassWriter) Service(org.talend.sdk.component.api.service.Service) ARETURN(org.apache.xbean.asm6.Opcodes.ARETURN) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) ClassRemapper(org.apache.xbean.asm6.commons.ClassRemapper) FileInputStream(java.io.FileInputStream) ClassWriter(org.apache.xbean.asm6.ClassWriter) ClassRemapper(org.apache.xbean.asm6.commons.ClassRemapper) Remapper(org.apache.xbean.asm6.commons.Remapper) FileOutputStream(java.io.FileOutputStream) ClassReader(org.apache.xbean.asm6.ClassReader) JarOutputStream(java.util.jar.JarOutputStream) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream) Stream(java.util.stream.Stream) InputStream(java.io.InputStream) File(java.io.File)

Example 2 with RETURN

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

the class PersistenceContextAnnFactory method addAnnotations.

public void addAnnotations(final Class c) throws OpenEJBException {
    if (!useAsm) {
        return;
    }
    if (processed.contains(c.getName())) {
        return;
    }
    try {
        final URL u = c.getResource("/" + c.getName().replace('.', '/') + ".class");
        final ClassReader r = new ClassReader(IO.read(u));
        r.accept(new PersistenceContextReader(contexts), ClassReader.SKIP_DEBUG);
    } catch (final IOException e) {
        throw new OpenEJBException("Unable to read class " + c.getName());
    }
    processed.add(c.getName());
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) ClassReader(org.apache.xbean.asm9.ClassReader) IOException(java.io.IOException) URL(java.net.URL)

Example 3 with RETURN

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

the class Cmp2Generator method createUnsetEntityContext.

public void createUnsetEntityContext() {
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "unsetEntityContext", "()V", null, null);
    mv.visitCode();
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 1);
    mv.visitEnd();
}
Also used : MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 4 with RETURN

use of org.apache.xbean.asm9.Opcodes.RETURN 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:
 *
 * 1)  There is a defined primary key field.  The
 * contents of that field are returned.
 *
 * 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.
 *
 * 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.asm9.MethodVisitor)

Example 5 with RETURN

use of org.apache.xbean.asm9.Opcodes.RETURN 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.asm9.MethodVisitor)

Aggregations

MethodVisitor (org.apache.xbean.asm9.MethodVisitor)40 ClassWriter (org.apache.xbean.asm9.ClassWriter)20 ClassReader (org.apache.xbean.asm9.ClassReader)17 IOException (java.io.IOException)12 Method (java.lang.reflect.Method)11 Label (org.apache.xbean.asm9.Label)11 InputStream (java.io.InputStream)10 Type (org.apache.xbean.asm9.Type)9 Stream (java.util.stream.Stream)8 File (java.io.File)7 HashMap (java.util.HashMap)6 List (java.util.List)6 Optional.ofNullable (java.util.Optional.ofNullable)6 JarOutputStream (java.util.jar.JarOutputStream)6 ZipEntry (java.util.zip.ZipEntry)6 AnnotationVisitor (org.apache.xbean.asm9.AnnotationVisitor)6 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)5 Serializable (java.io.Serializable)5 JarEntry (java.util.jar.JarEntry)5