Search in sources :

Example 1 with RETURN

use of org.apache.xbean.asm6.Opcodes.RETURN in project geronimo-xbean by apache.

the class XbeanAsmParameterNameLoader method createClassReader.

private static ClassReader createClassReader(Class declaringClass) throws IOException {
    InputStream in = null;
    try {
        ClassLoader classLoader = declaringClass.getClassLoader();
        in = classLoader.getResourceAsStream(declaringClass.getName().replace('.', '/') + ".class");
        ClassReader reader = new ClassReader(in);
        return reader;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignored) {
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) ClassReader(org.apache.xbean.asm6.ClassReader) IOException(java.io.IOException)

Example 2 with RETURN

use of org.apache.xbean.asm6.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) COMPUTE_FRAMES(org.apache.xbean.asm6.ClassWriter.COMPUTE_FRAMES) Optional.ofNullable(java.util.Optional.ofNullable) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ClassReader(org.apache.xbean.asm6.ClassReader) File(java.io.File) JarEntry(java.util.jar.JarEntry) Stream(java.util.stream.Stream) ClassRemapper(org.apache.xbean.asm6.commons.ClassRemapper) JarLocation.jarLocation(org.apache.ziplock.JarLocation.jarLocation) ClassWriter(org.apache.xbean.asm6.ClassWriter) EXPAND_FRAMES(org.apache.xbean.asm6.ClassReader.EXPAND_FRAMES) JarOutputStream(java.util.jar.JarOutputStream) Remapper(org.apache.xbean.asm6.commons.Remapper) 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) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream) Stream(java.util.stream.Stream) JarOutputStream(java.util.jar.JarOutputStream) InputStream(java.io.InputStream) File(java.io.File)

Example 3 with RETURN

use of org.apache.xbean.asm6.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 4 with RETURN

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

the class PluginGenerator method createModel.

private byte[] createModel(final JarOutputStream outputStream, String packageName) throws IOException {
    final String className = packageName + "/AModel.class";
    outputStream.putNextEntry(new ZipEntry(className));
    final ClassWriter writer = new ClassWriter(COMPUTE_FRAMES);
    writer.visit(V1_8, ACC_PUBLIC + ACC_SUPER, className.substring(0, className.length() - ".class".length()), null, Type.getInternalName(Object.class), null);
    writer.visitSource(className.replace(".class", ".java"), null);
    addConstructor(writer);
    // no real content (fields/methods) for now
    writer.visitEnd();
    return writer.toByteArray();
}
Also used : ZipEntry(java.util.zip.ZipEntry) ClassWriter(org.apache.xbean.asm6.ClassWriter)

Example 5 with RETURN

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

the class ProxyGenerator method createConstructor.

private String createConstructor(final ClassWriter cw, final Class<?> classToProxy, final String classFileName, final String proxyClassFileName, final Constructor<?> constructor, final boolean withInterceptors) {
    try {
        Constructor superDefaultCt;
        String parentClassFileName;
        String[] exceptions = null;
        if (classToProxy.isInterface()) {
            parentClassFileName = Type.getInternalName(Object.class);
            superDefaultCt = Object.class.getConstructor();
        } else {
            parentClassFileName = classFileName;
            if (constructor == null) {
                superDefaultCt = classToProxy.getConstructor();
            } else {
                superDefaultCt = constructor;
                Class<?>[] exceptionTypes = constructor.getExceptionTypes();
                exceptions = exceptionTypes.length == 0 ? null : new String[exceptionTypes.length];
                for (int i = 0; i < exceptionTypes.length; i++) {
                    exceptions[i] = Type.getInternalName(exceptionTypes[i]);
                }
            }
        }
        final String descriptor = Type.getConstructorDescriptor(superDefaultCt);
        final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", descriptor, null, exceptions);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        if (constructor != null) {
            for (int i = 1; i <= constructor.getParameterTypes().length; i++) {
                mv.visitVarInsn(ALOAD, i);
            }
        }
        mv.visitMethodInsn(INVOKESPECIAL, parentClassFileName, "<init>", descriptor, false);
        if (withInterceptors) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitInsn(ACONST_NULL);
            mv.visitFieldInsn(PUTFIELD, proxyClassFileName, FIELD_INTERCEPTOR_HANDLER, Type.getDescriptor(InterceptorHandler.class));
        }
        mv.visitInsn(RETURN);
        mv.visitMaxs(-1, -1);
        mv.visitEnd();
        return parentClassFileName;
    } catch (final NoSuchMethodException e) {
        throw new IllegalStateException(e);
    }
}
Also used : InterceptorHandler(org.talend.sdk.component.api.service.interceptor.InterceptorHandler) Constructor(java.lang.reflect.Constructor) AllArgsConstructor(lombok.AllArgsConstructor) MethodVisitor(org.apache.xbean.asm6.MethodVisitor)

Aggregations

ClassReader (org.apache.xbean.asm6.ClassReader)9 ClassWriter (org.apache.xbean.asm6.ClassWriter)7 MethodVisitor (org.apache.xbean.asm6.MethodVisitor)7 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 Stream (java.util.stream.Stream)4 ZipEntry (java.util.zip.ZipEntry)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 Constructor (java.lang.reflect.Constructor)3 Optional.ofNullable (java.util.Optional.ofNullable)3 JarEntry (java.util.jar.JarEntry)3 JarOutputStream (java.util.jar.JarOutputStream)3 EXPAND_FRAMES (org.apache.xbean.asm6.ClassReader.EXPAND_FRAMES)3 COMPUTE_FRAMES (org.apache.xbean.asm6.ClassWriter.COMPUTE_FRAMES)3 ClassRemapper (org.apache.xbean.asm6.commons.ClassRemapper)3 Remapper (org.apache.xbean.asm6.commons.Remapper)3 JarLocation.jarLocation (org.apache.ziplock.JarLocation.jarLocation)3 Assertions.fail (org.junit.jupiter.api.Assertions.fail)3