@@ -25,7 +25,88 @@ To be documented.
2525
2626\subsection java-class-section How a java program / class is represented in a .class
2727
28- To be documented.
28+ Every Java class is compiled into a .class file. Inner classes, anonymous
29+ classes or classes for tableswitches are also compiled into their own .class
30+ files.
31+
32+ There exists an [ official
33+ specification] ( https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html )
34+
35+ Each class files contains information about its version, the constant pool,
36+ information about the contained class, its super-class, its implemented
37+ interfaces, its fields, methods and finally additional attributes, such as
38+ information about lambda functions used in the methods of the class or inner
39+ classes the it contains.
40+
41+ The content of a .class file can be inspected via the ` javap ` tool which is part
42+ of the JDK installation. Useful options are ` -c ` for code, ` -p ` to display
43+ protected / private methods ` -l ` for line numbers and local variable
44+ information, as well as ` -verbose ` which prints a lot of additional information.
45+
46+ In general, all variable length entries in a class file are represented by first
47+ having an integer ` n ` that specifies the number of entries and then an array of
48+ ` n ` such entries in the file. All variable length attribute also contain
49+ information about their length in bytes.
50+
51+ The integer format used in class files are unsigned integers of size 8/16/32
52+ bit, which are named as ` u1 ` /` u2 ` /` u4 ` .
53+
54+ \subsection Access flags
55+
56+ The JVM specification defines different access flags, e.g., ` final ` , ` static ` ,
57+ ` protected ` , ` private ` etc. where different ones are applicable to the class
58+ itself, its fields or methods. All access flags are represented as bits, the set
59+ of bits that are defined for one entity is represented as disjunction of those
60+ values. Each of these values is defined as a constant with a name prefixed with
61+ ` ACC_ ` in JBMC, e.g., as ` #define ACC_PUBLIC 0x0001 ` or `#define ACC_ENUM
62+ 0x4000`.
63+
64+ \subsection Constant Pool
65+
66+ The constant pool contains all strings and referred values that are used in the
67+ .class. This includes the names of the class itself and its super-class, as well
68+ as the names and signatures of all fields and methods. All strings in the
69+ constant pool are in UTF-16 format.
70+
71+ \subsection Fields
72+
73+ Each member variable of a class has a field entry with a corresponding field
74+ information structure. This contains the name of the field, its raw JVM type
75+ (called the descriptor) and an optional signature.
76+
77+ A signature is an extension to the Java raw types and contains information about
78+ the generic type of an object if applicable.
79+
80+ The name of the field, the descriptor and the signature are all represented as
81+ indices into the constant pool of the class file.
82+
83+ \subsection Methods
84+
85+ Methods are represented in a similar way as fields. Each method has an
86+ associated name, descriptor and optional signature entry in the constant pool
87+ table.
88+
89+ An implemented method also has several attributes. One is the ` Code ` attribute
90+ that stores the actual bytecode instructions. There is also an optional
91+ ` LocalVariableTable ` which contains the names of local variables and
92+ parameters. In addition to this there is also an optional
93+ ` LocalVariableTypeTable ` that contains information about generic local variables
94+ and parameters. Finally the exception table is defined as entries in the
95+ ` Exceptions ` attribute.
96+
97+ Note: most information about generic types is optional and exists mainly as
98+ debugger information. This is because the Java compiler ensures that typing is
99+ correct and creates code accordingly. The same holds true for information on
100+ local variables. It is therefore advisable to compile Java projects with the
101+ ` -g ` option that adds debugging information in all cases.
102+
103+ \subsection Attributes
104+
105+ The last section contains additional attributes, e.g., ` SourceFile ` which
106+ specified from which source file the .class was compiled, ` BootstrapMethods `
107+ which is used for lambda functions or ` InnerClasses ` which refers to inner
108+ classes of the class in question.
109+
29110
30111\section java-bytecode-runtime-exceptions Adding runtime exceptions (java_bytecode_instrument)
31112
0 commit comments