An essential part of Java is the class file. The class file is the compiled representation of a class, created by the Java compiler. It is in this format that the JVM gets information about Java programs. The class file is platform independent, thus it is very well specified. A more detailed description of the class file's contents is found in appendix B.
The class file is created by a Java compiler. The compiler takes a Java program in textual form and converts it to a class file. Each class file describes one class. If a Java program contains several class declarations they are separated to individual class files by the compiler.
The class file contains the description of a class and how it is connected to other classes. The main parts are declaration of fields, method implementations and symbolic references. The methods are built by the Java specific bytecode which is a machine language. The JVM is constructed to execute these bytecodes. The name ``bytecode'' is derived from the size of the opcodes i.e. 8-bits. This format is chosen because many computers handle bytes well. Another reason is padding in the class file. There are few places that need to be padded with extra space, since a byte is small.
A class file may be loaded over a network, e.g. as an applet embedded
in a HTML
document, to the local machine
where it is executed. The JVM regards every loaded class file as
untrusted code. To ensure that the code can be executed and that the code
doesn't contain viruses etc, it is verified by the class file verifier
before it is executed. It guarantees that the class file doesn't do any illegal
operations. These checks are described in more detail in
section 6.3.1.
Every class file describes one class in detail. The class file is thoroughly specified and it contains all the information that is needed to execute Java methods. Other classes are referred to by symbolic references in the class file.
The main contents of the class file are a symbol table, called
constant_pool, field declarations, general information about the class
and methods, with their bytecode. The symbolic references are converted to real
ones by the JVM at run-time. The outline of the class file is shown in
figure 2.
The symbolic references are collected in the constant_pool and they are simply text strings representing the target. The different entries describe classes, variables and their locations and methods and their locations. Constants are also stored in the constant_pool.
Figure 2: A graphical representation of the class file.
There are four elements in the class file that are of variable length. These are cp_info, field_info, method_info and attribute_info. These are described in appendix B.1. More information about the class file can be found in [LinYel96].
The Parser is activated by the ClassLoader when a class file is loaded. The main task of the Parser is to organize the class file into structures that the JVM uses.
The overall structure representing the class file is depicted below. It contains information that is never used by the JVM, like magic. When we designed our implementation we were not sure what information the JVM needed and what could be omitted. The conclusions we made are summarized in chapter 10.
typedef struct ClassFile {
udword magic;
uword minor_version;
uword major_version;
uword constant_pool_count;
cp_info* constant_pool;
uword access_flags;
uword this_class;
uword super_class;
uword interfaces_count;
uword* interfaces;
uword fields_count;
field_info* fields;
uword methods_count;
method_info* methods;
uword attributes_count;
attribute_info* attributes;
uword object_size; /* JVM use only */
Boolean initialized; /* JVM use only */
bool* class_name_p; /* JVM use only */
struct object_info* object_info_p; /* JVM use only */
} ClassFile;
We have added some entries in the ClassFile structure to aid the JVM
during execution.
When an object is created its instance variable area is allocated by the JVM. The object_size gives the number of 32-bits memory units that are allocated. The variable initialized shows if this class has been initialized or not, i.e. static variables initialization.
The class_name_p is a pointer to the name of this class. It is used to aid the symbolic reference resolution.
The object_info_p is a pointer to an object of the built-in class Class (there is one such object for each class in Java). The Class object can be used by Java programs to get information about a class.