下面列出了怎么用org.objectweb.asm.util.Printer的API类实例代码及写法,或者点击链接到github查看源代码。
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]);
}
}
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]);
}
}
@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());
}
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());
}
}
}
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());
}
}
}
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();
}
}
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';
}
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;
}
@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);
}
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);
}
}
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]);
}
}
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]);
}
}
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]);
}
}
public static Type getCastType(int opcode) {
switch (opcode) {
case I2B:
return Type.BYTE_TYPE;
case I2C:
return Type.CHAR_TYPE;
case I2S:
return Type.SHORT_TYPE;
case L2I:
case F2I:
case D2I:
return Type.INT_TYPE;
case I2L:
case F2L:
case D2L:
return Type.LONG_TYPE;
case I2F:
case L2F:
case D2F:
return Type.FLOAT_TYPE;
case I2D:
case L2D:
case F2D:
return Type.DOUBLE_TYPE;
default:
throw new IllegalArgumentException(Printer.OPCODES[opcode]);
}
}
public static ValueComparisonType resolve(int opcode) {
if(opcode == LCMP) {
return CMP;
} else if(opcode == FCMPG || opcode == DCMPG) {
return ValueComparisonType.GT;
} else if(opcode == FCMPL || opcode == DCMPL) {
return ValueComparisonType.LT;
} else {
throw new UnsupportedOperationException(Printer.OPCODES[opcode]);
}
}
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]);
}
}
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]);
}
}
@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();
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
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();
}
@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;
}
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();
}
}
@Override
public String toString()
{
return instruction.getOpcode() != -1
? Printer.OPCODES[instruction.getOpcode()]
: super.toString();
}
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);
}
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);
}