According to https://en.wikipedia.org/wiki/Java_class_file#General_layout - the Java constant pool of a class file begins 10 bytes into the file.
So far, I've been able to parse everything before that (magic to check if it's a classfile, major/minor versions, constant pool size) but I still don't understand exactly how to parse the constant pool. Like, are there opcodes for specifying method refs and other things?
Is there any way I can reference each hex value before text is represented in hex to find out what the following value is?
Should I go about by splitting each set of entries by NOPs (0x00) and then parsing each byte that isn't a text value?
For example, how can I work out exactly what each of these values represents?
The only relevant documentation for class files you need is the The Java® Virtual Machine Specification, especially Chapter 4. The class File Format and, if you are going to parse more than the constant pool, Chapter 6. The Java Virtual Machine Instruction Set.
The constant pool consists of variable length items whose first byte determines its type which in turn dictates the size. Most items consist of one or two indices pointing to other items. A simple parsing code which doesn’t need any 3rd party library may look like this:
Don’t get confused by the
getChar()
calls, I used them as a convenient way for getting an unsigned short, instead ofgetShort()&0xffff
.The code above simply prints the indices of references to other pool items. For decoding the items, you may first store the data of all items into a random access data structure, i.e. array or
List
as items may refer to items with a higher index number. And mind the starting at index1
…