Project Jigsaw: Classfile changes in v51.0 for Modules
2009/2/3
Access flags
Module-private accessibility is expressed with a newaccess_flags flag, ACC_MODULE, value
0x8000, description: "Declared module; may not be
accessed from outside its module. May only be set if the
ClassFile has a Module attribute." The
flag appears in JVMS 4.1 (ClassFile.access_flags), 4.5
(field_info.access_flags), and 4.6
(method_info.access_flags), which gain the following
rules:
- 4.1
- At most one of the
ACC_MODULEandACC_PUBLICflags may be set (JLS3 §6.6.1). - 4.5
- Fields of classes may set any of the flags in Table 4.4.
However, a specific field of a class may have at most one of its
ACC_PRIVATE,ACC_PROTECTED,ACC_MODULE, andACC_PUBLICflags set (JLS3 §8.3.1) and must not have both itsACC_FINALandACC_VOLATILEflags set (JLS3 §8.3.1.4). - All fields of interfaces must have their
ACC_STATICandACC_FINALand eitherACC_PUBLICorACC_MODULEflags set; they may have theirACC_SYNTHETICflag set and must not have any of the other flags in Table 4.4 set (JLS3 §9.3). - 4.6
- Methods of classes may set any of the flags in Table 4.5.
However, a specific method of a class may have at most one of its
ACC_PRIVATE,ACC_PROTECTED,ACC_MODULE, andACC_PUBLICflags set (JLS3 §8.4.3). - All interface methods must have their
ACC_ABSTRACTand eitherACC_PUBLICorACC_MODULEflags set; they may have theirACC_VARARGS,ACC_BRIDGEandACC_SYNTHETICflags set and must not have any of the other flags in Table 4.5 set (JLS3 §9.4). - A specific instance initialization method (§3.9) may have
at most one of its
ACC_PRIVATE,ACC_PROTECTED,ACC_MODULE, andACC_PUBLICflags set, and may also have itsACC_STRICT,ACC_VARARGSandACC_SYNTHETICflags set, but must not have any of the other flags in Table 4.5 set.
InnerClasses attribute's
inner_class_access_flags table,
ACC_MODULE means "Marked module in
source. May only be set if the ClassFile has a
Module attribute."
The ACC_PUBLIC flag in all
access_flags is now defined as "Declared
public; may be accessed from outside its package or
module."
4.8.21 The Module attribute
The Module attribute is a fixed-length attribute in
the attributes table of a ClassFile (JVMS
4.1) structure. The Module attribute records the
named module (JLS 7.4.5) to which the class belongs. A
ClassFile structure without a Module
attribute is considered to be a member of the unnamed
module (JLS 7.4.6).
There can be no more than one Module attribute in
the attributes table of a given ClassFile
structure. A Module attribute may only appear in a
ClassFile structure that represents a class whose
binary name in internal form (as given by the constant pool entry
referenced by the name_index item of the
CONSTANT_Class_info structure referenced by
ClassFile.this_class) is qualified, i.e. contains at
least one /.
The Module attribute has the following format:
Module_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 module_id_index;
}
The items of the Module_attribute structure are as
follows:
attribute_name_index- The value of the
attribute_name_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure representing the string "Module". attribute_length- The value of the
attribute_lengthitem must be 2. module_id_index- The value of the
module_id_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_ModuleId_infostructure representing the name and version of the named module of which thisClassFileis a member. The module name must be encoded in internal form (4.2.1). If theversion_indexitem of theCONSTANT_ModuleId_infostructure is 0, then thisClassFileis a member of any version of the module named by thename_indexitem.
CONSTANT_ModuleId_info structure has the following
format:
CONSTANT_ModuleId_info {
u1 tag;
// 13
u2 name_index;
// Points to a CONSTANT_Utf8_info representing module name
u2 version_index;
// Points to a CONSTANT_Utf8_info representing module version
}
The module-info.class file
A module compilation unit (module-info.java) is
compiled to a ClassFile structure like any other
compilation unit. It follows the precedent set by compiling a
package compilation unit (package-info.java) which has
been possible since Java 5.0. Since neither compilation unit
describes a class as such, the following conventions are adopted
for their ClassFile structures.
package-info.class
major_version, minor_version: >=49.0this_class: [Package name in internal form (4.2.1)]/package-infoaccess_flags: v49: ACC_INTERFACE ACC_ABSTRACT >=v50: ACC_INTERFACE ACC_ABSTRACT ACC_SYNTHETICsuper_class,interfaces_count,fields_count,methods_count: 0attributes: except forModule,SourceFile,RuntimeVisibleAnnotationsandRuntimeInvisibleAnnotations, none of the pre-defined attributes in JVMS 4.7 may appear.
module-info.class
major_version, minor_version: >=51.0this_class: [Module name in internal form (4.2.1)]/module-infoaccess_flags: ACC_INTERFACE ACC_ABSTRACT ACC_SYNTHETICattributes: OneModuleattribute must be present, for the module name given in thethis_classitem.super_class,interfaces_count,fields_count,methods_count: 0attributes: OneModuleattribute must be present, for the module name given in thethis_classitem. At most one of each ofModuleDepends,ModulePermits, andModuleProvidesattributes must be present if the module has metadata. Except for these attributes andSourceFile,RuntimeVisibleAnnotations, andRuntimeInvisibleAnnotations, none of the pre-defined attributes in JVMS 4.7 may appear.- A module name may be referenced by at most one entry in the
depends_tableof aModuleDependsattribute and at most one entry in thepermits_tableof aModulePermitsattribute and at most one entry in theprovides_tableof aModuleProvidesattribute of aClassFile.
Attributes for the module-info.class file
TheModuleRequires attribute is defined as follows:
ModuleRequires_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 requires_length;
{ u2 requires_index; u1 flags; } requires_table[depends_length];
}
attribute_name_index- The value of the
attribute_name_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure representing the string "ModuleRequires". attribute_length- The value of the
attribute_lengthitem is the length of the attribute excluding the initial six bytes. requires_length- The value of the
requires_lengthindicates the number of entries in therequires_table. requires_table- Each
requires_indexmust be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_ModuleId_infostructure representing the target module on which this module depends.flags: 0x01 if this dependency on the target module is optional flags: 0x02 if the target module's types must be loaded by the same defining classloader as the types of the module represented by this ClassFile flags: 0x04 if the target module's public types are NOT observable to types of the module represented by this ClassFile
ModulePermits attribute is defined as follows:
ModulePermits_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 permits_length;
{ u2 permits_index } permits_table[permits_length];
}
attribute_name_index- The value of the
attribute_name_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure representing the string "ModulePermits". attribute_length- The value of the
attribute_lengthitem is the length of the attribute excluding the initial six bytes. permits_length- The value of the
permits_lengthindicates the number of entries in thepermits_table. permits_table- The value of each
permits_indexin this item must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure representing the name of a module which is permitted by the host system to have a dependency on the module represented by thisClassFile.
ModuleProvides attribute is defined as follows:
ModuleProvides_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 provides_length;
{ u2 provides_index; } provides_table[provides_length];
}
attribute_name_index- The value of the
attribute_name_indexitem must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_Utf8_infostructure representing the string "ModuleProvides". attribute_length- The value of the
attribute_lengthitem is the length of the attribute excluding the initial six bytes. provides_length- The value of the
provides_lengthindicates the number of entries in theprovides_table. provides_table- The value of each
provides_indexin this item must be a valid index into theconstant_pooltable. Theconstant_poolentry at that index must be aCONSTANT_ModuleId_infostructure representing a module identity that is an alias for the module represented by thisClassFile.

