📄 methodinfo.java
字号:
case opc_invokeinterface_fast: case opc_new_fast: case opc_multianewarray_fast: case opc_checkcast_fast: case opc_instanceof_fast: case opc_ldc_w: case opc_ldc2_w: case opc_instanceof: case opc_checkcast: case opc_new: case opc_putstatic: case opc_getstatic: case opc_putfield: case opc_getfield: case opc_invokevirtual: case opc_invokespecial: case opc_invokestatic: { int index = shortAt(codeBytes, offset+1); output.print(" #" + index + " "); offset += 3; break; } case opc_getfield_fast: case opc_getfieldp_fast: case opc_getfield2_fast: case opc_putfield_fast: case opc_putfield2_fast: { int index = at(codeBytes, offset+1); output.print(" [" + index + "] "); offset += 3; break; } case opc_jsr: case opc_goto: case opc_ifeq: case opc_ifge: case opc_ifgt: case opc_ifle: case opc_iflt: case opc_ifne: case opc_if_icmpeq: case opc_if_icmpne: case opc_if_icmpge: case opc_if_icmpgt: case opc_if_icmple: case opc_if_icmplt: case opc_if_acmpeq: case opc_if_acmpne: case opc_ifnull: case opc_ifnonnull: { int target = offset + (short) shortAt(codeBytes,offset+1); output.print(" " + target); offset += 3; break; } default: offset += opcodeLength(codeBytes, offset); break; } } output.close(); return sw.toString(); } public static String opcodeName (int opcode) { return opcNames[opcode]; } public String toString(){ String r = "Method: "+super.toString(); if ( code != null ){ r += " {"+code.length+" bytes of code}"; } return r; } // Case 1: expand code. // Convert ldc to ldc2: a. Insert extra bytes // b. Fix all branch targets/exception ranges // Case 2: smash code // ldc_w_fast has index which is less than 255. Change to use // ldc_w. public void relocateAndPackCode (ConstantObject co[]) { if (code == null) return; int opcode, adjustment = 0; int newOffsets[] = new int[code.length]; int indexByPC[] = new int[code.length]; // First figure out where we'll have to insert extra bytes in // order to fit opc_ldc_w instead of opc_ldc instructions. for (int pc = 0, pcindex = 0; pc < code.length; pc = pc + opcodeLength(pc), pcindex++) { opcode = (int) code[pc]&0xFF; newOffsets[pcindex] = pc + adjustment; indexByPC[pc] = pcindex; /******* * switch (opcode) { * case opc_ldc: { * // a conversion table which maps pcValue to new index. * int oldindex = (int)(code[pc+1] & 0xFF); * int index = co[oldindex].index; * * if (index >= 0x100) * adjustment++; * break; * } * * * case opc_ldc_w: { * // a conversion table which maps pcValue to new index. * int oldindex = (int)(((code[pc+1]&0xFF) << 8) * | (code[pc+2]&0xFF)); * int index = co[oldindex].index; * * if (index < 0x100) * adjustment--; * break; * } * * * * case opc_goto: { * // Calculate the displacement, sign extend high byte * int displ = (code[pc+1] << 8) | (code[pc+2] & 0xFF); * * if (displ == 3) { * adjustment -= 3; // remove no-use goto's. * } * break; * } * * case opc_nop: { * adjustment--; // remove * break; * } * * * case opc_tableswitch: * case opc_lookupswitch: { * int oldExtraPC = (( pc + 4 ) & ~3); * int newExtraPC = (( pc + adjustment + 4) & ~3); * adjustment = newExtraPC - oldExtraPC; * break; * } * } *******/ } // Now copy the code to the new location. At the same // time, we adjust all branch targets. byte newCode[] = new byte[code.length + adjustment]; for (int pc = 0, pcindex = 0; pc < code.length; pc = pc + opcodeLength(pc), pcindex++) { int outPos = newOffsets[pcindex]; int inPos = pc; opcode = (int) code[pc]&0xFF; for (int i = 0; i < opcodeLength(pc); i++) { newCode[outPos + i] = code[pc + i]; } switch (opcode) { case opc_ldc: { int oldindex = (int)(code[pc+1] & 0xFF); int index = co[oldindex].index; if (index >= 0x100) { new RuntimeException("load constant overflow"); } newCode[outPos +1] = (byte) index ; break; } /**** * // Remapping branches * case opc_ifeq: * case opc_ifne: * case opc_iflt: * case opc_ifge: * case opc_ifgt: * case opc_ifle: * case opc_if_icmpeq: * case opc_if_icmpne: * case opc_if_icmplt: * case opc_if_icmpge: * case opc_if_icmpgt: * case opc_if_icmple: * case opc_if_acmpeq: * case opc_if_acmpne: * case opc_ifnull: * case opc_ifnonnull: * case opc_goto: * case opc_jsr: { * // Calculate the displacement, sign extend high byte * int displ = (code[pc+1] << 8) | (code[pc+2] & 0xFF); * * * if (displ == 3 && opcode == opc_goto) { * * break; * * } * * int branchDest = pc + displ; * * if ((code[branchDest] & 0xFF) == opc_goto) { * // We're branching to a goto. We can just branch to * // where the goto was going. * branchDest += * (code[branchDest+1] << 8) | (code[branchDest+2] & 0xFF); * } * int newDest = newOffsets[indexByPC[branchDest]] - outPos; * newCode[outPos+1] = (byte) ((newDest >> 8) & 0xFF); * newCode[outPos+2] = (byte) (newDest & 0xFF); * break; * } * * case opc_tableswitch: { * newCode[outPos] = code[pc]; * outPos = (outPos + 4) & ~3; * inPos = (inPos + 4) & ~3; * * // Update the default destination * int oldDest = getInt(inPos) + pc; * int newDest = newOffsets[indexByPC[oldDest]] * - newOffsets[pcindex]; * putInt(newCode, outPos, newDest); * * // Update each of the destinations in the table * int low = getInt(inPos+4); * int high = getInt(inPos+8); * putInt(newCode, outPos+4, low); * putInt(newCode, outPos+8, high); * for (int j = 0; j <= high-low; j++) { * int offset = j * 4 + 12; * oldDest = getInt(inPos + offset) + pc; * newDest = newOffsets[indexByPC[oldDest]] * - newOffsets[pcindex]; * putInt(newCode, outPos + offset, newDest); * } * break; * } * * case opc_lookupswitch: { * newCode[outPos] = code[pc]; * // 0-3 byte pads * outPos = (outPos + 4) & ~3; * inPos = (inPos + 4) & ~3; * * // Update the default destination * int oldDest = getInt(inPos) + pc; * int newDest = newOffsets[indexByPC[oldDest]] * - newOffsets[pcindex]; * putInt(newCode, outPos, newDest); * * // Update each of the pairs of destinations in the list * int pairs = getInt(inPos+4); * putInt(newCode, outPos+4, pairs); * for (int j = 0; j < pairs; j++) { * int offset = (j + 1) * 8; * * // First copy the value * putInt(newCode, outPos + offset, * getInt(inPos + offset)); * offset += 4; * * // Now adjust the destination * oldDest = getInt(inPos + offset) + pc; * newDest = newOffsets[indexByPC[oldDest]] * - newOffsets[pcindex]; * putInt(newCode, outPos + offset, newDest); * } * break; * } ********/ // Byte-codes with constant pool access. Remap to new indices case opc_getfield: case opc_checkcast: case opc_getstatic: case opc_instanceof: case opc_ldc2_w: case opc_new: case opc_putfield: case opc_putstatic: case opc_invokevirtual: case opc_invokestatic: case opc_invokespecial: case opc_getstatic_fast: case opc_getstaticp_fast: case opc_getstatic2_fast: case opc_putstatic_fast: case opc_putstatic2_fast: case opc_invokevirtual_fast: case opc_invokespecial_fast: case opc_invokestatic_fast: case opc_anewarray_fast: case opc_checkcast_fast: case opc_instanceof_fast: case opc_multianewarray: case opc_multianewarray_fast: case opc_invokeinterface: case opc_invokeinterface_fast: case opc_ldc_w: { int oldindex = (int)(((code[pc+1]&0xFF) << 8) | (code[pc+2]&0xFF)); int index = co[oldindex].index; newCode[outPos] = (byte) opcode; newCode[outPos+1] = (byte) ((index >> 8) & 0xFF); newCode[outPos+2] = (byte) (index & 0xFF); break; } } } // Update the exception table for (int i = 0; i < exceptionTable.length; i++) { ExceptionEntry e = exceptionTable[i]; e.startPC = newOffsets[indexByPC[e.startPC]]; e.endPC = newOffsets[indexByPC[e.endPC]]; e.handlerPC = newOffsets[indexByPC[e.handlerPC]]; } // Update the line number table LineNumberTableEntry[] lntab = getLineNumberTable(); if (lntab != null) { for (int i = 0; i < lntab.length; i++) { LineNumberTableEntry e = lntab[i]; e.startPC = newOffsets[indexByPC[e.startPC]]; } } // Update the line number table LocalVariableTableEntry[] locvartab = getLocalVariableTable(); if (locvartab != null) { for (int i = 0; i < locvartab.length; i++) { LocalVariableTableEntry e = locvartab[i]; e.pc0 = newOffsets[indexByPC[e.pc0]]; } } // Update the stack maps if (stackMapTable != null) { for (int i = 0; i < stackMapTable.length; i++) { StackMapFrame frame = stackMapTable[i]; frame.offset = newOffsets[indexByPC[frame.offset]]; } } // make the changes permanent code = newCode; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -