Search in sources :

Example 1 with ClassReader

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

the class TargetClassInfoReader method visitEnd.

/**
	 * Stores signatures for all super public methods not already overridden by target class.
	 * All this methods will be accepted for proxyfication.
	 */
@Override
public void visitEnd() {
    // prepare class annotations
    if (classAnnotations != null) {
        annotations = classAnnotations.toArray(new AnnotationInfo[classAnnotations.size()]);
        classAnnotations = null;
    }
    List<String> superList = new ArrayList<>();
    Set<String> allInterfaces = new HashSet<>();
    if (nextInterfaces != null) {
        allInterfaces.addAll(nextInterfaces);
    }
    // check all public super methods that are not overridden in superclass
    while (nextSupername != null) {
        InputStream inputStream = null;
        ClassReader cr = null;
        try {
            inputStream = ClassLoaderUtil.getClassAsStream(nextSupername, classLoader);
            cr = new ClassReader(inputStream);
        } catch (IOException ioex) {
            throw new ProxettaException("Unable to inspect super class: " + nextSupername, ioex);
        } finally {
            StreamUtil.close(inputStream);
        }
        superList.add(nextSupername);
        // remember the super class reader
        superClassReaders.add(cr);
        cr.accept(new SuperClassVisitor(), 0);
        if (cr.getInterfaces() != null) {
            Collections.addAll(allInterfaces, cr.getInterfaces());
        }
    }
    superClasses = superList.toArray(new String[superList.size()]);
    // check all interface methods that are not overridden in super-interface
    for (String next : allInterfaces) {
        InputStream inputStream = null;
        ClassReader cr = null;
        try {
            inputStream = ClassLoaderUtil.getClassAsStream(next, classLoader);
            cr = new ClassReader(inputStream);
        } catch (IOException ioex) {
            throw new ProxettaException("Unable to inspect super interface: " + next, ioex);
        } finally {
            StreamUtil.close(inputStream);
        }
        // remember the super class reader
        superClassReaders.add(cr);
        cr.accept(new SuperClassVisitor(), 0);
    }
}
Also used : ProxettaException(jodd.proxetta.ProxettaException) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ClassReader(jodd.asm5.ClassReader) IOException(java.io.IOException) AnnotationInfo(jodd.proxetta.AnnotationInfo) HashSet(java.util.HashSet)

Example 2 with ClassReader

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

the class ProxettaBuilder method process.

/**
	 * Reads the target and creates destination class.
	 */
protected void process() {
    if (targetInputStream == null) {
        throw new ProxettaException("Target missing");
    }
    // create class reader
    ClassReader classReader;
    try {
        classReader = new ClassReader(targetInputStream);
    } catch (IOException ioex) {
        throw new ProxettaException("Error reading class input stream", ioex);
    }
    // reads information
    TargetClassInfoReader targetClassInfoReader = new TargetClassInfoReader(proxetta.getClassLoader());
    classReader.accept(targetClassInfoReader, 0);
    this.destClassWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    // create proxy
    if (log.isDebugEnabled()) {
        log.debug("processing: " + classReader.getClassName());
    }
    WorkData wd = process(classReader, targetClassInfoReader);
    // store important data
    proxyApplied = wd.proxyApplied;
    proxyClassName = wd.thisReference.replace('/', '.');
}
Also used : TargetClassInfoReader(jodd.proxetta.asm.TargetClassInfoReader) ClassReader(jodd.asm5.ClassReader) IOException(java.io.IOException) WorkData(jodd.proxetta.asm.WorkData) ClassWriter(jodd.asm5.ClassWriter)

Example 3 with ClassReader

use of org.apache.xbean.asm9.ClassReader in project apex-core by apache.

the class FastClassIndexReaderTest method testIndexReader.

@Test
public void testIndexReader() throws IOException {
    String javahome = System.getProperty("java.home");
    String jdkJar = javahome + "/lib/rt.jar";
    JarFile jar = new JarFile(jdkJar);
    java.util.Enumeration<JarEntry> entriesEnum = jar.entries();
    while (entriesEnum.hasMoreElements()) {
        JarEntry jarEntry = entriesEnum.nextElement();
        if (jarEntry.getName().endsWith("class")) {
            InputStream ins = jar.getInputStream(jarEntry);
            ClassReader classReader = new ClassReader(ins);
            ClassNodeType classN = new ClassNodeType();
            classReader.accept(classN, ClassReader.SKIP_CODE);
            CompactClassNode ccn = CompactUtil.compactClassNode(classN);
            ins.close();
            ins = jar.getInputStream(jarEntry);
            FastClassIndexReader fastClassIndexReader = new FastClassIndexReader(ins);
            Assert.assertEquals("The wrong class is " + classN.name, classN.name, fastClassIndexReader.getName());
            Assert.assertEquals("The wrong class is " + classN.name, classN.superName, fastClassIndexReader.getSuperName());
            Assert.assertEquals("The wrong class is " + classN.name, !ASMUtil.isAbstract(classN.access) && ASMUtil.isPublic(classN.access) && ccn.getDefaultConstructor() != null, fastClassIndexReader.isInstantiable());
            Assert.assertArrayEquals("The wrong class is " + classN.name, classN.interfaces.toArray(), fastClassIndexReader.getInterfaces());
        }
    }
}
Also used : InputStream(java.io.InputStream) ClassReader(org.apache.xbean.asm5.ClassReader) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Test(org.junit.Test)

Example 4 with ClassReader

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

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

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