如果您从 rt.jar 库反编译 java 中的 java.lang.Class 类,您会注意到有一个 native 方法声明:
native ConstantPool getConstantPool();
我不久前玩过使用Sun的.class文件规范的类反编译,我能够从每个.class文件中获取常量池记录。但这实际上是反编译类。
只是在Class类中看到这个签名让我很惊讶。所以我所做的就是在 Main() 方法中编写了一小段代码:
ConstantPool cp = new ConstantPool();
cp.getMethodAtIfLoaded(0);
如果你反编译sun.reflect.ConstantPool类,你会发现它有很多与类、方法、参数、声明的字段等相关的方法。
当我执行应用程序时,我收到此 HotSpot 异常:
#
# 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() 是一个 native /非公共(public)方法,我认为 Sun 不希望我直接调用它。
最佳答案
sun.* 下的类不是公共(public) API,可能会更改,恕不另行通知,并且会执行各种操作。幸运的是,不受信任的代码根本无法访问它们。 javac 对象供其使用。
无需反编译代码 - the original source is available ,尽管不一定是最新的。
关于java - 类.getConstantPool(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/387415/