Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
java_language_specification_7.pdf
Скачиваний:
13
Добавлен:
21.03.2016
Размер:
3.11 Mб
Скачать

15.12.3 Compile-Time Step 3: Is the Chosen Method Appropriate?

EXPRESSIONS

is created and used to infer constraints on the type arguments using the algorithm of §15.12.2.7.

Any equality constraints are resolved, and then, for each remaining constraint of the form Ti <: Uk, the argument Ti is inferred to be glb(U1, ..., Uk) (§5.1.10).

If Ti appears as a type argument in any Uk, then Ti is inferred to be a type variable X whose upper bound is the parameterized type given by glb(U1[Ti=X], ..., Uk[Ti=X]) and whose lower bound is the null type.

Any remaining type variable T that has not yet been inferred is then inferred to have type Object. If a previously inferred type variable P uses T, then P is inferred to be P[T=Object].

Otherwise, the unresolved type arguments are inferred by invoking the procedure described in this section under the assumption that the method result was assigned to a variable of type Object.

15.12.3 Compile-Time Step 3: Is the Chosen Method Appropriate?

If there is a most specific method declaration for a method invocation, it is called the compile-time declaration for the method invocation.

If the method invocation has, before the left parenthesis, a MethodName of the form Identifier, and the compile-time declaration is an instance method, then:

If the method invocation occurs in a static context (§8.1.3), then a compile-time error occurs.

The reason is that a method invocation of this form cannot be used to invoke an instance method in places where this (§15.8.3) is not defined.

Otherwise, let C be the innermost enclosing class of which the compile-time declaration is a member.

If the invocation is not directly enclosed by C or an inner class of C, then a compile-time error occurs.

If the method invocation has, before the left parenthesis, a MethodName of the form TypeName . Identifier, or if the method invocation, before the left parenthesis, has the form TypeName . NonWildTypeArguments Identifier, then the compile-time declaration must be static, or a compile-time error occurs.

The reason is that a method invocation of this form does not specify a reference to an object that can serve as this within the instance method.

478

EXPRESSIONS

Compile-Time Step 3: Is the Chosen Method Appropriate? 15.12.3

If the method invocation has, before the left parenthesis, the form super .

NonWildTypeArgumentsopt Identifier, then:

If the compile-time declaration is abstract, a compile-time error occurs.

If the method invocation occurs in a static context, a compile-time error occurs.

If the method invocation has, before the left parenthesis, the form ClassName . super . NonWildTypeArgumentsopt Identifier, then:

If the compile-time declaration is abstract, a compile-time error occurs.

If the method invocation occurs in a static context, a compile-time error occurs.

Otherwise, let C be the class denoted by ClassName.

If the invocation is not directly enclosed by C or an inner class of C, then a compile-time error occurs.

If the compile-time declaration is void, then the method invocation must be a top level expression (that is, the Expression in an expression statement (§14.8) or in the ForInit or ForUpdate part of a for statement (§14.14)), or a compile-time error occurs.

The reason is that such a method invocation produces no value and so must be used only in a situation where a value is not needed.

A method is signature polymorphic if and only if all of the following conditions hold:

It is declared in the java.lang.invoke.MethodHandle class.

It takes a single variable arity parameter (§8.4.1) whose declared type is

Object[].

It has a return type of Object.

It is native.

In Java SE 7, the only signature polymorphic methods are the invoke and invokeExact methods of the class java.lang.invoke.MethodHandle.

If the compile-time declaration for the method invocation is not a signature polymorphic method, then the types of its parameters are the types of its formal parameters, and the result type is its chosen result type.

Otherwise, if the compile-time declaration for the method invocation is a signature polymorphic method, then:

479

15.12.3 Compile-Time Step 3: Is the Chosen Method Appropriate?

EXPRESSIONS

The types of its parameters are the static types of the actual argument expressions. An argument expression which is the null literal null is treated as having the static type Void.

The result type is determined as follows:

If the method invocation expression is an expression statement, the method is void.

Otherwise, if the method invocation expression is the operand of a cast expression (§15.16), the return type is the erasure (§4.6) of the type of the cast expression.

Otherwise, the return type is the method's declared return type, Object.

The following compile-time information is then associated with the method invocation for use at run-time:

The name of the method.

The qualifying type of the method invocation (§13.1).

The number of parameters and the types of the parameters, in order.

The result type, or void.

The invocation mode, computed as follows:

If the compile-time declaration has the static modifier, then the invocation mode is static.

Otherwise, if the compile-time declaration has the private modifier, then the invocation mode is nonvirtual.

Otherwise, if the part of the method invocation before the left parenthesis is of the form super . Identifier or of the form ClassName . super . Identifier, then the invocation mode is super.

Otherwise, if the compile-time declaration is in an interface, then the invocation mode is interface.

Otherwise, the invocation mode is virtual.

If the compile-time declaration for the method invocation is not void, then the type of the method invocation expression is the result type specified in the compile-time declaration.

480

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]