Search in sources :

Example 1 with COMPUTE_FRAMES

use of org.apache.xbean.asm9.ClassWriter.COMPUTE_FRAMES 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 COMPUTE_FRAMES

use of org.apache.xbean.asm9.ClassWriter.COMPUTE_FRAMES in project openwebbeans by apache.

the class AbstractMetaDataDiscoveryTest method createBean.

private void createBean(final JarOutputStream outputStream, final String resource, final Class<?> itf) throws IOException {
    outputStream.putNextEntry(new JarEntry(resource));
    final ClassWriter writer = new ClassWriter(COMPUTE_FRAMES);
    // make it count for annotated mode
    writer.visitAnnotation(Type.getDescriptor(ApplicationScoped.class), true).visitEnd();
    writer.visit(V1_8, ACC_PUBLIC + ACC_SUPER, resource.substring(0, resource.length() - ".class".length()), null, Type.getInternalName(Object.class), itf == null ? null : new String[] { Type.getInternalName(itf) });
    writer.visitSource(resource.replace(".class", ".java"), null);
    final MethodVisitor constructor = writer.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    constructor.visitCode();
    constructor.visitVarInsn(ALOAD, 0);
    constructor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    constructor.visitInsn(RETURN);
    constructor.visitMaxs(1, 1);
    constructor.visitEnd();
    writer.visitEnd();
    outputStream.write(writer.toByteArray());
    outputStream.closeEntry();
}
Also used : JarEntry(java.util.jar.JarEntry) ClassWriter(org.apache.xbean.asm9.ClassWriter) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Example 3 with COMPUTE_FRAMES

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

use of org.apache.xbean.asm9.ClassWriter.COMPUTE_FRAMES in project component-runtime by Talend.

the class RepositoryModelBuilderTest method createChainPlugin.

private 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) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) COMPUTE_FRAMES(org.apache.xbean.asm9.ClassWriter.COMPUTE_FRAMES) ComponentFamilyMeta(org.talend.sdk.component.runtime.manager.ComponentFamilyMeta) HashMap(java.util.HashMap) EXPAND_FRAMES(org.apache.xbean.asm9.ClassReader.EXPAND_FRAMES) Collections.singletonList(java.util.Collections.singletonList) DataStore1(org.talend.test.DataStore1) JarEntry(java.util.jar.JarEntry) Collections.singleton(java.util.Collections.singleton) ClassReader(org.apache.xbean.asm9.ClassReader) ParameterModelService(org.talend.sdk.component.runtime.manager.reflect.ParameterModelService) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Path(java.nio.file.Path) JarOutputStream(java.util.jar.JarOutputStream) ParameterMeta(org.talend.sdk.component.runtime.manager.ParameterMeta) Collections.emptyMap(java.util.Collections.emptyMap) ClassWriter(org.apache.xbean.asm9.ClassWriter) Container(org.talend.sdk.component.container.Container) Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) FileOutputStream(java.io.FileOutputStream) WrappingStore(org.talend.test.WrappingStore) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IdGenerator(org.talend.sdk.component.runtime.manager.util.IdGenerator) Remapper(org.apache.xbean.asm9.commons.Remapper) Collectors.joining(java.util.stream.Collectors.joining) File(java.io.File) TestInfo(org.junit.jupiter.api.TestInfo) RepositoryModel(org.talend.sdk.component.design.extension.RepositoryModel) Test(org.junit.jupiter.api.Test) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) TempDir(org.junit.jupiter.api.io.TempDir) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) JarLocation.jarLocation(org.apache.ziplock.JarLocation.jarLocation) PropertyEditorRegistry(org.apache.xbean.propertyeditor.PropertyEditorRegistry) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) MigrationHandlerFactory(org.talend.sdk.component.runtime.manager.reflect.MigrationHandlerFactory) ReflectionService(org.talend.sdk.component.runtime.manager.reflect.ReflectionService) Collections(java.util.Collections) ClassRemapper(org.apache.xbean.asm9.commons.ClassRemapper) InputStream(java.io.InputStream) PartitionMapper1(org.talend.test.PartitionMapper1) 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.asm9.commons.ClassRemapper) FileInputStream(java.io.FileInputStream) ClassWriter(org.apache.xbean.asm9.ClassWriter) Remapper(org.apache.xbean.asm9.commons.Remapper) ClassRemapper(org.apache.xbean.asm9.commons.ClassRemapper) FileOutputStream(java.io.FileOutputStream) ClassReader(org.apache.xbean.asm9.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 5 with COMPUTE_FRAMES

use of org.apache.xbean.asm9.ClassWriter.COMPUTE_FRAMES in project component-runtime by Talend.

the class GenericComponentExtensionSupportTest method run.

@Test
void run(@TempDir final Path path) throws IOException {
    final Path pluginFolder = path.resolve("test-plugins_" + UUID.randomUUID().toString());
    Files.createDirectories(pluginFolder);
    final Path target = pluginFolder.resolve("test-compo.jar");
    try (final JarOutputStream outputStream = new JarOutputStream(Files.newOutputStream(target))) {
        final String packageName = "org.talend.generated.test." + getClass().getSimpleName();
        final String sourcePackage = "org/talend/test/generic";
        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());
                outputStream.closeEntry();
            } catch (final IOException e) {
                fail(e.getMessage());
            }
        });
        outputStream.putNextEntry(new JarEntry("META-INF/services/" + GenericComponentExtension.class.getName()));
        outputStream.write((packageName + '.' + MyGenericImpl.class.getSimpleName()).getBytes(StandardCharsets.UTF_8));
        outputStream.closeEntry();
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
    try (final ComponentManager manager = new ComponentManager(new File("target/test-dependencies"), "META-INF/test/dependencies", null)) {
        manager.addPlugin(target.toAbsolutePath().toString());
        final Mapper mapper = manager.findMapper("my-generic", "the-first", 1, singletonMap("a", "a->1")).orElseThrow(IllegalStateException::new);
        // we bypass a bit the lifecycle cause we know the test component we
        final Input input = mapper.create();
        // use
        final Record next = Record.class.cast(input.next());
        assertEquals("my-generic", next.getString("plugin"));
        assertEquals("the-first", next.getString("name"));
        assertEquals("a", next.getString("key"));
        assertEquals("a->1", next.getString("value"));
        assertNull(input.next());
    }
}
Also used : Path(java.nio.file.Path) Assertions.fail(org.junit.jupiter.api.Assertions.fail) COMPUTE_FRAMES(org.apache.xbean.asm9.ClassWriter.COMPUTE_FRAMES) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) EXPAND_FRAMES(org.apache.xbean.asm9.ClassReader.EXPAND_FRAMES) JarEntry(java.util.jar.JarEntry) ClassReader(org.apache.xbean.asm9.ClassReader) Collections.singletonMap(java.util.Collections.singletonMap) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Input(org.talend.sdk.component.runtime.input.Input) Path(java.nio.file.Path) JarOutputStream(java.util.jar.JarOutputStream) MyGenericImpl(org.talend.test.generic.MyGenericImpl) ClassWriter(org.apache.xbean.asm9.ClassWriter) Files(java.nio.file.Files) Optional.ofNullable(java.util.Optional.ofNullable) Record(org.talend.sdk.component.api.record.Record) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) UUID(java.util.UUID) Remapper(org.apache.xbean.asm9.commons.Remapper) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Mapper(org.talend.sdk.component.runtime.input.Mapper) Stream(java.util.stream.Stream) GenericComponentExtension(org.talend.sdk.component.spi.component.GenericComponentExtension) TempDir(org.junit.jupiter.api.io.TempDir) JarLocation.jarLocation(org.apache.ziplock.JarLocation.jarLocation) ClassRemapper(org.apache.xbean.asm9.commons.ClassRemapper) 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.asm9.commons.ClassRemapper) FileInputStream(java.io.FileInputStream) ClassWriter(org.apache.xbean.asm9.ClassWriter) Mapper(org.talend.sdk.component.runtime.input.Mapper) Input(org.talend.sdk.component.runtime.input.Input) Remapper(org.apache.xbean.asm9.commons.Remapper) ClassRemapper(org.apache.xbean.asm9.commons.ClassRemapper) GenericComponentExtension(org.talend.sdk.component.spi.component.GenericComponentExtension) ClassReader(org.apache.xbean.asm9.ClassReader) JarOutputStream(java.util.jar.JarOutputStream) FileInputStream(java.io.FileInputStream) Stream(java.util.stream.Stream) InputStream(java.io.InputStream) Record(org.talend.sdk.component.api.record.Record) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

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