aspose file tools*
The moose likes Java in General and the fly likes ConstantPool in .class file format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ConstantPool in .class file format" Watch "ConstantPool in .class file format" New topic
Author

ConstantPool in .class file format

Robert Konigsberg
Ranch Hand

Joined: Jun 23, 2004
Posts: 172
Hello all,

I am writing a .class file parser (for fun) and I'm going through the specification for the class file format. Here's my question: So the ConstantPool spec has a list of elements. Some of the elements within the constant pool refer to other elements inside the pool.

Can I assume that if the nth element in the constant pool refers to the m'th element in the constant pool, then 0 <= m < n?

In other words:


Here: Element 1 refers to Element 0, so when I process element 1, I know that I can resolve Element 1 in my code.

But can *this* happen?


(It may not be reasonable, but can it??!
[ August 12, 2004: Message edited by: Robert Konigsberg ]

SCJP 1.4 (91%)<br />SCJD 1.4 (376/400, 94%)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I assume that "org.brainkandy.TestClass" is a constant of type CONSTANT_Class_info, while "UTF8: 0" is a constant of type CONSTANT_Utf8_info. In which case I believe you have it backwards. UTF8 structures do not refer to other constants; they just define strings. Period. But Class constants do refer to other constants; namely, to a UTF8 constant representing the (fully qualified) name of the class.

The spec doesn't say anything about whether a class constant can precede the UTF8 constant it references. Naively I would have guessed that usually it would not, however I just ran a test (using javap -verbose) and found that the class constants preceded the UTF8 constants they referenced. I would say that you cannot make any assumptions about this - a compiler may legitimately generate constants in any order it feels like.


"I'm not back." - Bill Harding, Twister
Robert Konigsberg
Ranch Hand

Joined: Jun 23, 2004
Posts: 172
Whoops, you're right. This stuff fries my mind a bit. I edited my original post to reflect your comments (you might like to remove your comment about "got it wrong")

But thanks for your comments. Once I fixed my error, it made more sense. But wah.

Rob
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
you might like to remove your comment about "got it wrong"

Well, yeah, but then you'd need to edit your comment about my comment, and I'd have to edit this comment about your second comment. And nobody better add any other comments about this topic or we're really screwed.
Robert Konigsberg
Ranch Hand

Joined: Jun 23, 2004
Posts: 172
Why does this sound like an episode of The Honeymooners?
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
I find this thread very funny.

Hey, how about you delete this thread and start all over again?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ConstantPool in .class file format