ByteCode { public static void printOpCode(Instruction ins, ConstantPoolGen cpg) { if (ins instanceof InvokeInstruction) { InvokeInstruction invokeIns = (InvokeInstruction) ins; System.out.println(formatName(ins) + " " + invokeIns.getClassName(cpg).replaceAll("\\.", "/") + "." + invokeIns.getMethodName(cpg) + invokeIns.getSignature(cpg)); } else if (ins instanceof LDC) { LDC i = (LDC) ins; System.out.println(formatName(ins) + " \""+i.getValue(cpg).toString()+"\""); } else if (ins instanceof NEW) { NEW i = (NEW) ins; ObjectType type = i.getLoadClassType(cpg); System.out.println(formatName(ins) + " " + type.toString()); } else if (ins instanceof LoadInstruction) { LoadInstruction i = (LoadInstruction) ins; System.out.println(formatName(ins) +" "+i.getIndex() + " => [stack]"); } else if (ins instanceof StoreInstruction) { StoreInstruction i = (StoreInstruction) ins; System.out.println(formatName(ins) +" (objectref) => "+i.getIndex() + ""); } else if (ins instanceof FieldInstruction) { FieldInstruction i = (FieldInstruction) ins; System.out.println(formatName(ins) +" "+i.getFieldName(cpg) + ""); } else if (ins instanceof IfInstruction) { IfInstruction i = (IfInstruction) ins; System.out.println(formatName(ins) +" target => "+i.getTarget().toString()+ ""); } else if (ins instanceof ICONST) { ICONST i = (ICONST) ins; System.out.println(formatName(ins) +" "+i.getValue()+" ("+i.getType(cpg)+")"); } else if (ins instanceof GOTO) { GOTO i = (GOTO) ins; System.out.println(formatName(ins) +" target => "+i.getTarget().toString()); } else { System.out.println(formatName(ins)); } }  static void printOpCode(Instruction ins, ConstantPoolGen cpg); static T getConstantLDC(InstructionHandle h, ConstantPoolGen cpg, Class<T> clazz); static Integer getConstantInt(InstructionHandle h); static Number getPushNumber(InstructionHandle h); static T getPrevInstruction(InstructionHandle startHandle, Class<T> clazz);  }
@Test public void probeByteCodeDebug() { PrintStream sysOut = mock(PrintStream.class); System.out.println("Sysout hijack!"); PrintStream oldPrintStream = System.out; try { System.setOut(sysOut); InvokeInstruction ins = mock(InvokeInstruction.class); ConstantPoolGen cpg = mock(ConstantPoolGen.class); when(ins.getClassName(Matchers.<ConstantPoolGen>any())).thenReturn("ClassTest"); when(ins.getMethodName(Matchers.<ConstantPoolGen>any())).thenReturn("method"); when(ins.getSignature(Matchers.<ConstantPoolGen>any())).thenReturn("(Lsignature)Lblah"); ByteCode.printOpCode(ins, cpg); verify(sysOut, atLeastOnce()).println(contains("ClassTest.method")); } finally { System.setOut(oldPrintStream); System.out.println("Sysout is back!"); } }