Search in sources :

Example 1 with MethodNode

use of org.powermock.api.mockito.repackaged.asm.tree.MethodNode in project powermock by powermock.

the class CheckClassAdapter method verify.

/**
     * Checks a given class
     *
     * @param cr a {@code ClassReader} that contains bytecode for the
     *        analysis.
     * @param dump true if bytecode should be printed out not only when errors
     *        are found.
     * @param pw write where results going to be printed
     */
public static void verify(final ClassReader cr, final boolean dump, final PrintWriter pw) {
    ClassNode cn = new ClassNode();
    cr.accept(new CheckClassAdapter(cn), ClassReader.SKIP_DEBUG);
    Type syperType = cn.superName == null ? null : Type.getObjectType(cn.superName);
    List methods = cn.methods;
    for (int i = 0; i < methods.size(); ++i) {
        MethodNode method = (MethodNode) methods.get(i);
        Analyzer a = new Analyzer(new SimpleVerifier(Type.getObjectType(cn.name), syperType, false));
        try {
            a.analyze(cn.name, method);
            if (!dump) {
                continue;
            }
        } catch (Exception e) {
            e.printStackTrace(pw);
        }
        Frame[] frames = a.getFrames();
        TraceMethodVisitor mv = new TraceMethodVisitor();
        pw.println(method.name + method.desc);
        for (int j = 0; j < method.instructions.size(); ++j) {
            method.instructions.get(j).accept(mv);
            StringBuilder s = new StringBuilder();
            Frame f = frames[j];
            if (f == null) {
                s.append('?');
            } else {
                for (int k = 0; k < f.getLocals(); ++k) {
                    s.append(getShortName(f.getLocal(k).toString())).append(' ');
                }
                s.append(" : ");
                for (int k = 0; k < f.getStackSize(); ++k) {
                    s.append(getShortName(f.getStack(k).toString())).append(' ');
                }
            }
            while (s.length() < method.maxStack + method.maxLocals + 1) {
                s.append(' ');
            }
            pw.print(Integer.toString(j + 100000).substring(1));
            // mv.text.get(j));
            pw.print(" " + s + " : " + mv.buf);
        }
        for (int j = 0; j < method.tryCatchBlocks.size(); ++j) {
            ((TryCatchBlockNode) method.tryCatchBlocks.get(j)).accept(mv);
            pw.print(" " + mv.buf);
        }
        pw.println();
    }
    pw.flush();
}
Also used : ClassNode(org.powermock.api.mockito.repackaged.asm.tree.ClassNode) TryCatchBlockNode(org.powermock.api.mockito.repackaged.asm.tree.TryCatchBlockNode) Frame(org.powermock.api.mockito.repackaged.asm.tree.analysis.Frame) SimpleVerifier(org.powermock.api.mockito.repackaged.asm.tree.analysis.SimpleVerifier) Analyzer(org.powermock.api.mockito.repackaged.asm.tree.analysis.Analyzer) Type(org.powermock.api.mockito.repackaged.asm.Type) MethodNode(org.powermock.api.mockito.repackaged.asm.tree.MethodNode) List(java.util.List)

Aggregations

List (java.util.List)1 Type (org.powermock.api.mockito.repackaged.asm.Type)1 ClassNode (org.powermock.api.mockito.repackaged.asm.tree.ClassNode)1 MethodNode (org.powermock.api.mockito.repackaged.asm.tree.MethodNode)1 TryCatchBlockNode (org.powermock.api.mockito.repackaged.asm.tree.TryCatchBlockNode)1 Analyzer (org.powermock.api.mockito.repackaged.asm.tree.analysis.Analyzer)1 Frame (org.powermock.api.mockito.repackaged.asm.tree.analysis.Frame)1 SimpleVerifier (org.powermock.api.mockito.repackaged.asm.tree.analysis.SimpleVerifier)1