Search in sources :

Example 11 with ClassReader

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

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

use of org.apache.xbean.asm6.ClassReader 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) ComponentFamilyMeta(org.talend.sdk.component.runtime.manager.ComponentFamilyMeta) HashMap(java.util.HashMap) ClassReader(org.apache.xbean.asm6.ClassReader) Collections.singletonList(java.util.Collections.singletonList) DataStore1(org.talend.test.DataStore1) JarEntry(java.util.jar.JarEntry) Collections.singleton(java.util.Collections.singleton) ClassRemapper(org.apache.xbean.asm6.commons.ClassRemapper) WithTemporaryFolder(org.talend.sdk.component.junit.base.junit5.WithTemporaryFolder) ParameterModelService(org.talend.sdk.component.runtime.manager.reflect.ParameterModelService) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) EXPAND_FRAMES(org.apache.xbean.asm6.ClassReader.EXPAND_FRAMES) JarOutputStream(java.util.jar.JarOutputStream) Remapper(org.apache.xbean.asm6.commons.Remapper) ParameterMeta(org.talend.sdk.component.runtime.manager.ParameterMeta) Collections.emptyMap(java.util.Collections.emptyMap) COMPUTE_FRAMES(org.apache.xbean.asm6.ClassWriter.COMPUTE_FRAMES) 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) 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) TemporaryFolder(org.talend.sdk.component.junit.base.junit5.TemporaryFolder) JarLocation.jarLocation(org.apache.ziplock.JarLocation.jarLocation) ClassWriter(org.apache.xbean.asm6.ClassWriter) 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) 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.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 14 with ClassReader

use of org.apache.xbean.asm6.ClassReader in project jodd by oblac.

the class ProxyAspectData method getCachedAdviceClassReader.

/**
	 * Returns class reader for advice.
	 */
private ClassReader getCachedAdviceClassReader(Class<? extends ProxyAdvice> advice) {
    if (adviceClassReaderCache == null) {
        adviceClassReaderCache = new HashMap<>();
    }
    ClassReader adviceReader = adviceClassReaderCache.get(advice);
    if (adviceReader == null) {
        adviceReader = createAdviceClassReader(advice);
        adviceClassReaderCache.put(advice, adviceReader);
    }
    return adviceReader;
}
Also used : ClassReader(jodd.asm5.ClassReader)

Example 15 with ClassReader

use of org.apache.xbean.asm6.ClassReader in project jodd by oblac.

the class Paramo method resolveParameters.

/**
	 * Resolves method parameters from a method or constructor.
	 * Returns an empty array when target does not contain any parameter.
	 * No caching is involved in this process, i.e. class bytecode
	 * is examined every time this method is called.
	 */
public static MethodParameter[] resolveParameters(AccessibleObject methodOrCtor) {
    Class[] paramTypes;
    Class declaringClass;
    String name;
    if (methodOrCtor instanceof Method) {
        Method method = (Method) methodOrCtor;
        paramTypes = method.getParameterTypes();
        name = method.getName();
        declaringClass = method.getDeclaringClass();
    } else {
        Constructor constructor = (Constructor) methodOrCtor;
        paramTypes = constructor.getParameterTypes();
        declaringClass = constructor.getDeclaringClass();
        name = CTOR_METHOD;
    }
    if (paramTypes.length == 0) {
        return MethodParameter.EMPTY_ARRAY;
    }
    InputStream stream;
    try {
        stream = ClassLoaderUtil.getClassAsStream(declaringClass);
    } catch (IOException ioex) {
        throw new ParamoException("Failed to read class bytes: " + declaringClass.getName(), ioex);
    }
    if (stream == null) {
        throw new ParamoException("Class not found: " + declaringClass);
    }
    try {
        ClassReader reader = new ClassReader(stream);
        MethodFinder visitor = new MethodFinder(declaringClass, name, paramTypes);
        reader.accept(visitor, 0);
        return visitor.getResolvedParameters();
    } catch (IOException ioex) {
        throw new ParamoException(ioex);
    } finally {
        StreamUtil.close(stream);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) InputStream(java.io.InputStream) ClassReader(jodd.asm5.ClassReader) Method(java.lang.reflect.Method) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)15 InputStream (java.io.InputStream)12 ClassReader (org.apache.xbean.asm5.ClassReader)11 ClassReader (org.apache.xbean.asm6.ClassReader)8 JarEntry (java.util.jar.JarEntry)5 ClassReader (jodd.asm5.ClassReader)4 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 FileOutputStream (java.io.FileOutputStream)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Optional.ofNullable (java.util.Optional.ofNullable)3 JarOutputStream (java.util.jar.JarOutputStream)3 Stream (java.util.stream.Stream)3 EXPAND_FRAMES (org.apache.xbean.asm6.ClassReader.EXPAND_FRAMES)3 ClassWriter (org.apache.xbean.asm6.ClassWriter)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