Search in sources :

Example 6 with ClassReader

use of org.objectweb.asm.ClassReader in project cglib by cglib.

the class DebuggingClassWriter method toByteArray.

public byte[] toByteArray() {
    return (byte[]) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

        public Object run() {
            byte[] b = ((ClassWriter) DebuggingClassWriter.super.cv).toByteArray();
            if (debugLocation != null) {
                String dirs = className.replace('.', File.separatorChar);
                try {
                    new File(debugLocation + File.separatorChar + dirs).getParentFile().mkdirs();
                    File file = new File(new File(debugLocation), dirs + ".class");
                    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
                    try {
                        out.write(b);
                    } finally {
                        out.close();
                    }
                    if (traceCtor != null) {
                        file = new File(new File(debugLocation), dirs + ".asm");
                        out = new BufferedOutputStream(new FileOutputStream(file));
                        try {
                            ClassReader cr = new ClassReader(b);
                            PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
                            ClassVisitor tcv = (ClassVisitor) traceCtor.newInstance(new Object[] { null, pw });
                            cr.accept(tcv, 0);
                            pw.flush();
                        } finally {
                            out.close();
                        }
                    }
                } catch (Exception e) {
                    throw new CodeGenerationException(e);
                }
            }
            return b;
        }
    });
}
Also used : ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) ClassReader(org.objectweb.asm.ClassReader)

Example 7 with ClassReader

use of org.objectweb.asm.ClassReader in project cglib by cglib.

the class BridgeMethodResolver method resolveAll.

/**
     * Finds all bridge methods that are being called with invokespecial &
     * returns them.
     */
public Map resolveAll() {
    Map resolved = new HashMap();
    for (Iterator entryIter = declToBridge.entrySet().iterator(); entryIter.hasNext(); ) {
        Map.Entry entry = (Map.Entry) entryIter.next();
        Class owner = (Class) entry.getKey();
        Set bridges = (Set) entry.getValue();
        try {
            new ClassReader(classLoader.getResourceAsStream(owner.getName().replace('.', '/') + ".class")).accept(new BridgedFinder(bridges, resolved), ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG);
        } catch (IOException ignored) {
        }
    }
    return resolved;
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) Iterator(java.util.Iterator) ClassReader(org.objectweb.asm.ClassReader) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with ClassReader

use of org.objectweb.asm.ClassReader in project cglib by cglib.

the class AbstractClassLoader method loadClass.

public Class loadClass(String name) throws ClassNotFoundException {
    Class loaded = findLoadedClass(name);
    if (loaded != null) {
        if (loaded.getClassLoader() == this) {
            return loaded;
        }
    //else reload with this class loader
    }
    if (!filter.accept(name)) {
        return super.loadClass(name);
    }
    ClassReader r;
    try {
        java.io.InputStream is = classPath.getResourceAsStream(name.replace('.', '/') + ".class");
        if (is == null) {
            throw new ClassNotFoundException(name);
        }
        try {
            r = new ClassReader(is);
        } finally {
            is.close();
        }
    } catch (IOException e) {
        throw new ClassNotFoundException(name + ":" + e.getMessage());
    }
    try {
        DebuggingClassWriter w = new DebuggingClassWriter(ClassWriter.COMPUTE_FRAMES);
        getGenerator(r).generateClass(w);
        byte[] b = w.toByteArray();
        Class c = super.defineClass(name, b, 0, b.length, DOMAIN);
        postProcess(c);
        return c;
    } catch (RuntimeException e) {
        throw e;
    } catch (Error e) {
        throw e;
    } catch (Exception e) {
        throw new CodeGenerationException(e);
    }
}
Also used : DebuggingClassWriter(net.sf.cglib.core.DebuggingClassWriter) CodeGenerationException(net.sf.cglib.core.CodeGenerationException) ClassReader(org.objectweb.asm.ClassReader) IOException(java.io.IOException) CodeGenerationException(net.sf.cglib.core.CodeGenerationException) IOException(java.io.IOException)

Example 9 with ClassReader

use of org.objectweb.asm.ClassReader in project buck by facebook.

the class DxAnalysisMain method loadAllClasses.

private static ImmutableMap<String, ClassNode> loadAllClasses(String zipFileName) throws IOException {
    ImmutableMap.Builder<String, ClassNode> allClassesBuilder = ImmutableMap.builder();
    try (ZipFile inJar = new ZipFile(zipFileName)) {
        for (ZipEntry entry : Collections.list(inJar.entries())) {
            if (!entry.getName().endsWith(".class")) {
                continue;
            }
            // Skip external libraries.
            if (entry.getName().startsWith("junit/") || entry.getName().startsWith("org/junit/") || entry.getName().startsWith("com/google/common/")) {
                continue;
            }
            byte[] rawClass = ByteStreams.toByteArray(inJar.getInputStream(entry));
            ClassNode klass = new ClassNode();
            new ClassReader(rawClass).accept(klass, ClassReader.EXPAND_FRAMES);
            allClassesBuilder.put(klass.name, klass);
        }
    }
    return allClassesBuilder.build();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) ClassReader(org.objectweb.asm.ClassReader) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 10 with ClassReader

use of org.objectweb.asm.ClassReader in project buck by facebook.

the class FirstOrderTest method loadClassNode.

private static ClassNode loadClassNode(Class<?> input) {
    try {
        ClassReader reader = new ClassReader(input.getName());
        ClassNode node = new ClassNode(Opcodes.ASM4);
        reader.accept(node, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
        return node;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ClassReader(org.objectweb.asm.ClassReader) IOException(java.io.IOException)

Aggregations

ClassReader (org.objectweb.asm.ClassReader)437 ClassWriter (org.objectweb.asm.ClassWriter)182 Test (org.junit.Test)134 IOException (java.io.IOException)77 InputStream (java.io.InputStream)75 TreeMap (java.util.TreeMap)59 ClassNode (org.objectweb.asm.tree.ClassNode)58 SemanticVersioningClassVisitor (org.apache.aries.versioning.utils.SemanticVersioningClassVisitor)53 ClassVisitor (org.objectweb.asm.ClassVisitor)48 HashSet (java.util.HashSet)39 ZipEntry (java.util.zip.ZipEntry)34 BinaryCompatibilityStatus (org.apache.aries.versioning.utils.BinaryCompatibilityStatus)32 ZipFile (java.util.zip.ZipFile)29 InvocationTargetException (java.lang.reflect.InvocationTargetException)26 Method (java.lang.reflect.Method)25 OuterClass (com.android.tools.layoutlib.create.dataclass.OuterClass)23 InnerClass (com.android.tools.layoutlib.create.dataclass.OuterClass.InnerClass)23 PrintWriter (java.io.PrintWriter)23 MethodVisitor (org.objectweb.asm.MethodVisitor)23 MethodNode (org.objectweb.asm.tree.MethodNode)21