JDK-8131750 : VarHandles JVMS changes
  • Type: Enhancement
  • Component: specification
  • Sub-Component: vm
  • Affected Version: 8
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2015-07-17
  • Updated: 2018-08-03
  • Resolved: 2017-02-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9Fixed
Related Reports
Relates :  
Relates :  
Description
From the JEP 193 description: 

The following additions will be required to the Java Virtual Machine Specification:

1. Make reference to the signature-polymorphic access mode methods in the VarHandle class.

2. Specify invokevirtual byte code behaviour of invocation to access mode signature-polymorphic methods. It is anticipated that such behaviour can be specified by defining a transformation from the access mode method invocation to a MethodHandle which is then invoked using invokeExact with the same parameters (see previous use of MethodHandles.Lookup.findVirtual). Further investigation is required to determine if refined transformations are possible (now, or in the future without breaking compatibility) to make access mode method similar to MethodHandle.invoke, for example, by transforming the method handle via MethodHandle.asType where all reference types are cast from Object.
Comments
The first, second, and third steps may be regarded as linkage of the invokevirtual. Setting aside the possibility of objectref being null, they are guaranteed to succeed because they're driven solely by the name N of the sig-poly method, and that method was successfully resolved already. However, it is still possible for the bytecode containing invokevirtual to experience a linking exception, because the "inner" as-if-invokevirtual on the invoker method handle may throw one, in particular when the descriptor D of the sig-poly method is matched against the type of the invoker method handle. It would be appropriate to say: - Linking Exceptions: "... Otherwise, if the resolved method is signature polymorphic and declared in the java.lang.invoke.VarHandle class, then any _linking_ exception that may arise from invocation of the invoker method handle can be thrown. No linking exceptions are thrown from invocation of the valueFromMethodName, accessModeType, and varHandleExactInvoker methods." - Run-time Exceptions: "... Otherwise, if the resolved method is signature polymorphic and declared in the java.lang.invoke.VarHandle class, then any _run-time_ exception that may arise from invocation of the invoker method handle can be thrown. No run-time exceptions are thrown from invocation of the valueFromMethodName, accessModeType, and varHandleExactInvoker methods, except NPE if objectref is null."
14-02-2017

It is arguably clearer to leverage java.lang.invoke.MethodHandles.varHandleExactInvoker. Here is a suggested draft update for 6.5 invokevirtual (changes occur from "Second, ..." paragraph onwards). ----- ... If the resolved method is signature polymorphic and is declared in the java.lang.invoke.VarHandle class, then the invokevirtual instruction proceeds as follows, where N and D are the name and descriptor of the method symbolically referenced by the instruction. First, a reference to an instance of java.lang.invoke.VarHandle.AccessMode is obtained as if by a call to java.lang.invoke.VarHandle.AccessMode.valueFromMethodName with a String argument denoting N. Second, a reference to an instance of java,lang.invoke.MethodType is obtained as if by a call to java.lang.invoke.VarHandle.accessModeType on the receiving varhandle /objectref/, with the instance of java.lang.invoke.VarHandle.AccessMode as the argument. Third, a reference to an instance of java.lang.invoke.MethodHandle is obtained as if by a call to java.lang.invoke.MethodHandles.varHandleExactInvoker with the instance of java.lang.invoke.VarHandle.AccessMode as the first argument and the instance of java,lang.invoke.MethodType as the second argument. This instance is called the invoker method handle. Finally, the invoker method handle is invoked. The invocation occurs as if by execution of an invokevirtual instruction that indicates a run-time constant pool index to a symbolic reference R where: - R is a symbolic reference to a method of a class; - for the symbolic reference to the class in which the method is to be found, R specifies java.lang.invoke.MethodHandle; - for the name of the method, R specifies invoke; - for the descriptor of the method, R specifies a method descriptor with the first parameter descriptor "L/java/lang/invoke/VarHandle;" followed by, in order, the parameter descriptors of D, and with the same return descriptor as D. and where it is as if the reference to the java.lang.invoke.MethodHandle instance is pushed on to the operand stack, followed by the receiving varhandle /objectref/, followed by the nargs argument values, where the number, type, and order of the values must be consistent with the type descriptor of the method handle. -----
10-02-2017

For 6.5 invokevirtual: ----- If the resolved method is signature polymorphic ***and is declared in the java.lang.invoke.MethodHandle class,*** then the invokevirtual instruction proceeds as follows. ... If the resolved method is signature polymorphic and is declared in the java.lang.invoke.VarHandle class, then the invokevirtual instruction proceeds as follows, where N and D are the name and descriptor of the method symbolically referenced by the instruction. First, a reference to an instance of java.lang.invoke.VarHandle.AccessMode is obtained as if by a call to java.lang.invoke.VarHandle.AccessMode.valueFromMethodName with a String argument denoting N. Second, a reference to an instance of java.lang.invoke.MethodHandle is obtained as if by a call to java.lang.invoke.VarHandle.toMethodHandle on the receiving varhandle /objectref/, with the instance of java.lang.invoke.VarHandle.AccessMode as the argument. This instance is called the method handle bound to the varhandle. Finally, the method handle bound to the varhandle is invoked. The invocation occurs as if by execution of an invokevirtual instruction that indicates a run-time constant pool index to a symbolic reference R where: - R is a symbolic reference to a method of a class; - for the symbolic reference to the class in which the method is to be found, R specifies java.lang.invoke.MethodHandle; - for the name of the method, R specifies invoke; - for the descriptor of the method, R specifies D. and where it is as if the reference to the java.lang.invoke.MethodHandle instance is pushed on to the operand stack, followed by the nargs argument values, where the number, type, and order of the values must be consistent with the type descriptor of the method handle. -----
10-02-2017

For 2.9.3 Signature Polymorphic Methods: ----- A method is signature polymorphic if all of the following are true: ��� It is declared in the java.lang.invoke.MethodHandle class or the java.lang.invoke.VarHandle class. ��� It has a single formal parameter of type Object[]. ��� It has the ACC_VARARGS and ACC_NATIVE flags set. The Java Virtual Machine gives special treatment to signature polymorphic methods in the invokevirtual instruction (��invokevirtual), in order to effect invocation of a method handle or to effect access to a variable referenced by an instance of java.lang.invoke.VarHandle. A method handle is a dynamically strongly typed and directly executable reference to an underlying method, constructor, field, or similar low-level operation (��5.4.3.5), with optional transformations of arguments or return values. These transformations are quite general, and include such patterns as conversion, insertion, deletion, and substitution. An instance of java.lang.invoke.VarHandle is a dynamically strongly typed reference to a variable or family of variables, including static fields, non-static fields, array elements, or components of an off-heap data structure. See the java.lang.invoke package in the Java SE platform API for more information. -----
10-02-2017