Java Code Examples for org.objectweb.asm.util.Printer#print()

The following examples show how to use org.objectweb.asm.util.Printer#print() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AsmTestUtils.java    From grappa with Apache License 2.0 7 votes vote down vote up
public static String getMethodInstructionList(final MethodNode methodNode) {
    Preconditions.checkNotNull(methodNode, "methodNode");
    final Printer printer = new NonMaxTextifier();
    final TraceMethodVisitor traceMethodVisitor = new TraceMethodVisitor(printer);
    methodNode.accept(traceMethodVisitor);
    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);
    printer.print(printWriter);
    printWriter.flush();
    final String[] lines = PATTERN.split(stringWriter.toString());
    int lineNr = 0;
    for (int i = 0; i < lines.length; i++) {
        if (!lines[i].startsWith("  @")) {
            lines[i] = String.format("%2d %s", lineNr++, lines[i]);
        }
    }
    return "Method '" + methodNode.name + "':\n"
        + NEWLINE.join(lines) + '\n';
}
 
Example 2
Source File: Helper.java    From instrumentation with Apache License 2.0 6 votes vote down vote up
public static void viewByteCode(byte[] bytecode) {
    ClassReader cr = new ClassReader(bytecode);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);
    final List<MethodNode> mns = cn.methods;
    Printer printer = new Textifier();
    TraceMethodVisitor mp = new TraceMethodVisitor(printer);
    for (MethodNode mn : mns) {
        InsnList inList = mn.instructions;
        System.out.println(mn.name);
        for (int i = 0; i < inList.size(); i++) {
            inList.get(i).accept(mp);
            StringWriter sw = new StringWriter();
            printer.print(new PrintWriter(sw));
            printer.getText().clear();
            System.out.print(sw.toString());
        }
    }
}
 
Example 3
Source File: ClassFileStructurePrinter.java    From scott with MIT License 6 votes vote down vote up
public static void viewByteCode(byte[] bytecode) {
	ClassReader classReader = new ClassReader(bytecode);
	ClassNode classNode = new ClassNode();
	classReader.accept(classNode, 0);
	final List<MethodNode> methodNodes = classNode.methods;
	Printer printer = new Textifier();
	TraceMethodVisitor traceMethodVisitor = new TraceMethodVisitor(printer);
	
	for (MethodNode methodNode : methodNodes) {
		InsnList insnList = methodNode.instructions;
		System.out.println(methodNode.name);
		for (int i = 0; i < insnList.size(); i++) {
			insnList.get(i).accept(traceMethodVisitor);
			StringWriter sw = new StringWriter();
			printer.print(new PrintWriter(sw));
			printer.getText().clear();
			System.out.print(sw.toString());
		}
	}
}
 
Example 4
Source File: AsmTestUtils.java    From grappa with Apache License 2.0 5 votes vote down vote up
public static void assertTraceDumpEquality(
    final MethodNode method, final String traceDump) throws Exception {
    Preconditions.checkNotNull(method, "method");
    final Printer printer = new NonMaxTextifier();
    final TraceMethodVisitor traceMethodVisitor = new TraceMethodVisitor(printer);
    // MethodAdapter checkMethodAdapter = new MethodAdapter(traceMethodVisitor);
    final MethodVisitor checkMethodAdapter = new CheckMethodAdapter(traceMethodVisitor);
    method.accept(checkMethodAdapter);
    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);
    printer.print(printWriter);
    printWriter.flush();

    assertEquals(stringWriter.toString(), traceDump);
}
 
Example 5
Source File: Code.java    From es6draft with MIT License 5 votes vote down vote up
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();
}
 
Example 6
Source File: BytecodeEmitterContext.java    From Despector with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends TypeEntry> void emitOuterType(T ast) {
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    this.cw = writer;
    if (VERIFY_EMITTED_BYTECODE) {
        this.cw = new CheckClassAdapter(this.cw);
    }
    AstEmitter<AbstractEmitterContext, T> emitter = (AstEmitter<AbstractEmitterContext, T>) this.set.getAstEmitter(ast.getClass());
    if (emitter == null) {
        throw new IllegalArgumentException("No emitter for ast entry " + ast.getClass().getName());
    }
    emitter.emit(this, ast);
    this.cw.visitEnd();
    byte[] clazz = writer.toByteArray();
    if (DUMP_INSTRUCTIONS_AFTER_WRITE) {
        ClassReader cr = new ClassReader(clazz);
        ClassNode cn = new ClassNode();
        cr.accept(cn, 0);
        List<MethodNode> methods = cn.methods;
        for (MethodNode mn : methods) {
            System.out.println("Method: " + mn.name + mn.desc);
            Printer printer = new Textifier();
            TraceMethodVisitor mp = new TraceMethodVisitor(printer);
            for (Iterator<AbstractInsnNode> it = mn.instructions.iterator(); it.hasNext();) {
                AbstractInsnNode insn = it.next();
                insn.accept(mp);
            }
            StringWriter sw = new StringWriter();
            printer.print(new PrintWriter(sw));
            String s = sw.toString();
            if (s.endsWith("\n")) {
                s = s.substring(0, s.length() - 1);
            }
            System.out.println(s);
            mn.instructions.accept(mp);
        }
    }
    try {
        this.out.write(clazz);
    } catch (IOException e) {
        Throwables.propagate(e);
    }
}
 
Example 7
Source File: Instrumenter.java    From coroutines with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void verifyClassIntegrity(ClassNode classNode) {
    // Do not COMPUTE_FRAMES. If you COMPUTE_FRAMES and you pop too many items off the stack or do other weird things that mess up the
    // stack map frames, it'll crash on classNode.accept(cw).
    ClassWriter cw = new SimpleClassWriter(ClassWriter.COMPUTE_MAXS/* | ClassWriter.COMPUTE_FRAMES*/, classRepo);
    classNode.accept(cw);
    
    byte[] classData = cw.toByteArray();

    ClassReader cr = new ClassReader(classData);
    classNode = new SimpleClassNode();
    cr.accept(classNode, 0);

    for (MethodNode methodNode : classNode.methods) {
        Analyzer<BasicValue> analyzer = new Analyzer<>(new SimpleVerifier(classRepo));
        try {
            analyzer.analyze(classNode.name, methodNode);
        } catch (AnalyzerException e) {
            // IF WE DID OUR INSTRUMENTATION RIGHT, WE SHOULD NEVER GET AN EXCEPTION HERE!!!!
            StringWriter writer = new StringWriter();
            PrintWriter printWriter = new PrintWriter(writer);
            
            printWriter.append(methodNode.name + " encountered " + e);
            
            Printer printer = new Textifier();
            TraceMethodVisitor traceMethodVisitor = new TraceMethodVisitor(printer);
            
            AbstractInsnNode insn = methodNode.instructions.getFirst();
            while (insn != null) {
                if (insn == e.node) {
                    printer.getText().add("----------------- BAD INSTRUCTION HERE -----------------\n");
                }
                insn.accept(traceMethodVisitor);
                insn = insn.getNext();
            }
            printer.print(printWriter);
            printWriter.flush(); // we need this or we'll get incomplete results
            
            throw new IllegalStateException(writer.toString(), e);
        }
    }
}