Lambda Specification, Part J: Java Virtual Machine

Navigation: Overview - Part A - Part B - Part C - Part D - Part E - Part F - Part G - Part H - Part J
Sections: JVMS-4.4.2 - JVMS-4.6 - JVMS-5.4.3.3 - JVMS-5.4.3.4 - JVMS-6.5.invokeinterface - JVMS-6.5.invokespecial - JVMS-6.5.invokestatic - JVMS-6.5.invokevirtual
Version 0.6.1. Copyright © 2012 Oracle America, Inc. Legal Notice.

Summary

Changes to the Java Virtual Machine are necessary to support the implementation of default methods. Methods declared in interface class files need not be declared abstract, and thus can carry a Code attribute. Methods declared in interface class files may also be private or static. The rules for method resolution and invocation (via invokeinterface, invokespecial, invokestatic, and invokevirtual) are enhanced to support execution of code in interfaces.

JVMS 4.4.2 The CONSTANT_Fieldref_info, CONSTANT_Methodref_info, and CONSTANT_InterfaceMethodref_info Structures [Modified]

Compare JVMS 4.4.2

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

CONSTANT_Fieldref_info {
    u1 tag;
    u2 class_index;
    u2 name_and_type_index;
}

CONSTANT_Methodref_info {
    u1 tag;
    u2 class_index;
    u2 name_and_type_index;
}

CONSTANT_InterfaceMethodref_info {
    u1 tag;
    u2 class_index;
    u2 name_and_type_index;
}

The items in the structures are as follows:

tag
...
class_index
The value of the class_index item must be a valid index into the constant_pool table. The constant_pool entry at that index msut be a CONSTANT_Class_info (4.4.1) structure representing a class or interface type that has the field or method as a member. [jvms-4.4.2-200-B]

In a class file whose version number is 52.0 or above, the class_index item of a CONSTANT_Methodref_info structure may be a class type or an interface type; in all other class files, the class_index must be a class type, not an interface type. [jvms-4.4.2-200-B.1]

The class_index item of a CONSTANT_InterfaceMethodref_info structure must be an interface type. [jvms-4.4.2-200-B.2]

The class_index item of a CONSTANT_Fieldref_info structure may be either a class type or an interface type. [jvms-4.4.2-200-B.3]

name_and_type_index
...

JVMS 4.6 Methods [Modified]

Compare JVMS 4.6

...

Methods of classes may set any of the flags in Table 4.20. However, a specific method of a class may have at most one of its ACC_PRIVATE, ACC_PROTECTED, and ACC_PUBLIC flags set (8.4.3). [jvms-4.6-200-A.5]

Methods of interfaces may set any of the flags in Table 4.5 except ACC_PROTECTED, ACC_FINAL, ACC_NATIVE, and ACC_SYNCHRONIZED (9.4); they must have exactly one of the ACC_PUBLIC or ACC_PRIVATE flags set. [jsr335-4.6-200-A.10]

An interface method in a class file whose version number is not 52.0 or above must have its ACC_ABSTRACT and ACC_PUBLIC flags set they may have their ACC_VARARGS, ACC_BRIDGE, and ACC_SYNTHETIC flags set and must not have any of the other flags in Table 4.5 set (9.4). [jvms-4.6-200-A.6]

If a specific method of a class or interface has its ACC_ABSTRACT flag set, it must not have any of its ACC_FINAL, ACC_NATIVE, ACC_PRIVATE, ACC_STATIC, ACC_STRICT, or ACC_SYNCHRONIZED flags set (8.4.3.1, 8.4.3.3, 8.4.3.4). [jsr335-4.6-200-A.11]

...

Discussion and motivation:
  1. Interface methods prior to version 52.0 must be abstract, and thus must not be private, static, or strictfp; they are also prohibited from having Code attributes (4.7.3). In version 52.0, an interface method may be non-abstract and thus is free to use these flags and have a Code attribute.

JVMS 5.4.3.3 Method Resolution [Modified]

Compare JVMS 5.4.3.3

To resolve an unresolved symbolic reference from D to a method in a class or interface C, the symbolic reference to C given by the method reference is first resolved (5.4.3.1). Therefore, any exception that can be thrown as a result of failure of resolution of a class or interface reference can be thrown as a result of method resolution. If the reference to C can be successfully resolved, exceptions relating to the resolution of the method reference itself can be thrown. [jvms-5.4.3.3-100]

When resolving a method reference: [jvms-5.4.3.3-200]

If method lookup fails, method resolution throws a NoSuchMethodError. [jvms-5.4.3.3-210-A]

If method lookup succeeds and the method is abstract, but C is not abstract, method resolution throws an AbstractMethodError. [jvms-5.4.3.3-210-B]

Otherwise, if the referenced method is not accessible (5.4.4) to D, method resolution throws an IllegalAccessError. [jvms-5.4.3.3-210-C]

...

JVMS 5.4.3.4 Interface Method Resolution [Modified]

Compare JVMS 5.4.3.4

To resolve an unresolved symbolic reference from D to an interface method in an interface C, the symbolic reference to C given by the interface method reference is first resolved (5.4.3.1). Therefore, any exception that can be thrown as a result of failure of resolution of an interface reference can be thrown as a result of interface method resolution. If the reference to C can be successfully resolved, exceptions relating to the resolution of the method reference itself can be thrown. [jvms-5.4.3.4-100]

When resolving an interface method reference: [jvms-5.4.3.4-200]

If method lookup fails, method resolution throws a NoSuchMethodError. Otherwise, if the referenced method is not accessible (5.4.4) to D, method resolution throws an IllegalAccessError. [jsr335-5.4.3.4-210]

Given a referenced method, let ... [jvms-5.4.3.4-200-C]

...

JVMS 6.5 invokeinterface [Modified]

Compare JVMS 6.5.invokeinterface

...

Let C be the class of objectref. The actual method to be invoked is selected by the following lookup procedure: [jvms-6.5.invokeinterface.desc-200]

...

During resolution of the symbolic reference to the interface method, any of the exceptions pertaining to interface method resolution (JVMS-5.4.3.4) can be thrown. [jvms-6.5.invokeinterface.linking-100]

Otherwise, if the resolved method is a static method, the invokeinterface instructions throws an IncompatibleClassChangeError. [jsr335-6.5.invokeinterface.linking-20]

...

JVMS 6.5 invokespecial [Modified]

Compare JVMS 6.5.invokespecial

...

The unsigned indexbyte1 and indexbyte2 are used to construct an index into the runtime constant pool of the current class (2.6), where the value of the index is (indexbyte1 << 8) | indexbyte2. The runtime constant pool item at that index must be a symbolic reference to a method (5.1), which gives the name and descriptor (4.3.3) of the method as well as a symbolic reference to the class or interface in which the method is to be found. The named method is resolved (5.4.3.3). Finally, if the resolved method is protected (4.6), and it is a member of a superclass of the current class, and the method is not declared in the same runtime package (5.3) as the current class, then the class of objectref must be either the current class or a subclass of the current class. [jvms-6.5.invokespecial.desc-100]

...

JVMS 6.5 invokestatic [Modified]

Compare JVMS 6.5.invokestatic

...

The unsigned indexbyte1 and indexbyte2 are used to construct an index into the runtime constant pool of the current class (2.6), where the value of the index is (indexbyte1 << 8) | indexbyte2. The runtime constant pool item at that index must be a symbolic reference to a method (5.1), which gives the name and descriptor (4.3.3) of the method as well as a symbolic reference to the class or interface in which the method is to be found. The named method is resolved (5.4.3.3). The resolved method must not be an instance initialization method (2.9) or the a class or interface initialization method (2.9). It must be static, and therefore cannot be abstract. If the resolved method is declared in an interface, that interface must be the one named by the symbolic reference. [jvms-6.5.invokestatic.desc-100]

On successful resolution of the method, the class or interface that declared the resolved method is initialized (5.5) if that class or interface has not already been initialized. [jvms-6.5.invokestatic.desc-110]

...

Otherwise, if the resolved method is an instance method, the invokestatic instruction throws an IncompatibleClassChangeError. [jvms-6.5.invokestatic.linking-110]

Otherwise, if the resolved method is declared in an interface other than the class or interface named by the method reference, the invokestatic instruction throws an IncompatibleClassChangeError. [jsr335-6.5.invokestatic.linking-20]

Otherwise, if execution of this invokestatic instruction causes initialization of the referenced class or interface, invokestatic may throw an Error as detailed in 5.5. [jvms-6.5.invokestatic.runtime-100]

...

JVMS 6.5 invokevirtual [Modified]

Compare JVMS 6.5.invokevirtual

...

The unsigned indexbyte1 and indexbyte2 are used to construct an index into the runtime constant pool of the current class (2.6), where the value of the index is (indexbyte1 << 8) | indexbyte2. The runtime constant pool item at that index must be a symbolic reference to a method (5.1), which gives the name and descriptor (4.3.3) of the method as well as a symbolic reference to the class or interface in which the method is to be found. The named method is resolved (5.4.3.3). The resolved method must not be an instance initialization method (2.9) or the a class or interface initialization method (2.9). Finally, if the resolved method is protected (4.6), and it is a member of a superclass of the current class, and the method is not declared in the same runtime package (5.3) as the current class, then the class of objectref must be either the current class or a subclass of the current class. [jvms-6.5.invokevirtual.desc-100]

If the resolved method is not signature polymorphic (2.9), then the invokevirtual instruction proceeds as follows. [jvms-6.5.invokevirtual.desc-200]

Let C be the class of objectref. The actual method to be invoked is selected by the following lookup procedure: [jvms-6.5.invokevirtual.desc-210]

...

Otherwise, if the resolved method is not signature polymorphic: [jvms-6.5.invokevirtual.runtime-110]

...