解析常量池信息时需要先解析出常量池对象的数量,然后遍历常量池,解析cp_info对象。

    1. u2 constant_pool_count;
    2. cp_info constant_pool[constant_pool_count-1];

    为了便于理解解析过程,特意将常量池解析流程单独拆开成如下几步:

    1. 读取常量池数量(u2 constant_pool_count;);
    2. 读取tag
    3. 根据不同的tag类型解析常量池对象;
    4. 解析常量池中的对象;
    5. 链接常量池中的索引引用;

    常量池解析片段:

    1. /**
    2. * 解析常量池数据
    3. *
    4. * @throws IOException 数据读取异常
    5. */
    6. private void parseConstantPool() throws IOException {
    7. // u2 constant_pool_count;
    8. this.poolCount = dis.readUnsignedShort();
    9. // cp_info constant_pool[constant_pool_count-1];
    10. for (int i = 1; i <= poolCount - 1; i++) {
    11. // cp_info {
    12. // u1 tag;
    13. // u1 info[];
    14. // }
    15. int tag = dis.readUnsignedByte();
    16. Constant constant = Constant.getConstant(tag);
    17. if (constant == null) {
    18. throw new RuntimeException("解析常量池异常,无法识别的常量池类型:" + tag);
    19. }
    20. // 解析常量池对象
    21. parseConstantItems(constant, i);
    22. // LongDouble是宽类型,占两位
    23. if (CONSTANT_LONG == constant || CONSTANT_DOUBLE == constant) {
    24. i++;
    25. }
    26. }
    27. // 链接常量池中的引用
    28. linkConstantPool();
    29. }

    解析常量池对象代码片段:

    1. /**
    2. * 解析常量池中的对象
    3. *
    4. * @param constant 常量池
    5. * @param index 常量池中的索引位置
    6. * @throws IOException 数据读取异常
    7. */
    8. private void parseConstantItems(Constant constant, int index) throws IOException {
    9. Map<String, Object> map = new LinkedHashMap<>();
    10. switch (constant) {
    11. case CONSTANT_UTF8:
    12. // CONSTANT_Utf8_info {
    13. // u1 tag;
    14. // u2 length;
    15. // u1 bytes[length];
    16. // }
    17. int length = dis.readUnsignedShort();
    18. byte[] bytes = new byte[length];
    19. dis.read(bytes);
    20. map.put("tag", CONSTANT_UTF8);
    21. map.put("value", new String(bytes, UTF_8));
    22. break;
    23. case CONSTANT_INTEGER:
    24. // CONSTANT_Integer_info {
    25. // u1 tag;
    26. // u4 bytes;
    27. // }
    28. map.put("tag", CONSTANT_INTEGER);
    29. map.put("value", dis.readInt());
    30. break;
    31. case CONSTANT_FLOAT:
    32. // CONSTANT_Float_info {
    33. // u1 tag;
    34. // u4 bytes;
    35. // }
    36. map.put("tag", CONSTANT_FLOAT);
    37. map.put("value", dis.readFloat());
    38. break;
    39. case CONSTANT_LONG:
    40. // CONSTANT_Long_info {
    41. // u1 tag;
    42. // u4 high_bytes;
    43. // u4 low_bytes;
    44. // }
    45. map.put("tag", CONSTANT_LONG);
    46. map.put("value", dis.readLong());
    47. break;
    48. case CONSTANT_DOUBLE:
    49. // CONSTANT_Double_info {
    50. // u1 tag;
    51. // u4 high_bytes;
    52. // u4 low_bytes;
    53. // }
    54. map.put("tag", CONSTANT_DOUBLE);
    55. map.put("value", dis.readDouble());
    56. break;
    57. case CONSTANT_CLASS:
    58. // CONSTANT_Class_info {
    59. // u1 tag;
    60. // u2 name_index;
    61. // }
    62. map.put("tag", CONSTANT_CLASS);
    63. map.put("nameIndex", dis.readUnsignedShort());
    64. break;
    65. case CONSTANT_STRING:
    66. // CONSTANT_String_info {
    67. // u1 tag;
    68. // u2 string_index;
    69. // }
    70. map.put("tag", CONSTANT_STRING);
    71. map.put("stringIndex", dis.readUnsignedShort());
    72. break;
    73. case CONSTANT_FIELD_REF:
    74. // CONSTANT_Fieldref_info {
    75. // u1 tag;
    76. // u2 class_index;
    77. // u2 name_and_type_index;
    78. // }
    79. map.put("tag", CONSTANT_FIELD_REF);
    80. map.put("classIndex", dis.readUnsignedShort());
    81. map.put("nameAndTypeIndex", dis.readUnsignedShort());
    82. break;
    83. case CONSTANT_METHOD_REF:
    84. // CONSTANT_Methodref_info {
    85. // u1 tag;
    86. // u2 class_index;
    87. // u2 name_and_type_index;
    88. // }
    89. map.put("tag", CONSTANT_METHOD_REF);
    90. map.put("classIndex", dis.readUnsignedShort());
    91. map.put("nameAndTypeIndex", dis.readUnsignedShort());
    92. break;
    93. case CONSTANT_INTERFACE_METHOD_REF:
    94. // CONSTANT_InterfaceMethodref_info {
    95. // u1 tag;
    96. // u2 class_index;
    97. // u2 name_and_type_index;
    98. // }
    99. map.put("tag", CONSTANT_INTERFACE_METHOD_REF);
    100. map.put("classIndex", dis.readUnsignedShort());
    101. map.put("nameAndTypeIndex", dis.readUnsignedShort());
    102. break;
    103. case CONSTANT_NAME_AND_TYPE:
    104. // CONSTANT_NameAndType_info {
    105. // u1 tag;
    106. // u2 name_index;
    107. // u2 descriptor_index;
    108. // }
    109. map.put("tag", CONSTANT_NAME_AND_TYPE);
    110. map.put("nameIndex", dis.readUnsignedShort());
    111. map.put("descriptorIndex", dis.readUnsignedShort());
    112. break;
    113. case CONSTANT_METHOD_HANDLE:
    114. // CONSTANT_MethodHandle_info {
    115. // u1 tag;
    116. // u1 reference_kind;
    117. // u2 reference_index;
    118. // }
    119. map.put("tag", CONSTANT_METHOD_HANDLE);
    120. map.put("referenceKind", dis.readUnsignedByte());
    121. map.put("referenceIndex", dis.readUnsignedShort());
    122. break;
    123. case CONSTANT_METHOD_TYPE:
    124. // CONSTANT_MethodType_info {
    125. // u1 tag;
    126. // u2 descriptor_index;
    127. // }
    128. map.put("tag", CONSTANT_METHOD_TYPE);
    129. map.put("descriptorIndex", dis.readUnsignedShort());
    130. break;
    131. case CONSTANT_DYNAMIC:
    132. // CONSTANT_Dynamic_info {
    133. // u1 tag;
    134. // u2 bootstrap_method_attr_index;
    135. // u2 name_and_type_index;
    136. // }
    137. map.put("tag", CONSTANT_DYNAMIC);
    138. map.put("bootstrapMethodAttrIdx", dis.readUnsignedShort());
    139. map.put("nameAndTypeIndex", dis.readUnsignedShort());
    140. break;
    141. case CONSTANT_INVOKE_DYNAMIC:
    142. // CONSTANT_InvokeDynamic_info {
    143. // u1 tag;
    144. // u2 bootstrap_method_attr_index;
    145. // u2 name_and_type_index;
    146. // }
    147. map.put("tag", CONSTANT_INVOKE_DYNAMIC);
    148. map.put("bootstrapMethodAttrIdx", dis.readUnsignedShort());
    149. map.put("nameAndTypeIndex", dis.readUnsignedShort());
    150. break;
    151. case CONSTANT_MODULE:
    152. // CONSTANT_Module_info {
    153. // u1 tag;
    154. // u2 name_index;
    155. // }
    156. map.put("tag", CONSTANT_MODULE);
    157. map.put("nameIndex", dis.readUnsignedShort());
    158. break;
    159. case CONSTANT_PACKAGE:
    160. // CONSTANT_Package_info {
    161. // u1 tag;
    162. // u2 name_index;
    163. // }
    164. map.put("tag", CONSTANT_PACKAGE);
    165. map.put("nameIndex", dis.readUnsignedShort());
    166. break;
    167. }
    168. constantPoolMap.put(index, map);
    169. }

    解析完常量池的对象后会发现很多数据结构中都引用了其他对象,比如ID(索引位置)为1的常量池对象CONSTANT_METHOD_REF引用了ID为21的CONSTANT_CLASS对象和ID为64的CONSTANT_NAME_AND_TYPE对象,而CONSTANT_CLASS对象又引用了CONSTANT_UTF8java/lang/Object)、CONSTANT_NAME_AND_TYPE同时引用了CONSTANT_UTF8<init>)和CONSTANT_UTF8()V),为了能够直观的看到常量池ID为1的对象信息我们就必须要将所有使用索引方式链接的映射关系改成直接字符串引用,最终得到如下结果:

    1. {
    2. "constantPoolMap": {
    3. "1": {
    4. "tag": "CONSTANT_METHOD_REF",
    5. "classIndex": 21,
    6. "nameAndTypeIndex": 64,
    7. "classValue": "java/lang/Object",
    8. "nameAndTypeValue": "<init>"
    9. }
    10. .... 省略其他对象
    11. }
    12. }

    常量池对象链接代码片段:

    1. /**
    2. * 链接常量池中的引用
    3. */
    4. private void linkConstantPool() {
    5. for (Integer id : constantPoolMap.keySet()) {
    6. Map<String, Object> valueMap = constantPoolMap.get(id);
    7. if (!valueMap.containsKey("value")) {
    8. Map<String, Object> newMap = new LinkedHashMap<>();
    9. for (String key : valueMap.keySet()) {
    10. if (key.endsWith("Index")) {
    11. Object value = recursionValue((Integer) valueMap.get(key));
    12. if (value != null) {
    13. String newKey = key.substring(0, key.indexOf("Index"));
    14. newMap.put(newKey + "Value", value);
    15. }
    16. }
    17. }
    18. valueMap.putAll(newMap);
    19. }
    20. }
    21. }
    22. /**
    23. * 递归查找ID对应的常量池中的值
    24. *
    25. * @param id 常量池ID
    26. * @return 常量池中存储的值
    27. */
    28. private Object recursionValue(Integer id) {
    29. Map<String, Object> map = constantPoolMap.get(id);
    30. if (map.containsKey("value")) {
    31. return map.get("value");
    32. }
    33. for (String key : map.keySet()) {
    34. if (key.endsWith("Index")) {
    35. Integer value = (Integer) map.get(key);
    36. return recursionValue(value);
    37. }
    38. }
    39. return null;
    40. }

    为了方便通过ID(常量池索引)访问常量池中的对象值,封装了一个getConstantPoolValue方法:

    1. /**
    2. * 通过常量池中的索引ID和名称获取常量池中的值
    3. *
    4. * @param index 索引ID
    5. * @return 常量池对象值
    6. */
    7. private Object getConstantPoolValue(int index) {
    8. if (constantPoolMap.containsKey(index)) {
    9. Map<String, Object> dataMap = constantPoolMap.get(index);
    10. Constant constant = (Constant) dataMap.get("tag");
    11. switch (constant) {
    12. case CONSTANT_UTF8:
    13. case CONSTANT_INTEGER:
    14. case CONSTANT_FLOAT:
    15. case CONSTANT_LONG:
    16. case CONSTANT_DOUBLE:
    17. return dataMap.get("value");
    18. case CONSTANT_CLASS:
    19. case CONSTANT_MODULE:
    20. case CONSTANT_PACKAGE:
    21. return dataMap.get("nameValue");
    22. case CONSTANT_STRING:
    23. return dataMap.get("stringValue");
    24. case CONSTANT_FIELD_REF:
    25. case CONSTANT_METHOD_REF:
    26. case CONSTANT_INTERFACE_METHOD_REF:
    27. return dataMap.get("classValue") + "." + dataMap.get("nameAndTypeValue");
    28. case CONSTANT_NAME_AND_TYPE:
    29. case CONSTANT_METHOD_TYPE:
    30. return dataMap.get("descriptorValue");
    31. case CONSTANT_METHOD_HANDLE:
    32. return dataMap.get("referenceValue");
    33. case CONSTANT_DYNAMIC:
    34. case CONSTANT_INVOKE_DYNAMIC:
    35. return dataMap.get("bootstrapMethodAttrValue") + "." + dataMap.get("nameAndTypeValue");
    36. default:
    37. break;
    38. }
    39. }
    40. return null;
    41. }