Class.getConstantPool()

一世执手 提交于 2019-12-08 05:06:22

问题


If you decompile the java.lang.Class class in java from the rt.jar library you will notice there is a native method declaration:

native ConstantPool getConstantPool();

I played a while ago with class decompilation using Sun's .class file specification and I was able to obtain the constant pool record from each .class file. But that was actually decompiling classes.

It's just that I was surprised to see this signature in the Class class. So what I did is I wrote a small piece of code in the Main() method:

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

If you decompile the sun.reflect.ConstantPool class you will notice it has a lot of method related to classes, method, parameters, fields declared, etc.

When I execute the app, I get this HotSpot exception:

#
# 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
#

Why can't I get the ConstantPool of any class? Considering the fact that getConstantPool() is a native/non-public method I assume Sun does not want me to call it directly.


回答1:


Classes under sun.* are not public APIs, may change without notice and do all sorts. Forutnately untrusted code cannot access them at all. javac objects to their use.

There's no need to decompile the code - the original source is available, although not necessarily up to date.



来源:https://stackoverflow.com/questions/387415/class-getconstantpool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!