如何获取java类常量池中的常量映射列表?

时间:2014-04-24 00:53:50

标签: java clojure java-bytecode-asm

当我在特定课程上运行javap -c Address.class > Address.txt时,

我得到一堆输出,#<NUMBER>作为常量池的索引

Compiled from "Address.java"
public class test.Address extends test.Entity {
  public test.Address();
    Code:
       0: aload_0       
       1: invokespecial #1                  // Method test/DefaultEntity."<init>":()V
       4: aload_0       
       5: aconst_null   
       6: putfield      #2                  // Field io:Ljava/lang/String;
       9: aload_0       
      10: aconst_null   
      11: putfield      #3                  // Field zip4:Ljava/lang/String;
      14: aload_0       
      15: aconst_null   
      16: putfield      #4                  // Field zip:Ljava/lang/String;
      19: aload_0 

我如何以hashmap的形式获取池?即:

{1  "Method test/DefaultEntity."<init>":()"
 2   "Field io:Ljava/lang/String;"}

1 个答案:

答案 0 :(得分:1)

使用ASM,您可以使用ClassReader读取.class文件,然后使用getItemCount()和各种读取方法来读取所有常量池项。

(def cr (clojure.asm.ClassReader "test.Address"))
(.getItemCount cr) 
;; etc