JCOD Syntax
This chapter describes JCOD syntax, and how to encode class files using JCOD. Jcoder is a low-level assembler that accepts text in the JCOD format and produces a .class
file for use with a Java Virtual Machine. Jcoder's primary use is as a tool for producing specialized tests for testing a JVM implementation.
Jcoder is called a low-level assembler because it provides very few higher abstractions beyond the class file format. Typically, there is a 1-1 relationship between constructs in the class file, and constructs in the JCOD format. All Java Spec come from Chapter 4 of the Java Virtual Machine Specification.
This chapter contains the following sections:
Syntax
The source text file can be free form (newlines are considered blanks) and may contain Java-style commenting. The first line of a JCOD file represents the name of the resulting file in the destination directory. This name does not affect the content of the resulting file. This line has two forms:
In the latter case, extension .class
will be added to form FILENAME. Jcod's -d option
allows you to define the destination directory. A list of structured data items follows the class name. The length (in bytes) of each item is determined by its representation.
Primitive Types
Types:
Decimal Values
Optional Size Indicators
Examples
Hexadecimal Values
Examples
Unicode Values
Unicode strings use the same syntax as strings in the Java programming language.
Pure unicode strings are bounded with character \' instead of \". Unlike ordinary strings, they are not prepended with two-byte integer representing actual string's length. As a result, they may exceed 64Kbyte limit.
Constant Values
Constant Pool Tags:
Types for access flags typically are encoded in 2 bytes:
Note - access flags are defined separately for Class, Field, Methods etc. You should look
at the VM spec to see the flags that are applicable for a given entity.
Structured Data Types
Types:
Record
A record is a sequence of simple data types separated by blanks or commas. Records may be limited by a semicolon or right curly brace `}'.
Example
Open List
An open list is sequence of records separated by semicolons.
Example
Closed List
Open list enclosed in curly braces `{ }'.
Example
Array
An integer in square brackets followed by a closed list. The integer represents the length of the array and occupies 2 bytes in the resulting class file. If omitted, the integer will represent the number of records in the following closed list. If the right square bracket is followed by letter "b", then the length occupies 1 byte. Such arrays are used in JavaCard .cap files.
Example
Byte Array
A byte array is defined through the use of the "Byte" keyword followed by length of the array and the array itself. By default, the length of the array occupies 4 bytes in the resulting .class
file. If the right square bracket is followed by letter "b", then the length occupies 1 byte, letter "s" sets the size of length to 2 bytes. If the length is omitted, the number of bytes in the following closed list is used.
Example
Attribute
An attribute is defined through the use of the "Attr" keyword. This keyword is followed by a 2 byte integer index in the constant pool and a 4 byte integer representing the length of the variable part of the attribute in bytes. These two values should be inclosed in parenthesis and separated by commas. If the second value is omitted (length), the actual number of bytes in the following closed list is used.
Example
Component
A Component is very similar to Attribute. The only difference is that first value in parenthesis (used to be index in attribute and tag in component) occupies 1 byte and the second value (length) occupies 2 bytes. Component are useful to represent JavaCard .cap files.
Example
Structured Data Type Rules
It is possible to define a length of an array, byte array or attribute that differs from the actual length of array, byte array or attribute. In this case, a warning is generated but the your defined length will be used.
General Class Structure
Class File Spec | JCOD |
---|---|
|
Class Attributes
The following examples describe the attributes that may appear in a classes attributes list.
Class File Spec | JCOD |
---|---|
[4.7.10] | |
[4.7.15] | |
[4.7.8] | |
[4.7.9] | |
[4.7.23] | |
[4.7.6] | |
[4.7.7] | |
[4.7.11] | |
[4.7.16] | |
[4.7.17] |
The Constant Pool and Constant Elements
A variety of information is stored in the constant pool of a class file.
Class File Spec | JCOD |
---|---|
|
Constant Declarations
Class File Spec | JCOD |
---|---|
[4.4]
| |
[4.4.1] | |
[4.4.2] | |
[4.4.2] | |
[4.4.2] | |
[4.4.3] | |
[4.4.4] | |
[4.4.4] | |
[4.4.5] | |
[4.4.5] | |
[4.4.6] | |
[4.4.7] |
|
[4.4.8]
| |
[4.4.9] | |
[4.4.10]
|
Fields and Field Elements
Field Attributes
Methods and Method Elements
Method Attributes
The following examples describe the various attributes that may exist in a method attributes list.
Code Attributes
StackMapInfo Frames
Annotations and Annotation Elements