Search in sources :

Example 56 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 57 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 58 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 59 with ClassReader

use of org.objectweb.asm.ClassReader in project enumerable by hraberg.

the class LambdaExpressionTrees method findMethodNode.

@SuppressWarnings("unchecked")
static MethodNode findMethodNode(Method method) throws IOException {
    String className = method.getDeclaringClass().getName();
    ClassReader cr;
    if (InMemoryCompiler.bytesByClassName.containsKey(className))
        cr = new ClassReader(InMemoryCompiler.bytesByClassName.get(className));
    else
        cr = new ClassReader(className);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);
    String descriptor = getMethodDescriptor(method);
    for (MethodNode mn : (List<MethodNode>) cn.methods) {
        if (method.getName().equals(mn.name) && descriptor.equals(mn.desc))
            return mn;
    }
    throw new IllegalStateException("Cannot find method which does exist");
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) ClassReader(org.objectweb.asm.ClassReader) List(java.util.List)

Example 60 with ClassReader

use of org.objectweb.asm.ClassReader in project semantic-versioning by jeluard.

the class ClassInheritanceTest method addClassInfo.

private void addClassInfo(Map<String, ClassInfo> classMap, Class klass, JarDiff jd) throws Exception {
    ClassInfo classInfo = jd.loadClassInfo(new ClassReader(klass.getName()));
    classMap.put(classInfo.getName(), classInfo);
}
Also used : ClassReader(org.objectweb.asm.ClassReader) ClassInfo(org.osjava.jardiff.ClassInfo)

Aggregations

ClassReader (org.objectweb.asm.ClassReader)449 ClassWriter (org.objectweb.asm.ClassWriter)187 Test (org.junit.Test)134 IOException (java.io.IOException)78 InputStream (java.io.InputStream)76 TreeMap (java.util.TreeMap)59 ClassNode (org.objectweb.asm.tree.ClassNode)58 ClassVisitor (org.objectweb.asm.ClassVisitor)54 SemanticVersioningClassVisitor (org.apache.aries.versioning.utils.SemanticVersioningClassVisitor)53 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