Run-time Evaluation of Method References

Một phần của tài liệu Java SE 8 edition for everyone Java SE 8 edition for everyone Java SE 8 edtion for everyone (Trang 561 - 564)

At run time, evaluation of a method reference expression is similar to evaluation of a class instance creation expression, insofar as normal completion produces a reference to an object. Evaluation of a method reference expression is distinct from invocation of the method itself.

First, if the method reference expression begins with an ExpressionName or a Primary, this subexpression is evaluated. If the subexpression evaluates to null, a

NullPointerException is raised, and the method reference expression completes abruptly. If the subexpression completes abruptly, the method reference expression completes abruptly for the same reason.

Next, either a new instance of a class with the properties below is allocated and initialized, or an existing instance of a class with the properties below is referenced.

If a new instance is to be created, but there is insufficient space to allocate the object, evaluation of the method reference expression completes abruptly by throwing an OutOfMemoryError.

The value of a method reference expression is a reference to an instance of a class with the following properties:

• The class implements the targeted functional interface type and, if the target type is an intersection type, every other interface type mentioned in the intersection.

• Where the method reference expression has type U, for each non-static member method m of U:

15.13 Method Reference Expressions EXPRESSIONS

If the function type of U has a subsignature of the signature of m, then the class declares an invocation method that overrides m. The invocation method's body invokes the referenced method, creates a class instance, or creates an array, as described below. If the invocation method's result is not void, then the body returns the result of the method invocation or object creation, after any necessary assignment conversions (§5.2).

If the erasure of the type of a method being overridden differs in its signature from the erasure of the function type of U, then before the method invocation or object creation, an invocation method's body checks that each argument value is an instance of a subclass or subinterface of the erasure of the corresponding parameter type in the function type of U; if not, a ClassCastException is thrown.

• The class overrides no other methods of the functional interface type or other interface types mentioned above, although it may override methods of the Object

class.

The body of an invocation method depends on the form of the method reference expression, as follows:

• If the form is ExpressionName :: [TypeArguments] Identifier or Primary ::

[TypeArguments] Identifier, then the body of the invocation method has the effect of a method invocation expression for a compile-time declaration which is the compile-time declaration of the method reference expression. Run-time evaluation of the method invocation expression is as specified in §15.12.4.3,

§15.12.4.4, and §15.12.4.5, where:

– The invocation mode is derived from the compile-time declaration as specified in §15.12.3.

– The target reference is the value of ExpressionName or Primary, as determined when the method reference expression was evaluated.

– The arguments to the method invocation expression are the formal parameters of the invocation method.

• If the form is ReferenceType :: [TypeArguments] Identifier, the body of the invocation method similarly has the effect of a method invocation expression for a compile-time declaration which is the compile-time declaration of the method reference expression. Run-time evaluation of the method invocation expression is as specified in §15.12.4.3, §15.12.4.4, and §15.12.4.5, where:

– The invocation mode is derived from the compile-time declaration as specified in §15.12.3.

EXPRESSIONS Method Reference Expressions 15.13

– If the compile-time declaration is an instance method, then the target reference is the first formal parameter of the invocation method. Otherwise, there is no target reference.

– If the compile-time declaration is an instance method, then the arguments to the method invocation expression (if any) are the second and subsequent formal parameters of the invocation method. Otherwise, the arguments to the method invocation expression are the formal parameters of the invocation method.

• If the form is super:: [TypeArguments] Identifier or TypeName . super ::

[TypeArguments] Identifier, the body of the invocation method has the effect of a method invocation expression for a compile-time declaration which is the compile-time declaration of the method reference expression. Run-time evaluation of the method invocation expression is as specified in §15.12.4.3,

§15.12.4.4, and §15.12.4.5, where:

– The invocation mode is super.

– If the method reference expression begins with a TypeName that names a class, the target reference is the value of TypeName .this at the point at which the method reference is evaluated. Otherwise, the target reference is the value of

this at the point at which the method reference is evaluated.

– The arguments to the method invocation expression are the formal parameters of the invocation method.

• If the form is ClassType :: [TypeArguments] new, the body of the invocation method has the effect of a class instance creation expression of the form new

[TypeArguments] ClassType(A1, ..., An), where the arguments A1, ..., An are the formal parameters of the invocation method, and where:

– The enclosing instance for the new object, if any, is derived from the site of the method reference expression, as specified in §15.9.2.

– The constructor to invoke is the constructor that corresponds to the compile- time declaration of the method reference (§15.13.1).

• If the form is Type[]k ::new (k ≥ 1), then the body of the invocation method has the same effect as an array creation expression of the form new Type [ size ] []k-1, where size is the invocation method's single parameter. (The notation []k

indicates a sequence of k bracket pairs.)

If the body of the invocation method has the effect of a method invocation expression, then the compile-time parameter types and the compile-time result of the method invocation are determined as specified in §15.12.3. For the purpose

Một phần của tài liệu Java SE 8 edition for everyone Java SE 8 edition for everyone Java SE 8 edtion for everyone (Trang 561 - 564)

Tải bản đầy đủ (PDF)

(780 trang)