views:

127

answers:

1

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  A: 

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.

Tom Hawtin - tackline
You are right, the source is there, it's not necessary to decompile it.This might be a silly question, but how do you make your code/app "trusted", or is that something only Sun can do?
Alexandru Luchian
"untrusted"/"trusted" only applies where you have a scurity manager present (for instance with WebStart or the PlugIn). To trust in those environments, jars can be signed or the policy file modified. Package access is controlled by the package.access *security* property.
Tom Hawtin - tackline