Search in sources :

Example 46 with ClassReader

use of org.objectweb.asm.ClassReader in project spring-loaded by spring-projects.

the class ClassPrinter method main.

public static void main(String[] argv) throws Exception {
    ClassReader reader = new ClassReader(Utils.loadBytesFromStream(new FileInputStream(new File(argv[0]))));
    reader.accept(new ClassPrinter(System.out, INCLUDE_BYTECODE), 0);
}
Also used : ClassReader(org.objectweb.asm.ClassReader) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 47 with ClassReader

use of org.objectweb.asm.ClassReader in project es6draft by anba.

the class Code method toByteCode.

private static String toByteCode(ClassVisitor cv, Printer p, byte[] bytes) {
    new ClassReader(bytes).accept(cv, ClassReader.EXPAND_FRAMES);
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    p.print(pw);
    pw.flush();
    return sw.toString();
}
Also used : StringWriter(java.io.StringWriter) ClassReader(org.objectweb.asm.ClassReader) PrintWriter(java.io.PrintWriter)

Example 48 with ClassReader

use of org.objectweb.asm.ClassReader in project flink by apache.

the class JarFileCreator method addDependencies.

/**
	 * Add the dependencies within the given packages automatically.
	 * @throws IOException
	 * 			throw if an error occurs while read the class file.
	 */
private synchronized void addDependencies() throws IOException {
    List<String> dependencies = new ArrayList<String>();
    for (Class clazz : classSet) {
        dependencies.add(clazz.getName());
    }
    //Traverse the dependency tree using BFS.
    int head = 0;
    while (head != dependencies.size()) {
        DependencyVisitor v = new DependencyVisitor(Opcodes.ASM5);
        v.addNameSpace(this.packages);
        InputStream classInputStream = null;
        String name = dependencies.get(head);
        try {
            Class clazz = Class.forName(name);
            int n = name.lastIndexOf('.');
            String className = null;
            if (n > -1) {
                className = name.substring(n + 1, name.length());
            }
            classInputStream = clazz.getResourceAsStream(className + CLASS_EXTENSION);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e.getMessage());
        }
        new ClassReader(classInputStream).accept(v, 0);
        classInputStream.close();
        //Update the BFS queue.
        Set<String> classPackages = v.getPackages();
        for (String s : classPackages) {
            if (!dependencies.contains(s.replace('/', '.'))) {
                dependencies.add(s.replace('/', '.'));
            }
        }
        head++;
    }
    for (String dependency : dependencies) {
        try {
            this.classSet.add(Class.forName(dependency));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ClassReader(org.objectweb.asm.ClassReader)

Example 49 with ClassReader

use of org.objectweb.asm.ClassReader in project groovy by apache.

the class SunClassLoader method loadFromRes.

protected void loadFromRes(String name) throws IOException {
    final InputStream asStream = SunClassLoader.class.getClassLoader().getResourceAsStream(resName(name));
    ClassReader reader = new ClassReader(asStream);
    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    reader.accept(cw, ClassWriter.COMPUTE_MAXS);
    asStream.close();
    define(cw.toByteArray(), name);
}
Also used : InputStream(java.io.InputStream) ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter)

Example 50 with ClassReader

use of org.objectweb.asm.ClassReader in project groovy by apache.

the class VerifyClass method readClass.

private boolean readClass(String clazz) throws IOException {
    ClassReader cr = new ClassReader(new FileInputStream(clazz));
    ClassNode ca = new ClassNode() {

        public void visitEnd() {
        //accept(cv);
        }
    };
    cr.accept(new CheckClassAdapter(ca), ClassWriter.COMPUTE_MAXS);
    boolean failed = false;
    List methods = ca.methods;
    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = (MethodNode) methods.get(i);
        if (method.instructions.size() > 0) {
            Analyzer a = new Analyzer(new SimpleVerifier());
            try {
                a.analyze(ca.name, method);
                continue;
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (!failed) {
                failed = true;
                log("verifying of class " + clazz + " failed");
            }
            if (verbose)
                log(method.name + method.desc);
            TraceMethodVisitor mv = new TraceMethodVisitor(null);
            /*= new TraceMethodVisitor(null) {
                    public void visitMaxs(int maxStack, int maxLocals) {
                        StringBuffer buffer = new StringBuffer();
                        for (int i = 0; i < text.size(); ++i) {
                            String s = frames[i] == null ? "null" : frames[i].toString();
                            while (s.length() < maxStack + maxLocals + 1) {
                                s += " ";
                            }
                            buffer.append(Integer.toString(i + 100000).substring(1));
                            buffer.append(" ");
                            buffer.append(s);
                            buffer.append(" : ");
                            buffer.append(text.get(i));
                        }
                        if (verbose) log(buffer.toString());
                    }
                };*/
            for (int j = 0; j < method.instructions.size(); ++j) {
                Object insn = method.instructions.get(j);
                if (insn instanceof AbstractInsnNode) {
                    ((AbstractInsnNode) insn).accept(mv);
                } else {
                    mv.visitLabel((Label) insn);
                }
            }
            mv.visitMaxs(method.maxStack, method.maxLocals);
        }
    }
    return !failed;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) SimpleVerifier(org.objectweb.asm.tree.analysis.SimpleVerifier) Analyzer(org.objectweb.asm.tree.analysis.Analyzer) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) TraceMethodVisitor(org.objectweb.asm.util.TraceMethodVisitor) FileInputStream(java.io.FileInputStream) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) MethodNode(org.objectweb.asm.tree.MethodNode) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader) List(java.util.List)

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