org.objectweb.asm.util.Textifier Java Examples

The following examples show how to use org.objectweb.asm.util.Textifier. 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: ValueHolderReplacementVisitor.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public void visitEnd() {
  try {
    accept(inner);
    super.visitEnd();
  } catch(Exception e){
    Textifier t = new Textifier();
    accept(new TraceMethodVisitor(t));
    StringBuilderWriter sw = new StringBuilderWriter();
    PrintWriter pw = new PrintWriter(sw);
    t.print(pw);
    pw.flush();
    String bytecode = sw.getBuilder().toString();
    logger.error(String.format("Failure while rendering method %s, %s, %s.  ByteCode:\n %s", name, desc, signature, bytecode), e);
    throw new RuntimeException(String.format("Failure while rendering method %s, %s, %s.  ByteCode:\n %s", name, desc, signature, bytecode), e);
  }
}
 
Example #2
Source File: ASMHelper.java    From CodeChickenLib with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static void dump(Acceptor acceptor, File file, boolean filterImportant, boolean sortLocals, boolean textify) {
    try {
        if(!file.getParentFile().exists())
            file.getParentFile().mkdirs();
        if(!file.exists())
            file.createNewFile();

        PrintWriter pout = new PrintWriter(file);
        ClassVisitor cv = new TraceClassVisitor(null, textify ? new Textifier() : new ASMifier(), pout);
        if(filterImportant) cv = new ImportantInsnVisitor(cv);
        if(sortLocals) cv = new LocalVariablesSorterVisitor(cv);
        acceptor.accept(cv);
        pout.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
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: 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 #5
Source File: BytecodePrettyPrinter.java    From Concurnas with MIT License 5 votes vote down vote up
@Override
protected Textifier createTextifier() {
	if(null ==  nextTracker){
		return new Textifier();
	}
	return nextTracker;
}
 
Example #6
Source File: JarDumper.java    From buck with Apache License 2.0 5 votes vote down vote up
private Stream<String> dumpClassFile(InputStream stream) throws IOException {
  byte[] textifiedClass;
  try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
      PrintWriter pw = new PrintWriter(bos)) { // NOPMD required by API
    ClassReader reader = new ClassReader(stream);
    TraceClassVisitor traceVisitor = new TraceClassVisitor(null, new Textifier(), pw);
    reader.accept(traceVisitor, asmFlags);
    textifiedClass = bos.toByteArray();
  }

  try (InputStreamReader streamReader =
      new InputStreamReader(new ByteArrayInputStream(textifiedClass))) {
    return CharStreams.readLines(streamReader).stream();
  }
}
 
Example #7
Source File: InsnListSection.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String toString() {
    Textifier t = new Textifier();
    accept(new TraceMethodVisitor(t));
    StringWriter sw = new StringWriter();
    t.print(new PrintWriter(sw));
    return sw.toString();
}
 
Example #8
Source File: ASMBytecodeDisassembler.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
public String dumpBytecode(final byte[] bytecode) {
    if (bytecode == null) {
        throw new NullPointerException("bytecode");
    }

    return writeBytecode(bytecode, new Textifier());
}
 
Example #9
Source File: InfiniteLoopBaseTest.java    From pitest with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example #10
Source File: ClassTree.java    From pitest with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
  final StringWriter writer = new StringWriter();
  this.rawNode.accept(new TraceClassVisitor(null, new Textifier(), new PrintWriter(
      writer)));
  return writer.toString();

}
 
Example #11
Source File: MutantExportInterceptor.java    From pitest with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #12
Source File: BytecodePrettyPrinter.java    From Concurnas with MIT License 5 votes vote down vote up
@Override
  public Textifier visitMethod(final int access, final String name, final String desc, final String signature, final String[] exceptions) {
nextTracker = new TextifierLabelTrack();
alllabelMaps.put(name + desc,  nextTracker.getLabelNames());

  	return super.visitMethod(access, name,   desc,   signature,  exceptions);
  }
 
Example #13
Source File: ByteCodeUtils.java    From Stark with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the given method to a String.
 */
public static String textify(@NonNull MethodNode method) {
    Textifier textifier = new Textifier();
    TraceMethodVisitor trace = new TraceMethodVisitor(textifier);
    method.accept(trace);
    String ret = "";
    for (Object line : textifier.getText()) {
        ret += line;
    }
    return ret;
}
 
Example #14
Source File: AsmUtils.java    From turbine with Apache License 2.0 5 votes vote down vote up
public static String textify(byte[] bytes, boolean skipDebug) {
  Printer textifier = new Textifier();
  StringWriter sw = new StringWriter();
  new ClassReader(bytes)
      .accept(
          new TraceClassVisitor(null, textifier, new PrintWriter(sw, true)),
          ClassReader.SKIP_FRAMES
              | ClassReader.SKIP_CODE
              | (skipDebug ? ClassReader.SKIP_DEBUG : 0));
  return sw.toString();
}
 
Example #15
Source File: ClassVisitorExample.java    From grappa with Apache License 2.0 4 votes vote down vote up
@Override
public MethodVisitor visitMethod(final int access, final String name,
    final String desc, final String signature, final String[] exceptions)
{
    // Unused?
    /*
    final MethodVisitor mv = super.visitMethod(access, name, desc,
        signature, exceptions);
    */

    return new MethodNode(Opcodes.ASM5, access, name, desc, signature,
        exceptions)
    {
        @Override
        public void visitEnd()
        {
            super.visitEnd();

            try {
                final BasicInterpreter basicInterpreter
                    = new BasicInterpreter();
                final Analyzer<BasicValue> analyzer
                    = new Analyzer<>(basicInterpreter);
                final AbstractInsnNode[] nodes = instructions.toArray();
                final Frame<BasicValue>[] frames
                    = analyzer.analyze(className, this);
                int areturn = -1;
                for (int i = nodes.length -1; i >= 0; i--)
                {
                    if (nodes[i].getOpcode() == Opcodes.ARETURN) {
                        areturn = i;
                        System.out.println(className + "." + name + desc);
                        System.out.println("Found areturn at: " + i);
                    } else if (areturn != -1
                        && nodes[i].getOpcode() != -1
                        && frames[i].getStackSize() == 0) {
                        System.out.println("Found start of block at: " + i);

                        final InsnList list = new InsnList();
                        for (int j = i; j <= areturn; j++)
                            list.add(nodes[j]);
                        final Textifier textifier = new Textifier();
                        final PrintWriter pw = new PrintWriter(System.out);
                        list.accept(new TraceMethodVisitor(textifier));
                        textifier.print(pw);
                        pw.flush();
                        System.out.println("\n\n");
                        areturn = -1;
                    }
                }
            }
            catch (AnalyzerException e) {
                e.printStackTrace();
            }

            if (mv != null)
                accept(mv);
        }
    };
}
 
Example #16
Source File: SimpleTypeTextifier.java    From es6draft with MIT License 4 votes vote down vote up
@Override
protected Textifier createTextifier() {
    return new SimpleTypeTextifier();
}
 
Example #17
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 #18
Source File: DebuggingWrapper.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
public static AsmVisitorWrapper makeDefault(boolean check) {
    return new DebuggingWrapper(System.out, new Textifier(), check);
}
 
Example #19
Source File: ASMTextifierDecompiler.java    From bytecode-viewer with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String decompileClassNode(ClassNode cn, byte[] b) {
	StringWriter writer = new StringWriter();
	cn.accept(new TraceClassVisitor(null, new Textifier(), new PrintWriter(writer)));
	return writer.toString();
}
 
Example #20
Source File: AbstractJavacTurbineCompilationTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
static String textify(byte[] bytes) {
  StringWriter sw = new StringWriter();
  ClassReader cr = new ClassReader(bytes);
  cr.accept(new TraceClassVisitor(null, new Textifier(), new PrintWriter(sw, true)), 0);
  return sw.toString();
}
 
Example #21
Source File: JavacTurbineTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
static String textify(byte[] bytes) {
  StringWriter sw = new StringWriter();
  ClassReader cr = new ClassReader(bytes);
  cr.accept(new TraceClassVisitor(null, new Textifier(), new PrintWriter(sw, true)), 0);
  return sw.toString();
}
 
Example #22
Source File: BytecodeTraceUtil.java    From tascalate-async-await with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static String toString(MethodNode mn) {
     Textifier t = new Textifier();
     TraceMethodVisitor tmv = new TraceMethodVisitor(t);
     mn.accept(tmv);
     return t.toString();
}
 
Example #23
Source File: LoggableTextifier.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
protected Textifier createTextifier() {
    return new LoggableTextifier();
}
 
Example #24
Source File: LoggableTextifier.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public Textifier visitClassAnnotation(String desc, boolean visible) {
    Textifier t = super.visitClassAnnotation(desc, visible);
    log();
    return t;
}
 
Example #25
Source File: LoggableTextifier.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public Textifier visitAnnotation(String name, String desc) {
    Textifier t = super.visitAnnotation(name, desc);
    log();
    return t;
}
 
Example #26
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);
        }
    }
}
 
Example #27
Source File: LoggableTextifier.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public Textifier visitField(int access, String name, String desc, String signature, Object value) {
    Textifier t = super.visitField(access, name, desc, signature, value);
    log();
    return t;
}
 
Example #28
Source File: LoggableTextifier.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public Textifier visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    Textifier t = super.visitMethod(access, name, desc, signature, exceptions);
    log();
    return t;
}
 
Example #29
Source File: ExtraTextifier.java    From TickDynamic with MIT License 4 votes vote down vote up
@Override
protected Textifier createTextifier() {
    return new ExtraTextifier();
}
 
Example #30
Source File: LoggableTextifier.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public Textifier visitAnnotableParameterCount(int parameterCount, boolean visible) {
    Textifier t = super.visitAnnotableParameterCount(parameterCount, visible);
    log();
    return t;
}