DATABASE OR FILE ACCESSING

Method and apparatus for pre-processing and packaging class files

5966702

Abstract

A method and apparatus for pre-processing and packaging class files. Embodiments remove duplicate information elements from a set of class files to reduce the size of individual class files and to prevent redundant resolution of the information elements. Memory allocation requirements are determined in advance for the set of classes as a whole to reduce the complexity of memory allocation when the set of classes are loaded. The class files are stored in a single package for efficient storage, transfer and processing as a unit. In an embodiment, a pre-processor examines each class file in a set of class files to locate duplicate information in the form of redundant constants contained in a constant pool. The duplicate constant is placed in a separate shared table, and all occurrences of the constant are removed from the respective constant pools of the individual class files. During pre-processing, memory allocation requirements are determined for each class file, and used to determine a total allocation requirement for the set of class files. The shared table, the memory allocation requirements and the reduced class files are packaged as a unit in a multi-class file.


Claims

We claim:

1. A method of pre-processing class files comprising:

determining plurality of duplicated elements in a plurality of class files;

forming a shared table comprising said plurality of duplicated elements;

removing said duplicated elements from said plurality of class files to obtain a plurality of reduced class files; and

forming a multi-class file comprising said plurality of reduced class files and said shared table.

2. The method of claim 1, further comprising:

computing an individual memory allocation requirement for each of said plurality of reduced class files;

computing a total memory allocation requirement for said plurality of class files from said individual memory allocation requirement of each of said plurality of reduced class files; and

storing said total memory allocation requirement in said multi-class file.

3. The method of claim 2, further comprising:

reading said total memory allocation requirement from said multi-class file;

allocating a portion of memory based on said total memory allocation requirement; and

loading said reduced class files and said shared table into said portion of memory.

4. The method of claim 3, further comprising:

accessing said shared table in said portion of memory to obtain one or more elements not found in one or more of said reduced class files.

5. The method of claim 1, wherein said step of determining a plurality of duplicated elements comprises:

determining one or more constants shared between two or more class files.

6. The method of claim 5, wherein said step of forming a shared table comprises:

forming a shared constant table comprising said one or more constants shared between said two or more class files.

7. A computer program product comprising:

a computer usable medium having computer readable program code embodied therein for pre-processing class files, said computer program product comprising:

computer readable program code configured to cause a computer to determine a plurality of duplicated elements in a plurality of class files;

computer readable program code configured to cause a computer to form a shared table comprising said plurality of duplicated elements;

computer readable program code configured to cause a computer to remove said duplicated elements from said plurality of class files to obtain a plurality of reduced class files; and

computer readable program code configured to cause a computer to form a multi-class file comprising said plurality of reduced class files and said shared table.

8. The computer program product of claim 7, further comprising:

computer readable program code configured to cause a computer to compute an individual memory allocation requirement of each of said plurality of reduced class files;

computer readable program code configured to cause a computer to compute a total memory allocation requirement of said plurality of class files from said individual memory allocation requirement of each of said plurality of reduced class files; and

computer readable program code configured to cause a computer to store said total memory allocation requirement in said multi-class file.

9. The computer program product of claim 8, further comprising:

computer readable program code configured to cause a computer to read said total memory allocation requirement from said multi-class file;

computer readable program code configured to cause a computer to allocate a portion of memory based on said total memory allocation requirement; and

computer readable program code configured to cause a computer to load said reduced class files and said shared table into said portion of memory.

10. The computer program product of claim 9, further comprising:

computer readable program code configured to cause a computer to access said shared table in said portion of memory to obtain one or more elements not found in one or more of said reduced class files.

11. The computer program product of claim 7, wherein said computer readable program code configured to cause a computer to determine said plurality of duplicated elements comprises:

computer readable program code configured to cause a computer to determine one or more constants shared between two or more class files.

12. The computer program product of claim 11, wherein said computer readable program code configured to cause a computer to form said shared table comprises:

computer readable program code configured to cause a computer to form a shared constant table comprising said one or more constants shared between said two or more class files.

13. An apparatus comprising:

a processor;

a memory coupled to said processor;

a plurality of class files stored in said memory;

a process executing on said processor, said process configured to form a multi-class file comprising:

a plurality of reduced class files obtained from said plurality of class files by removing one or more elements that are duplicated between two or more of said plurality of class files; and

a shared table comprising said duplicated elements.

14. The apparatus of claim 13, wherein said multi-class file further comprises a memory requirement, said memory requirement being computed by said process.

15. The apparatus of claim 13, wherein said duplicated elements comprise elements of constant pools of respective class files, said shared table comprising a shared constant pool.

16. The apparatus of claim 13, further comprising:

a virtual machine having a class loader and a runtime data area, said class loader configured to obtain and load said multi-class file into said runtime data area.

17. The apparatus of claim 16, wherein said class loader is configured to allocate a portion of said runtime data area based on said memory requirement in said multi-class file.

18. The apparatus of claim 17, wherein said class loader is configured to load said plurality of reduced class files and said shared table into said portion of said runtime data area.

19. The apparatus of claim 16, wherein said virtual machine is configured to access said shared table when a desired element associated with a first class file is not present in a corresponding one of said plurality of reduced class files.

20. A memory configured to store data for access by a virtual machine executing in a computer system, comprising:

a data structure stored in said memory, said data structure comprising:

a plurality of reduced class files associated with a plurality of corresponding classes, said plurality of reduced class files configured to be loaded by the virtual machine for execution of said plurality of classes;

a shared table comprising one or more elements that are duplicated between two or more of said plurality of classes, said shared table configured to be loaded into the virtual machine to be accessed for said duplicated elements; and

a memory requirement value configured to be read by a class loader of the virtual machine to allocate a portion of a runtime data area for loading said plurality of reduced class files and said shared table.

21. The memory of claim 20, wherein said duplicated elements are removed from said plurality of reduced class files.

22. The memory of claim 20, wherein said duplicated elements comprise constants and said shared table comprises a shared constant pool.

23. The memory of claim 20, wherein said memory requirement value is computed from individual memory requirements of said plurality of reduced class files and a memory requirement of said shared table.


Description

BACKGROUND OF THE INVENTION

1. Field of the Invention

This invention relates to the field of computer software, and, more specifically, to object-oriented computer applications.

Portions of the disclosure of this patent document contain material that is subject to copyright protection. The copyright owner has no objection to the facsimile reproduction by anyone of the patent document or the patent disclosure as it appears in the Patent and Trademark Office file or records, but otherwise reserves all copyright rights whatsoever.

2. Background Art

With advancements in network technology, the use of networks for facilitating the distribution of media information, such as text, graphics, and audio, has grown dramatically, particularly in the case of the Internet and the World Wide Web. One area of focus for current developmental efforts is in the field of web applications and network interactivity. In addition to passive media content, such as HTML definitions, computer users or "clients" coupled to the network are able to access or download application content, in the form of applets, for example, from "servers" on the network.

To accommodate the variety of hardware systems used by clients, applications or applets are distributed in a platform-independent format such as the Java.RTM. class file format. Object-oriented applications are formed from multiple class files that are accessed from servers and downloaded individually as needed. Class files contain bytecode instructions. A "virtual machine" process that executes on a specific hardware platform loads the individual class files and executes the bytecodes contained within.

A problem with the class file format and the class loading process is that class files often contain duplicated data. The storage, transfer and processing of the individual class files is thus inefficient due to the redundancy of the information. Also, an application may contain many class files, all of which are loaded and processed in separate transactions. This slows down the application and degrades memory allocator performance. Further, a client is required to maintain a physical connection to the server for the duration of the application in order to access class files on demand.

These problems can be understood from a review of general object-oriented programming and an example of a current network application environment.

Object-Oriented Programming

Object-oriented programming is a method of creating computer programs by combining certain fundamental building blocks, and creating relationships among and between the building blocks. The building blocks in object-oriented programming systems are called "objects." An object is a programming unit that groups together a data structure (one or more instance variables) and the operations (methods) that can use or affect that data. Thus, an object consists of data and one or more operations or procedures that can be performed on that data. The joining of data and operations into a unitary building block is called "encapsulation."

An object can be instructed to perform one of its methods when it receives a "message." A message is a command or instruction sent to the object to execute a certain method. A message consists of a method selection (e.g., method name) and a plurality of arguments. A message tells the receiving object what operations to perform.

One advantage of object-oriented programming is the way in which methods are invoked. When a message is sent to an object, it is not necessary for the message to instruct the object how to perform a certain method. It is only necessary to request that the object execute the method. This greatly simplifies program development.

Object-oriented programming languages are predominantly based on a "class" scheme. The class-based object-oriented programming scheme is generally described in Lieberman, "Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems," OOPSLA 86 Proceedings, September 1986, pp. 214-223.

A class defines a type of object that typically includes both variables and methods for the class. An object class is used to create a particular instance of an object. An instance of an object class includes the variables and methods defined for the class. Multiple instances of the same class can be created from an object class. Each instance that is created from the object class is said to be of the same type or class.

To illustrate, an employee object class can include "name" and "salary" instance variables and a "set.sub.-- salary" method. Instances of the employee object class can be created, or instantiated for each employee in an organization. Each object instance is said to be of type "employee." Each employee object instance includes "name" and "salary" instance variables and the "set.sub.-- salary" method. The values associated with the "name" and "salary" variables in each employee object instance contain the name and salary of an employee in the organization. A message can be sent to an employee's employee object instance to invoke the "set.sub.-- salary" method to modify the employee's salary (i.e., the value associated with the "salary" variable in the employee's employee object).

A hierarchy of classes can be defined such that an object class definition has one or more subclasses. A subclass inherits its parent's (and grandparent's etc.) definition. Each subclass in the hierarchy may add to or modify the behavior specified by its parent class. Some object-oriented programming languages support multiple inheritance where a subclass may inherit a class definition from more than one parent class. Other programming languages support only single inheritance, where a subclass is limited to inheriting the class definition of only one parent class. The Java programming language also provides a mechanism known as an "interface" which comprises a set of constant and abstract method declarations. An object class can implement the abstract methods defined in an interface. Both single and multiple inheritance are available to an interface. That is, an interface can inherit an interface definition from more than one parent interface.

An object is a generic term that is used in the object-oriented programming environment to refer to a module that contains related code and variables. A software application can be written using an object-oriented programming language whereby the program's functionality is implemented using objects.

A Java program is composed of a number of classes and interfaces. Unlike many programming languages, in which a program is compiled into machine-dependent, executable program code, Java classes are compiled into machine independent bytecode class files. Each class contains code and data in a platform-independent format called the class file format. The computer system acting as the execution vehicle contains a program called a virtual machine, which is responsible for executing the code in Java classes. The virtual machine provides a level of abstraction between the machine independence of the bytecode classes and the machine-dependent instruction set of the underlying computer hardware. A "class loader" within the virtual machine is responsible for loading the bytecode class files as needed, and either an interpreter executes the bytecodes directly, or a "just-in-time" (JIT) compiler transforms the bytecodes into machine code, so that they can be executed by the processor. FIG. 1 is a block diagram illustrating a sample Java network environment comprising a client platform 102 coupled over a network 101 to a server 100 for the purpose of accessing Java class files for execution of a Java application or applet.

Sample Java Network Application Environment

In FIG. 1, server 100 comprises Java development environment 104 for use in creating the Java class files for a given application. The Java development environment 104 provides a mechanism, such as an editor and an applet viewer, for generating class files and previewing applets. A set of Java core classes 103 comprise a library of Java classes that can be referenced by source files containing other/new Java classes. From Java development environment 104, one or more Java source files 105 are generated. Java source files 105 contain the programmer readable class definitions, including data structures, method implementations and references to other classes. Java source files 105 are provided to Java compiler 106, which compiles Java source files 105 into compiled ".class" files 107 that contain bytecodes executable by a Java virtual machine. Bytecode class files 107 are stored (e.g., in temporary or permanent storage) on server 100, and are available for download over network 101.

Client platform 102 contains a Java virtual machine (JVM) 111 which, through the use of available native operating system (O/S) calls 112, is able to execute bytecode class files and execute native O/S calls when necessary during execution.

Java class files are often identified in applet tags within an HTML (hypertext markup language) document. A web server application 108 is executed on server 100 to respond to HTTP (hypertext transport protocol) requests containing URLs (universal resource locators) to HTML documents, also referred to as "web pages." When a browser application executing on client platform 102 requests an HTML document, such as by forwarding URL 109 to web server 108, the browser automatically initiates the download of the class files 107 identified in the applet tag of the HTML document. Class files 107 are typically downloaded from the server and loaded into virtual machine 111 individually as needed.

It is typical for the classes of a Java program to be loaded as late during the program's execution as possible; they are loaded on demand from the network (stored on a server), or from a local file system, when first referenced during the Java program's execution. The virtual machine locates and loads each class file, parses the class file format, allocates memory for the class's various components, and links the class with other already loaded classes. This process makes the code in the class readily executable by the virtual machine.

The individualized class loading process, as it is typically executed, has disadvantages with respect to use of storage resources on storage devices, allocation of memory, and execution speed and continuity. Those disadvantages are magnified by the fact that a typical Java application can contain hundreds or thousands of small class files. Each class file is self-contained. This often leads to information redundancy between class files, for example, with two or more class files sharing common constants. As a result, multiple classes inefficiently utilize large amounts of storage space on permanent storage devices to separately store duplicate information. Similarly, loading each class file separately causes unnecessary duplication of information in application memory as well. Further, because common constants are resolved separately per class during the execution of Java code, the constant resolution process is unnecessarily repeated.

Because classes are loaded one by one, each small class requires a separate set of dynamic memory allocations. This creates memory fragmentation, which wastes memory, and degrades allocator performance. Also, separate loading "transactions" are required for each class. The virtual machine searches for a class file either on a network device, or on a local file system, and sets up a connection to load the class and parse it. This is a relatively slow process, and has to be repeated for each class. The execution of a Java program is prone to indeterminate pauses in response/execution caused by each class loading procedure, especially, when loading classes over a network. These pauses create a problem for systems in which interactive or real-time performance is important.

A further disadvantage of the individual class loading process is that the computer executing the Java program must remain physically connected to the source of Java classes during the duration of the program's execution. This is a problem especially for mobile or embedded computers without local disk storage or dedicated network access. If the physical connection is disrupted during execution of a Java application, class files will be inaccessible and the application will fail when a new class is needed. Also, it is often the case that physical connections to networks such as the Internet have a cost associated with the duration of such a connection. Therefore, in addition to the inconvenience associated with maintaining a connection throughout application execution, there is added cost to the user as a result of the physical connection.

A Java archive (JAR) format has been developed to group class files together in a single transportable package known as a JAR file. JAR files encapsulate Java classes in archived, compressed format. A JAR file can be identified in an HTML document within an applet tag. When a browser application reads the HTML document and finds the applet tag, the JAR file is downloaded to the client computer and decompressed. Thus, a group of class files may be downloaded from a server to a client in one download transaction. After downloading and decompressing, the archived class files are available on the client system for individual loading as needed in accordance with standard class loading procedures. The archived class files remain subject to storage inefficiencies due to duplicated data between files, as well as memory fragmentation due to the performance of separate memory allocations for each class file.

SUMMARY OF THE INVENTION

A method and apparatus for pre-processing and packaging class files is described. Embodiments of the invention remove duplicate information elements from a set of class files to reduce the size of individual class files and to prevent redundant resolution of the information elements. Memory allocation requirements are determined in advance for the set of classes as a whole to reduce the complexity of memory allocation when the set of classes are loaded. The class files are stored in a single package for efficient storage, transfer and processing as a unit.

In an embodiment of the invention, a pre-processor examines each class file in a set of class files to locate duplicate information in the form of redundant constants contained in a constant pool. The duplicate constant is placed in a separate shared table, and all occurrences of the constant are removed from the respective constant pools of the individual class files. During pre-processing, memory allocation requirements are determined for each class file, and used to determine a total allocation requirement for the set of class files. The shared table, the memory allocation requirements and the reduced class files are packaged as a unit in a multi-class file.

When a virtual machine wishes to load the classes in the multi-class file, the location of the multi-class file is determined and the multi-class file is downloaded from a server, if needed. The memory allocation information in the multi-class file is used by the virtual machine to allocate memory from the virtual machine's heap for the set of classes. The individual classes, with respective reduced constant pools, are loaded, along with the shared table, into the virtual machine. Constant resolution is carried out on demand on the respective reduced constant pools and the shared table.

BRIEF DESCRIPTION OF THE DRAWINGS

FIG. 1 is an embodiment of a Java network application environment.

FIG. 2 is a block diagram of an embodiment of a computer system capable of providing a suitable execution environment for an embodiment of the invention.

FIG. 3 is a block diagram of an embodiment of a class file format.

FIG. 4 is a flow diagram of a class file pre-processing method in accordance with an embodiment of the invention.

FIG. 5 is a block diagram of an multi-class file format in accordance with an embodiment of the invention.

FIG. 6 is a block diagram of the runtime data areas of a virtual machine in accordance with an embodiment of the invention.

DETAILED DESCRIPTION OF THE INVENTION

The invention is a method and apparatus for pre-processing and packaging class files. In the following description, numerous specific details are set forth to provide a more thorough description of embodiments of the invention. It will be apparent, however, to one skilled in the art, that the invention may be practiced without these specific details. In other instances, well known features have not been described in detail so as not to obscure the invention.

Embodiment of Computer Execution Environment (Hardware)

An embodiment of the invention can be implemented as computer software in the form of computer readable program code executed on a general purpose computer such as computer 200 illustrated in FIG. 2, or in the form of bytecode class files executable by a virtual machine running on such a computer. A keyboard 210 and mouse 211 are coupled to a bi-directional system bus 218. The keyboard and mouse are for introducing user input to the computer system and communicating that user input to central processing unit (CPU) 213. Other suitable input devices may be used in addition to, or in place of, the mouse 211 and keyboard 210. I/O (input/output) unit 219 coupled to bi-directional system bus 218 represents such I/O elements as a printer, A/V (audio/video) I/O, etc.

Computer 200 includes a video memory 214, main memory 215 and mass storage 212, all coupled to bidirectional system bus 218 along with keyboard 210, mouse 211 and CPU 213. The mass storage 212 may include both fixed and removable media, such as magnetic, optical or magnetic optical storage systems or any other available mass storage technology. Bus 218 may contain, for example, thirty-two address lines for addressing video memory 214 or main memory 215. The system bus 218 also includes, for example, a 32-bit data bus for transferring data between and among the components, such as CPU 213, main memory 215, video memory 214 and mass storage 212. Alternatively, multiplex data/address lines may be used instead of separate data and address lines.

In one embodiment of the invention, the CPU 213 is a microprocessor manufactured by Motorola.RTM., such as the 680X0 processor or a microprocessor manufactured by Intel.RTM., such as the 80X86, or Pentium.RTM. processor, or a SPARC.RTM. microprocessor from Sun Microsystems.RTM.. However, any other suitable microprocessor or microcomputer may be utilized. Main memory 215 is comprised of dynamic random access memory (DRAM). Video memory 214 is a dual-ported video random access memory. One port of the video memory 214 is coupled to video amplifier 216. The video amplifier 216 is used to drive the cathode ray tube (CRT) raster monitor 217. Video amplifier 216 is well known in the art and may be implemented by any suitable apparatus. This circuitry converts pixel data stored in video memory 214 to a raster signal suitable for use by monitor 217. Monitor 217 is a type of monitor suitable for displaying graphic images.

Computer 200 may also include a communication interface 220 coupled to bus 218. Communication interface 220 provides a two-way data communication coupling via a network link 221 to a local network 222. For example, if communication interface 220 is an integrated services digital network (ISDN) card or a modem, communication interface 220 provides a data communication connection to the corresponding type of telephone line, which comprises part of network link 221. If communication interface 220 is a local area network (LAN) card, communication interface 220 provides a data communication connection via network link 221 to a compatible LAN. Wireless links are also possible. In any such implementation, communication interface 220 sends and receives electrical, electromagnetic or optical signals which carry digital data streams representing various types of information.

Network link 221 typically provides data communication through one or more networks to other data devices. For example, network link 221 may provide a connection through local network 222 to host computer 223 or to data equipment operated by an Internet Service Provider (ISP) 224. ISP 224 in turn provides data communication services through the world wide packet data communication network now commonly referred to as the "Internet" 225. Local network 222 and Internet 225 both use electrical, electromagnetic or optical signals which carry digital data streams. The signals through the various networks and the signals on network link 221 and through communication interface 220, which carry the digital data to and from computer 200, are exemplary forms of carrier waves transporting the information.

Computer 200 can send messages and receive data, including program code, through the network(s), network link 221, and communication interface 220. In the Internet example, server 226 might transmit a requested code for an application program through Internet 225, ISP 224, local network 222 and communication interface 220. In accord with the invention, one such downloaded application is the apparatus for pre-processing and packaging class files described herein.

The received code may be executed by CPU 213 as it is received, and/or stored in mass storage 212, or other non-volatile storage for later execution. In this manner, computer 200 may obtain application code in the form of a carrier wave.

The computer systems described above are for purposes of example only. An embodiment of the invention may be implemented in any type of computer system or programming or processing environment.

Class File Structure

Embodiments of the invention can be better understood with reference to aspects of the class file format. Description is provided below of the Java class file format. Also, enclosed as Section A of this specification are Chapter 4, "The class File Format," and Chapter 5, "Constant Pool Resolution," of The Java Virtual Machine Specification, by Tim Lindholm and Frank Yellin, published by Addison-Wesley in September 1996, .COPYRGT.Sun Microsystems, Inc.

The Java class file consists of a stream of 8-bit bytes, with 16-bit, 32-bit and 64-bit structures constructed from consecutive 8-bit bytes. A single class or interface file structure is contained in the class file. This class file structure appears as follows:

    ______________________________________
    ClassFile  {
    u4 magic;
    u2 minor.sub.-- version;
    u2 major.sub.-- version;
    u2 constant.sub.-- pool.sub.-- count;
    cp.sub.-- info constant.sub.-- pool[constant.sub.-- pool.sub.-- count-1];
    u2 access.sub.-- flags;
    u2 this.sub.-- class;
    u2 super.sub.-- class;
    u2 interfaces.sub.-- count;
    u2 interfaces[interfaces.sub.-- count];
    u2 fields.sub.-- count;
    field.sub.-- info fields[fields.sub.-- count];
    u2 methods.sub.-- count;
    method.sub.-- info methods[methods.sub.-- count];
    u2 attributes.sub.-- count;
    attribute.sub.-- info attributes[attributes.sub.-- count];
    ______________________________________


where u2 and u4 refer to unsigned two-byte and four-byte quantities. This structure is graphically illustrated in FIG. 3.

In FIG. 3, class file 300 comprises four-byte magic value 301, two-byte minor version number 302, two-byte major version number 303 , two-byte constant pool count value 304, constant pool table 305 corresponding to the constant pool array of variable length elements, two-byte access flags value 306, two-byte "this class" identifier 307, two-byte super class identifier 308, two-byte interfaces count value 309, interfaces table 310 corresponding to the interfaces array of two-byte elements, two-byte fields count value 311, fields table 312 corresponding to the fields array of variable length elements, two-byte methods count value 313, methods table 314 corresponding to the methods array of variable length elements, two-byte attributes count value 315, and attributes table 316 corresponding to the attributes array of variable-length elements. Each of the above structures is briefly described below.

Magic value 301 contains a number identifying the class file format. For the Java class file format, the magic number has the value 0xCAFEBABE. The minor version number 302 and major version number 303 specify the minor and major version numbers of the compiler responsible for producing the class file.

The constant pool count value 304 identifies the number of entries in constant pool table 305. Constant pool table 305 is a table of variable-length data structures representing various string constants, numerical constants, class names, field names, and other constants that are referred to within the ClassFile structure. Each entry in the constant pool table has the following general structure:

    ______________________________________
             cp.sub.-- info  {
               u1 tag;
               u1 info[ ];
             }
    ______________________________________


where the one-byte "tag" specifies a particular constant type. The format of the info[ ] array differs based on the constant type. The info[ ] array may be a numerical value such as for integer and float constants, a string value for a string constant, or an index to another entry of a different constant type in the constant pool table. Further details on the constant pool table structure and constant types are available in Chapter 4 of Section A.

Access flags value 306 is a mask of modifiers used with class and interface declarations. The "this class" value 307 is an index into constant pool table 305 to a constant type structure representing the class or interface defined by this class file. The super class value 308 is either zero, indicating the class is a subclass of java.lang.Object, or an index into the constant pool table to a constant type structure representing the superclass of the class defined by this class file.

Interfaces count value 309 identifies the number of direct superinterfaces of this class or interface, and accordingly, the number of elements in interfaces table 310. Interfaces table 310 contains two-byte indices into constant pool table 305. Each corresponding entry in constant pool table 305 is a constant type structure representing an interface which is a direct superinterface of the class or interface defined by this class file.

The fields count value 311 provides the number of structures in fields table 312. Each entry in fields table 312 is a variable-length structure providing a description of a field in the class type. Fields table 312 includes only those fields that are declared by the class or interface defined by this class file.

The methods count value 313 indicates the number of structures in methods table 314. Each element of methods table 314 is a variable-length structure giving a description of, and virtual machine code for, a method in the class or interface.

The attributes count value 315 indicates the number of structures in attributes table 316. Each element in attributes table 316 is a variable-length attribute structure. Attribute structures are discussed in section 4.7 of Section A.

Embodiments of the invention examine the constant pool table for each class in a set of classes to determine where duplicate information exists. For example, where two or more classes use the same string constant, the string constant may be removed from each class file structure and placed in a shared constant pool table. In the simple case, if N classes have the same constant entry, N units of memory space are taken up in storage resources. By removing all constant entries and providing one shared entry, N-1 units of memory space are freed. The memory savings increase with N. Also, by implementing a shared constant table, entries in the constant table need be fully resolved at most once. After the initial resolution, future code references to the constant may directly use the constant.

Pre-processing and Packaging Classes

An embodiment of the invention uses a class pre-processor to package classes in a format called an "mclass" or multi-class file. A method for pre-processing and packaging a set of class files is illustrated in the flow diagram of FIG. 4.

The method begins in step 400 with a set of arbitrary class files "S" (typically part of one application). In step 401, the pre-processor reads and parses each class in "S." In step 402, the pre-processor examines the constant pool tables of each class to determine the set of class file constants (such as strings and numerics, as well as others specific to the class file format) that can be shared between classes in "S." A shared constant pool table is created in step 403, with all duplicate constants determined from step 402. In step 404, the pre-processor removes the duplicate, shared constants from the individual constant pool tables of each class.

In step 405, the pre-processor computes the in-core memory requirements of each class in "S," as would normally be determined by the class loader for the given virtual machine. This is the amount of memory the virtual machine would allocate for each class, if it were to load each class separately. After considering all classes in "S" and the additional memory requirement for the shared constant pool table, the total memory requirement for loading "S" is computed in step 406.

In step 407, the pre-processor produces a multi-class (mclass) file that contains the shared constant pool table created in step 403, information about memory allocation requirements determined in steps 405 and 406, and all classes in "S," with their respective reduced constant pool tables. The mclass file for the class set "S" is output in step 408. In some embodiments, to further reduce the size of the multi-class file, the multi-class file may be compressed.

An example of one embodiment of a multi-class file structure may be represented as follows:

    ______________________________________
    MclassFile  {
    u2 shared.sub.-- pool.sub.-- count;
    cp.sub.-- info shared.sub.-- pool[shared.sub.-- pool.sub.-- count-1];
    u2 mem.sub.-- alloc.sub.-- req;
    u2 classfile.sub.-- count;
    ClassFile classfiles[classfile.sub.-- count];
    ______________________________________


In one embodiment of the invention, a new constant type is defined with a corresponding constant type tag. The new constant type provides as its info[ ] element an index into the shared constant table. During pre-processing, duplicated constant elements are placed in the shared constant pool as a shared element, and an element of the new constant type replaces the duplicated element in the reduced pool to direct constant resolution to the shared element in the shared constant pool. Reduction occurs because the replacement element is just a pointer to the actual constant placed in the shared constant pool.

FIG. 5 is a simplified block diagram of an embodiment of the multi-class file format. Mclass file 500 comprises shared constant pool table 501, memory allocation requirements 502 and the set of individual classes 503. The set of individual classes 503 comprises the class file structures for classes 1-N (N being the number of classes in the set), along with the corresponding reduced constant pool tables 1-N. The size of the shared constant pool table 501 is dependent on the number of duplicate constants found in the set of classes. The memory allocation requirements 502 may be represented as a single value indicating the total memory needed to load all class structures (classes 1-N) in individual classes 503, as well as the shared constant pool table 501. The shared pool count and classfile count (not shown in FIG. 5) identify the number of elements in the shared constant pool table 501 and the classfiles array of ClassFile structures (represented by classes 503), respectively.

The multi-class file is typically considerably smaller than the sum of the sizes of the individual class files that it was derived from. It can be loaded by the virtual machine during or prior to the execution of an application, instead of having to load each contained class on demand. The virtual machine is also able to take advantage of the allocation requirements information to pre-allocate all required memory for the multi-class set. This solves many of the problems associated with class loading.

Classes in a multi-class set share information between classes, and therefore are smaller. This provides the following advantages:

a) the classes take up less space on servers or storage devices;

b) the classes take less network or file transfer time to read;

c) the classes take up less memory when loaded; and

d) execution is faster, since shared constants are resolved at most once.

Multi-class sets consolidate the loading of required classes instead of loading the classes one by one. Using allocation information, only one dynamic memory allocation is needed instead of multiple allocation operations. This results in less fragmentation, less time spent in the allocator, and less waste of memory space.

Because the class files are consolidated in a single multi-class file, only a single transaction is needed to perform a network or file system search, to set up a transfer session (e.g., HTTP) and to transfer the entire set of classes. This minimizes pauses in the execution that can result from such transactions and provides for deterministic execution, with no pauses for class loading during a program run. Also, once the multi-class file is loaded and parsed, there is no need for the computer executing the program to remain connected to the source of the classes.

FIG. 6 illustrates the runtime data areas of the virtual machine when a multi-class file is processed and loaded in accordance with an embodiment of the invention. In FIG. 6, runtime data areas 600 comprise multiple program counter registers (PC REG 1-M) and multiple stacks 1-M. One program counter register and one stack are allocated to each thread executing in the virtual machine. Each program counter register contains the address of the virtual machine instruction for the current method being executed by the respective thread. The stacks are used by the respective threads to store local variables, partial results and an operand stack.

Runtime data areas 600 further comprise heap 601, which contains method area 602. Heap 601 is the runtime data area from which memory for all class instances and arrays is allocated. Method area 602 is shared among all threads, and stores class structures such as the constant pool, field and method data, and the code for methods. Within method area 602, memory block 603, which may or may not be contiguous, is allocated to the multi-class set of classes "S." Other regions in heap 601 may be allocated to "S" as well. Reduced constant pools 1-N, along with shared constant pool 604, reside within block 603.

Due to the removal of redundant constants in accordance with an embodiment of the invention, the size of block 603 required to contain reduced constant pools 1-N and shared constant pool 604 is much smaller than would be required to accommodate constant pools 1-N, were they not reduced. Also, the allocations in block 603 are much less fragmented (and may be found in contiguous memory) than the memory that would be allocated were the classes to be loaded one by one.

Thus, a method and apparatus for pre-processing and packaging class files has been described in conjunction with one or more specific embodiments. The invention is defined by the claims and their full scope of equivalents.

CHAPTER 4

The Class File Format

This chapter describes the Java Virtual Machine class file format. Each class file contains one Java type, either a class or an interface. Compliant Java Virtual Machine implementations must be capable of dealing with all class files that conform to the specification provided by this book.

A class file consists of a stream of 8-bit bytes. All 16-bit, 32-bit, and 64-bit quantities are constructed by reading in two, four, and eight consecutive 8-bit bytes, respectively. Multibyte data items are always stored in big-endian order, where the high bytes come first. In Java, this format is supported by inter-faces java.io.DataInput and java.io.DataOutput and classes such as java.io.DataInputStream and java.io.DataOutputStream.

This chapter defines its own set of data types representing Java class file data: The types u1, u2, and u4 represent an unsigned one-, two-, or four-byte quantity, respectively. In Java, these types may be read by methods such as readUnsignedByte, readUnsignedShort, and readint of the interface java.io.DataInput.

The Java class file format is presented using pseudostructures written in a C-like structure notation. To avoid confusion with the fields of Java Virtual Machine classes and class instances, the contents of the structures describing the Java class file format are referred to as items. Unlike the fields of a C structure, successive items are stored in the Java class file sequentially, without padding or alignment.

Variable-sized tables, consisting of variable-sized items, are used in several class file structures. Although we will use C-like array syntax to refer to table items, the fact that tables are streams of varying-sized structures means that it is not possible to directly translate a table index into a byte offset into the table.

Where we refer to a data structure as an array, it is literally an array.

4.1 ClassFile

A class file contains a single ClassFile structure:

    ______________________________________
    ClassFile  {
    u4 magic;
    u2 minor.sub.-- version;
    u2 major.sub.-- version;
    u2 constant.sub.-- pool.sub.-- count;
    cp.sub.-- info constant.sub.-- pool[constant.sub.-- pool.sub.-- count-1];
    u2 access.sub.-- flags;
    u2 this.sub.-- class;
    u2 super.sub.-- class;
    u2 interfaces.sub.-- count;
    u2 interfaces[interfaces.sub.-- count]
    u2 fields.sub.-- count;
    field.sub.-- info fields[fields.sub.-- count]
    u2 methods.sub.-- count;
    method.sub.-- info methods[methods.sub.-- count];
    u2 attributes.sub.-- count;
    attribute.sub.-- info attributes [attributes.sub.-- count];
    ______________________________________


The items in the ClassFile structure are as follows:

magic

The magic item supplies the magic number identifying the class file format; it has the value 0xCAFEBABE.

minor.sub.-- version, major.sub.-- version

The values of the minor.sub.-- version and major.sub.-- version items are the minor and major version numbers of the compiler that produced this class file. An implementation of the Java Virtual Machine normally supports class files having a given major version number and minor version numbers 0 through some particular minor.sub.-- version.

If an implementation of the Java Virtual Machine supports some range of minor version numbers and a class file of the same major version but a higher minor version is encountered, the Java Virtual Machine must not attempt to run the newer code. However, unless the major version number differs, it will be feasible to implement a new Java Virtual Machine that can run code of minor versions up to and including that of the newer code.

A Java Virtual Machine must not attempt to run code with a different major version. A change of the major version number indicates a major incompatible change, one that requires a fundamentally different Java Virtual Machine.

In Sun's Java Developer's Kit (JDK) 1.0.2 release, documented by this book, the value of major.sub.-- version is 45. The value of minor.sub.-- version is 3. Only Sun may define the meaning of new class file version numbers.

constant.sub.-- pool.sub.-- count

The value of the constantsool.sub.-- count item must be greater than zero. It gives the number of entries in the constant.sub.-- pool table of the class file, where the constant.sub.-- pool entry at index zero is included in the count but is not present in the constant.sub.-- pool table of the class file. A constant.sub.-- pool index is considered valid if it is greater than zero and less than constant.sub.-- pool.sub.-- count.

constant.sub.-- pool[ ]

The constant.sub.-- pool is a table of variable-length structures (.sctn.4.4) representing various string constants, class names, field names, and other constants that are referred to within the ClassFile structure and its substructures.

The first entry of the constant.sub.-- pool table, constant.sub.-- pool [0], is reserved for internal use by a Java Virtual Machine implementation. That entry is not present in the class file. The first entry in the class file is constant.sub.-- pool [1].

Each of the constant.sub.-- pool table entries at indices 1 through constant.sub.-- pool.sub.-- count.sub.-- 1 is a variable-length structure (.sctn.4.4) whose format is indicated by its first "tag" byte.

access.sub.-- flags

The value of the access.sub.-- flags item is a mask of modifiers used with class and interface declarations. The access.sub.-- flags modifiers are shown in Table 4.1.

    __________________________________________________________________________
    Flag Name
             Value
                 Meaning               Used By
    __________________________________________________________________________
    ACC.sub.-- PUBLIC
             0x0001
                 Is public; may be accessed from outside its
                                       Class, interface
    ACC.sub.-- FINAL
             0x0010
                 Is final; no subclasses allowed.
                                       Class
    ACC.sub.-- SUPER
             0x0020
                 Treat superclass methods specially in invokespecial.
                                       Class, interface
    ACC.sub.-- INTERFACE
             0x0200
                 Is an interface.      Interface
    ACC.sub.-- ABSTRACT
             0x0400
                 Is abstract; may not be instantiated.
                                       Class, interface
    __________________________________________________________________________


An interface is distinguished by its ACC.sub.-- INTERFACE flag being set. If ACC.sub.-- INTERFACE is not set, this class file defines a class, not an interface.

Interfaces may only use flags indicated in Table 4.1 as used by interfaces. Classes may only use flags indicated in Table 4.1 as used by classes. An interface is implicitly abstract (.sctn.2.13.1); its ACC.sub.-- ABSTRACT flag must be set. An interface cannot be final; its implementation could never be completed (.sctn.2.13.1) if it were, so it could not have its ACC.sub.-- FINAL flag set.

The flags ACC.sub.-- FINAL and ACC.sub.-- ABSTRACT cannot both be set for a class; the implementation of such a class could never be completed (.sctn.2.8.2).

The setting of the ACC.sub.-- SUPER flag directs the Java Virtual Machine which of two alternative semantics for its invokespecial instruction to express; it exists for backward compatibility for code compiled by Sun's older Java compilers. All new implementations of the Java Virtual Machine should implement the semantics for invokespecial documented in Chapter 6, "Java Virtual Machine Instruction Set." All new compilers to the Java Virtual Machine's instruction set should set the ACC.sub.-- SUPER flag. Sun's older Java compilers generate ClassFile flags with ACC.sub.-- SUPER unset. Sun's older Java Virtual Machine implementations ignore the flag if it is set.

All unused bits of the access.sub.-- flags item, including those not assigned in Table 4.1, are reserved for future use. They should be set to zero in generated class files and should be ignored by Java Virtual Machine implementations.

this.sub.-- class

The value of the this.sub.-- class item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Class.sub.-- info (.sctn.4.4.1) structure representing the class or interface defined by this class file.

super.sub.-- class

For a class, the value of the super.sub.-- class item either must be zero or must be a valid index into the constant.sub.-- pool table. If the value of the super.sub.-- class item is nonzero, the constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Class.sub.-- info (.sctn.4.4.1) structure representing the superclass of the class defined by this class file. Neither the superclass nor any of its superclasses may be a final class.

If the value of super.sub.-- class is zero, then this class file must represent the class java.lang.Object, the only class or interface without a superclass.

For an interface, the value of super.sub.-- class must always be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Class.sub.-- info structure representing the class java.lang.Object.

interfaces.sub.-- count

The value of the interfaces.sub.-- count item gives the number of direct superinterfaces of this class or interface type.

interfaces[ ]

Each value in the interfaces array must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at each value of interfaces [i], where 0 .English Pound. i < interfaces.sub.-- count, must be a CONSTANT.sub.-- Class.sub.-- info (.sctn.4.4.1) structure representing an interface which is a direct superinterface of this class or interface type, in the left-to-right order given in the source for the type.

fields.sub.-- count

The value of the fields.sub.-- count item gives the number of field.sub.-- info structures in the fields table. The field.sub.-- info (.sctn.4.5) structures represent all fields, both class variables and instance variables, declared by this class or interface type.

fields[ ]

Each value in the fields table must be a variable-length field.sub.-- info(.sctn.4.5) structure giving a complete description of a field in the class or interface type. The fields table includes only those fields that are declared by this class or interface. It does not include items representing fields that are inherited from superclasses or superinterfaces.

methods.sub.-- count

The value of the methods.sub.-- count item gives the number of method.sub.-- info structures in the methods table.

methods[ ]

Each value in the methods table must be a variable-length method.sub.-- info (.sctn.4.6) structure giving a complete description of and Java Virtual Machine code for a method in the class or interface.

The method.sub.-- info structures represent all methods, both instance methods and, for classes, class (static) methods, declared by this class or interface type. The methods table only includes those methods that are explicitly declared by this class. Interfaces have only the single method <clinit>, the interface initialization method (.sctn.3.8). The methods table does not include items representing methods that are inherited from superclasses or superinterfaces.

attributes.sub.-- count

The value of the attributes.sub.-- count item gives the number of attributes (.sctn.4.7) in the attributes table of this class.

attributes[ ]

Each value of the attributes table must be a variable-length attribute structure. A ClassFile structure can have any number of attributes (.sctn.4.7) associated with it.

The only attribute defined by this specification for the attributes table of a ClassFile structure is the SourceFile attribute (.sctn.4.7.2).

A Java Virtual Machine implementation is required to silently ignore any or all attributes in the attributes table of a ClassFile structure that it does not recognize. Attributes not defined in this specification are not allowed to affect the semantics of the class file, but only to provide additional descriptive information (.sctn.4.7.1).

4.2 Internal Form of Fully Qualified Class Names

Class names that appear in class file structures are always represented in a fully qualified form (.sctn.2.7.9). These class names are always represented as CONSTANT.sub.-- Utf8.sub.-- info (.sctn.4.4.7) structures, and they are referenced from those CONSTANT.sub.-- NameAndType.sub.-- info (.sctn.4.4.6) structures that have class names as part of their descriptor (.sctn.4.3, as well as from all CONSTANT.sub.-- Class.sub.-- info (.sctn.4.4.1) structures.

For historical reasons the exact syntax of fully qualified class names that appear in class file structures differs from the familiar Java fully qualified class name documented in .sctn.2.7.9. In the internal form, the ASCII periods (`.`) that normally separate the identifiers (.sctn.2.2) that make up the fully qualified name are replaced by ASCII forward slashes (`/`). For example, the normal fully qualified name of class Thread is java.lang.Thread. In the form used in descriptors in class files, a reference to the name of class Thread is implemented using a CONSTANT.sub.-- Utf8.sub.-- info structure representing the string "java/lang/Thread".

4.3 Descriptors

A descriptor is a string representing the type of a field or method.

4.3.1 Grammar Notation

Descriptors are specified using a grammar. This grammar is a set of productions that describe how sequences of characters can form syntactically correct descriptors of various types. Terminal symbols of the grammar are shown in bold fixed-width font. Nonterminal symbols are shown in italic type. The definition of a nonterminal is introduced by the name of the nonterminal being defined, followed by a colon. One or more alternative right-hand sides for the nonterminal then follow on succeeding lines. A nonterminal symbol on the right-hand side of a production that is followed by an asterisk (*) represents zero or more possibly different values produced from that nonterrinal, appended without any intervening space.

4.3.2 Field Descriptors

A field descriptor represents the type of a class or instance variable. It is a series of characters generated by the grammar:

FieldDescriptor:

FieldType

ComponentType:

FieldType

FieldType:

BaseType

ObjectType

ArrayType

BaseType:

C

D

F

I

J

S

Z

ObjectType:

L<classname>;

ArrayType:

[ComponentType

The characters of BaseType, the L and; of ObjectType, and the [ of ArrayType are all ASCII characters. The <classname> represents a fully qualified class name, for instance, java.lang.Thread. For historical reasons it is stored in a class file in a modified internal form (.sctn.4.2).

The meaning of the field types is as follows:

    ______________________________________
    B           byte     signed byte
    C           char     character
    D           double   double-precision IEEE 754 float
    F           float    single-precision IEEE 754 float
    I           int      integer
    J           long     long integer
    L<classname>;
                . . .    an instance of the class
    S           short    signed short
    Z           boolean  true or false
    [           . . .    one array dimension
    ______________________________________


For example, the descriptor of an int instance variable is simply I. The descriptor of an instance variable of type Object is Ljava/lang/Object;. Note that the internal form of the fully qualified class name for class Object is used. The descriptor of an instance variable that is a multidimensional double array,

double d[ ] [ ] [ ];

is

[ [ [D

4.3.3 Method Descriptors

A parameter descriptor represents a parameter passed to a method:

ParameterDescriptor:

FieldType

A method descriptor represents the parameters that the method takes and the value that it returns:

MethodDescriptor:

(ParameterDescriptor*)ReturnDescriptor

A return descriptor represents the return value from a method. It is a series of characters generated by the grammar:

ReturnDescriptor:

FieldType

The character V indicates that the method returns no value (its return type is void). Otherwise, the descriptor indicates the type of the return value.

A valid Java method descriptor must represent 255 or fewer words of method parameters, where that limit includes the word for this in the case of instance method invocations. The limit is on the number of words of method parameters and not on the number of parameters themselves; parameters of type long and double each use two words.

For example, the method descriptor for the method

Object mymethod(int i, double d, Thread t)

is

(IDLjava/lang/Thread;)Ljava/lang/Object;

Note that internal forms of the fully qualified class names of Thread and Object are used in the method descriptor.

The method descriptor for mymethod is the same whether mymethod is static or is an instance method. Although an instance method is passed this, a reference to the current class instance, in addition to its intended parameters, that fact is not reflected in the method descriptor. (A reference to this is not passed to a static method.) The reference to this is passed implicitly by the method invocation instructions of the Java Virtual Machine used to invoke instance methods.

4.4 Constant Pool

All constant.sub.-- pool table entries have the following general format:

    ______________________________________
             cp.sub.-- info {
               u1 tag;
               u1 info [ ];
             }
    ______________________________________


Each item in the constant.sub.-- pool table must begin with a 1-byte tag indicating the kind of cp.sub.-- info entry. The contents of the info array varies with the value of tag. The valid tags and their values are listed in Table 4.2

    ______________________________________
    Constant Type        Value
    ______________________________________
    CONSTANT.sub.-- Class
                         7
    CONSTANT.sub.-- Fieldref
                         9
    CONSTANT.sub.-- Methodref
                         10
    CONSTANT.sub.-- InterfaceMethodref
                         11
    CONSTANT.sub.-- String
                         8
    CONSTANT.sub.-- Integer
                         3
    CONSTANT.sub.-- Float
                         4
    CONSTANT.sub.-- Long 5
    CONSTANT.sub.-- Double
                         6
    CONSTANT.sub.-- NameAndType
                         12
    CONSTANT.sub.-- Utf8 1
    ______________________________________


Each tag byte must be followed by two or more bytes giving information about the specific constant. The format of the additional information varies with the tag value.

4.4.1 CONSTANT.sub.-- Class

The CONSTANT.sub.-- Class.sub.-- info structure is used to represent a class or an interface:

    ______________________________________
             CONSTANT.sub.-- Class.sub.-- info {
                u1 tag;
                u2 name.sub.-- index;
             }
    ______________________________________


The items of the CONSTANT.sub.-- Class.sub.-- info structure are the following:

tag

The tag item has the value CONSTANT.sub.-- Class (7).

name.sub.-- index

The value of the name.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Utf8.sub.-- info (.sctn.4.4.7) structure representing a valid fully qualified Java class name (.sctn.2.8.1) that has been converted to the class file's internal form (.sctn.4.2).

Because arrays are objects, the opcodes anewarray and multianewarray can reference array "classes" via CONSTANT.sub.-- Class.sub.-- info (.sctn.4.4.1) structures in the constant.sub.-- pool table. In this case, the name of the class is the descriptor of the array type. For example, the class name representing a two-dimensional int array type;

int[ ] [ ]

is

[[I

The class name representing the type array of class Thread;

Thread[ ]

is

[Ljava.lang.Thread;

A valid Java array type descriptor must have 255 or fewer array dimensions.

4.4.2 CONSTANT.sub.-- Fieldref, CONSTANT.sub.-- Methodref, and CONSTANT.sub.-- InterfaceMethodref

Fields, methods, and interface methods are represented by similar structures:

    ______________________________________
    CONSTANT.sub.-- Fieldref.sub.-- info {
      u1 tag;
      u2 class.sub.-- index;
      u2 name.sub.-- and.sub.-- type.sub.-- index;
    CONSTANT.sub.-- Methodref.sub.-- info {
      u1 tag;
      u2 class.sub.-- index;
      u2 name.sub.-- and.sub.-- type.sub.-- index;
    }
    CONSTANT.sub.-- InterfaceMethodref.sub.-- info {
      u1 tag;
      u2 class.sub.-- index;
      u2 name.sub.-- and.sub.-- type.sub.-- index;
    }
    ______________________________________


The items of these structures are as follows:

tag

The tag item of a CONSTANT.sub.-- Fieldref.sub.-- info structure has the value CONSTANT.sub.-- Fieldref (9).

The tag item of a CONSTANT.sub.-- Methodref.sub.-- info structure has the value CONSTANT.sub.-- Methodref (10).

The tag item of a CONSTANT.sub.-- InterfaceMethodref.sub.-- info structure has the value CONSTANT.sub.-- InterfaceMethodref (11).

class.sub.-- index

The value of the class.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Class.sub.-- info (.sctn.4.4.1) structure representing the class or interface type that contains the declaration of the field or method.

The class.sub.-- index item of a CONSTANT.sub.-- Fieldref.sub.-- info or a CONSTANT.sub.-- Methodref.sub.-- info structure must be a class type, not an interface type. The class.sub.-- index item of a CONSTANT.sub.-- InterfaceMethodref.sub.-- info structure must be an interface type that declares the given method.

name.sub.-- and.sub.-- type.sub.-- index

The value of the name.sub.-- and.sub.-- type.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- NameAndType.sub.-- info (.sctn.4.4.6) structure. This constant.sub.-- pool entry indicates the name and descriptor of the field or method.

If the name of the method of a CONSTANT.sub.-- Methodref.sub.-- info or CONSTANT.sub.-- InterfaceMethodref.sub.-- info begins with a `<` (`u003c`), then the name must be one of the special internal methods (.sctn.3,8), either <init> or <clinit>. In this case, the method must return no value.

4.4.3 CONSTANT.sub.-- String

The CONSTANT.sub.-- String.sub.-- info structure is used to represent constant objects of the type java.lang.String:

    ______________________________________
             CONSTANT.sub.-- String.sub.-- info {
                u1 tag;
                u2 string.sub.-- index;
             }
    ______________________________________


The items of the CONSTANT.sub.-- String.sub.-- info structure are as follows:

tag

The tag item of the CONSTANT.sub.-- String.sub.-- info structure has the value CONSTANT.sub.-- String (8).

string.sub.-- index

The value of the string.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Utf8.sub.-- info (.sctn.4.4.3) structure representing the sequence of characters to which the java.lang.String object is to be initialized.

4.4.4 CONSTANT.sub.-- Integer and CONSTANT.sub.-- Float

The CONSTANT.sub.-- Integer.sub.-- info and CONSTANT.sub.-- Float.sub.-- info structures represent four-byte numeric (int and float) constants:

    ______________________________________
             CONSTANT.sub.-- Integer.sub.-- info {
               u1 tag;
               u4 bytes;
             }
             CONSTANT.sub.-- Float.sub.-- info {
               u1 tag;
               u4 bytes;
             }
    ______________________________________


The items of these structures are as follows:

tag

The tag item of the CONSTANT.sub.-- Integer.sub.-- info structure has the value CONSTANT.sub.-- Integer (3).

The tag item of the CONSTANT.sub.-- Float.sub.-- info structure has the value CONSTANT.sub.-- Float (4).

bytes

The bytes item of the CONSTANT.sub.-- Integer.sub.-- info structure contains the value of the int constant. The bytes of the value are stored in big-endian (high byte first) order.

The bytes item of the CONSTANT.sub.-- Float.sub.-- info structure contains the value of the float constant in IEEE 754 floating-point "single format" bit layout. The bytes of the value are stored in big-endian (high byte first) order, and are first converted into an int argument. Then:

If the argument is 0x7f800000, the float value will be positive infinity.

If the argument is 0xff800000, the float value will be negative infinity.

If the argument is in the range 0x7f800001 through 0x7fffffff or in the range 0xff800001 through 0xffffffff, the float value will be NaN.

In all other cases, let s, e, and m be three values that might be computed by

int s=((bytes >> 31) == 0) ? 1 : -1;

int e=((bytes >> 23) & 0xff);

int m=(e == 0) ?

(bytes & 0x7fffff) << 1 :

(bytes & 0x7fffff) .vertline. 0x800000;

Then the float value equals the result of the mathematical expression

s.multidot.m.multidot.2.sup.e-150

4.4.5 CONSTANT.sub.-- Long and CONSTANT.sub.-- Double

The CONSTANT.sub.-- Long.sub.-- info and CONSTANT.sub.-- Double.sub.-- info represent eight-byte numeric (long and double) constants:

    ______________________________________
             CONSTANT.sub.-- Long.sub.-- info {
               u1 tag;
               u4 high.sub.-- bytes;
               u4 low.sub.-- bytes;
             }
             CONSTANT.sub.-- Double.sub.-- info {
               u1 tag;
               u4 high.sub.-- bytes;
               u4 low.sub.-- bytes;
             }
    ______________________________________


All eight-byte constants take up two entries in the constant.sub.-- pool table of the class file, as well as in the in-memory version of the constant pool that is constructed when a class file is read. If a CONSTANT.sub.-- Long.sub.-- info or CONSTANT.sub.-- Double.sub.-- info structure is the item in the constant.sub.-- pool table at index n, then the next valid item in the pool is located at index n+2. The constant.sub.-- pool index n+1 must be considered invalid and must not be used..sup.1

.sup.1 In retrospect, making eight-byte constants take two constant pool entries was a poor choice.

The items of these structures are as follows:

tag

The tag item of the CONSTANT.sub.-- Long.sub.-- info structure has the value CONSTANT.sub.-- Long (5).

The tag item of the CONSTANT.sub.-- Double.sub.-- info structure has the value CONSTANT.sub.-- Double (6).

high.sub.-- bytes, low.sub.-- bytes

The unsigned high bytes and low bytes items of the CONSTANT.sub.-- Long structure together contain the value of the long constant ((long)high.sub.-- bytes<<32)+low-bytes, where the bytes of each of high.sub.-- bytes and low.sub.-- bytes are stored in big-endian (high byte first) order.

The high.sub.-- bytes and low.sub.-- bytes items of the CONSTANT.sub.-- Double.sub.-- info structure contain the double value in IEEE 754 floating-point "double format" bit layout. The bytes of each item are stored in big-endian (high byte first) order. The high.sub.-- bytes and low.sub.-- bytes items are first converted into a long argument. Then:

If the argument is 0x7f80000000000000L, the double value will be positive infinity.

If the argument is 0xff80000000000000L, the double value will be negative infinity.

If the argument is in the range 0x7ff0000000000001L through 0x7fffffffffffffffL or in the range 0xfff0000000000001L through 0xffffffffffffffffL, the double value will be NaN.

In all other cases, let s, e, and m be three values that might be computed from the argument:

int s=((bits >> 63) == 0) ? 1 : -1;

int e=(int)((bits >> 52) & 0x7ffL);

long m=(e == 0) ?

(bits & 0xfffffffffffffL) << 1 :

(bits & 0xfffffffffffffL) .vertline. 0x10000000000000L;

Then the floating-point value equals the double value of the mathematical expression

s.multidot.m.multidot.2.sup.e-1075

4.4.6 CONSTANT.sub.-- NameAndType

The CONSTANT.sub.-- NameAndType.sub.-- info structure is used to represent a field or method, without indicating which class or interface type it belongs to:

    ______________________________________
             CONSTANT.sub.-- NameAndType.sub.-- info {
               u1 tag;
               u2 name.sub.-- index;
               u2 descriptor.sub.-- index;
             }
    ______________________________________


The items of the CONSTANT.sub.-- NameAndType.sub.-- info structure are as follows:

tag

The tag item of the CONSTANT.sub.-- NameAndType.sub.-- info structure has the value CONSTANT.sub.-- NameAndType (12).

name.sub.-- index

The value of the name.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Utf8.sub.-- info (.sctn.4.4.7) structure representing a valid Java field name or method name (.sctn.2.7) stored as a simple (not fully qualified) name (.sctn.2.7.1), that is, as a Java identifier.

descriptor.sub.-- index

The value of the descriptor.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Utf 8.sub.-- info (.sctn.4.4.7) structure representing a valid Java field descriptor (.sctn.4.3.2) or method descriptor (.sctn.4.3.3).

4.4.7 CONSTANT.sub.-- Utf8

The CONSTANT.sub.-- Utf8.sub.-- info structure is used to represent constant string values.

UTF-8 strings are encoded so that character sequences that contain only non-null ASCII characters can be represented using only one byte per character, but characters of up to 16 bits can be represented. All characters in the range `u0001` to `u007F` are represented by a single byte:

0 bits 0-7

The seven bits of data in the byte give the value of the character represented. The null character (`u0000`) and characters in the range `u0080`, to `u07FF` are represented by a pair of bytes x and y:

x: 1 1 0 bits 6-10 y: 1 0 bits 0-5

The bytes represent the character with the value ((x & 0x1f) << 6)+(y & 0x3f).

Characters in the range `u0800` to `uFFFF` are represented by three bytes x, y, and z:

x: 1 1 1 0 bits 12-15 y: 1 0 bits 6-11 z: 1 0 bits 0-5

The character with the value ((x & 0xf)<<12)+((y & 0x3f)<<6)+(z & 0x3f) is represented by the bytes. The bytes of multibyte characters are stored in the class file in big-endian (high byte first) order. There are two differences between this format and the "standard" UTF-8 format. First, the null byte (byte)0 is encoded using the two-byte format rather than the one-byte format, so that Java Virtual Machine UTF-8 strings never have embedded nulls. Second, only the one-byte, two-byte, and three-byte formats are used. The Java Virtual Machine does not recognize the longer UTF-8 formats.

For more information regarding the UTF-8 format, see File System Safe UCS Transfonnation Format(FSS.sub.-- UTF), X/Open Preliminary Specification, X/Open Company Ltd., Document Number: P316. This information also appears in ISO/IEC 10646, Annex P.

The CONSTANT.sub.-- Utf8 .sub.-- info structure is

    ______________________________________
             CONSTANT.sub.-- Utf8.sub.-- info {
               u1 tag;
               u2 length;
               u1 bytes[length];
             }
    ______________________________________


The items of the CONSTANT.sub.-- Utf8 .sub.-- info structure are the following:

tag

The tag item of the CONSTANT.sub.-- Utf8.sub.-- info structure has the value CONSTANT.sub.-- Utf8 (1).

length

The value of the length item gives the number of bytes in the bytes array (not the length of the resulting string). The strings in the CONSTANT.sub.-- Utf8.sub.-- info structure are not null-terminated.

bytes[ ]

The bytes array contains the bytes of the string. No byte may have the value (byte)0 or (byte)0xf0-(byte)0xff.

4.5 Fields

Each field is described by a variable-length field.sub.-- info structure. The format of this structure is

    ______________________________________
             field.sub.-- info {
                u2 access.sub.-- flags
                u2 name.sub.-- index;
                u2 descriptor.sub.-- index;
                u2 attributes.sub.-- count;
                attribute.sub.-- info attributes[attributes.sub.-- count];
             }
    ______________________________________


The items of the field.sub.-- info structure are as follows:

access.sub.-- flags

The value of the access.sub.-- flags item is a mask of modifiers used to describe access permission to and properties of a field. The access.sub.-- flags modifiers are shown in Table 4.3.

    __________________________________________________________________________
    Flag Name Value
                  Meaning               Used By
    __________________________________________________________________________
    ACC.sub.-- PUBLIC
              0x0001
                  Is public; may be accessed from outside its
                                        Any field
    ACC.sub.-- PRIVATE
              0x0002
                  Is private; usable only within the defining
                                        Class field
    ACC.sub.-- PROTECTED
              0x0004
                  Is protected; may be accessed within subclasses.
                                        Class field
    ACC.sub.-- STATIC
              0x0008
                  Is static.            Any field
    ACC.sub.-- FINAL
              0x0010
                  Is final; no further overriding or assignment
                                        Any field
                  initialization.
    ACC.sub.-- VOLATILE
              0x0040
                  Is volatile; cannot be cached.
                                        Class field
    ACC.sub.-- TRANSIENT
              0x0080
                  Is transient; not written or read by a persistent
                                        Class field
                  object manager.
    __________________________________________________________________________


Fields of interfaces may only use flags indicated in Table 4.3 as used by any field. Fields of classes may use any of the flags in Table 4.3.

All unused bits of the access.sub.-- flags item, including those not assigned in Table 4.3, are reserved for future use. They should be set to zero in generated class files and should be ignored by Java Virtual Machine implementations.

Class fields may have at most one of flags ACC.sub.-- PUBLIC, ACC.sub.-- PROTECTED, and ACC.sub.-- PRIVATE set (.sctn.2.7.8). A class field may not have both ACC.sub.-- FINAL and ACC.sub.-- VOLATILE set (.sctn.2.9.1).

Each interface field is implicitly static and final (.sctn.2.13.4) and must have both its ACC.sub.-- STATIC and ACC.sub.-- FINAL flags set. Each interface field is implicitly public (.sctn.2.13.4) and must have its ACC.sub.-- PUBLIC flag set.

name.sub.-- index

The value of the name.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Utf8.sub.-- info (.sctn.4.4.7) structure which must represent a valid Java field name (.sctn.2.7) stored as a simple (not fully qualified) name (.sctn.2.7.1), that is, as a Java identifier.

descriptor.sub.-- index

The value of the descriptor.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Utf8 (.sctn.4.4.7) structure which must represent a valid Java field descriptor (.sctn.4.3.2).

attributes.sub.-- count

The value of the attributes.sub.-- count item indicates the number of additional attributes (.sctn.4.7) of this field.

attributes[ ]

Each value of the attributes table must be a variable-length attribute structure. A field can have any number of attributes (.sctn.4.7) associated with it.

The only attribute defined for the attributes table of a field.sub.-- info structure by this specification is the ConstantValue attribute (.sctn.4.7.3).

A Java Virtual Machine implementation must recognize ConstantValue attributes in the attributes table of a field.sub.-- info structure. A Java Virtual Machine implementation is required to silently ignore any or all other attributes in the attributes table that it does not recognize. Attributes not defined in this specification are not allowed to affect the semantics of the class file, but only to provide additional descriptive information (.sctn.4.7.1).

4.6 Methods

Each method, and each instance initialization method <init>, is described by a variable-length method.sub.-- info structure. The structure has the following format:

    ______________________________________
    method.sub.-- info {
    u2 access.sub.-- flags;
    u2 name.sub.-- index;
    u2 descriptor.sub.-- index;
    u2 attributes.sub.-- count;
    attribute.sub.-- info attributes [attributes.sub.-- count];
    ______________________________________


The items of the method.sub.-- info structure are as follows:

access.sub.-- flags

The value of the access.sub.-- flags item is a mask of modifiers used to describe access permission to and properties of a method or instance initialization method (3.8). The access.sub.-- flags modifiers are shown in Table 4.4.

    __________________________________________________________________________
    Flag Name   Value
                    Meaning           Used By
    __________________________________________________________________________
    ACC.sub.-- PUBLIC
                0x0001
                    Is public; may be accessed from outside
                                      Any method
                    package.
    ACC.sub.-- PRIVATE
                0x0002
                    Is private; usable only within the defining
                                      Class/instance method
                    class.
    ACC.sub.-- PROTECTED
                0x0004
                    Is protected; may be accessed within
                                      Class/instance method
                    subclasses.
    ACC.sub.-- STATIC
                0x0008
                    Is static.        Class/instance method
    ACC.sub.-- FINAL
                0x0010
                    Is final; no overriding is allowed.
                                      Class/instance method
    ACC.sub.-- SYNCHRONIZED
                0x0020
                    Is synchronized; wrap use in monitor lock.
                                      Class/instance method
    ACC.sub.-- NATIVE
                0x0100
                    Is native; implemented in a language other
                                      Class/instance method
                    than Java.
    ACC.sub.-- ABSTRACT
                0x0400
                    Is abstract, no implementation is provided.
                                      Any method
    __________________________________________________________________________


Methods in interfaces may only use flags indicated in Table 4.4 as used by any method. Class and instance methods (.sctn.2.10.3) may use any of the flags in Table 4.4. Instance initialization methods (.sctn.3.8) may only use ACC.sub.-- PUBLIC, ACC.sub.-- PROTECTED, and ACC.sub.-- PRIVATE.

All unused bits of the access.sub.-- flags item, including those not assigned in Table 4.4, are reserved for future use. They should be set to zero in generated class files and should be ignored by Java Virtual Machine implementations.

At most one of the flags ACC.sub.-- PUBLIC, ACC.sub.-- PROTECTED, and ACC.sub.-- PRIVATE may be set for any method. Class and instance methods may not use ACC.sub.-- ABSTRACT together with ACC.sub.-- FINAL, ACC.sub.-- NATIVE, or ACC.sub.-- SYNCHRONIZED (that is, native and synchronized methods require an implementation). A class or instance method may not use ACC.sub.-- PRIVATE with ACC.sub.-- ABSTRACT (that is, a private method cannot be overridden, so such a method could never be implemented or used). A class or instance method may not use ACC.sub.-- STATIC with ACC.sub.-- ABSTRACT (that is, a static method is implicitly final and thus cannot be overridden, so such a method could never be implemented or used).

Class and interface initialization methods (.sctn.3.8), that is, methods named <clinit>, are called implicitly by the Java Virtual Machine; the value of their access.sub.-- flags item is ignored.

Each interface method is implicitly abstract, and so must have its ACC.sub.-- ABSTRACT flag set. Each interface method is implicitly public (.sctn.2.13.5), and so must have its ACC.sub.-- PUBLIC flag set.

name.sub.-- index

The value of the name.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Utf8.sub.-- info (.sctn.4.4.7) structure representing either one of the special internal method names (.sctn.3.8), either <init>or <clinit>, or a valid Java method name (.sctn.2.7), stored as a simple (not fully qualified) name (.sctn.2.7.1).

descriptor.sub.-- index

The value of the descriptor.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Utf8.sub.-- info (.sctn.4.4.7) structure representing a valid Java method descriptor (.sctn.4.3.3).

attributes.sub.-- count

The value of the attributes.sub.-- count item indicates the number of additional attributes (.sctn.4.7) of this method.

attributes[ ]

Each value of the attributes table must be a variable-length attribute structure. A method can have any number of optional attributes (.sctn.4.7) associated with it.

The only attributes defined by this specification for the attributes table of a method.sub.-- info structure are the Code (.sctn.4.7.4) and Exceptions (.sctn.4.7.5) attributes.

A Java Virtual Machine implementation must recognize Code (.sctn.4.7.4) and Exceptions (.sctn.4.7.5) attributes. A Java Virtual Machine implementation is required to silently ignore any or all other attributes in the attributes table of a method.sub.-- info structure that it does not recognize. Attributes not defined in this specification are not allowed to affect the semantics of the class file, but only to provide additional descriptive information (.sctn.4.7.1).

4.7 Attributes

Attributes are used in the ClassFile (.sctn.4.1), field.sub.-- info (.sctn.4.5), method.sub.-- info (.sctn.4.6), and Code.sub.-- attribute (.sctn.4.7.4) structures of the class file format. All attributes have the following general format:

    ______________________________________
    attribute.sub.-- info {
            u2 attribute.sub.-- name.sub.-- index;
            u4 attribute.sub.-- length;
            u1 info[attribute.sub.-- length];
    ______________________________________


For all attributes, the attribute.sub.-- name.sub.-- index must be a valid unsigned 16-bit index into the constant pool of the class. The constant.sub.-- pool entry at attribute.sub.-- name.sub.-- index must be a CONSTANT.sub.-- Utf8 (.sctn.4.4.7) string representing the name of the attribute. The value of the attribute.sub.-- length item indicates the length of the subsequent information in bytes. The length does not include the initial six bytes that contain the attribute.sub.-- name.sub.-- index and attribute.sub.-- length items.

Certain attributes are predefined as part of the class file specification. The predefined attributes are the SourceFile (4.7.2), ConstantValue (.sctn.4.7.3), Code (.sctn.4.7.4), Exceptions (.sctn.4.7.5), LineNumberTable (.sctn.4.7.6), and Local-VariableTable (.sctn.4.7.7) attributes. Within the context of their use in this specification, that is, in the attributes tables of the class file structures in which they appear, the names of these predefined attributes are reserved.

Of the predefined attributes, the Code, ConstantValue, and Exceptions attributes must be recognized and correctly read by a class file reader for correct interpretation of the class file by a Java Virtual Machine. Use of the remaining predefined attributes is optional; a class file reader may use the information they contain, and otherwise must silently ignore those attributes.

4.7.1 Defining and Naming New Attributes

Compilers for Java source code are permitted to define and emit class files containing new attributes in the attributes tables of class file structures. Java Virtual Machine implementations are permitted to recognize and use new attributes found in the attributes tables of class file structures. However, all attributes not defined as part of this Java Virtual Machine specification must not affect the semantics of class or interface types. Java Virtual Machine implementations are required to silently ignore attributes they do not recognize.

For instance, defining a new attribute to support vendor-specific debugging is permitted. Because Java Virtual Machine implementations are required to ignore attributes they do not recognize, class files intended for that particular Java Virtual Machine implementation will be usable by other implementations even if those implementations cannot make use of the additional debugging information that the class files contain.

Java Virtual Machine implementations are specifically prohibited from throwing an exception or otherwise refusing to use class files simply because of the presence of some new attribute. Of course, tools operating on class files may not run correctly if given class files that do not contain all the attributes they require.

Two attributes that are intended to be distinct, but that happen to use the same attribute name and are of the same length, will conflict on implementations that recognize either attribute. Attributes defined other than by Sun must have names chosen according to the package naming convention defined by The Java Language Specification. For instance, a new attribute defined by Netscape might have the name "COM.Netscape.new-attribute".

Sun may define additional attributes in future versions of this class file specification.

4.7.2 SourceFile Attribute

The SourceFile attribute is an optional fixed-length attribute in the attributes table of the ClassFile (.sctn.4.1) structure. There can be no more than one SourceFile attribute in the attributes table of a given ClassFile structure.

The SourceFile attribute has the format

    ______________________________________
    SourceFile.sub.-- attribute {
            u2 attribute.sub.-- name.sub.-- index;
            u4 attribute.sub.-- length;
            u2 sourcefile.sub.-- index;
           }
    ______________________________________


The items of the SourceFile.sub.-- attribute structure are as follows:

attribute.sub.-- name.sub.-- index

The value of the attribute.sub.-- name.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Utf8.sub.-- info (.sctn.4.4.7) structure representing the string "SourceFile".

attribute.sub.-- length

The value of the attribute.sub.-- length item of a SourceFile.sub.-- attribute structure must be 2.

sourcefile.sub.-- index

The value of the sourcefile.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant pool entry at that index must be a CONSTANT.sub.-- Utf8.sub.-- info (.sctn.4.4.7) structure representing the string giving the name of the source file from which this class file was compiled.

Only the name of the source file is given by the SourceFile attribute. It never represents the name of a directory containing the file or an absolute path name for the file. For instance, the SourceFile attribute might contain the file name foo.java but not the UNIX pathname /home/lindholm/foo.java.

4.7.3 ConstantValue Attribute

The Constantvalue attribute is a fixed-length attribute used in the attributes table of the field.sub.-- info (.sctn.4.5) structures. A ConstantValue attribute represents the value of a constant field that must be (explicitly or implicitly) static; that is, the ACC.sub.-- STATIC bit (.sctn.Table 4.3) in the flags item of the field.sub.-- info structure must be set. The field is not required to be final. There can be no more than one ConstantValue attribute in the attributes table of a given field.sub.-- info structure. The constant field represented by the field.sub.-- info structure is assigned the value referenced by its ConstantValue attribute as part of its initialization (.sctn.2.16.4).

Every Java Virtual Machine implementation must recognize ConstantValue attributes.

The ConstantValue attribute has the format

    ______________________________________
    ConstantValue.sub.-- attribute {
            u2 attribute.sub.-- name.sub.-- index;
            u4 attribute.sub.-- length;
            u2 constantvalue.sub.-- index;
    ______________________________________


The items of the ConstantValue.sub.-- attribute structure are as follows:

attribute.sub.-- name.sub.-- index

The value of the attribute.sub.-- name.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Utf8.sub.-- info (.sctn.4.4.7) structure representing the string "ConstantValue".

attribute.sub.-- length

The value of the attribute.sub.-- length item of a ConstantValue.sub.-- attribute structure must be 2.

constantvalue.sub.-- index

The value of the constantvalue.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must give the constant value represented by this attribute.

The constant.sub.-- pool entry must be of a type appropriate to the field, as shown by Table 4.5.

    ______________________________________
    Field Type          Entry Type
    ______________________________________
    long                CONSTANT.sub.-- Long
    float               CONSTANT.sub.-- Float
    double              CONSTANT.sub.-- Double
    int, short, char, byte, boolean
                        CONSTANT.sub.-- Integer
    java.lang.String    CONSTANT.sub.-- String
    ______________________________________


4.7.4 Code Attribute

The Code attribute is a variable-length attribute used in the attributes table of method.sub.-- info structures. A Code attribute contains the Java Virtual Machine instructions and auxiliary information for a single Java method, instance initialization method (.sctn.3.8), or class or interface initialization method (.sctn.3.8). Every Java Virtual Machine implementation must recognize Code attributes. There must be exactly one Code attribute in each method.sub.-- info structure.

The Code attribute has the format

    ______________________________________
    Code.sub.-- attribute {
    u2 attribute.sub.-- name.sub.-- index;
    u4 attribute.sub.-- length;
    u2 max.sub.-- stack
    u2 max.sub.-- locals;
    u4 code.sub.-- length;
    u1 code[code.sub.-- length];
    u2 exception.sub.-- table.sub.-- length;
    {        u2 start.sub.-- pc;
             u2 end.sub.-- pc;
             u2 handler.sub.-- pc;
             u2 catch.sub.-- type;
    }        exception.sub.-- table[exception.sub.-- table.sub.-- length];
    u2 attributes.sub.-- count;
    attribute.sub.-- info attributes[attributes.sub.-- count];
    ______________________________________


The items of the Code.sub.-- attribute structure are as follows:

attribute.sub.-- name.sub.-- index

The value of the attribute.sub.-- name.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Utf8.sub.-- info (.sctn.4.4.7) structure representing the string "Code".

attribute.sub.-- length

The value of the attribute.sub.-- length item indicates the length of the attribute, excluding the initial six bytes.

max.sub.-- stack

The value of the max.sub.-- stack item gives the maximum number of words on the operand stack at any point during execution of this method.

max.sub.-- locals

The value of the max.sub.-- locals item gives the number of local variables used by this method, including the parameters passed to the method on invocation. The index of the first local variable is 0. The greatest local variable index for a one-word value is max.sub.-- locals-1. The greatest local variable index for a two-word value is max.sub.-- locals-2.

code.sub.-- length

The value of the code.sub.-- length item gives the number of bytes in the code array for this method. The value of code.sub.-- length must be greater than zero; the code array must not be empty.

code[ ]

The code array gives the actual bytes of Java Virtual Machine code that implement the method.

When the code array is read into memory on a byte addressable machine, if the first byte of the array is aligned on a 4-byte boundary, the tableswitch and lookupswitch 32-bit offsets will be 4-byte aligned; refer to the descriptions of those instructions for more information on the consequences of code array alignment.

The detailed constraints on the contents of the code array are extensive and are given in a separate section (.sctn.4.8).

exception.sub.-- table.sub.-- length

The value of the exception.sub.-- table.sub.-- length item gives the number of entries in the exception.sub.-- table table.

exception.sub.-- table[ ]

Each entry in the exception.sub.-- table array describes one exception handler in the code array. Each exception.sub.-- table entry contains the following items:

start.sub.-- pc, end.sub.-- pc

The values of the two items start.sub.-- pc and end.sub.-- pc indicate the ranges in the code array at which the exception handler is active. The value of start.sub.-- pc must be a valid index into the code array of the opcode of an instruction. The value of end.sub.-- pc either must be a valid index into the code array of the opcode of an instruction, or must be equal to code.sub.-- length, the length of the code array. The value of start.sub.-- pc must be less than the value of end.sub.-- pc.

The start.sub.-- pc is inclusive and end.sub.-- pc is exclusive; that is, the exception handler must be active while the program counter is within the interval [start.sub.-- pc, end.sub.-- pc)..sup.2

.sup.2 The fact that end.sub.-- pc is exclusive is an historical mistake in the Java Virtual Machine: if the Java Virtual Machine code for a method is exactly 65535 bytes long and ends with an instruction that is one byte long, then that instruction cannot be protected by an exception handler. A compiler writer can work around this bug by limiting the maximum size of the generated Java Virtual Machine code for any method, instance initialization method, or static initializer (the size of any code array) to 65534 bytes.

handler.sub.-- pc

The value of the handler.sub.-- pc item indicates the start of the exception handler. The value of the item must be a valid index into the code array, must be the index of the opcode of an instruction, and must be less than the value of the code.sub.-- length item.

catch.sub.-- type

If the value of the catch.sub.-- type item is nonzero, it must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Class.sub.-- info (.sctn.4.4.1) structure representing a class of exceptions that this exception handler is designated to catch. This class must be the class Throwable or one of its subclasses. The exception handler will be called only if the thrown exception is an instance of the given class or one of its subclasses.

If the value of the catch.sub.-- type item is zero, this exception handler is called for all exceptions. This is used to implement finally (see Section 7.13, "Compiling finally").

attributes.sub.-- count

The value of the attributes.sub.-- count item indicates the number of attributes of the Code attribute.

attributes[ ]

Each value of the attributes table must be a variable-length attribute structure. A Code attribute can have any number of optional attributes associated with it.

Currently, the LineNumberTable (.sctn.4.7.6) and LocalVariableTable (.sctn.4.7.7) attributes, both of which contain debugging information, are defined and used with the Code attribute.

A Java Virtual Machine implementation is permitted to silently ignore any or all attributes in the attributes table of a Code attribute. Attributes not defined in this specification are not allowed to affect the semantics of the class file, but only to provide additional descriptive information (.sctn.4.7.1).

4.7.5 Exceptions Attribute

The Exceptions attribute is a variable-length attribute used in the attributes table of a method.sub.-- info (.sctn.4.6 ) structure. The Exceptions attribute indicates which checked exceptions a method may throw. There must be exactly one Exceptions attribute in each method.sub.-- info structure.

The Exceptions attribute has the format

    ______________________________________
    Exceptions.sub.-- attribute {
    u2 attribute.sub.-- name.sub.-- index;
    u4 attribute.sub.-- length;
    u2 number.sub.-- of.sub.-- exceptions;
    u2 exception.sub.-- index.sub.-- table[number.sub.-- of.sub.-- exceptions]
    }
    ______________________________________


The items of the Exceptions.sub.-- attribute structure are as follows:

attribute.sub.-- name.sub.-- index

The value of the attribute.sub.-- name.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be the CONSTANT.sub.-- Utf8.sub.-- info (.sctn.4.4.7) structure representing the string "Exceptions".

attribute.sub.-- length

The value of the attribute.sub.-- length item indicates the attribute length, excluding the initial six bytes.

number.sub.-- of.sub.-- exceptions

The value of the number.sub.-- of.sub.-- exceptions item indicates the number of entries in the exception.sub.-- index.sub.-- table.

exception.sub.-- index.sub.-- table[ ]

Each nonzero value in the exception.sub.-- index.sub.-- table array must be a valid index into the constant.sub.-- pool table. For each table item, if exception index.sub.-- table[i] !=0, where 0 .English Pound. i<number.sub.-- of.sub.-- exceptions, then the constant.sub.-- pool entry at index exception.sub.-- index.sub.-- table[i] must be a CONSTANT.sub.-- Class.sub.-- info (4.4.1) structure representing a class type that this method is declared to throw.

A method should only throw an exception if at least one of the following three criteria is met:

The exception is an instance of RuntimeException or one of its subclasses.

The exception is an instance of Error or one of its subclasses.

The exception is an instance of one of the exception classes specified in the exception.sub.-- index.sub.-- table above, or one of their subclasses.

The above requirements are not currently enforced by the Java Virtual Machine; they are only enforced at compile time. Future versions of the Java language may require more rigorous checking of throws clauses when classes are verified.

4.7.6 LineNumberTable Attribute

The LineNumberTable attribute is an optional variable-length attribute in the attributes table of a Code (.sctn.4.7.4) attribute. It may be used by debuggers to determine which part of the Java Virtual Machine code array corresponds to a given line number in the original Java source file. If LineNumberTable attributes are present in the attributes table of a given Code attribute, then they may appear in any order. Furthermore, multiple LineNumberTable attributes may together represent a given line of a Java source file; that is, LineNumberTable attributes need not be one-to-one with source lines..sup.3

.sup.3 The javac compiler in Sun's JDK 1.0.2 release can in fact generate LineNumberTable attributes which are not in line number order and which are not one-to-one with source lines. This is unfortunate, as we would prefer to specify a one-to-one, ordered mapping of LineNumberTable attributes to source lines, but must yield to backward compatibility.

The LineNumberTable attribute has the format

    ______________________________________
    LineNumberTable.sub.-- attributes {
    u2 attribute.sub.-- name.sub.-- index;
    u4 attribute.sub.-- length;
    u2 line.sub.-- number.sub.-- table.sub.-- length;
    {       u2 start.sub.-- pc;
            u2 line.sub.-- number
    }       line.sub.-- number.sub.-- table[line.sub.-- number.sub.--
            table.sub.-- length];
    ______________________________________


The items of the LineNumberTable.sub.-- attribute structure are as follows:

attribute.sub.-- name.sub.-- index

The value of the attribute.sub.-- name.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Utf8.sub.-- info (.sctn.4.4.7) structure representing the string "LineNumberTable".

attribute.sub.-- length

The value of the attribute.sub.-- length item indicates the length of the attribute, excluding the initial six bytes.

line.sub.-- number.sub.-- table.sub.-- length

The value of the line number.sub.-- table.sub.-- length item indicates the number of entries in the line.sub.-- number.sub.-- table array.

line.sub.-- number.sub.-- table[ ]

Each entry in the line.sub.-- number.sub.-- table array indicates that the line number in the original Java source file changes at a given point in the code array. Each entry must contain the following items:

start.sub.-- pc

The value of the start.sub.-- pc item must indicate the index into the code array at which the code for a new line in the original Java source file begins. The value of start.sub.-- pc must be less than the value of the code.sub.-- length item of the Code attribute of which this LineNumberTable is an attribute.

line.sub.-- number

The value of the line.sub.-- number item must give the corresponding line number in the original Java source file.

4.7.7 LocalVariableTable Attribute

The LocalVariableTable attribute is an optional variable-length attribute of a Code (.sctn.4.7.4) attribute. It may be used by debuggers to determine the value of a given local variable during the execution of a method. If LocalVariableTable attributes are present in the attributes table of a given Code attribute, then they may appear in any order. There may be no more than one LocalVariableTable attribute per local variable in the Code attribute.

The LocalVariableTable attribute has the format

    ______________________________________
    LocalVariableTable.sub.-- attribute {
    u2 attribute.sub.-- name.sub.-- index;
    u4 attribute.sub.-- length;
    u2 local.sub.-- variable.sub.-- table.sub.-- length;
    {       u2 start.sub.-- pc;
            u2 length;
            u2 name.sub.-- index;
            u2 descriptor.sub.-- index;
            u2 index;
    }      local.sub.-- variable.sub.-- table [local.sub.-- variable.sub.--
           table.sub.-- length];
    ______________________________________


The items of the LocalVariableTable.sub.-- attribute structure are as follows:

attribute.sub.-- name.sub.-- index

The value of the attribute.sub.-- name.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must be a CONSTANT.sub.-- Utf8.sub.-- info (.sctn.4.4.7) structure representing the string "LocalVariableTable".

attribute.sub.-- length

The value of the attribute.sub.-- length item indicates the length of the attribute, excluding the initial six bytes.

local.sub.-- variable.sub.-- table.sub.-- length

The value of the local.sub.-- variable.sub.-- table.sub.-- length item indicates the number of entries in the local.sub.-- variable.sub.-- table array.

local.sub.-- variable.sub.-- table[ ]

Each entry in the local.sub.-- variable.sub.-- table array indicates a range of code array offsets within which a local variable has a value. It also indicates the index into the local variables of the current frame at which that local variable can be found. Each entry must contain the following items:

start.sub.-- pc, length

The given local variable must have a value at indices into the code array in the interval [start.sub.-- pc, start.sub.-- pc+length], that is, between start.sub.-- pc and start.sub.-- pc+length inclusive. The value of start.sub.-- pc must be a valid index into the code array of this Code attribute of the opcode of an instruction. The value of start.sub.-- pc+length must be either a valid index into the code array of this Code attribute of the opcode of an instruction, or the first index beyond the end of that code array.

name.sub.-- index, descriptor.sub.-- index

The value of the name.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must contain a CONSTANT.sub.-- Utf8.sub.-- info (.sctn.4.4.7) structure representing a valid Java local variable name stored as a simple name (.sctn.2.7.1).

The value of the descriptor.sub.-- index item must be a valid index into the constant.sub.-- pool table. The constant.sub.-- pool entry at that index must contain a CONSTANT.sub.-- Utf 8.sub.-- info (.sctn.4.4.7) structure representing a valid descriptor for a Java local variable. Java local variable descriptors have the same form as field descriptors (.sctn.4.3.2).

index

The given local variable must be at index in its method's local variables. If the local variable at index is a two-word type (double or long), it occupies both index and index+1.

4.8 Constraints on Java Virtual Machine Code

The Java Virtual Machine code for a method, instance initialization method (.sctn.3.8), or class or interface initialization method (.sctn.3.8) is stored in the code array of the Code attribute of a method.sub.-- info structure of a class file. This section describes the constraints associated with the contents of the Code.sub.-- attribute structure.

4.8.1 Static Constraints

The static constraints on a class file are those defining the well-formedness of the file. With the exception of the static constraints on the Java Virtual Machine code of the class file, these constraints have been given in the previous section. The static constraints on the Java Virtual Machine code in a class file specify how Java Virtual Machine instructions must be laid out in the code array, and what the operands of individual instructions must be.

The static constraints on the instructions in the code array are as follows:

The code array must not be empty, so the code.sub.-- length attribute cannot have the value 0.

The opcode of the first instruction in the code array begins at index 0.

Only instances of the instructions documented in (.sctn.6.4) may appear in the code array. Instances of instructions using the reserved opcodes (.sctn.6.2), the .sub.-- quick opcodes documented in Chapter 9, "An Optimization," or any opcodes not documented in this specification may not appear in the code array.

For each instruction in the code array except the last, the index of the opcode of the next instruction equals the index of the opcode of the current instruction plus the length of that instruction, including all its operands. The wide instruction is treated like any other instruction for these purposes; the opcode specifying the operation that a wide instruction is to modify is treated as one of the operands of that wide instruction. That opcode must never be directly reachable by the computation.

The last byte of the last instruction in the code array must be the byte at index code.sub.-- length.sub.-- 1.

The static constraints on the operands of instructions in the code array are as follows:

The target of each jump and branch instruction (jsr, jsr.sub.-- w, goto, goto.sub.-- w, ifeq, ifne, iflt, ifge, ifgt, ifle, ifnull, ifnonnull, if.sub.-- icmpeq, if.sub.-- icmpne, if.sub.-- icmplt, if.sub.-- icmpge, if.sub.-- icmpgt, if.sub.-- icmple, if.sub.-- acmpeq, if.sub.-- acmpne) must be the opcode of an instruction within this method. The target of a jump or branch instruction must never be the opcode used to specify the operation to be modified by a wide instruction; a jump or branch target may be the wide instruction itself.

Each target, including the default, of each tableswitch instruction must be the opcode of an instruction within this method. Each tableswitch instruction must have a number of entries in its jump table that is consistent with its low and high jump table operands, and its low value must be less than or equal to its high value. No target of a tableswitch instruction may be the opcode used to specify the operation to be modified by a wide instruction; a tableswitch target may be a wide instruction itself.

Each target, including the default, of each lookupswitch instruction must be the opcode of an instruction within this method. Each lookupswitch instruction must have a number of match-offset pairs that is consistent with its npairs operand. The match-offset pairs must be sorted in increasing numerical order by signed match value. No target of a lookupswitch instruction may be the opcode used to specify the operation to be modified by a wide instruction; a lookupswitch target may be a wide instruction itself

The operand of each ldc and ldc.sub.-- w instruction must be a valid index into the constant.sub.-- pool table. The constant pool entry referenced by that index must be of type CONSTANT.sub.-- Integer, CONSTANT.sub.-- Float, or CONSTANT.sub.-- String.

The operand of each ldc2.sub.-- w instruction must be a valid index into the constant.sub.-- pool table. The constant pool entry referenced by that index must be of type CONSTANT.sub.-- Long or CONSTANT.sub.-- double. In addition, the subsequent constant pool index must also be a valid index into the constant pool, and the constant pool entry at that index must not be used.

The operand of each getfield, putfield, getstatic, and putstatic instruction must be a valid index into the constant.sub.-- pool table. The constant pool entry referenced by that index must be of type CONSTANT.sub.-- Fieldref.

The index operand of each invokevirtual, invokespecial, and invokestatic instruction must be a valid index into the constant.sub.-- pool table. The constant pool entry referenced by that index must be of type CONSTANT.sub.-- Methodref.

Only the invokespecial instruction is allowed to invoke the method <init>, the instance initialization method (.sctn.3.8). No other method whose name begins with the character `<` (`u003c`) may be called by the method invocation instructions. In particular, the class initialization method <clinit> is never called explicitly from Java Virtual Machine instructions, but only implicitly by the Java Virtual Machine itself.

The index operand of each invokeinterface instruction must be a valid index into the constant.sub.-- pool table. The constant pool entry referenced by that index must be of type CONSTANT.sub.-- InterfaceMethodref. The value of the nargs operand of each invokeinterface instruction must be the same as the number of argument words implied by the descriptor of the CONSTANT.sub.-- NameAndType.sub.-- info structure referenced by the CONSTANT.sub.-- InterfaceMethodref constant pool entry. The fourth operand byte of each invokeinterface instruction must have the value zero.

The index operand of each instanceof, checkcast, new, anewarray, and multi-anewarray instruction must be a valid index into the constant.sub.-- pool table. The constant pool entry referenced by that index must be of type CONSTANT.sub.-- Class.

No anewarray instruction may be used to create an array of more than 255 dimensions.

No new instruction may reference a CONSTANT.sub.-- Class constant.sub.-- pool table entry representing an array class. The new instruction cannot be used to create an array. The new instruction also cannot be used to create an interface or an instance of an abstract class, but those checks are performed at link time.

A multianewarray instruction must only be used to create an array of a type that has at least as many dimensions as the value of its dimensions operand. That is, while a multianewarray instruction is not required to create all of the dimensions of the array type referenced by its CONSTANT.sub.-- Class operand, it must not attempt to create more dimensions than are in the array type. The dimensions operand of each multianewarray instruction must not be zero.

The atype operand of each newarray instruction must take one of the values T.sub.-- BOOLEAN (4), T.sub.-- CHAR (5), T.sub.-- FLOAT (.sctn.6), T.sub.-- DOUBLE (7), T.sub.-- BYTE (8), T.sub.-- SHORT (9), T.sub.-- INT (10), or T.sub.-- LONG (11).

The index operand of each iload, fload, aload, istore, fstore, astore, wide, iinc, and ret instruction must be a natural number no greater than max.sub.-- locals-1.

The implicit index of each iload.sub.-- <n>, fload.sub.-- <n>, aload.sub.13 <n>, istore.sub.-- <n>, fstore.sub.-- <n>, and astore.sub.-- <n> instruction must be no greater than the value of max.sub.-- locals-1.

The index operand of each lload, dload, lstore, and dstore instruction must be no greater than the value of max.sub.-- locals-2.

The implicit index of each lload.sub.-- <n>, dload.sub.-- <n>, lstore.sub.-- <n>, and dstore.sub.-- <n> instruction must be no greater than the value of max.sub.-- locals-2.

4.8.2 Structural Constraints

The structural constraints on the code array specify constraints on relationships between Java Virtual Machine instructions. The structural constraints are as follows:

Each instruction must only be executed with the appropriate type and number of arguments in the operand stack and local variables, regardless of the execution path that leads to its invocation. An instruction operating on values of type int is also permitted to operate on values of type byte, char, and short. (As noted in .sctn.3.11.1, the Java Virtual Machine internally converts values of types byte, char, and short to type int.)

Where an instruction can be executed along several different execution paths, the operand stack must have the same size prior to the execution of the instruction, regardless of the path taken.

At no point during execution can the order of the words of a two-word type (long or double) be reversed or split up. At no point can the words of a two-word type be operated on individually.

No local variable (or local variable pair, in the case of a two-word type) can be accessed before it is assigned a value.

At no point during execution can the operand stack grow to contain more than max.sub.-- stack words.

At no point during execution can more words be popped from the operand stack than it contains.

Each invokespecial instruction must name only an instance initialization method <init>, a method in this, a private method, or a method in a superclass of this.

When the instance initialization method <init>is invoked, an uninitialized class instance must be in an appropriate position on the operand stack. The <init> method must never be invoked on an initialized class instance.

When any instance method is invoked, or when any instance variable is accessed, the class instance that contains the instance method or instance variable must already be initialized.

There must never be an uninitialized class instance on the operand stack or in a local variable when any backwards branch is taken. There must never be an uninitialized class instance in a local variable in code protected by an exception handler or a finally clause. However, an uninitialized class instance may be on the operand stack in code protected by an exception handler or a finally clause. When an exception is thrown, the contents of the operand stack are discarded.

Each instance initialization method (.sctn.3.8), except for the instance initialization method derived from the constructor of class Object, must call either another instance initialization method of this or an instance initialization method of its immediate superclass super before its instance members are accessed. However, this is not necessary in the case of class Object, which does not have a superclass (.sctn.2.4.6).

The arguments to each method invocation must be method invocation compatible (.sctn.2.6.7) with the method descriptor (.sctn.4.3.3).

An abstract method must never be invoked.

Each return instruction must match its method's return type. If the method returns a byte, char, short, or int, only the ireturn instruction may be used. If the method returns a float, long, or double, only an freturn, lreturn, or dreturn instruction, respectively, may be used. If the method returns a reference type, it must do so using an areturn instruction, and the returned value must be assignment compatible (.sctn.2.6.6) with the return descriptor (.sctn.4.3.3) of the method. All instance initialization methods, static initializers, and methods declared to return void must only use the return instruction.

If getfield or putfield is used to access a protected field of a superclass, then the type of the class instance being accessed must be the same as or a subclass of the current class. If invokevirtual is used to access a protected method of a superclass, then the type of the class instance being accessed must be the same as or a subclass of the current class.

The type of every class instance loaded from or stored into by a getfield or putfield instruction must be an instance of the class type or a subclass of the class type.

The type of every value stored by a putfield or putstatic instruction must be compatible with the descriptor of the field (.sctn.4.3.2) of the class instance or class being stored into. If the descriptor type is byte, char, short, or int, then the value must be an int. If the descriptor type is float, long, or double, then the value must be a float, long, or double, respectively. If the descriptor type is a reference type, then the value must be of a type that is assignment compatible (.sctn.2.6.6) with the descriptor type.

The type of every value stored into an array of type reference by an aastore instruction must be assignment compatible (.sctn.2.6.6) with the component type of the array.

Each athrow instruction must only throw values that are instances of class Throwable or of subclasses of Throwable.

Execution never falls off the bottom of the code array.

No return address (a value of type returnAddress) may be loaded from a local variable.

The instruction following each jsr or jsr.sub.-- w instruction only may be returned to by a single ret instruction.

No jsr or jsr.sub.-- w instruction may