Class.getConstantPool()

如果从rt.jar库反编译java中的java.lang.Class类,您会注意到有一个本机方法声明:

native ConstantPool getConstantPool(); 

我前一段时间使用Sun的.class文件规范进行类反编译,我能够从每个.class文件中获取常量池记录。 但这实际上是反编译类。

只是我很惊讶在Class类中看到这个签名。 所以我做的是在Main()方法中写了一小段代码:

 ConstantPool cp = new ConstantPool(); cp.getMethodAtIfLoaded(0); 

如果您反编译sun.reflect.ConstantPool类,您会注意到它有很多与类,方法,参数,声明的字段等相关的方法。

当我执行应用程序时,我收到此HotSpotexception:

 # # An unexpected error has been detected by HotSpot Virtual Machine: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d7e01d3, pid=2816, tid=5464 # # Java VM: Java HotSpot(TM) Client VM (1.5.0_15-b04 mixed mode) # Problematic frame: # V [jvm.dll+0xa01d3] # # An error report file with more information is saved as hs_err_pid2816.log # # If you would like to submit a bug report, please visit: # http://java.sun.com/webapps/bugreport/crash.jsp # 

为什么我不能获得任何类的ConstantPool ? 考虑到getConstantPool()是本机/非公共方法的事实,我假设Sun不希望我直接调用它。

sun。*下的类不是公共API,可能会更改,恕不另行通知并进行各种操作。 Forutnately不受信任的代码根本无法访问它们。 javac反对使用它们。

没有必要反编译代码 – 原始资源可用 ,但不一定是最新的。