org.objectweb.asm.util.Printer Java Examples

The following examples show how to use org.objectweb.asm.util.Printer. 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: MethodASMifier.java    From CodeChickenCore with MIT License 6 votes vote down vote up
public static void printMethod(ObfMapping method, byte[] bytes, Printer printer, File toFile) {
    try {
        if (!toFile.getParentFile().exists())
            toFile.getParentFile().mkdirs();
        if (!toFile.exists())
            toFile.createNewFile();

        PrintWriter printWriter = new PrintWriter(toFile);

        ClassVisitor cv = new MethodASMifier(method, printer, printWriter);
        ClassReader cr = new ClassReader(bytes);
        cr.accept(cv, 0);

        printWriter.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #3
Source File: TypeUtils.java    From maple-ir with GNU General Public License v3.0 6 votes vote down vote up
public static Type getPrimitiveArrayType(int opcode) {
	switch (opcode) {
		case T_BOOLEAN:
			return Type.getType("[Z");
		case T_BYTE:
			return Type.getType("[B");
		case T_SHORT:
			return Type.getType("[S");
		case T_CHAR:
			return Type.getType("[C");
		case T_INT:
			return Type.getType("[I");
		case T_LONG:
			return Type.getType("[J");
		case T_FLOAT:
			return Type.getType("[F");
		case T_DOUBLE:
			return Type.getType("[D");
		default:
			throw new IllegalArgumentException(Printer.OPCODES[opcode]);
	}
}
 
Example #4
Source File: ConditionalJumpStmt.java    From maple-ir with GNU General Public License v3.0 6 votes vote down vote up
public static ComparisonType getType(int opcode) {
	switch (opcode) {
		case Opcodes.IF_ACMPEQ:
		case Opcodes.IF_ICMPEQ:
		case Opcodes.IFEQ:
			return EQ;
		case Opcodes.IF_ACMPNE:
		case Opcodes.IF_ICMPNE:
		case Opcodes.IFNE:
			return NE;
		case Opcodes.IF_ICMPGT:
		case Opcodes.IFGT:
			return GT;
		case Opcodes.IF_ICMPGE:
		case Opcodes.IFGE:
			return GE;
		case Opcodes.IF_ICMPLT:
		case Opcodes.IFLT:
			return LT;
		case Opcodes.IF_ICMPLE:
		case Opcodes.IFLE:
			return LE;
		default:
			throw new IllegalArgumentException(Printer.OPCODES[opcode]);
	}
}
 
Example #5
Source File: ExtraTextifier.java    From TickDynamic with MIT License 6 votes vote down vote up
@Override
public void visitLdcInsn(final Object cst) {
    buf.setLength(0);
    buf.append(tab2).append("LDC ");
    if (cst instanceof String) {
        Printer.appendString(buf, (String) cst);
    } else if(cst instanceof Float) {
    	buf.append(cst).append("F");
    } else if(cst instanceof Double) {
    	buf.append(cst).append("D");
    } else if(cst instanceof Long) {
    	buf.append(cst).append("L");
    } else if (cst instanceof Type) {
        buf.append(((Type) cst).getDescriptor()).append(".class");
    } else {
        buf.append(cst);
    }
    buf.append('\n');
    text.add(buf.toString());
}
 
Example #6
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 #7
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 #8
Source File: SAXCodeAdapter.java    From JByteMod-Beta with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final void visitFieldInsn(final int opcode, final String owner, final String name, final String desc) {
  AttributesImpl attrs = new AttributesImpl();
  attrs.addAttribute("", "owner", "owner", "", owner);
  attrs.addAttribute("", "name", "name", "", name);
  attrs.addAttribute("", "desc", "desc", "", desc);
  sa.addElement(Printer.OPCODES[opcode], attrs);
}
 
Example #9
Source File: SAXCodeAdapter.java    From JByteMod-Beta with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final void visitMultiANewArrayInsn(final String desc, final int dims) {
  AttributesImpl attrs = new AttributesImpl();
  attrs.addAttribute("", "desc", "desc", "", desc);
  attrs.addAttribute("", "dims", "dims", "", Integer.toString(dims));
  sa.addElement(Printer.OPCODES[Opcodes.MULTIANEWARRAY], attrs);
}
 
Example #10
Source File: SAXCodeAdapter.java    From JByteMod-Beta with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final void visitMethodInsn(final int opcode, final String owner, final String name, final String desc, final boolean itf) {
  AttributesImpl attrs = new AttributesImpl();
  attrs.addAttribute("", "owner", "owner", "", owner);
  attrs.addAttribute("", "name", "name", "", name);
  attrs.addAttribute("", "desc", "desc", "", desc);
  attrs.addAttribute("", "itf", "itf", "", itf ? "true" : "false");
  sa.addElement(Printer.OPCODES[opcode], attrs);
}
 
Example #11
Source File: SAXCodeAdapter.java    From JByteMod-Beta with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final void visitIincInsn(final int var, final int increment) {
  AttributesImpl attrs = new AttributesImpl();
  attrs.addAttribute("", "var", "var", "", Integer.toString(var));
  attrs.addAttribute("", "inc", "inc", "", Integer.toString(increment));
  sa.addElement(Printer.OPCODES[Opcodes.IINC], attrs);
}
 
Example #12
Source File: SAXCodeAdapter.java    From JByteMod-Beta with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final void visitTableSwitchInsn(final int min, final int max, final Label dflt, final Label... labels) {
  AttributesImpl attrs = new AttributesImpl();
  attrs.addAttribute("", "min", "min", "", Integer.toString(min));
  attrs.addAttribute("", "max", "max", "", Integer.toString(max));
  attrs.addAttribute("", "dflt", "dflt", "", getLabel(dflt));
  String o = Printer.OPCODES[Opcodes.TABLESWITCH];
  sa.addStart(o, attrs);
  for (int i = 0; i < labels.length; i++) {
    AttributesImpl att2 = new AttributesImpl();
    att2.addAttribute("", "name", "name", "", getLabel(labels[i]));
    sa.addElement("label", att2);
  }
  sa.addEnd(o);
}
 
Example #13
Source File: SAXCodeAdapter.java    From JByteMod-Beta with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final void visitLookupSwitchInsn(final Label dflt, final int[] keys, final Label[] labels) {
  AttributesImpl att = new AttributesImpl();
  att.addAttribute("", "dflt", "dflt", "", getLabel(dflt));
  String o = Printer.OPCODES[Opcodes.LOOKUPSWITCH];
  sa.addStart(o, att);
  for (int i = 0; i < labels.length; i++) {
    AttributesImpl att2 = new AttributesImpl();
    att2.addAttribute("", "name", "name", "", getLabel(labels[i]));
    att2.addAttribute("", "key", "key", "", Integer.toString(keys[i]));
    sa.addElement("label", att2);
  }
  sa.addEnd(o);
}
 
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: MethodASMifier.java    From CodeChickenCore with MIT License 5 votes vote down vote up
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (method.matches(name, desc)) {
        Printer localPrinter = asmifier.visitMethod(access, name, desc, signature, exceptions);
        return new TraceMethodVisitor(null, localPrinter);
    }

    return null;
}
 
Example #16
Source File: MethodASMifier.java    From CodeChickenCore with MIT License 5 votes vote down vote up
public static void printMethod(ObfMapping method, Printer printer, File toFile) {
    try {
        printMethod(method, Launch.classLoader.getClassBytes(method.javaClass()), printer, toFile);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #17
Source File: InstructionGraphNode.java    From grappa with Apache License 2.0 5 votes vote down vote up
@Override
public String toString()
{
    return instruction.getOpcode() != -1
        ? Printer.OPCODES[instruction.getOpcode()]
        : super.toString();
}
 
Example #18
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 #19
Source File: ASMBytecodeDisassembler.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private void accept(byte[] bytecode, Printer printer, PrintWriter writer) {

        final ClassReader cr = new ClassReader(bytecode);
        final ClassWriter cw = new ClassWriter(this.cwFlag);
        final TraceClassVisitor tcv = new TraceClassVisitor(cw, printer, writer);
        cr.accept(tcv, this.crFlag);
    }
 
Example #20
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 #21
Source File: Code.java    From es6draft with MIT License 5 votes vote down vote up
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (methodName.equals(name) && methodType.toMethodDescriptorString().equals(desc)) {
        Printer p = printer.visitMethod(access, name, desc, signature, exceptions);
        return new TraceMethodVisitor(null, p);
    }
    return null;
}
 
Example #22
Source File: ArithmeticExpr.java    From maple-ir with GNU General Public License v3.0 5 votes vote down vote up
public static Operator resolve(int bOpcode) {
	if(bOpcode >= IADD && bOpcode <= DREM){
		return values()[(int)Math.floor((bOpcode - IADD) / 4) + Operator.ADD.ordinal()];
	} else if(bOpcode >= ISHL && bOpcode <= LUSHR) {
		return values()[(int)Math.floor((bOpcode - ISHL) / 2) + Operator.SHL.ordinal()];
	} else if(bOpcode == IAND || bOpcode == LAND) {
		return Operator.AND;
	} else if(bOpcode == IOR || bOpcode == LOR) {
		return Operator.OR;
	} else if(bOpcode == IXOR || bOpcode == LXOR) {
		return Operator.XOR;
	} else {
		throw new UnsupportedOperationException(Printer.OPCODES[bOpcode]);
	}
}
 
Example #23
Source File: GenerationVerifier.java    From maple-ir with GNU General Public License v3.0 5 votes vote down vote up
List<VerifierRule> find_verify_matches() {
	throwNoContext();
	
	int op = currentContext.insn.getOpcode();
	ExpressionStack stack = currentContext.stack;
	
	List<VerifierRule> rules = vrules.get(op);
	
	if(rules == null) {
		System.err.println("Cannot verify " + Printer.OPCODES[op] + " (no rules).  Stack: " + stack);
		return null;
	}
	
	List<VerifierRule> possible = new ArrayList<>();
	
	for(VerifierRule r : rules) {
		if(r.match_attempt(stack, true)) {
			possible.add(r);
		}
	}
	
	if(possible.isEmpty() && !rules.isEmpty()) {
		throw new VerifyException(ExceptionStage.PRE, currentContext);
	}
	
	return possible;
}
 
Example #24
Source File: ComparisonExpr.java    From maple-ir with GNU General Public License v3.0 5 votes vote down vote up
public static Type resolveType(int opcode) {
	if(opcode == LCMP) {
		return Type.LONG_TYPE;
	} else if(opcode == FCMPG || opcode == FCMPL) {
		return Type.FLOAT_TYPE;
	} else if(opcode == DCMPG || opcode == DCMPL) {
		return Type.DOUBLE_TYPE;
	} else {
		throw new UnsupportedOperationException(Printer.OPCODES[opcode]);
	}
}
 
Example #25
Source File: BasicBlock.java    From AVM with MIT License 5 votes vote down vote up
@Override
public String toString() {
    final var builder = new StringBuilder("BasicBlock{");
    opcodeSequence.stream().map(i -> Printer.OPCODES[i]).forEach(s -> builder.append(s).append('\n'));
    builder.append('}');
    return builder.toString();
}
 
Example #26
Source File: GenerationVerifier.java    From maple-ir with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String toString() {
	StringBuilder msglog = new StringBuilder();
	for(GenerationEvent e : events) {
		msglog.append(e).append(System.lineSeparator());
	}
	return String.format("%s, b: #%s, stack: %s%n  Eventlog(%d):%s", Printer.OPCODES[insn.getOpcode()].toLowerCase(), block.getDisplayName(), stack, events.size(), msglog);
}
 
Example #27
Source File: GenerationPass.java    From maple-ir with GNU General Public License v3.0 5 votes vote down vote up
protected void _load_field(int opcode, String owner, String name, String desc) {
	save_stack(false);
	if(opcode == GETFIELD || opcode == GETSTATIC) {
		Expr inst = null;
		if(opcode == GETFIELD) {
			inst = pop();
		}
		FieldLoadExpr fExpr = new FieldLoadExpr(inst, owner, name, desc, opcode == GETSTATIC);
		int index = currentStack.height();
		Type type = assign_stack(index, fExpr);
		push(load_stack(index, type));
	} else {
		throw new UnsupportedOperationException(Printer.OPCODES[opcode] + " " + owner + "." + name + "   " + desc);
	}
}
 
Example #28
Source File: TypeUtils.java    From maple-ir with GNU General Public License v3.0 5 votes vote down vote up
public static ArrayType resolve(int opcode) {
	if(opcode >= IALOAD && opcode <= SALOAD) {
		return values()[opcode - IALOAD];
	} else if(opcode >= IASTORE && opcode <= SASTORE) {
		return values()[opcode - IASTORE];
	} else {
		throw new UnsupportedOperationException(Printer.OPCODES[opcode]);
	}
}
 
Example #29
Source File: TypeUtils.java    From maple-ir with GNU General Public License v3.0 5 votes vote down vote up
public static Type getLoadType(int opcode) {
	if (opcode == ILOAD) {
		return Type.INT_TYPE;
	} else if (opcode == LLOAD) {
		return Type.LONG_TYPE;
	} else if (opcode == FLOAD) {
		return Type.FLOAT_TYPE;
	} else if (opcode == DLOAD) {
		return Type.DOUBLE_TYPE;
	} else if (opcode == ALOAD) {
		return OBJECT_TYPE;
	} else {
		throw new IllegalArgumentException(Printer.OPCODES[opcode]);
	}
}
 
Example #30
Source File: TypeUtils.java    From maple-ir with GNU General Public License v3.0 5 votes vote down vote up
public static Type getStoreType(int opcode) {
	if (opcode == ISTORE) {
		return Type.INT_TYPE;
	} else if (opcode == LSTORE) {
		return Type.LONG_TYPE;
	} else if (opcode == FSTORE) {
		return Type.FLOAT_TYPE;
	} else if (opcode == DSTORE) {
		return Type.DOUBLE_TYPE;
	} else if (opcode == ASTORE) {
		return OBJECT_TYPE;
	} else {
		throw new IllegalArgumentException(Printer.OPCODES[opcode]);
	}
}