Preface
Learn jvm word code, you need to understand the way class is composed, this article explains the process of parsing the class file format in java method, I hope it can help you.
Class file specification
The code that is compiled and executed by the Java virtual machine uses a platform-neutral (hardware- and operating system-independent). It is often (but not always) stored as a file, so this format is called the Class file format. The Class file format defines the precise representation of classes and interfaces, including the conventions in the platform-dependent target file format. The Class file format defines the precise representation of classes and interfaces, including some detailed conventions in the platform-related target file format
|
|
Next, we start to parse out how each field is identified What do u4, u2 mean? u is an unsigned number, followed by a number indicating how many bytes it takes up u4 occupies 4 bytes u2 occupies 2 bytes
- magic takes up 4 bytes, (ca fe ba be)
- minor_version subversion number, 2 bytes
- major_version major version good 2 byte number
- constant_pool_count Number of constant pools 2 bytes
- constant_pool[constant_pool_count-1] Array of constant pools
- access_flags access_flags 2 byte number
- this_class index of the class name
- super_class index of the superclass name
- interfaces_count the number of interfaces
- interfaces[interfaces_count] Array of interfaces
- fields_count Number of fields
- fields[fields_count] Array of fields
- methods_count Number of methods
- methods[methods_count] Array of methods
- attributes_count The number of attributes
- attributes[attributes_count] array of attributes
How to solve a class file by yourself
I believe that most of the first time I saw the above protocol, I could read it, but to parse out the meaning of each field by myself. It is impossible to start.
- read the class file
|
|
- read magic, (magic u4 takes up 4 bytes)
- read minor_version u2 occupies 2 bytes
- read major_version u2 occupies 2 bytes
See the above parsing, whether to understand, in fact, or very regular, as long as you look carefully at the protocol document (to see many times to do)
The final parsing class document is this
|
|