/** * Constructs a new {@link GaTraceClassVisitor}. * * @param pw the print writer to be used to print the class. */ public GaTraceClassVisitor(final PrintWriter pw) { super(Opcodes.ASM5, null); this.pw = pw; this.p = new Textifier(); }
static void printAnalyzerResult(MethodNode method, Analyzer a, final PrintWriter pw) throws IllegalArgumentException { Frame[] frames = a.getFrames(); Textifier t = new Textifier(); TraceMethodVisitor mv = new TraceMethodVisitor(t); String format = "%05d %-" + (method.maxStack + method.maxLocals + 6) + "s|%s";
static void printAnalyzerResult(MethodNode method, Analyzer a, final PrintWriter pw) throws IllegalArgumentException, IllegalAccessException { Frame[] frames = a.getFrames(); Textifier t = new Textifier(); TraceMethodVisitor mv = new TraceMethodVisitor(t); String format = "%05d %-" + (method.maxStack + method.maxLocals + 6) + "s|%s"; for (int j = 0; j < method.instructions.size(); ++j) { method.instructions.get(j).accept(mv); StringBuffer s = new StringBuffer(); 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())); } s.append(" : "); for (int k = 0; k < f.getStackSize(); ++k) { s.append(getShortName(f.getStack(k).toString())); } } pw.printf(format, j, s, buf.get(t)); // mv.text.get(j)); } for (int j = 0; j < method.tryCatchBlocks.size(); ++j) { ((TryCatchBlockNode) method.tryCatchBlocks.get(j)).accept(mv); pw.print(" " + buf.get(t)); } pw.println(); pw.flush(); }
@Override public String toString() { final StringWriter writer = new StringWriter(); this.rawNode.accept(new TraceClassVisitor(null, new Textifier(), new PrintWriter( writer))); return writer.toString(); }
private void writeBytecodeToDisk(final byte[] clazz, Path folder) throws IOException { final ClassReader reader = new ClassReader(clazz); final CharArrayWriter buffer = new CharArrayWriter(); reader.accept(new TraceClassVisitor(null, new Textifier(), new PrintWriter( buffer)), ClassReader.EXPAND_FRAMES); final Path outFile = folder.resolve(this.currentClass.asJavaName() + ".txt"); Files.write(outFile, Collections.singleton(buffer.toString()), StandardCharsets.UTF_8, StandardOpenOption.CREATE); }
private String toString(MethodTree mt) { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final TraceMethodVisitor mv = new TraceMethodVisitor(new Textifier()); mt.rawNode().accept(mv); try (PrintWriter pw = new PrintWriter(bos)) { mv.p.print(pw); } return "Byte code is \n" + new String(bos.toByteArray()); }
/** * Creates a new TraceVisitor instance. * * @return a new TraceVisitor. */ protected Textifier createTextifier() { return new Textifier(); }
/** * Constructs a new {@link TraceClassVisitor}. * * @param cv * the {@link ClassVisitor} to which this visitor delegates * calls. May be <tt>null</tt>. * @param pw * the print writer to be used to print the class. */ public TraceClassVisitor(final ClassVisitor cv, final PrintWriter pw) { this(cv, new Textifier(), pw); }
/** * Constructs a new {@link TraceClassVisitor}. * * @param classVisitor the class visitor to which to delegate calls. May be {@literal null}. * @param printWriter the print writer to be used to print the class. May be {@literal null}. */ public TraceClassVisitor(final ClassVisitor classVisitor, final PrintWriter printWriter) { this(classVisitor, new Textifier(), printWriter); }
/** * Constructs a new {@link TraceClassVisitor}. * * @param classVisitor the class visitor to which to delegate calls. May be {@literal null}. * @param printWriter the print writer to be used to print the class. May be {@literal null}. */ public TraceClassVisitor(final ClassVisitor classVisitor, final PrintWriter printWriter) { this(classVisitor, new Textifier(), printWriter); }
/** * Creates a new {@link Textifier}. * * @return a new {@link Textifier}. */ protected Textifier createTextifier() { return new Textifier(); } }
@Override public Textifier visitAnnotationDefault() { return new Textifier(); }
@Override public Textifier visitMethodAnnotation(final String desc, final boolean visible) { return new Textifier(); }
@Override public Textifier visitParameterAnnotation(final int parameter, final String desc, final boolean visible) { return new Textifier(); }
/** * Creates a new {@link Textifier}. * * @return a new {@link Textifier}. */ protected Textifier createTextifier() { return new Textifier(); } }
@Override public String toString() { StringWriter sw = new StringWriter(); new ClassReader(data) .accept(new TraceClassVisitor(null, new Textifier(), new PrintWriter(sw)), 0); return sw.toString(); } }
@Override public String toString() { StringWriter sw = new StringWriter(); new ClassReader(data) .accept(new TraceClassVisitor(null, new Textifier(), new PrintWriter(sw)), 0); return sw.toString(); } }
private static void dumpClass(String className, byte[] data) { System.out.println("DUMP OF CLASS: " + className); ClassReader cr = new ClassReader(data); ClassVisitor cv = new TraceClassVisitor(null, new Textifier(), new PrintWriter(System.out)); cr.accept(cv, ClassReader.SKIP_FRAMES); System.out.println("================="); }
/* Create a "printer" that renders text versions of instructions */ Printer printer = new Textifier(); TraceMethodVisitor methodPrinter = new TraceMethodVisitor(printer); /* render the instruction as a string and add it to printer's internal buffer */ insnNode.accept(methodPrinter); /* convert printer's internal buffer to string and clear the buffer (so we can reuse it later) */ StringWriter sw = new StringWriter(); printer.print(new PrintWriter(sw)); printer.getText().clear(); String insnNodeAsString = sw.toString(); /* print the string out */ System.out.print(insnNodeAsString);
private static String getNodeString(AbstractInsnNode node) { Printer printer = new Textifier(); TraceMethodVisitor visitor = new TraceMethodVisitor(printer); node.accept(visitor); StringWriter sw = new StringWriter(); printer.print(new PrintWriter(sw)); printer.getText().clear(); return sw.toString().replaceAll("\n", "").trim(); }