Search in sources :

Example 6 with ClassReader

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

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

use of org.apache.xbean.asm9.ClassReader in project tomee by apache.

the class TempClassLoader method isAnnotationClass.

/**
 * Fast-parse the given class bytecode to determine if it is an
 * annotation class.
 */
private static boolean isAnnotationClass(final byte[] bytes) {
    final IsAnnotationVisitor isAnnotationVisitor = new IsAnnotationVisitor();
    final ClassReader classReader = new ClassReader(bytes);
    classReader.accept(isAnnotationVisitor, ClassReader.SKIP_DEBUG);
    return isAnnotationVisitor.isAnnotation;
}
Also used : ClassReader(org.apache.xbean.asm9.ClassReader)

Example 9 with ClassReader

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

use of org.apache.xbean.asm9.ClassReader in project tomee by apache.

the class AnnotationFinder method readClassDef.

private void readClassDef(String className, final ClassVisitor visitor) {
    classes++;
    if (!className.endsWith(".class")) {
        className = className.replace('.', '/') + ".class";
    }
    try {
        final URL resource = classLoader.getResource(className);
        if (resource != null) {
            InputStream in = resource.openStream();
            in = new BufferedInputStream(in);
            try {
                final ClassReader classReader = new ClassReader(in);
                classReader.accept(visitor, ASM_FLAGS);
            } finally {
                in.close();
            }
        } else {
            new Exception("Could not load " + className).printStackTrace();
        }
    } catch (final IOException e) {
        e.printStackTrace();
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) ClassReader(org.apache.xbean.asm9.ClassReader) IOException(java.io.IOException) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)16 ClassReader (org.apache.xbean.asm9.ClassReader)13 InputStream (java.io.InputStream)12 JarEntry (java.util.jar.JarEntry)5 ClassReader (org.apache.xbean.asm6.ClassReader)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 ProxyGenerationException (org.apache.openejb.util.proxy.ProxyGenerationException)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