jdk/src/java.base/share/classes/java/lang/invoke/VarHandle.java
changeset 37344 52d3d8517efc
parent 37343 35a2231828a7
child 37345 9cb6e1141bdb
equal deleted inserted replaced
37343:35a2231828a7 37344:52d3d8517efc
   152  * <p>Factory methods that produce or {@link java.lang.invoke.MethodHandles.Lookup
   152  * <p>Factory methods that produce or {@link java.lang.invoke.MethodHandles.Lookup
   153  * lookup} VarHandle instances document the set of access modes that are
   153  * lookup} VarHandle instances document the set of access modes that are
   154  * supported, which may also include documenting restrictions based on the
   154  * supported, which may also include documenting restrictions based on the
   155  * variable type and whether a variable is read-only.  If an access mode is not
   155  * variable type and whether a variable is read-only.  If an access mode is not
   156  * supported then the corresponding signature-polymorphic method will on
   156  * supported then the corresponding signature-polymorphic method will on
   157  * invocation throw an {@code UnsupportedOperationException}.
   157  * invocation throw an {@code UnsupportedOperationException}.  Factory methods
       
   158  * should document any additional undeclared exceptions that may be thrown by
       
   159  * access mode methods.
   158  * The {@link #get get} access mode is supported for all
   160  * The {@link #get get} access mode is supported for all
   159  * VarHandle instances and the corresponding method never throws
   161  * VarHandle instances and the corresponding method never throws
   160  * {@code UnsupportedOperationException}.
   162  * {@code UnsupportedOperationException}.
   161  * If a VarHandle references a read-only variable (for example a {@code final}
   163  * If a VarHandle references a read-only variable (for example a {@code final}
   162  * field) then write, atomic update and numeric atomic update access modes are
   164  * field) then write, atomic update and numeric atomic update access modes are
   166  * {@code get} and {@code set}, provide atomic access for
   168  * {@code get} and {@code set}, provide atomic access for
   167  * reference types and all primitive types.
   169  * reference types and all primitive types.
   168  * Unless stated otherwise in the documentation of a factory method, the access
   170  * Unless stated otherwise in the documentation of a factory method, the access
   169  * modes {@code get} and {@code set} (if supported) provide atomic access for
   171  * modes {@code get} and {@code set} (if supported) provide atomic access for
   170  * reference types and all primitives types, with the exception of {@code long}
   172  * reference types and all primitives types, with the exception of {@code long}
   171  * and {@code double} on 32-bit platforms
   173  * and {@code double} on 32-bit platforms.
   172  *
   174  *
   173  * <p>Access modes will override any memory ordering effects specified at
   175  * <p>Access modes will override any memory ordering effects specified at
   174  * the declaration site of a variable.  For example, a VarHandle accessing a
   176  * the declaration site of a variable.  For example, a VarHandle accessing a
   175  * a field using the {@code get} access mode will access the field as
   177  * a field using the {@code get} access mode will access the field as
   176  * specified <em>by its access mode</em> even if that field is declared
   178  * specified <em>by its access mode</em> even if that field is declared
   465      * @return the signature-polymorphic result that is the value of the
   467      * @return the signature-polymorphic result that is the value of the
   466      * variable
   468      * variable
   467      * , statically represented using {@code Object}.
   469      * , statically represented using {@code Object}.
   468      * @throws WrongMethodTypeException if the access mode type is not
   470      * @throws WrongMethodTypeException if the access mode type is not
   469      * compatible with the caller's symbolic type descriptor.
   471      * compatible with the caller's symbolic type descriptor.
       
   472      * @throws ClassCastException if the access mode type is compatible with the
       
   473      * caller's symbolic type descriptor, but a reference cast fails.
   470      */
   474      */
   471     public final native
   475     public final native
   472     @MethodHandle.PolymorphicSignature
   476     @MethodHandle.PolymorphicSignature
   473     @HotSpotIntrinsicCandidate
   477     @HotSpotIntrinsicCandidate
   474     Object get(Object... args);
   478     Object get(Object... args);
   489      * , statically represented using varargs.
   493      * , statically represented using varargs.
   490      * @throws UnsupportedOperationException if the access mode is unsupported
   494      * @throws UnsupportedOperationException if the access mode is unsupported
   491      * for this VarHandle.
   495      * for this VarHandle.
   492      * @throws WrongMethodTypeException if the access mode type is not
   496      * @throws WrongMethodTypeException if the access mode type is not
   493      * compatible with the caller's symbolic type descriptor.
   497      * compatible with the caller's symbolic type descriptor.
       
   498      * @throws ClassCastException if the access mode type is compatible with the
       
   499      * caller's symbolic type descriptor, but a reference cast fails.
   494      */
   500      */
   495     public final native
   501     public final native
   496     @MethodHandle.PolymorphicSignature
   502     @MethodHandle.PolymorphicSignature
   497     @HotSpotIntrinsicCandidate
   503     @HotSpotIntrinsicCandidate
   498     void set(Object... args);
   504     void set(Object... args);
   518      * , statically represented using {@code Object}.
   524      * , statically represented using {@code Object}.
   519      * @throws UnsupportedOperationException if the access mode is unsupported
   525      * @throws UnsupportedOperationException if the access mode is unsupported
   520      * for this VarHandle.
   526      * for this VarHandle.
   521      * @throws WrongMethodTypeException if the access mode type is not
   527      * @throws WrongMethodTypeException if the access mode type is not
   522      * compatible with the caller's symbolic type descriptor.
   528      * compatible with the caller's symbolic type descriptor.
       
   529      * @throws ClassCastException if the access mode type is compatible with the
       
   530      * caller's symbolic type descriptor, but a reference cast fails.
   523      */
   531      */
   524     public final native
   532     public final native
   525     @MethodHandle.PolymorphicSignature
   533     @MethodHandle.PolymorphicSignature
   526     @HotSpotIntrinsicCandidate
   534     @HotSpotIntrinsicCandidate
   527     Object getVolatile(Object... args);
   535     Object getVolatile(Object... args);
   534      *
   542      *
   535      * <p>The symbolic type descriptor at the call site of {@code setVolatile}
   543      * <p>The symbolic type descriptor at the call site of {@code setVolatile}
   536      * must match the access mode type that is the result of calling
   544      * must match the access mode type that is the result of calling
   537      * {@code accessModeType(VarHandle.AccessMode.setVolatile)} on this VarHandle.
   545      * {@code accessModeType(VarHandle.AccessMode.setVolatile)} on this VarHandle.
   538      *
   546      *
       
   547      * @apiNote
       
   548      * Ignoring the many semantic differences from C and C++, this method has
       
   549      * memory ordering effects compatible with {@code memory_order_seq_cst}.
       
   550      *
   539      * @param args the signature-polymorphic parameter list of the form
   551      * @param args the signature-polymorphic parameter list of the form
   540      * {@code (CT, T newValue)}
   552      * {@code (CT, T newValue)}
   541      * , statically represented using varargs.
   553      * , statically represented using varargs.
   542      * @throws UnsupportedOperationException if the access mode is unsupported
   554      * @throws UnsupportedOperationException if the access mode is unsupported
   543      * for this VarHandle.
   555      * for this VarHandle.
   544      * @throws WrongMethodTypeException if the access mode type is not
   556      * @throws WrongMethodTypeException if the access mode type is not
   545      * compatible with the caller's symbolic type descriptor.
   557      * compatible with the caller's symbolic type descriptor.
   546      * @apiNote Ignoring the many semantic differences from C and C++, this
   558      * @throws ClassCastException if the access mode type is compatible with the
   547      * method has memory ordering effects compatible with
   559      * caller's symbolic type descriptor, but a reference cast fails.
   548      * {@code memory_order_seq_cst}.
       
   549      */
   560      */
   550     public final native
   561     public final native
   551     @MethodHandle.PolymorphicSignature
   562     @MethodHandle.PolymorphicSignature
   552     @HotSpotIntrinsicCandidate
   563     @HotSpotIntrinsicCandidate
   553     void setVolatile(Object... args);
   564     void setVolatile(Object... args);
   571      * , statically represented using {@code Object}.
   582      * , statically represented using {@code Object}.
   572      * @throws UnsupportedOperationException if the access mode is unsupported
   583      * @throws UnsupportedOperationException if the access mode is unsupported
   573      * for this VarHandle.
   584      * for this VarHandle.
   574      * @throws WrongMethodTypeException if the access mode type is not
   585      * @throws WrongMethodTypeException if the access mode type is not
   575      * compatible with the caller's symbolic type descriptor.
   586      * compatible with the caller's symbolic type descriptor.
       
   587      * @throws ClassCastException if the access mode type is compatible with the
       
   588      * caller's symbolic type descriptor, but a reference cast fails.
   576      */
   589      */
   577     public final native
   590     public final native
   578     @MethodHandle.PolymorphicSignature
   591     @MethodHandle.PolymorphicSignature
   579     @HotSpotIntrinsicCandidate
   592     @HotSpotIntrinsicCandidate
   580     Object getOpaque(Object... args);
   593     Object getOpaque(Object... args);
   595      * , statically represented using varargs.
   608      * , statically represented using varargs.
   596      * @throws UnsupportedOperationException if the access mode is unsupported
   609      * @throws UnsupportedOperationException if the access mode is unsupported
   597      * for this VarHandle.
   610      * for this VarHandle.
   598      * @throws WrongMethodTypeException if the access mode type is not
   611      * @throws WrongMethodTypeException if the access mode type is not
   599      * compatible with the caller's symbolic type descriptor.
   612      * compatible with the caller's symbolic type descriptor.
       
   613      * @throws ClassCastException if the access mode type is compatible with the
       
   614      * caller's symbolic type descriptor, but a reference cast fails.
   600      */
   615      */
   601     public final native
   616     public final native
   602     @MethodHandle.PolymorphicSignature
   617     @MethodHandle.PolymorphicSignature
   603     @HotSpotIntrinsicCandidate
   618     @HotSpotIntrinsicCandidate
   604     void setOpaque(Object... args);
   619     void setOpaque(Object... args);
   613      * <p>The method signature is of the form {@code (CT)T}.
   628      * <p>The method signature is of the form {@code (CT)T}.
   614      *
   629      *
   615      * <p>The symbolic type descriptor at the call site of {@code getAcquire}
   630      * <p>The symbolic type descriptor at the call site of {@code getAcquire}
   616      * must match the access mode type that is the result of calling
   631      * must match the access mode type that is the result of calling
   617      * {@code accessModeType(VarHandle.AccessMode.getAcquire)} on this VarHandle.
   632      * {@code accessModeType(VarHandle.AccessMode.getAcquire)} on this VarHandle.
       
   633      *
       
   634      * @apiNote
       
   635      * Ignoring the many semantic differences from C and C++, this method has
       
   636      * memory ordering effects compatible with {@code memory_order_acquire}
       
   637      * ordering.
   618      *
   638      *
   619      * @param args the signature-polymorphic parameter list of the form
   639      * @param args the signature-polymorphic parameter list of the form
   620      * {@code (CT)}
   640      * {@code (CT)}
   621      * , statically represented using varargs.
   641      * , statically represented using varargs.
   622      * @return the signature-polymorphic result that is the value of the
   642      * @return the signature-polymorphic result that is the value of the
   624      * , statically represented using {@code Object}.
   644      * , statically represented using {@code Object}.
   625      * @throws UnsupportedOperationException if the access mode is unsupported
   645      * @throws UnsupportedOperationException if the access mode is unsupported
   626      * for this VarHandle.
   646      * for this VarHandle.
   627      * @throws WrongMethodTypeException if the access mode type is not
   647      * @throws WrongMethodTypeException if the access mode type is not
   628      * compatible with the caller's symbolic type descriptor.
   648      * compatible with the caller's symbolic type descriptor.
   629      * @apiNote Ignoring the many semantic differences from C and C++, this
   649      * @throws ClassCastException if the access mode type is compatible with the
   630      * method has memory ordering effects compatible with
   650      * caller's symbolic type descriptor, but a reference cast fails.
   631      * {@code memory_order_acquire} ordering.
       
   632      */
   651      */
   633     public final native
   652     public final native
   634     @MethodHandle.PolymorphicSignature
   653     @MethodHandle.PolymorphicSignature
   635     @HotSpotIntrinsicCandidate
   654     @HotSpotIntrinsicCandidate
   636     Object getAcquire(Object... args);
   655     Object getAcquire(Object... args);
   643      *
   662      *
   644      * <p>The symbolic type descriptor at the call site of {@code setRelease}
   663      * <p>The symbolic type descriptor at the call site of {@code setRelease}
   645      * must match the access mode type that is the result of calling
   664      * must match the access mode type that is the result of calling
   646      * {@code accessModeType(VarHandle.AccessMode.setRelease)} on this VarHandle.
   665      * {@code accessModeType(VarHandle.AccessMode.setRelease)} on this VarHandle.
   647      *
   666      *
       
   667      * @apiNote
       
   668      * Ignoring the many semantic differences from C and C++, this method has
       
   669      * memory ordering effects compatible with {@code memory_order_release}
       
   670      * ordering.
       
   671      *
   648      * @param args the signature-polymorphic parameter list of the form
   672      * @param args the signature-polymorphic parameter list of the form
   649      * {@code (CT, T newValue)}
   673      * {@code (CT, T newValue)}
   650      * , statically represented using varargs.
   674      * , statically represented using varargs.
   651      * @throws UnsupportedOperationException if the access mode is unsupported
   675      * @throws UnsupportedOperationException if the access mode is unsupported
   652      * for this VarHandle.
   676      * for this VarHandle.
   653      * @throws WrongMethodTypeException if the access mode type is not
   677      * @throws WrongMethodTypeException if the access mode type is not
   654      * compatible with the caller's symbolic type descriptor.
   678      * compatible with the caller's symbolic type descriptor.
   655      * @apiNote Ignoring the many semantic differences from C and C++, this
   679      * @throws ClassCastException if the access mode type is compatible with the
   656      * method has memory ordering effects compatible with
   680      * caller's symbolic type descriptor, but a reference cast fails.
   657      * {@code memory_order_release} ordering.
       
   658      */
   681      */
   659     public final native
   682     public final native
   660     @MethodHandle.PolymorphicSignature
   683     @MethodHandle.PolymorphicSignature
   661     @HotSpotIntrinsicCandidate
   684     @HotSpotIntrinsicCandidate
   662     void setRelease(Object... args);
   685     void setRelease(Object... args);
   685      * witness value was not the same as the {@code expectedValue}.
   708      * witness value was not the same as the {@code expectedValue}.
   686      * @throws UnsupportedOperationException if the access mode is unsupported
   709      * @throws UnsupportedOperationException if the access mode is unsupported
   687      * for this VarHandle.
   710      * for this VarHandle.
   688      * @throws WrongMethodTypeException if the access mode type is not
   711      * @throws WrongMethodTypeException if the access mode type is not
   689      * compatible with the caller's symbolic type descriptor.
   712      * compatible with the caller's symbolic type descriptor.
       
   713      * @throws ClassCastException if the access mode type is compatible with the
       
   714      * caller's symbolic type descriptor, but a reference cast fails.
   690      * @see #setVolatile(Object...)
   715      * @see #setVolatile(Object...)
   691      * @see #getVolatile(Object...)
   716      * @see #getVolatile(Object...)
   692      */
   717      */
   693     public final native
   718     public final native
   694     @MethodHandle.PolymorphicSignature
   719     @MethodHandle.PolymorphicSignature
   718      * , statically represented using {@code Object}.
   743      * , statically represented using {@code Object}.
   719      * @throws UnsupportedOperationException if the access mode is unsupported
   744      * @throws UnsupportedOperationException if the access mode is unsupported
   720      * for this VarHandle.
   745      * for this VarHandle.
   721      * @throws WrongMethodTypeException if the access mode type is not
   746      * @throws WrongMethodTypeException if the access mode type is not
   722      * compatible with the caller's symbolic type descriptor.
   747      * compatible with the caller's symbolic type descriptor.
       
   748      * @throws ClassCastException if the access mode type is compatible with the
       
   749      * caller's symbolic type descriptor, but a reference cast fails.
   723      * @see #setVolatile(Object...)
   750      * @see #setVolatile(Object...)
   724      * @see #getVolatile(Object...)
   751      * @see #getVolatile(Object...)
   725      */
   752      */
   726     public final native
   753     public final native
   727     @MethodHandle.PolymorphicSignature
   754     @MethodHandle.PolymorphicSignature
   751      * , statically represented using {@code Object}.
   778      * , statically represented using {@code Object}.
   752      * @throws UnsupportedOperationException if the access mode is unsupported
   779      * @throws UnsupportedOperationException if the access mode is unsupported
   753      * for this VarHandle.
   780      * for this VarHandle.
   754      * @throws WrongMethodTypeException if the access mode type is not
   781      * @throws WrongMethodTypeException if the access mode type is not
   755      * compatible with the caller's symbolic type descriptor.
   782      * compatible with the caller's symbolic type descriptor.
       
   783      * @throws ClassCastException if the access mode type is compatible with the
       
   784      * caller's symbolic type descriptor, but a reference cast fails.
   756      * @see #set(Object...)
   785      * @see #set(Object...)
   757      * @see #getAcquire(Object...)
   786      * @see #getAcquire(Object...)
   758      */
   787      */
   759     public final native
   788     public final native
   760     @MethodHandle.PolymorphicSignature
   789     @MethodHandle.PolymorphicSignature
   783      * , statically represented using {@code Object}.
   812      * , statically represented using {@code Object}.
   784      * @throws UnsupportedOperationException if the access mode is unsupported
   813      * @throws UnsupportedOperationException if the access mode is unsupported
   785      * for this VarHandle.
   814      * for this VarHandle.
   786      * @throws WrongMethodTypeException if the access mode type is not
   815      * @throws WrongMethodTypeException if the access mode type is not
   787      * compatible with the caller's symbolic type descriptor.
   816      * compatible with the caller's symbolic type descriptor.
       
   817      * @throws ClassCastException if the access mode type is compatible with the
       
   818      * caller's symbolic type descriptor, but a reference cast fails.
   788      * @see #setRelease(Object...)
   819      * @see #setRelease(Object...)
   789      * @see #get(Object...)
   820      * @see #get(Object...)
   790      */
   821      */
   791     public final native
   822     public final native
   792     @MethodHandle.PolymorphicSignature
   823     @MethodHandle.PolymorphicSignature
   820      * operation spuriously failed.
   851      * operation spuriously failed.
   821      * @throws UnsupportedOperationException if the access mode is unsupported
   852      * @throws UnsupportedOperationException if the access mode is unsupported
   822      * for this VarHandle.
   853      * for this VarHandle.
   823      * @throws WrongMethodTypeException if the access mode type is not
   854      * @throws WrongMethodTypeException if the access mode type is not
   824      * compatible with the caller's symbolic type descriptor.
   855      * compatible with the caller's symbolic type descriptor.
       
   856      * @throws ClassCastException if the access mode type is compatible with the
       
   857      * caller's symbolic type descriptor, but a reference cast fails.
   825      * @see #set(Object...)
   858      * @see #set(Object...)
   826      * @see #get(Object...)
   859      * @see #get(Object...)
   827      */
   860      */
   828     public final native
   861     public final native
   829     @MethodHandle.PolymorphicSignature
   862     @MethodHandle.PolymorphicSignature
   855      * operation spuriously failed.
   888      * operation spuriously failed.
   856      * @throws UnsupportedOperationException if the access mode is unsupported
   889      * @throws UnsupportedOperationException if the access mode is unsupported
   857      * for this VarHandle.
   890      * for this VarHandle.
   858      * @throws WrongMethodTypeException if the access mode type is not
   891      * @throws WrongMethodTypeException if the access mode type is not
   859      * compatible with the caller's symbolic type descriptor.
   892      * compatible with the caller's symbolic type descriptor.
       
   893      * @throws ClassCastException if the access mode type is compatible with the
       
   894      * caller's symbolic type descriptor, but a reference cast fails.
   860      * @see #set(Object...)
   895      * @see #set(Object...)
   861      * @see #getAcquire(Object...)
   896      * @see #getAcquire(Object...)
   862      */
   897      */
   863     public final native
   898     public final native
   864     @MethodHandle.PolymorphicSignature
   899     @MethodHandle.PolymorphicSignature
   890      * operation spuriously failed.
   925      * operation spuriously failed.
   891      * @throws UnsupportedOperationException if the access mode is unsupported
   926      * @throws UnsupportedOperationException if the access mode is unsupported
   892      * for this VarHandle.
   927      * for this VarHandle.
   893      * @throws WrongMethodTypeException if the access mode type is not
   928      * @throws WrongMethodTypeException if the access mode type is not
   894      * compatible with the caller's symbolic type descriptor.
   929      * compatible with the caller's symbolic type descriptor.
       
   930      * @throws ClassCastException if the access mode type is compatible with the
       
   931      * caller's symbolic type descriptor, but a reference cast fails.
   895      * @see #setRelease(Object...)
   932      * @see #setRelease(Object...)
   896      * @see #get(Object...)
   933      * @see #get(Object...)
   897      */
   934      */
   898     public final native
   935     public final native
   899     @MethodHandle.PolymorphicSignature
   936     @MethodHandle.PolymorphicSignature
   920      * , statically represented using {@code Object}.
   957      * , statically represented using {@code Object}.
   921      * @throws UnsupportedOperationException if the access mode is unsupported
   958      * @throws UnsupportedOperationException if the access mode is unsupported
   922      * for this VarHandle.
   959      * for this VarHandle.
   923      * @throws WrongMethodTypeException if the access mode type is not
   960      * @throws WrongMethodTypeException if the access mode type is not
   924      * compatible with the caller's symbolic type descriptor.
   961      * compatible with the caller's symbolic type descriptor.
       
   962      * @throws ClassCastException if the access mode type is compatible with the
       
   963      * caller's symbolic type descriptor, but a reference cast fails.
   925      * @see #setVolatile(Object...)
   964      * @see #setVolatile(Object...)
   926      * @see #getVolatile(Object...)
   965      * @see #getVolatile(Object...)
   927      */
   966      */
   928     public final native
   967     public final native
   929     @MethodHandle.PolymorphicSignature
   968     @MethodHandle.PolymorphicSignature
   954      * , statically represented using {@code Object}.
   993      * , statically represented using {@code Object}.
   955      * @throws UnsupportedOperationException if the access mode is unsupported
   994      * @throws UnsupportedOperationException if the access mode is unsupported
   956      * for this VarHandle.
   995      * for this VarHandle.
   957      * @throws WrongMethodTypeException if the access mode type is not
   996      * @throws WrongMethodTypeException if the access mode type is not
   958      * compatible with the caller's symbolic type descriptor.
   997      * compatible with the caller's symbolic type descriptor.
       
   998      * @throws ClassCastException if the access mode type is compatible with the
       
   999      * caller's symbolic type descriptor, but a reference cast fails.
   959      * @see #setVolatile(Object...)
  1000      * @see #setVolatile(Object...)
   960      * @see #getVolatile(Object...)
  1001      * @see #getVolatile(Object...)
   961      */
  1002      */
   962     public final native
  1003     public final native
   963     @MethodHandle.PolymorphicSignature
  1004     @MethodHandle.PolymorphicSignature
   984      * , statically represented using {@code Object}.
  1025      * , statically represented using {@code Object}.
   985      * @throws UnsupportedOperationException if the access mode is unsupported
  1026      * @throws UnsupportedOperationException if the access mode is unsupported
   986      * for this VarHandle.
  1027      * for this VarHandle.
   987      * @throws WrongMethodTypeException if the access mode type is not
  1028      * @throws WrongMethodTypeException if the access mode type is not
   988      * compatible with the caller's symbolic type descriptor.
  1029      * compatible with the caller's symbolic type descriptor.
       
  1030      * @throws ClassCastException if the access mode type is compatible with the
       
  1031      * caller's symbolic type descriptor, but a reference cast fails.
   989      * @see #setVolatile(Object...)
  1032      * @see #setVolatile(Object...)
   990      * @see #getVolatile(Object...)
  1033      * @see #getVolatile(Object...)
   991      */
  1034      */
   992     public final native
  1035     public final native
   993     @MethodHandle.PolymorphicSignature
  1036     @MethodHandle.PolymorphicSignature