JVM Class(基于openjdk17)
Parse Class File
The key and the entry method of parsing class file is located in src/hotspot/share/classfile/classFileParser.cpp
.
// src/hotspot/share/classfile/classFileParser.cpp
void ClassFileParser::parse_stream(const ClassFileStream* const stream,
TRAPS) {
assert(stream != NULL, "invariant");
assert(_class_name != NULL, "invariant");
// BEGIN STREAM PARSING
stream->guarantee_more(8, CHECK); // magic, major, minor
// Magic value
const u4 magic = stream->get_u4_fast();
guarantee_property(magic == JAVA_CLASSFILE_MAGIC,
"Incompatible magic value %u in class file %s",
magic, CHECK);
At the very first, parse_stream
reads the first four bytes from class file,
and compare to the JAVA_CLASSFILE_MAGIC CAFEBABE
.
This is a very well-known magic number in Java world, when people talk about class file.
If you open class file in hex:
hexdump -C Main.class | head -n 1
00000000 ca fe ba be 00 00 00 3c 00 2f 0a 00 02 00 03 07 |.......<./......|
You can see the class file starts with "cafebabe".
Parse Method
ClassFileParser::parse_stream
ClassFileParser::parse_methods
ClassFileParser::parse_method
...
ClassFileParser::parse_localvariable_table // for LocalVariableTable(LVT)
ClassFileParser::parse_localvariable_table // for LocalVariableTypeTable(LVTT)
...
TODOs
ClassLoader::load_class
src/hotspot/share/classfile/classFileParser.cpp
src/hotspot/share/classfile/javaClasses.hpp
// Read and parse class file and create Klass instance
src/hotspot/share/classfile/klassFactory.hpp
src/hotspot/share/oops/symbol.hpp