6.2 Is It a Structurally Valid class File?

6.2 Is It a Structurally Valid class File?

There are some ways in which the verification algorithm can tell right away that it's not going to like a file. The first four bytes must contain the hex values CA, FE, BA, BE. This magic number lets the verification algorithm immediately reject files that have been garbled and files that were never intended to be thought of as class files (like Java source files, Java archive files, and all the other files hanging around in the system).

Following the magic number are the minor version and the major version. Each takes up two bytes and is interpreted as a 16-bit unsigned value. For JDK 1.0 and 1.1, the major version number is 45 (0x2d) and the minor version number is 3. The Java 2 platform also accepts major version 46 (0x23) and minor version 0, as well as minor versions greater than 3 for version 45.

The Java 2 platform is backward compatible. If your class files are not meant to be read on a JVM prior to Java 2, then they may be marked with version 46. The Java 2 platform javac compiler supports this with the -target option; -target 1.2 will produce a class file with version 46.0, and -target 1.1 will produce a class file with version 45.3.

If all three numbers are acceptable, the verification algorithm scans the rest of the file to ensure that it has the correct format. Figure 6.1 depicts where the bytes belong in a properly formatted class file.

Figure 6.1. Is the file properly formatted?

graphics/06fig01.gif

Table 6.1. Constant tags
Tag Type Format Interpretation
1 UTF8 4+n bytes The first four bytes are an unsigned integer n; the remaining n bytes are the text of the constant.
2 not defined    
3 Integer 4 bytes Signed integer
4 Float 4 bytes IEEE 754 floating-point number
5 Long 8 bytes Long signed integer
6 Double 8 bytes IEEE 754 double-precision number
7 Class 2 bytes Reference to a UTF8 constant that is the name of a class
8 String 2 bytes Reference to a UTF8 constant that is the value of the String
9 Fieldref 4 bytes The first two bytes are a reference to a Class; the second two point to a NameAndType.
10 Methodref 4 bytes Same as Fieldref
11 InterfaceMethodref 4 bytes Same as Fieldref
12 NameAndType 4 bytes The first two bytes point to a UTF8 that is the name of the field or method; the second two point to a UTF8, which is its descriptor.

Most sections begin with a count, which is a two-byte unsigned integer, followed by that many instances of some pattern of bytes. For example, following the major version number is the count of the number of constants. Each constant begins with a tag describing what sort of constant it is, which in turn tells how many bytes make up the constant. The set of constant tags is defined by the virtual machine specification. If any constant tag is invalid, or if the file ends before the correct number of constants are found, then the file is rejected. The valid constant tags are given in Table 6.1.

Similar rules apply to the other sections. If the file ends before all of the parts are found, or if there are extra bytes at the end, then the file is rejected. For more about the details of the inner workings of the class file, see chapter 9.



Programming for the Java Virtual Machine
Programming for the Javaв„ў Virtual Machine
ISBN: 0201309726
EAN: 2147483647
Year: 1998
Pages: 158
Authors: Joshua Engel

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net