Skip to content

Commit 6f2cf51

Browse files
author
Matthias Güdemann
committed
Document class file content for JBMC / java_bytecode readme
1 parent dbd6988 commit 6f2cf51

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

jbmc/src/java_bytecode/README.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,80 @@ 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 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.
38+
39+
The content of a .class file can be inspected via the `javap` tool which is part
40+
of the JDK installation.
41+
42+
In general, all variable length entries in a class file are represented by first
43+
having an integer `n` that specified the number of entries and then an array of
44+
`n` such entries in the file.
45+
46+
The integer format used in class files are unsigned integers of size 8/16/32
47+
bit, which are named as `u1`/`u2`/`u4`.
48+
49+
\subsection Access flags
50+
51+
The JVM specification defines different access flags, e.g., `final`, `static`,
52+
`protected`, `private` etc. where different ones are applicable to the class
53+
itself, its fields or methods. All access flags are represented as bits, the set
54+
of bits that are defined for one entity is represented as disjunction of those
55+
values. Each of these values is defined as a constant with a name prefixed with
56+
`ACC_`.
57+
58+
\subsection Constant Pool
59+
60+
The constant pool contains all strings and referred values that are used in the
61+
.class. This includes the names of the class itself and its super-class, as well
62+
as the names and signatures of all fields and methods. All strings in the
63+
constant pool are in UTF-16 format.
64+
65+
\subsection Fields
66+
67+
Each member variable of a class has a field entry with a corresponding field
68+
information structure. This contains the name of the field, its raw JVM type
69+
(called the descriptor) and an optional signature.
70+
71+
A signature is an extension to the Java raw types and contains information about
72+
the generic type of an object if applicable.
73+
74+
The name of the field, the descriptor and the signature are all represented as
75+
indices into the constant pool of the class file.
76+
77+
\subsection Methods
78+
79+
Methods are represented in a similar way as fields. Each method has an
80+
associated name, descriptor and optional signature entry in the constant pool
81+
table.
82+
83+
An implemented method also has several attributes. One is the `Code` attribute
84+
that stores the actual bytecode instructions. There is also an optional
85+
`LocalVariableTable` which contains the names of local variables and
86+
parameters. In addition to this there is also an optional
87+
`LocalVariableTypeTable` that contains information about generic local variables
88+
and parameters. Finally the exception table is defined as entries in the
89+
`Exceptions` attribute.
90+
91+
Note: most information about generic types is optional and exists mainly as
92+
debugger information. This is because the Java compiler ensures that typing is
93+
correct and creates code accordingly.
94+
95+
\subsection Attributes
96+
97+
The last section contains additional attributes, e.g., `SourceFile` which
98+
specified from which source file the .class was compiled, `BootstrapMethods`
99+
which is used for lambda functions or `InnerClasses` which refers to inner
100+
classes of the class in question.
101+
29102

30103
\section java-bytecode-runtime-exceptions Adding runtime exceptions (java_bytecode_instrument)
31104

0 commit comments

Comments
 (0)