Sunday, March 29, 2009

Preon Class File Format

A couple of weeks ago, I blogged a little about Preon's CodecDescriptor, and that it should be changed. This weekend, I completed the new interface, and made sure the entire documentation generation process is now using that interface. I'm quite pleased with the result, although I noticed one area where it can be improved.

The description below is what the new setup produces for the Java Classfile Codec produced from the POJO classes here. Note that the output is a little messy, but that's mostly Blogger.com's style. The actual output looks a little better and can be downloaded here. The same goes for the hyperlinks. The hyperlinks down below don't work. But they do work in the original source document.

Class file

Class file is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Magic

a 32-bit integer value (big endian).

32 (4 bytes)

Minor version

a 16-bit integer value (big endian).

16 (2 bytes)

Major version

a 16-bit integer value (big endian).

16 (2 bytes)

Constant pool count

a 16-bit integer value (big endian).

16 (2 bytes)

Constant pool

a list of elements.

The number of elements in the list is the difference between Constant pool count of Class file and 1.

The particular choice is based on a 8-bit value preceeding the actual encoded value. If 7, then Class cp info will be choosen. If 6, then Double cp info will be choosen. If 9, then Field ref cp info will be choosen. If 4, then Float cp info will be choosen. If 3, then Integer cp info will be choosen. If 11, then Interface method ref cp info will be choosen. If 10, then Method ref cp info will be choosen. If 12, then Name and type cp info will be choosen. If 8, then String cp info will be choosen. If 1, then Utf8 cp info will be choosen.

(unknown)

Access flags

a 16-bit integer value (big endian).

16 (2 bytes)

This class

a 16-bit integer value (big endian).

16 (2 bytes)

Super class

a 16-bit integer value (big endian).

16 (2 bytes)

Interfaces count

a 16-bit integer value (big endian).

16 (2 bytes)

Interfaces

a 32-bit integer value (little endian).

The number of elements in the list is Interfaces count of Class file.

Interfaces count of Class file times 32

Field count

a 16-bit integer value (big endian).

16 (2 bytes)

Fields

Field info.

The number of elements in the list is Field count of Class file.

(unknown)

Method count

a 16-bit integer value (big endian).

16 (2 bytes)

Methods

Method info.

The number of elements in the list is Method count of Class file.

(unknown)

Attribute count

a 16-bit integer value (big endian).

16 (2 bytes)

Attributes

Source file or Deprecated.

The number of elements in the list is Attribute count of Class file.

The particular type of data structure is selected based on the value of 16 leading bits. These bits are interpreted as an unsigned int. The table below lists the conditions, and the data structure assumed when these conditions are met.

Condition

Data structure

the value (a String) of the nth element of Constant pool of Class file (with n being the difference between the value of the first 16 bits and 1) equals the String "SourceFile"

Source file

the value (a String) of the nth element of Constant pool of Class file (with n being the difference between the value of the first 16 bits and 1) equals the String "Deprecated"

Deprecated

(unknown)

Class cp info

Class cp info is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Name index

a 16-bit integer value (big endian).

16 (2 bytes)

Double cp info

Double cp info is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Value

a 64-bit integer value (big endian).

64 (8 bytes)

Field ref cp info

Field ref cp info is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Class index

a 16-bit integer value (big endian).

16 (2 bytes)

Name and type index

a 16-bit integer value (big endian).

16 (2 bytes)

Float cp info

Float cp info is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Value

a 32-bit integer value (big endian).

32 (4 bytes)

Integer cp info

Integer cp info is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Value

a 32-bit integer value (big endian).

32 (4 bytes)

Interface method ref cp info

Interface method ref cp info is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Class index

a 16-bit integer value (big endian).

16 (2 bytes)

Name and type index

a 16-bit integer value (big endian).

16 (2 bytes)

Method ref cp info

Method ref cp info is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Class index

a 16-bit integer value (big endian).

16 (2 bytes)

Name and type index

a 16-bit integer value (big endian).

16 (2 bytes)

Name and type cp info

Name and type cp info is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Name index

a 16-bit integer value (big endian).

16 (2 bytes)

Descriptor index

a 16-bit integer value (big endian).

16 (2 bytes)

String cp info

String cp info is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

String index

a 16-bit integer value (big endian).

16 (2 bytes)

Utf8 cp info

Utf8 cp info is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Length

a 16-bit integer value (big endian).

16 (2 bytes)

Value

A sequence of characters, encoded in ASCII.

The number of characters of the string is Length of Utf8 cp info.

8 times Length of Utf8 cp info

Constant value

Constant value is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Attribute length

a 32-bit integer value (big endian).

32 (4 bytes)

Constant value index

a 16-bit integer value (big endian).

16 (2 bytes)

Synthetic

Synthetic is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Attribute length

a 32-bit integer value (big endian).

32 (4 bytes)

Deprecated

Deprecated is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Attribute length

a 32-bit integer value (big endian).

32 (4 bytes)

Field info

Field info is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Access flags

a 16-bit integer value (big endian).

16 (2 bytes)

Name index

a 16-bit integer value (big endian).

16 (2 bytes)

Descriptor index

a 16-bit integer value (big endian).

16 (2 bytes)

Attributes count

a 16-bit integer value (big endian).

16 (2 bytes)

Attributes

Constant value, Synthetic or Deprecated.

The number of elements in the list is Attributes count of Field info.

The particular type of data structure is selected based on the value of 16 leading bits. These bits are interpreted as an unsigned int. The table below lists the conditions, and the data structure assumed when these conditions are met.

Condition

Data structure

the value (a String) of the nth element of Constant pool of Class file (with n being the difference between the value of the first 16 bits and 1) equals the String "ConstantValue"

Constant value

the value (a String) of the nth element of Constant pool of Class file (with n being the difference between the value of the first 16 bits and 1) equals the String "Synthetic"

Synthetic

the value (a String) of the nth element of Constant pool of Class file (with n being the difference between the value of the first 16 bits and 1) equals the String "Deprecated"

Deprecated

(unknown)

Line number table entry

Line number table entry is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Start pc

a 16-bit integer value (big endian).

16 (2 bytes)

Line number

a 16-bit integer value (big endian).

16 (2 bytes)

Line number table

Line number table is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Attribute length

a 32-bit integer value (big endian).

32 (4 bytes)

Line number table length

a 16-bit integer value (big endian).

16 (2 bytes)

Line number table

Line number table entry.

The number of elements in the list is Line number table length of Line number table.

Line number table length of Line number table times 32

Local variable table entry

Local variable table entry is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Start pc

a 16-bit integer value (big endian).

16 (2 bytes)

Length

a 16-bit integer value (big endian).

16 (2 bytes)

Name index

a 16-bit integer value (big endian).

16 (2 bytes)

Descriptor index

a 16-bit integer value (big endian).

16 (2 bytes)

Index

a 16-bit integer value (big endian).

16 (2 bytes)

Local variable table

Local variable table is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Attribute length

a 32-bit integer value (big endian).

32 (4 bytes)

Local variable table length

a 16-bit integer value (big endian).

16 (2 bytes)

Local variable table

Local variable table entry.

The number of elements in the list is Local variable table length of Local variable table.

Local variable table length of Local variable table times 80

Code

Code is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Attribute length

a 32-bit integer value (big endian).

32 (4 bytes)

Max stack

a 16-bit integer value (big endian).

16 (2 bytes)

Max locals

a 16-bit integer value (big endian).

16 (2 bytes)

Code length

a 32-bit integer value (big endian).

32 (4 bytes)

Code

a 8-bit integer value (little endian).

The number of elements in the list is Code length of Code.

Code length of Code times 8

Exception table length

a 16-bit integer value (big endian).

16 (2 bytes)

Attributes count

a 16-bit integer value (big endian).

16 (2 bytes)

Attributes

Line number table or Local variable table.

The number of elements in the list is Attributes count of Code.

The particular type of data structure is selected based on the value of 16 leading bits. These bits are interpreted as an unsigned int. The table below lists the conditions, and the data structure assumed when these conditions are met.

Condition

Data structure

the value (a String) of the nth element of Constant pool of Class file (with n being the difference between the value of the first 16 bits and 1) equals the String "LineNumberTable"

Line number table

the value (a String) of the nth element of Constant pool of Class file (with n being the difference between the value of the first 16 bits and 1) equals the String "LocalVariableTable"

Local variable table

(unknown)

Exceptions

Exceptions is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Attribute length

a 32-bit integer value (big endian).

32 (4 bytes)

Number of exceptions

a 16-bit integer value (big endian).

16 (2 bytes)

Exception index table

a 32-bit integer value (little endian).

The number of elements in the list is Number of exceptions of Exceptions.

Number of exceptions of Exceptions times 32

Method info

Method info is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Access flags

a 16-bit integer value (big endian).

16 (2 bytes)

Name index

a 16-bit integer value (big endian).

16 (2 bytes)

Descriptor index

a 16-bit integer value (big endian).

16 (2 bytes)

Attributes count

a 16-bit integer value (big endian).

16 (2 bytes)

Attributes

a data structure selected from a list of 4.

The number of elements in the list is Attributes count of Method info.

The particular type of data structure is selected based on the value of 16 leading bits. These bits are interpreted as an unsigned int. The table below lists the conditions, and the data structure assumed when these conditions are met.

Condition

Data structure

the value (a String) of the nth element of Constant pool of Class file (with n being the difference between the value of the first 16 bits and 1) equals the String "Code"

Code

the value (a String) of the nth element of Constant pool of Class file (with n being the difference between the value of the first 16 bits and 1) equals the String "Exceptions"

Exceptions

the value (a String) of the nth element of Constant pool of Class file (with n being the difference between the value of the first 16 bits and 1) equals the String "Synthetic"

Synthetic

the value (a String) of the nth element of Constant pool of Class file (with n being the difference between the value of the first 16 bits and 1) equals the String "Deprecated"

Deprecated

(unknown)

Source file

Source file is composed out of several other smaller elements. The table below provides an overview.

Name

Description

Size (in bits)

Attribute length

a 32-bit integer value (big endian).

32 (4 bytes)

Source file index

a 16-bit integer value (big endian).

16 (2 bytes)

0 comments: