1 /*
   2  * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang.invoke;
  27 
  28 import java.lang.constant.ClassDesc;
  29 import java.lang.constant.Constable;
  30 import java.lang.constant.ConstantDesc;
  31 import java.lang.constant.ConstantDescs;
  32 import java.lang.constant.DirectMethodHandleDesc;
  33 import java.lang.constant.DynamicConstantDesc;
  34 import java.util.HashMap;
  35 import java.util.List;
  36 import java.util.Map;
  37 import java.util.Objects;
  38 import java.util.Optional;
  39 import java.util.function.BiFunction;
  40 import java.util.function.Function;
  41 
  42 import jdk.internal.HotSpotIntrinsicCandidate;
  43 import jdk.internal.util.Preconditions;
  44 import jdk.internal.vm.annotation.ForceInline;
  45 import jdk.internal.vm.annotation.Stable;
  46 
  47 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
  48 import static java.lang.invoke.MethodHandleStatics.newInternalError;
  49 
  50 /**
  51  * A VarHandle is a dynamically strongly typed reference to a variable, or to a
  52  * parametrically-defined family of variables, including static fields,
  53  * non-static fields, array elements, or components of an off-heap data
  54  * structure.  Access to such variables is supported under various
  55  * <em>access modes</em>, including plain read/write access, volatile
  56  * read/write access, and compare-and-set.
  57  *
  58  * <p>VarHandles are immutable and have no visible state.  VarHandles cannot be
  59  * subclassed by the user.
  60  *
  61  * <p>A VarHandle has:
  62  * <ul>
  63  * <li>a {@link #varType variable type} T, the type of every variable referenced
  64  * by this VarHandle; and
  65  * <li>a list of {@link #coordinateTypes coordinate types}
  66  * {@code CT1, CT2, ..., CTn}, the types of <em>coordinate expressions</em> that
  67  * jointly locate a variable referenced by this VarHandle.
  68  * </ul>
  69  * Variable and coordinate types may be primitive or reference, and are
  70  * represented by {@code Class} objects.  The list of coordinate types may be
  71  * empty.
  72  *
  73  * <p>Factory methods that produce or {@link java.lang.invoke.MethodHandles.Lookup
  74  * lookup} VarHandle instances document the supported variable type and the list
  75  * of coordinate types.
  76  *
  77  * <p>Each access mode is associated with one <em>access mode method</em>, a
  78  * <a href="MethodHandle.html#sigpoly">signature polymorphic</a> method named
  79  * for the access mode.  When an access mode method is invoked on a VarHandle
  80  * instance, the initial arguments to the invocation are coordinate expressions
  81  * that indicate in precisely which object the variable is to be accessed.
  82  * Trailing arguments to the invocation represent values of importance to the
  83  * access mode.  For example, the various compare-and-set or compare-and-exchange
  84  * access modes require two trailing arguments for the variable's expected value
  85  * and new value.
  86  *
  87  * <p>The arity and types of arguments to the invocation of an access mode
  88  * method are not checked statically.  Instead, each access mode method
  89  * specifies an {@link #accessModeType(AccessMode) access mode type},
  90  * represented as an instance of {@link MethodType}, that serves as a kind of
  91  * method signature against which the arguments are checked dynamically.  An
  92  * access mode type gives formal parameter types in terms of the coordinate
  93  * types of a VarHandle instance and the types for values of importance to the
  94  * access mode.  An access mode type also gives a return type, often in terms of
  95  * the variable type of a VarHandle instance.  When an access mode method is
  96  * invoked on a VarHandle instance, the symbolic type descriptor at the
  97  * call site, the run time types of arguments to the invocation, and the run
  98  * time type of the return value, must <a href="#invoke">match</a> the types
  99  * given in the access mode type.  A runtime exception will be thrown if the
 100  * match fails.
 101  *
 102  * For example, the access mode method {@link #compareAndSet} specifies that if
 103  * its receiver is a VarHandle instance with coordinate types
 104  * {@code CT1, ..., CTn} and variable type {@code T}, then its access mode type
 105  * is {@code (CT1 c1, ..., CTn cn, T expectedValue, T newValue)boolean}.
 106  * Suppose that a VarHandle instance can access array elements, and that its
 107  * coordinate types are {@code String[]} and {@code int} while its variable type
 108  * is {@code String}.  The access mode type for {@code compareAndSet} on this
 109  * VarHandle instance would be
 110  * {@code (String[] c1, int c2, String expectedValue, String newValue)boolean}.
 111  * Such a VarHandle instance may be produced by the
 112  * {@link MethodHandles#arrayElementVarHandle(Class) array factory method} and
 113  * access array elements as follows:
 114  * <pre> {@code
 115  * String[] sa = ...
 116  * VarHandle avh = MethodHandles.arrayElementVarHandle(String[].class);
 117  * boolean r = avh.compareAndSet(sa, 10, "expected", "new");
 118  * }</pre>
 119  *
 120  * <p>Access modes control atomicity and consistency properties.
 121  * <em>Plain</em> read ({@code get}) and write ({@code set})
 122  * accesses are guaranteed to be bitwise atomic only for references
 123  * and for primitive values of at most 32 bits, and impose no observable
 124  * ordering constraints with respect to threads other than the
 125  * executing thread. <em>Opaque</em> operations are bitwise atomic and
 126  * coherently ordered with respect to accesses to the same variable.
 127  * In addition to obeying Opaque properties, <em>Acquire</em> mode
 128  * reads and their subsequent accesses are ordered after matching
 129  * <em>Release</em> mode writes and their previous accesses.  In
 130  * addition to obeying Acquire and Release properties, all
 131  * <em>Volatile</em> operations are totally ordered with respect to
 132  * each other.
 133  *
 134  * <p>Access modes are grouped into the following categories:
 135  * <ul>
 136  * <li>read access modes that get the value of a variable under specified
 137  * memory ordering effects.
 138  * The set of corresponding access mode methods belonging to this group
 139  * consists of the methods
 140  * {@link #get get},
 141  * {@link #getVolatile getVolatile},
 142  * {@link #getAcquire getAcquire},
 143  * {@link #getOpaque getOpaque}.
 144  * <li>write access modes that set the value of a variable under specified
 145  * memory ordering effects.
 146  * The set of corresponding access mode methods belonging to this group
 147  * consists of the methods
 148  * {@link #set set},
 149  * {@link #setVolatile setVolatile},
 150  * {@link #setRelease setRelease},
 151  * {@link #setOpaque setOpaque}.
 152  * <li>atomic update access modes that, for example, atomically compare and set
 153  * the value of a variable under specified memory ordering effects.
 154  * The set of corresponding access mode methods belonging to this group
 155  * consists of the methods
 156  * {@link #compareAndSet compareAndSet},
 157  * {@link #weakCompareAndSetPlain weakCompareAndSetPlain},
 158  * {@link #weakCompareAndSet weakCompareAndSet},
 159  * {@link #weakCompareAndSetAcquire weakCompareAndSetAcquire},
 160  * {@link #weakCompareAndSetRelease weakCompareAndSetRelease},
 161  * {@link #compareAndExchangeAcquire compareAndExchangeAcquire},
 162  * {@link #compareAndExchange compareAndExchange},
 163  * {@link #compareAndExchangeRelease compareAndExchangeRelease},
 164  * {@link #getAndSet getAndSet},
 165  * {@link #getAndSetAcquire getAndSetAcquire},
 166  * {@link #getAndSetRelease getAndSetRelease}.
 167  * <li>numeric atomic update access modes that, for example, atomically get and
 168  * set with addition the value of a variable under specified memory ordering
 169  * effects.
 170  * The set of corresponding access mode methods belonging to this group
 171  * consists of the methods
 172  * {@link #getAndAdd getAndAdd},
 173  * {@link #getAndAddAcquire getAndAddAcquire},
 174  * {@link #getAndAddRelease getAndAddRelease},
 175  * <li>bitwise atomic update access modes that, for example, atomically get and
 176  * bitwise OR the value of a variable under specified memory ordering
 177  * effects.
 178  * The set of corresponding access mode methods belonging to this group
 179  * consists of the methods
 180  * {@link #getAndBitwiseOr getAndBitwiseOr},
 181  * {@link #getAndBitwiseOrAcquire getAndBitwiseOrAcquire},
 182  * {@link #getAndBitwiseOrRelease getAndBitwiseOrRelease},
 183  * {@link #getAndBitwiseAnd getAndBitwiseAnd},
 184  * {@link #getAndBitwiseAndAcquire getAndBitwiseAndAcquire},
 185  * {@link #getAndBitwiseAndRelease getAndBitwiseAndRelease},
 186  * {@link #getAndBitwiseXor getAndBitwiseXor},
 187  * {@link #getAndBitwiseXorAcquire getAndBitwiseXorAcquire},
 188  * {@link #getAndBitwiseXorRelease getAndBitwiseXorRelease}.
 189  * </ul>
 190  *
 191  * <p>Factory methods that produce or {@link java.lang.invoke.MethodHandles.Lookup
 192  * lookup} VarHandle instances document the set of access modes that are
 193  * supported, which may also include documenting restrictions based on the
 194  * variable type and whether a variable is read-only.  If an access mode is not
 195  * supported then the corresponding access mode method will on invocation throw
 196  * an {@code UnsupportedOperationException}.  Factory methods should document
 197  * any additional undeclared exceptions that may be thrown by access mode
 198  * methods.
 199  * The {@link #get get} access mode is supported for all
 200  * VarHandle instances and the corresponding method never throws
 201  * {@code UnsupportedOperationException}.
 202  * If a VarHandle references a read-only variable (for example a {@code final}
 203  * field) then write, atomic update, numeric atomic update, and bitwise atomic
 204  * update access modes are not supported and corresponding methods throw
 205  * {@code UnsupportedOperationException}.
 206  * Read/write access modes (if supported), with the exception of
 207  * {@code get} and {@code set}, provide atomic access for
 208  * reference types and all primitive types.
 209  * Unless stated otherwise in the documentation of a factory method, the access
 210  * modes {@code get} and {@code set} (if supported) provide atomic access for
 211  * reference types and all primitives types, with the exception of {@code long}
 212  * and {@code double} on 32-bit platforms.
 213  *
 214  * <p>Access modes will override any memory ordering effects specified at
 215  * the declaration site of a variable.  For example, a VarHandle accessing
 216  * a field using the {@code get} access mode will access the field as
 217  * specified <em>by its access mode</em> even if that field is declared
 218  * {@code volatile}.  When mixed access is performed extreme care should be
 219  * taken since the Java Memory Model may permit surprising results.
 220  *
 221  * <p>In addition to supporting access to variables under various access modes,
 222  * a set of static methods, referred to as memory fence methods, is also
 223  * provided for fine-grained control of memory ordering.
 224  *
 225  * The Java Language Specification permits other threads to observe operations
 226  * as if they were executed in orders different than are apparent in program
 227  * source code, subject to constraints arising, for example, from the use of
 228  * locks, {@code volatile} fields or VarHandles.  The static methods,
 229  * {@link #fullFence fullFence}, {@link #acquireFence acquireFence},
 230  * {@link #releaseFence releaseFence}, {@link #loadLoadFence loadLoadFence} and
 231  * {@link #storeStoreFence storeStoreFence}, can also be used to impose
 232  * constraints.  Their specifications, as is the case for certain access modes,
 233  * are phrased in terms of the lack of "reorderings" -- observable ordering
 234  * effects that might otherwise occur if the fence was not present.  More
 235  * precise phrasing of the specification of access mode methods and memory fence
 236  * methods may accompany future updates of the Java Language Specification.
 237  *
 238  * <h2>Compiling invocation of access mode methods</h2>
 239  * A Java method call expression naming an access mode method can invoke a
 240  * VarHandle from Java source code.  From the viewpoint of source code, these
 241  * methods can take any arguments and their polymorphic result (if expressed)
 242  * can be cast to any return type.  Formally this is accomplished by giving the
 243  * access mode methods variable arity {@code Object} arguments and
 244  * {@code Object} return types (if the return type is polymorphic), but they
 245  * have an additional quality called <em>signature polymorphism</em> which
 246  * connects this freedom of invocation directly to the JVM execution stack.
 247  * <p>
 248  * As is usual with virtual methods, source-level calls to access mode methods
 249  * compile to an {@code invokevirtual} instruction.  More unusually, the
 250  * compiler must record the actual argument types, and may not perform method
 251  * invocation conversions on the arguments.  Instead, it must generate
 252  * instructions to push them on the stack according to their own unconverted
 253  * types.  The VarHandle object itself will be pushed on the stack before the
 254  * arguments.  The compiler then generates an {@code invokevirtual} instruction
 255  * that invokes the access mode method with a symbolic type descriptor which
 256  * describes the argument and return types.
 257  * <p>
 258  * To issue a complete symbolic type descriptor, the compiler must also
 259  * determine the return type (if polymorphic).  This is based on a cast on the
 260  * method invocation expression, if there is one, or else {@code Object} if the
 261  * invocation is an expression, or else {@code void} if the invocation is a
 262  * statement.  The cast may be to a primitive type (but not {@code void}).
 263  * <p>
 264  * As a corner case, an uncasted {@code null} argument is given a symbolic type
 265  * descriptor of {@code java.lang.Void}.  The ambiguity with the type
 266  * {@code Void} is harmless, since there are no references of type {@code Void}
 267  * except the null reference.
 268  *
 269  *
 270  * <h2><a id="invoke">Performing invocation of access mode methods</a></h2>
 271  * The first time an {@code invokevirtual} instruction is executed it is linked
 272  * by symbolically resolving the names in the instruction and verifying that
 273  * the method call is statically legal.  This also holds for calls to access mode
 274  * methods.  In this case, the symbolic type descriptor emitted by the compiler
 275  * is checked for correct syntax, and names it contains are resolved.  Thus, an
 276  * {@code invokevirtual} instruction which invokes an access mode method will
 277  * always link, as long as the symbolic type descriptor is syntactically
 278  * well-formed and the types exist.
 279  * <p>
 280  * When the {@code invokevirtual} is executed after linking, the receiving
 281  * VarHandle's access mode type is first checked by the JVM to ensure that it
 282  * matches the symbolic type descriptor.  If the type
 283  * match fails, it means that the access mode method which the caller is
 284  * invoking is not present on the individual VarHandle being invoked.
 285  *
 286  * <p>
 287  * Invocation of an access mode method behaves as if an invocation of
 288  * {@link MethodHandle#invoke}, where the receiving method handle accepts the
 289  * VarHandle instance as the leading argument.  More specifically, the
 290  * following, where {@code {access-mode}} corresponds to the access mode method
 291  * name:
 292  * <pre> {@code
 293  * VarHandle vh = ..
 294  * R r = (R) vh.{access-mode}(p1, p2, ..., pN);
 295  * }</pre>
 296  * behaves as if:
 297  * <pre> {@code
 298  * VarHandle vh = ..
 299  * VarHandle.AccessMode am = VarHandle.AccessMode.valueFromMethodName("{access-mode}");
 300  * MethodHandle mh = MethodHandles.varHandleExactInvoker(
 301  *                       am,
 302  *                       vh.accessModeType(am));
 303  *
 304  * R r = (R) mh.invoke(vh, p1, p2, ..., pN)
 305  * }</pre>
 306  * (modulo access mode methods do not declare throwing of {@code Throwable}).
 307  * This is equivalent to:
 308  * <pre> {@code
 309  * MethodHandle mh = MethodHandles.lookup().findVirtual(
 310  *                       VarHandle.class,
 311  *                       "{access-mode}",
 312  *                       MethodType.methodType(R, p1, p2, ..., pN));
 313  *
 314  * R r = (R) mh.invokeExact(vh, p1, p2, ..., pN)
 315  * }</pre>
 316  * where the desired method type is the symbolic type descriptor and a
 317  * {@link MethodHandle#invokeExact} is performed, since before invocation of the
 318  * target, the handle will apply reference casts as necessary and box, unbox, or
 319  * widen primitive values, as if by {@link MethodHandle#asType asType} (see also
 320  * {@link MethodHandles#varHandleInvoker}).
 321  *
 322  * More concisely, such behaviour is equivalent to:
 323  * <pre> {@code
 324  * VarHandle vh = ..
 325  * VarHandle.AccessMode am = VarHandle.AccessMode.valueFromMethodName("{access-mode}");
 326  * MethodHandle mh = vh.toMethodHandle(am);
 327  *
 328  * R r = (R) mh.invoke(p1, p2, ..., pN)
 329  * }</pre>
 330  * Where, in this case, the method handle is bound to the VarHandle instance.
 331  *
 332  *
 333  * <h2>Invocation checking</h2>
 334  * In typical programs, VarHandle access mode type matching will usually
 335  * succeed.  But if a match fails, the JVM will throw a
 336  * {@link WrongMethodTypeException}.
 337  * <p>
 338  * Thus, an access mode type mismatch which might show up as a linkage error
 339  * in a statically typed program can show up as a dynamic
 340  * {@code WrongMethodTypeException} in a program which uses VarHandles.
 341  * <p>
 342  * Because access mode types contain "live" {@code Class} objects, method type
 343  * matching takes into account both type names and class loaders.
 344  * Thus, even if a VarHandle {@code VH} is created in one class loader
 345  * {@code L1} and used in another {@code L2}, VarHandle access mode method
 346  * calls are type-safe, because the caller's symbolic type descriptor, as
 347  * resolved in {@code L2}, is matched against the original callee method's
 348  * symbolic type descriptor, as resolved in {@code L1}.  The resolution in
 349  * {@code L1} happens when {@code VH} is created and its access mode types are
 350  * assigned, while the resolution in {@code L2} happens when the
 351  * {@code invokevirtual} instruction is linked.
 352  * <p>
 353  * Apart from type descriptor checks, a VarHandles's capability to
 354  * access it's variables is unrestricted.
 355  * If a VarHandle is formed on a non-public variable by a class that has access
 356  * to that variable, the resulting VarHandle can be used in any place by any
 357  * caller who receives a reference to it.
 358  * <p>
 359  * Unlike with the Core Reflection API, where access is checked every time a
 360  * reflective method is invoked, VarHandle access checking is performed
 361  * <a href="MethodHandles.Lookup.html#access">when the VarHandle is
 362  * created</a>.
 363  * Thus, VarHandles to non-public variables, or to variables in non-public
 364  * classes, should generally be kept secret.  They should not be passed to
 365  * untrusted code unless their use from the untrusted code would be harmless.
 366  *
 367  *
 368  * <h2>VarHandle creation</h2>
 369  * Java code can create a VarHandle that directly accesses any field that is
 370  * accessible to that code.  This is done via a reflective, capability-based
 371  * API called {@link java.lang.invoke.MethodHandles.Lookup
 372  * MethodHandles.Lookup}.
 373  * For example, a VarHandle for a non-static field can be obtained
 374  * from {@link java.lang.invoke.MethodHandles.Lookup#findVarHandle
 375  * Lookup.findVarHandle}.
 376  * There is also a conversion method from Core Reflection API objects,
 377  * {@link java.lang.invoke.MethodHandles.Lookup#unreflectVarHandle
 378  * Lookup.unreflectVarHandle}.
 379  * <p>
 380  * Access to protected field members is restricted to receivers only of the
 381  * accessing class, or one of its subclasses, and the accessing class must in
 382  * turn be a subclass (or package sibling) of the protected member's defining
 383  * class.  If a VarHandle refers to a protected non-static field of a declaring
 384  * class outside the current package, the receiver argument will be narrowed to
 385  * the type of the accessing class.
 386  *
 387  * <h2>Interoperation between VarHandles and the Core Reflection API</h2>
 388  * Using factory methods in the {@link java.lang.invoke.MethodHandles.Lookup
 389  * Lookup} API, any field represented by a Core Reflection API object
 390  * can be converted to a behaviorally equivalent VarHandle.
 391  * For example, a reflective {@link java.lang.reflect.Field Field} can
 392  * be converted to a VarHandle using
 393  * {@link java.lang.invoke.MethodHandles.Lookup#unreflectVarHandle
 394  * Lookup.unreflectVarHandle}.
 395  * The resulting VarHandles generally provide more direct and efficient
 396  * access to the underlying fields.
 397  * <p>
 398  * As a special case, when the Core Reflection API is used to view the
 399  * signature polymorphic access mode methods in this class, they appear as
 400  * ordinary non-polymorphic methods.  Their reflective appearance, as viewed by
 401  * {@link java.lang.Class#getDeclaredMethod Class.getDeclaredMethod},
 402  * is unaffected by their special status in this API.
 403  * For example, {@link java.lang.reflect.Method#getModifiers
 404  * Method.getModifiers}
 405  * will report exactly those modifier bits required for any similarly
 406  * declared method, including in this case {@code native} and {@code varargs}
 407  * bits.
 408  * <p>
 409  * As with any reflected method, these methods (when reflected) may be invoked
 410  * directly via {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke},
 411  * via JNI, or indirectly via
 412  * {@link java.lang.invoke.MethodHandles.Lookup#unreflect Lookup.unreflect}.
 413  * However, such reflective calls do not result in access mode method
 414  * invocations.  Such a call, if passed the required argument (a single one, of
 415  * type {@code Object[]}), will ignore the argument and will throw an
 416  * {@code UnsupportedOperationException}.
 417  * <p>
 418  * Since {@code invokevirtual} instructions can natively invoke VarHandle
 419  * access mode methods under any symbolic type descriptor, this reflective view
 420  * conflicts with the normal presentation of these methods via bytecodes.
 421  * Thus, these native methods, when reflectively viewed by
 422  * {@code Class.getDeclaredMethod}, may be regarded as placeholders only.
 423  * <p>
 424  * In order to obtain an invoker method for a particular access mode type,
 425  * use {@link java.lang.invoke.MethodHandles#varHandleExactInvoker} or
 426  * {@link java.lang.invoke.MethodHandles#varHandleInvoker}.  The
 427  * {@link java.lang.invoke.MethodHandles.Lookup#findVirtual Lookup.findVirtual}
 428  * API is also able to return a method handle to call an access mode method for
 429  * any specified access mode type and is equivalent in behaviour to
 430  * {@link java.lang.invoke.MethodHandles#varHandleInvoker}.
 431  *
 432  * <h2>Interoperation between VarHandles and Java generics</h2>
 433  * A VarHandle can be obtained for a variable, such as a field, which is
 434  * declared with Java generic types.  As with the Core Reflection API, the
 435  * VarHandle's variable type will be constructed from the erasure of the
 436  * source-level type.  When a VarHandle access mode method is invoked, the
 437  * types
 438  * of its arguments or the return value cast type may be generic types or type
 439  * instances.  If this occurs, the compiler will replace those types by their
 440  * erasures when it constructs the symbolic type descriptor for the
 441  * {@code invokevirtual} instruction.
 442  *
 443  * @see MethodHandle
 444  * @see MethodHandles
 445  * @see MethodType
 446  * @since 9
 447  */
 448 public abstract class VarHandle implements Constable {
 449     final VarForm vform;
 450 
 451     VarHandle(VarForm vform) {
 452         this.vform = vform;
 453     }
 454 
 455     RuntimeException unsupported() {
 456         return new UnsupportedOperationException();
 457     }
 458 
 459     // Plain accessors
 460 
 461     /**
 462      * Returns the value of a variable, with memory semantics of reading as
 463      * if the variable was declared non-{@code volatile}.  Commonly referred to
 464      * as plain read access.
 465      *
 466      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn)T}.
 467      *
 468      * <p>The symbolic type descriptor at the call site of {@code get}
 469      * must match the access mode type that is the result of calling
 470      * {@code accessModeType(VarHandle.AccessMode.GET)} on this VarHandle.
 471      *
 472      * <p>This access mode is supported by all VarHandle instances and never
 473      * throws {@code UnsupportedOperationException}.
 474      *
 475      * @param args the signature-polymorphic parameter list of the form
 476      * {@code (CT1 ct1, ..., CTn)}
 477      * , statically represented using varargs.
 478      * @return the signature-polymorphic result that is the value of the
 479      * variable
 480      * , statically represented using {@code Object}.
 481      * @throws WrongMethodTypeException if the access mode type does not
 482      * match the caller's symbolic type descriptor.
 483      * @throws ClassCastException if the access mode type matches the caller's
 484      * symbolic type descriptor, but a reference cast fails.
 485      */
 486     public final native
 487     @MethodHandle.PolymorphicSignature
 488     @HotSpotIntrinsicCandidate
 489     Object get(Object... args);
 490 
 491     /**
 492      * Sets the value of a variable to the {@code newValue}, with memory
 493      * semantics of setting as if the variable was declared non-{@code volatile}
 494      * and non-{@code final}.  Commonly referred to as plain write access.
 495      *
 496      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T newValue)void}
 497      *
 498      * <p>The symbolic type descriptor at the call site of {@code set}
 499      * must match the access mode type that is the result of calling
 500      * {@code accessModeType(VarHandle.AccessMode.SET)} on this VarHandle.
 501      *
 502      * @param args the signature-polymorphic parameter list of the form
 503      * {@code (CT1 ct1, ..., CTn ctn, T newValue)}
 504      * , statically represented using varargs.
 505      * @throws UnsupportedOperationException if the access mode is unsupported
 506      * for this VarHandle.
 507      * @throws WrongMethodTypeException if the access mode type does not
 508      * match the caller's symbolic type descriptor.
 509      * @throws ClassCastException if the access mode type matches the caller's
 510      * symbolic type descriptor, but a reference cast fails.
 511      */
 512     public final native
 513     @MethodHandle.PolymorphicSignature
 514     @HotSpotIntrinsicCandidate
 515     void set(Object... args);
 516 
 517 
 518     // Volatile accessors
 519 
 520     /**
 521      * Returns the value of a variable, with memory semantics of reading as if
 522      * the variable was declared {@code volatile}.
 523      *
 524      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn)T}.
 525      *
 526      * <p>The symbolic type descriptor at the call site of {@code getVolatile}
 527      * must match the access mode type that is the result of calling
 528      * {@code accessModeType(VarHandle.AccessMode.GET_VOLATILE)} on this
 529      * VarHandle.
 530      *
 531      * @param args the signature-polymorphic parameter list of the form
 532      * {@code (CT1 ct1, ..., CTn ctn)}
 533      * , statically represented using varargs.
 534      * @return the signature-polymorphic result that is the value of the
 535      * variable
 536      * , statically represented using {@code Object}.
 537      * @throws UnsupportedOperationException if the access mode is unsupported
 538      * for this VarHandle.
 539      * @throws WrongMethodTypeException if the access mode type does not
 540      * match the caller's symbolic type descriptor.
 541      * @throws ClassCastException if the access mode type matches the caller's
 542      * symbolic type descriptor, but a reference cast fails.
 543      */
 544     public final native
 545     @MethodHandle.PolymorphicSignature
 546     @HotSpotIntrinsicCandidate
 547     Object getVolatile(Object... args);
 548 
 549     /**
 550      * Sets the value of a variable to the {@code newValue}, with memory
 551      * semantics of setting as if the variable was declared {@code volatile}.
 552      *
 553      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T newValue)void}.
 554      *
 555      * <p>The symbolic type descriptor at the call site of {@code setVolatile}
 556      * must match the access mode type that is the result of calling
 557      * {@code accessModeType(VarHandle.AccessMode.SET_VOLATILE)} on this
 558      * VarHandle.
 559      *
 560      * @apiNote
 561      * Ignoring the many semantic differences from C and C++, this method has
 562      * memory ordering effects compatible with {@code memory_order_seq_cst}.
 563      *
 564      * @param args the signature-polymorphic parameter list of the form
 565      * {@code (CT1 ct1, ..., CTn ctn, T newValue)}
 566      * , statically represented using varargs.
 567      * @throws UnsupportedOperationException if the access mode is unsupported
 568      * for this VarHandle.
 569      * @throws WrongMethodTypeException if the access mode type does not
 570      * match the caller's symbolic type descriptor.
 571      * @throws ClassCastException if the access mode type matches the caller's
 572      * symbolic type descriptor, but a reference cast fails.
 573      */
 574     public final native
 575     @MethodHandle.PolymorphicSignature
 576     @HotSpotIntrinsicCandidate
 577     void setVolatile(Object... args);
 578 
 579 
 580     /**
 581      * Returns the value of a variable, accessed in program order, but with no
 582      * assurance of memory ordering effects with respect to other threads.
 583      *
 584      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn)T}.
 585      *
 586      * <p>The symbolic type descriptor at the call site of {@code getOpaque}
 587      * must match the access mode type that is the result of calling
 588      * {@code accessModeType(VarHandle.AccessMode.GET_OPAQUE)} on this
 589      * VarHandle.
 590      *
 591      * @param args the signature-polymorphic parameter list of the form
 592      * {@code (CT1 ct1, ..., CTn ctn)}
 593      * , statically represented using varargs.
 594      * @return the signature-polymorphic result that is the value of the
 595      * variable
 596      * , statically represented using {@code Object}.
 597      * @throws UnsupportedOperationException if the access mode is unsupported
 598      * for this VarHandle.
 599      * @throws WrongMethodTypeException if the access mode type does not
 600      * match the caller's symbolic type descriptor.
 601      * @throws ClassCastException if the access mode type matches the caller's
 602      * symbolic type descriptor, but a reference cast fails.
 603      */
 604     public final native
 605     @MethodHandle.PolymorphicSignature
 606     @HotSpotIntrinsicCandidate
 607     Object getOpaque(Object... args);
 608 
 609     /**
 610      * Sets the value of a variable to the {@code newValue}, in program order,
 611      * but with no assurance of memory ordering effects with respect to other
 612      * threads.
 613      *
 614      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T newValue)void}.
 615      *
 616      * <p>The symbolic type descriptor at the call site of {@code setOpaque}
 617      * must match the access mode type that is the result of calling
 618      * {@code accessModeType(VarHandle.AccessMode.SET_OPAQUE)} on this
 619      * VarHandle.
 620      *
 621      * @param args the signature-polymorphic parameter list of the form
 622      * {@code (CT1 ct1, ..., CTn ctn, T newValue)}
 623      * , statically represented using varargs.
 624      * @throws UnsupportedOperationException if the access mode is unsupported
 625      * for this VarHandle.
 626      * @throws WrongMethodTypeException if the access mode type does not
 627      * match the caller's symbolic type descriptor.
 628      * @throws ClassCastException if the access mode type matches the caller's
 629      * symbolic type descriptor, but a reference cast fails.
 630      */
 631     public final native
 632     @MethodHandle.PolymorphicSignature
 633     @HotSpotIntrinsicCandidate
 634     void setOpaque(Object... args);
 635 
 636 
 637     // Lazy accessors
 638 
 639     /**
 640      * Returns the value of a variable, and ensures that subsequent loads and
 641      * stores are not reordered before this access.
 642      *
 643      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn)T}.
 644      *
 645      * <p>The symbolic type descriptor at the call site of {@code getAcquire}
 646      * must match the access mode type that is the result of calling
 647      * {@code accessModeType(VarHandle.AccessMode.GET_ACQUIRE)} on this
 648      * VarHandle.
 649      *
 650      * @apiNote
 651      * Ignoring the many semantic differences from C and C++, this method has
 652      * memory ordering effects compatible with {@code memory_order_acquire}
 653      * ordering.
 654      *
 655      * @param args the signature-polymorphic parameter list of the form
 656      * {@code (CT1 ct1, ..., CTn ctn)}
 657      * , statically represented using varargs.
 658      * @return the signature-polymorphic result that is the value of the
 659      * variable
 660      * , statically represented using {@code Object}.
 661      * @throws UnsupportedOperationException if the access mode is unsupported
 662      * for this VarHandle.
 663      * @throws WrongMethodTypeException if the access mode type does not
 664      * match the caller's symbolic type descriptor.
 665      * @throws ClassCastException if the access mode type matches the caller's
 666      * symbolic type descriptor, but a reference cast fails.
 667      */
 668     public final native
 669     @MethodHandle.PolymorphicSignature
 670     @HotSpotIntrinsicCandidate
 671     Object getAcquire(Object... args);
 672 
 673     /**
 674      * Sets the value of a variable to the {@code newValue}, and ensures that
 675      * prior loads and stores are not reordered after this access.
 676      *
 677      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T newValue)void}.
 678      *
 679      * <p>The symbolic type descriptor at the call site of {@code setRelease}
 680      * must match the access mode type that is the result of calling
 681      * {@code accessModeType(VarHandle.AccessMode.SET_RELEASE)} on this
 682      * VarHandle.
 683      *
 684      * @apiNote
 685      * Ignoring the many semantic differences from C and C++, this method has
 686      * memory ordering effects compatible with {@code memory_order_release}
 687      * ordering.
 688      *
 689      * @param args the signature-polymorphic parameter list of the form
 690      * {@code (CT1 ct1, ..., CTn ctn, T newValue)}
 691      * , statically represented using varargs.
 692      * @throws UnsupportedOperationException if the access mode is unsupported
 693      * for this VarHandle.
 694      * @throws WrongMethodTypeException if the access mode type does not
 695      * match the caller's symbolic type descriptor.
 696      * @throws ClassCastException if the access mode type matches the caller's
 697      * symbolic type descriptor, but a reference cast fails.
 698      */
 699     public final native
 700     @MethodHandle.PolymorphicSignature
 701     @HotSpotIntrinsicCandidate
 702     void setRelease(Object... args);
 703 
 704 
 705     // Compare and set accessors
 706 
 707     /**
 708      * Atomically sets the value of a variable to the {@code newValue} with the
 709      * memory semantics of {@link #setVolatile} if the variable's current value,
 710      * referred to as the <em>witness value</em>, {@code ==} the
 711      * {@code expectedValue}, as accessed with the memory semantics of
 712      * {@link #getVolatile}.
 713      *
 714      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)boolean}.
 715      *
 716      * <p>The symbolic type descriptor at the call site of {@code
 717      * compareAndSet} must match the access mode type that is the result of
 718      * calling {@code accessModeType(VarHandle.AccessMode.COMPARE_AND_SET)} on
 719      * this VarHandle.
 720      *
 721      * @param args the signature-polymorphic parameter list of the form
 722      * {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)}
 723      * , statically represented using varargs.
 724      * @return {@code true} if successful, otherwise {@code false} if the
 725      * witness value was not the same as the {@code expectedValue}.
 726      * @throws UnsupportedOperationException if the access mode is unsupported
 727      * for this VarHandle.
 728      * @throws WrongMethodTypeException if the access mode type does not
 729      * match the caller's symbolic type descriptor.
 730      * @throws ClassCastException if the access mode type matches the caller's
 731      * symbolic type descriptor, but a reference cast fails.
 732      * @see #setVolatile(Object...)
 733      * @see #getVolatile(Object...)
 734      */
 735     public final native
 736     @MethodHandle.PolymorphicSignature
 737     @HotSpotIntrinsicCandidate
 738     boolean compareAndSet(Object... args);
 739 
 740     /**
 741      * Atomically sets the value of a variable to the {@code newValue} with the
 742      * memory semantics of {@link #setVolatile} if the variable's current value,
 743      * referred to as the <em>witness value</em>, {@code ==} the
 744      * {@code expectedValue}, as accessed with the memory semantics of
 745      * {@link #getVolatile}.
 746      *
 747      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)T}.
 748      *
 749      * <p>The symbolic type descriptor at the call site of {@code
 750      * compareAndExchange}
 751      * must match the access mode type that is the result of calling
 752      * {@code accessModeType(VarHandle.AccessMode.COMPARE_AND_EXCHANGE)}
 753      * on this VarHandle.
 754      *
 755      * @param args the signature-polymorphic parameter list of the form
 756      * {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)}
 757      * , statically represented using varargs.
 758      * @return the signature-polymorphic result that is the witness value, which
 759      * will be the same as the {@code expectedValue} if successful
 760      * , statically represented using {@code Object}.
 761      * @throws UnsupportedOperationException if the access mode is unsupported
 762      * for this VarHandle.
 763      * @throws WrongMethodTypeException if the access mode type is not
 764      * compatible with the caller's symbolic type descriptor.
 765      * @throws ClassCastException if the access mode type is compatible with the
 766      * caller's symbolic type descriptor, but a reference cast fails.
 767      * @see #setVolatile(Object...)
 768      * @see #getVolatile(Object...)
 769      */
 770     public final native
 771     @MethodHandle.PolymorphicSignature
 772     @HotSpotIntrinsicCandidate
 773     Object compareAndExchange(Object... args);
 774 
 775     /**
 776      * Atomically sets the value of a variable to the {@code newValue} with the
 777      * memory semantics of {@link #set} if the variable's current value,
 778      * referred to as the <em>witness value</em>, {@code ==} the
 779      * {@code expectedValue}, as accessed with the memory semantics of
 780      * {@link #getAcquire}.
 781      *
 782      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)T}.
 783      *
 784      * <p>The symbolic type descriptor at the call site of {@code
 785      * compareAndExchangeAcquire}
 786      * must match the access mode type that is the result of calling
 787      * {@code accessModeType(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE)} on
 788      * this VarHandle.
 789      *
 790      * @param args the signature-polymorphic parameter list of the form
 791      * {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)}
 792      * , statically represented using varargs.
 793      * @return the signature-polymorphic result that is the witness value, which
 794      * will be the same as the {@code expectedValue} if successful
 795      * , statically represented using {@code Object}.
 796      * @throws UnsupportedOperationException if the access mode is unsupported
 797      * for this VarHandle.
 798      * @throws WrongMethodTypeException if the access mode type does not
 799      * match the caller's symbolic type descriptor.
 800      * @throws ClassCastException if the access mode type matches the caller's
 801      * symbolic type descriptor, but a reference cast fails.
 802      * @see #set(Object...)
 803      * @see #getAcquire(Object...)
 804      */
 805     public final native
 806     @MethodHandle.PolymorphicSignature
 807     @HotSpotIntrinsicCandidate
 808     Object compareAndExchangeAcquire(Object... args);
 809 
 810     /**
 811      * Atomically sets the value of a variable to the {@code newValue} with the
 812      * memory semantics of {@link #setRelease} if the variable's current value,
 813      * referred to as the <em>witness value</em>, {@code ==} the
 814      * {@code expectedValue}, as accessed with the memory semantics of
 815      * {@link #get}.
 816      *
 817      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)T}.
 818      *
 819      * <p>The symbolic type descriptor at the call site of {@code
 820      * compareAndExchangeRelease}
 821      * must match the access mode type that is the result of calling
 822      * {@code accessModeType(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE)}
 823      * on this VarHandle.
 824      *
 825      * @param args the signature-polymorphic parameter list of the form
 826      * {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)}
 827      * , statically represented using varargs.
 828      * @return the signature-polymorphic result that is the witness value, which
 829      * will be the same as the {@code expectedValue} if successful
 830      * , statically represented using {@code Object}.
 831      * @throws UnsupportedOperationException if the access mode is unsupported
 832      * for this VarHandle.
 833      * @throws WrongMethodTypeException if the access mode type does not
 834      * match the caller's symbolic type descriptor.
 835      * @throws ClassCastException if the access mode type matches the caller's
 836      * symbolic type descriptor, but a reference cast fails.
 837      * @see #setRelease(Object...)
 838      * @see #get(Object...)
 839      */
 840     public final native
 841     @MethodHandle.PolymorphicSignature
 842     @HotSpotIntrinsicCandidate
 843     Object compareAndExchangeRelease(Object... args);
 844 
 845     // Weak (spurious failures allowed)
 846 
 847     /**
 848      * Possibly atomically sets the value of a variable to the {@code newValue}
 849      * with the semantics of {@link #set} if the variable's current value,
 850      * referred to as the <em>witness value</em>, {@code ==} the
 851      * {@code expectedValue}, as accessed with the memory semantics of
 852      * {@link #get}.
 853      *
 854      * <p>This operation may fail spuriously (typically, due to memory
 855      * contention) even if the witness value does match the expected value.
 856      *
 857      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)boolean}.
 858      *
 859      * <p>The symbolic type descriptor at the call site of {@code
 860      * weakCompareAndSetPlain} must match the access mode type that is the result of
 861      * calling {@code accessModeType(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN)}
 862      * on this VarHandle.
 863      *
 864      * @param args the signature-polymorphic parameter list of the form
 865      * {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)}
 866      * , statically represented using varargs.
 867      * @return {@code true} if successful, otherwise {@code false} if the
 868      * witness value was not the same as the {@code expectedValue} or if this
 869      * operation spuriously failed.
 870      * @throws UnsupportedOperationException if the access mode is unsupported
 871      * for this VarHandle.
 872      * @throws WrongMethodTypeException if the access mode type does not
 873      * match the caller's symbolic type descriptor.
 874      * @throws ClassCastException if the access mode type matches the caller's
 875      * symbolic type descriptor, but a reference cast fails.
 876      * @see #set(Object...)
 877      * @see #get(Object...)
 878      */
 879     public final native
 880     @MethodHandle.PolymorphicSignature
 881     @HotSpotIntrinsicCandidate
 882     boolean weakCompareAndSetPlain(Object... args);
 883 
 884     /**
 885      * Possibly atomically sets the value of a variable to the {@code newValue}
 886      * with the memory semantics of {@link #setVolatile} if the variable's
 887      * current value, referred to as the <em>witness value</em>, {@code ==} the
 888      * {@code expectedValue}, as accessed with the memory semantics of
 889      * {@link #getVolatile}.
 890      *
 891      * <p>This operation may fail spuriously (typically, due to memory
 892      * contention) even if the witness value does match the expected value.
 893      *
 894      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)boolean}.
 895      *
 896      * <p>The symbolic type descriptor at the call site of {@code
 897      * weakCompareAndSet} must match the access mode type that is the
 898      * result of calling {@code accessModeType(VarHandle.AccessMode.WEAK_COMPARE_AND_SET)}
 899      * on this VarHandle.
 900      *
 901      * @param args the signature-polymorphic parameter list of the form
 902      * {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)}
 903      * , statically represented using varargs.
 904      * @return {@code true} if successful, otherwise {@code false} if the
 905      * witness value was not the same as the {@code expectedValue} or if this
 906      * operation spuriously failed.
 907      * @throws UnsupportedOperationException if the access mode is unsupported
 908      * for this VarHandle.
 909      * @throws WrongMethodTypeException if the access mode type does not
 910      * match the caller's symbolic type descriptor.
 911      * @throws ClassCastException if the access mode type matches the caller's
 912      * symbolic type descriptor, but a reference cast fails.
 913      * @see #setVolatile(Object...)
 914      * @see #getVolatile(Object...)
 915      */
 916     public final native
 917     @MethodHandle.PolymorphicSignature
 918     @HotSpotIntrinsicCandidate
 919     boolean weakCompareAndSet(Object... args);
 920 
 921     /**
 922      * Possibly atomically sets the value of a variable to the {@code newValue}
 923      * with the semantics of {@link #set} if the variable's current value,
 924      * referred to as the <em>witness value</em>, {@code ==} the
 925      * {@code expectedValue}, as accessed with the memory semantics of
 926      * {@link #getAcquire}.
 927      *
 928      * <p>This operation may fail spuriously (typically, due to memory
 929      * contention) even if the witness value does match the expected value.
 930      *
 931      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)boolean}.
 932      *
 933      * <p>The symbolic type descriptor at the call site of {@code
 934      * weakCompareAndSetAcquire}
 935      * must match the access mode type that is the result of calling
 936      * {@code accessModeType(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE)}
 937      * on this VarHandle.
 938      *
 939      * @param args the signature-polymorphic parameter list of the form
 940      * {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)}
 941      * , statically represented using varargs.
 942      * @return {@code true} if successful, otherwise {@code false} if the
 943      * witness value was not the same as the {@code expectedValue} or if this
 944      * operation spuriously failed.
 945      * @throws UnsupportedOperationException if the access mode is unsupported
 946      * for this VarHandle.
 947      * @throws WrongMethodTypeException if the access mode type does not
 948      * match the caller's symbolic type descriptor.
 949      * @throws ClassCastException if the access mode type matches the caller's
 950      * symbolic type descriptor, but a reference cast fails.
 951      * @see #set(Object...)
 952      * @see #getAcquire(Object...)
 953      */
 954     public final native
 955     @MethodHandle.PolymorphicSignature
 956     @HotSpotIntrinsicCandidate
 957     boolean weakCompareAndSetAcquire(Object... args);
 958 
 959     /**
 960      * Possibly atomically sets the value of a variable to the {@code newValue}
 961      * with the semantics of {@link #setRelease} if the variable's current
 962      * value, referred to as the <em>witness value</em>, {@code ==} the
 963      * {@code expectedValue}, as accessed with the memory semantics of
 964      * {@link #get}.
 965      *
 966      * <p>This operation may fail spuriously (typically, due to memory
 967      * contention) even if the witness value does match the expected value.
 968      *
 969      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)boolean}.
 970      *
 971      * <p>The symbolic type descriptor at the call site of {@code
 972      * weakCompareAndSetRelease}
 973      * must match the access mode type that is the result of calling
 974      * {@code accessModeType(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_RELEASE)}
 975      * on this VarHandle.
 976      *
 977      * @param args the signature-polymorphic parameter list of the form
 978      * {@code (CT1 ct1, ..., CTn ctn, T expectedValue, T newValue)}
 979      * , statically represented using varargs.
 980      * @return {@code true} if successful, otherwise {@code false} if the
 981      * witness value was not the same as the {@code expectedValue} or if this
 982      * operation spuriously failed.
 983      * @throws UnsupportedOperationException if the access mode is unsupported
 984      * for this VarHandle.
 985      * @throws WrongMethodTypeException if the access mode type does not
 986      * match the caller's symbolic type descriptor.
 987      * @throws ClassCastException if the access mode type matches the caller's
 988      * symbolic type descriptor, but a reference cast fails.
 989      * @see #setRelease(Object...)
 990      * @see #get(Object...)
 991      */
 992     public final native
 993     @MethodHandle.PolymorphicSignature
 994     @HotSpotIntrinsicCandidate
 995     boolean weakCompareAndSetRelease(Object... args);
 996 
 997     /**
 998      * Atomically sets the value of a variable to the {@code newValue} with the
 999      * memory semantics of {@link #setVolatile} and returns the variable's
1000      * previous value, as accessed with the memory semantics of
1001      * {@link #getVolatile}.
1002      *
1003      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T newValue)T}.
1004      *
1005      * <p>The symbolic type descriptor at the call site of {@code getAndSet}
1006      * must match the access mode type that is the result of calling
1007      * {@code accessModeType(VarHandle.AccessMode.GET_AND_SET)} on this
1008      * VarHandle.
1009      *
1010      * @param args the signature-polymorphic parameter list of the form
1011      * {@code (CT1 ct1, ..., CTn ctn, T newValue)}
1012      * , statically represented using varargs.
1013      * @return the signature-polymorphic result that is the previous value of
1014      * the variable
1015      * , statically represented using {@code Object}.
1016      * @throws UnsupportedOperationException if the access mode is unsupported
1017      * for this VarHandle.
1018      * @throws WrongMethodTypeException if the access mode type does not
1019      * match the caller's symbolic type descriptor.
1020      * @throws ClassCastException if the access mode type matches the caller's
1021      * symbolic type descriptor, but a reference cast fails.
1022      * @see #setVolatile(Object...)
1023      * @see #getVolatile(Object...)
1024      */
1025     public final native
1026     @MethodHandle.PolymorphicSignature
1027     @HotSpotIntrinsicCandidate
1028     Object getAndSet(Object... args);
1029 
1030     /**
1031      * Atomically sets the value of a variable to the {@code newValue} with the
1032      * memory semantics of {@link #set} and returns the variable's
1033      * previous value, as accessed with the memory semantics of
1034      * {@link #getAcquire}.
1035      *
1036      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T newValue)T}.
1037      *
1038      * <p>The symbolic type descriptor at the call site of {@code getAndSetAcquire}
1039      * must match the access mode type that is the result of calling
1040      * {@code accessModeType(VarHandle.AccessMode.GET_AND_SET_ACQUIRE)} on this
1041      * VarHandle.
1042      *
1043      * @param args the signature-polymorphic parameter list of the form
1044      * {@code (CT1 ct1, ..., CTn ctn, T newValue)}
1045      * , statically represented using varargs.
1046      * @return the signature-polymorphic result that is the previous value of
1047      * the variable
1048      * , statically represented using {@code Object}.
1049      * @throws UnsupportedOperationException if the access mode is unsupported
1050      * for this VarHandle.
1051      * @throws WrongMethodTypeException if the access mode type does not
1052      * match the caller's symbolic type descriptor.
1053      * @throws ClassCastException if the access mode type matches the caller's
1054      * symbolic type descriptor, but a reference cast fails.
1055      * @see #setVolatile(Object...)
1056      * @see #getVolatile(Object...)
1057      */
1058     public final native
1059     @MethodHandle.PolymorphicSignature
1060     @HotSpotIntrinsicCandidate
1061     Object getAndSetAcquire(Object... args);
1062 
1063     /**
1064      * Atomically sets the value of a variable to the {@code newValue} with the
1065      * memory semantics of {@link #setRelease} and returns the variable's
1066      * previous value, as accessed with the memory semantics of
1067      * {@link #get}.
1068      *
1069      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T newValue)T}.
1070      *
1071      * <p>The symbolic type descriptor at the call site of {@code getAndSetRelease}
1072      * must match the access mode type that is the result of calling
1073      * {@code accessModeType(VarHandle.AccessMode.GET_AND_SET_RELEASE)} on this
1074      * VarHandle.
1075      *
1076      * @param args the signature-polymorphic parameter list of the form
1077      * {@code (CT1 ct1, ..., CTn ctn, T newValue)}
1078      * , statically represented using varargs.
1079      * @return the signature-polymorphic result that is the previous value of
1080      * the variable
1081      * , statically represented using {@code Object}.
1082      * @throws UnsupportedOperationException if the access mode is unsupported
1083      * for this VarHandle.
1084      * @throws WrongMethodTypeException if the access mode type does not
1085      * match the caller's symbolic type descriptor.
1086      * @throws ClassCastException if the access mode type matches the caller's
1087      * symbolic type descriptor, but a reference cast fails.
1088      * @see #setVolatile(Object...)
1089      * @see #getVolatile(Object...)
1090      */
1091     public final native
1092     @MethodHandle.PolymorphicSignature
1093     @HotSpotIntrinsicCandidate
1094     Object getAndSetRelease(Object... args);
1095 
1096     // Primitive adders
1097     // Throw UnsupportedOperationException for refs
1098 
1099     /**
1100      * Atomically adds the {@code value} to the current value of a variable with
1101      * the memory semantics of {@link #setVolatile}, and returns the variable's
1102      * previous value, as accessed with the memory semantics of
1103      * {@link #getVolatile}.
1104      *
1105      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T value)T}.
1106      *
1107      * <p>The symbolic type descriptor at the call site of {@code getAndAdd}
1108      * must match the access mode type that is the result of calling
1109      * {@code accessModeType(VarHandle.AccessMode.GET_AND_ADD)} on this
1110      * VarHandle.
1111      *
1112      * @param args the signature-polymorphic parameter list of the form
1113      * {@code (CT1 ct1, ..., CTn ctn, T value)}
1114      * , statically represented using varargs.
1115      * @return the signature-polymorphic result that is the previous value of
1116      * the variable
1117      * , statically represented using {@code Object}.
1118      * @throws UnsupportedOperationException if the access mode is unsupported
1119      * for this VarHandle.
1120      * @throws WrongMethodTypeException if the access mode type does not
1121      * match the caller's symbolic type descriptor.
1122      * @throws ClassCastException if the access mode type matches the caller's
1123      * symbolic type descriptor, but a reference cast fails.
1124      * @see #setVolatile(Object...)
1125      * @see #getVolatile(Object...)
1126      */
1127     public final native
1128     @MethodHandle.PolymorphicSignature
1129     @HotSpotIntrinsicCandidate
1130     Object getAndAdd(Object... args);
1131 
1132     /**
1133      * Atomically adds the {@code value} to the current value of a variable with
1134      * the memory semantics of {@link #set}, and returns the variable's
1135      * previous value, as accessed with the memory semantics of
1136      * {@link #getAcquire}.
1137      *
1138      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T value)T}.
1139      *
1140      * <p>The symbolic type descriptor at the call site of {@code getAndAddAcquire}
1141      * must match the access mode type that is the result of calling
1142      * {@code accessModeType(VarHandle.AccessMode.GET_AND_ADD_ACQUIRE)} on this
1143      * VarHandle.
1144      *
1145      * @param args the signature-polymorphic parameter list of the form
1146      * {@code (CT1 ct1, ..., CTn ctn, T value)}
1147      * , statically represented using varargs.
1148      * @return the signature-polymorphic result that is the previous value of
1149      * the variable
1150      * , statically represented using {@code Object}.
1151      * @throws UnsupportedOperationException if the access mode is unsupported
1152      * for this VarHandle.
1153      * @throws WrongMethodTypeException if the access mode type does not
1154      * match the caller's symbolic type descriptor.
1155      * @throws ClassCastException if the access mode type matches the caller's
1156      * symbolic type descriptor, but a reference cast fails.
1157      * @see #setVolatile(Object...)
1158      * @see #getVolatile(Object...)
1159      */
1160     public final native
1161     @MethodHandle.PolymorphicSignature
1162     @HotSpotIntrinsicCandidate
1163     Object getAndAddAcquire(Object... args);
1164 
1165     /**
1166      * Atomically adds the {@code value} to the current value of a variable with
1167      * the memory semantics of {@link #setRelease}, and returns the variable's
1168      * previous value, as accessed with the memory semantics of
1169      * {@link #get}.
1170      *
1171      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T value)T}.
1172      *
1173      * <p>The symbolic type descriptor at the call site of {@code getAndAddRelease}
1174      * must match the access mode type that is the result of calling
1175      * {@code accessModeType(VarHandle.AccessMode.GET_AND_ADD_RELEASE)} on this
1176      * VarHandle.
1177      *
1178      * @param args the signature-polymorphic parameter list of the form
1179      * {@code (CT1 ct1, ..., CTn ctn, T value)}
1180      * , statically represented using varargs.
1181      * @return the signature-polymorphic result that is the previous value of
1182      * the variable
1183      * , statically represented using {@code Object}.
1184      * @throws UnsupportedOperationException if the access mode is unsupported
1185      * for this VarHandle.
1186      * @throws WrongMethodTypeException if the access mode type does not
1187      * match the caller's symbolic type descriptor.
1188      * @throws ClassCastException if the access mode type matches the caller's
1189      * symbolic type descriptor, but a reference cast fails.
1190      * @see #setVolatile(Object...)
1191      * @see #getVolatile(Object...)
1192      */
1193     public final native
1194     @MethodHandle.PolymorphicSignature
1195     @HotSpotIntrinsicCandidate
1196     Object getAndAddRelease(Object... args);
1197 
1198 
1199     // Bitwise operations
1200     // Throw UnsupportedOperationException for refs
1201 
1202     /**
1203      * Atomically sets the value of a variable to the result of
1204      * bitwise OR between the variable's current value and the {@code mask}
1205      * with the memory semantics of {@link #setVolatile} and returns the
1206      * variable's previous value, as accessed with the memory semantics of
1207      * {@link #getVolatile}.
1208      *
1209      * <p>If the variable type is the non-integral {@code boolean} type then a
1210      * logical OR is performed instead of a bitwise OR.
1211      *
1212      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1213      *
1214      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseOr}
1215      * must match the access mode type that is the result of calling
1216      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_OR)} on this
1217      * VarHandle.
1218      *
1219      * @param args the signature-polymorphic parameter list of the form
1220      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1221      * , statically represented using varargs.
1222      * @return the signature-polymorphic result that is the previous value of
1223      * the variable
1224      * , statically represented using {@code Object}.
1225      * @throws UnsupportedOperationException if the access mode is unsupported
1226      * for this VarHandle.
1227      * @throws WrongMethodTypeException if the access mode type does not
1228      * match the caller's symbolic type descriptor.
1229      * @throws ClassCastException if the access mode type matches the caller's
1230      * symbolic type descriptor, but a reference cast fails.
1231      * @see #setVolatile(Object...)
1232      * @see #getVolatile(Object...)
1233      */
1234     public final native
1235     @MethodHandle.PolymorphicSignature
1236     @HotSpotIntrinsicCandidate
1237     Object getAndBitwiseOr(Object... args);
1238 
1239     /**
1240      * Atomically sets the value of a variable to the result of
1241      * bitwise OR between the variable's current value and the {@code mask}
1242      * with the memory semantics of {@link #set} and returns the
1243      * variable's previous value, as accessed with the memory semantics of
1244      * {@link #getAcquire}.
1245      *
1246      * <p>If the variable type is the non-integral {@code boolean} type then a
1247      * logical OR is performed instead of a bitwise OR.
1248      *
1249      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1250      *
1251      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseOrAcquire}
1252      * must match the access mode type that is the result of calling
1253      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_OR_ACQUIRE)} on this
1254      * VarHandle.
1255      *
1256      * @param args the signature-polymorphic parameter list of the form
1257      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1258      * , statically represented using varargs.
1259      * @return the signature-polymorphic result that is the previous value of
1260      * the variable
1261      * , statically represented using {@code Object}.
1262      * @throws UnsupportedOperationException if the access mode is unsupported
1263      * for this VarHandle.
1264      * @throws WrongMethodTypeException if the access mode type does not
1265      * match the caller's symbolic type descriptor.
1266      * @throws ClassCastException if the access mode type matches the caller's
1267      * symbolic type descriptor, but a reference cast fails.
1268      * @see #set(Object...)
1269      * @see #getAcquire(Object...)
1270      */
1271     public final native
1272     @MethodHandle.PolymorphicSignature
1273     @HotSpotIntrinsicCandidate
1274     Object getAndBitwiseOrAcquire(Object... args);
1275 
1276     /**
1277      * Atomically sets the value of a variable to the result of
1278      * bitwise OR between the variable's current value and the {@code mask}
1279      * with the memory semantics of {@link #setRelease} and returns the
1280      * variable's previous value, as accessed with the memory semantics of
1281      * {@link #get}.
1282      *
1283      * <p>If the variable type is the non-integral {@code boolean} type then a
1284      * logical OR is performed instead of a bitwise OR.
1285      *
1286      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1287      *
1288      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseOrRelease}
1289      * must match the access mode type that is the result of calling
1290      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_OR_RELEASE)} on this
1291      * VarHandle.
1292      *
1293      * @param args the signature-polymorphic parameter list of the form
1294      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1295      * , statically represented using varargs.
1296      * @return the signature-polymorphic result that is the previous value of
1297      * the variable
1298      * , statically represented using {@code Object}.
1299      * @throws UnsupportedOperationException if the access mode is unsupported
1300      * for this VarHandle.
1301      * @throws WrongMethodTypeException if the access mode type does not
1302      * match the caller's symbolic type descriptor.
1303      * @throws ClassCastException if the access mode type matches the caller's
1304      * symbolic type descriptor, but a reference cast fails.
1305      * @see #setRelease(Object...)
1306      * @see #get(Object...)
1307      */
1308     public final native
1309     @MethodHandle.PolymorphicSignature
1310     @HotSpotIntrinsicCandidate
1311     Object getAndBitwiseOrRelease(Object... args);
1312 
1313     /**
1314      * Atomically sets the value of a variable to the result of
1315      * bitwise AND between the variable's current value and the {@code mask}
1316      * with the memory semantics of {@link #setVolatile} and returns the
1317      * variable's previous value, as accessed with the memory semantics of
1318      * {@link #getVolatile}.
1319      *
1320      * <p>If the variable type is the non-integral {@code boolean} type then a
1321      * logical AND is performed instead of a bitwise AND.
1322      *
1323      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1324      *
1325      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseAnd}
1326      * must match the access mode type that is the result of calling
1327      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_AND)} on this
1328      * VarHandle.
1329      *
1330      * @param args the signature-polymorphic parameter list of the form
1331      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1332      * , statically represented using varargs.
1333      * @return the signature-polymorphic result that is the previous value of
1334      * the variable
1335      * , statically represented using {@code Object}.
1336      * @throws UnsupportedOperationException if the access mode is unsupported
1337      * for this VarHandle.
1338      * @throws WrongMethodTypeException if the access mode type does not
1339      * match the caller's symbolic type descriptor.
1340      * @throws ClassCastException if the access mode type matches the caller's
1341      * symbolic type descriptor, but a reference cast fails.
1342      * @see #setVolatile(Object...)
1343      * @see #getVolatile(Object...)
1344      */
1345     public final native
1346     @MethodHandle.PolymorphicSignature
1347     @HotSpotIntrinsicCandidate
1348     Object getAndBitwiseAnd(Object... args);
1349 
1350     /**
1351      * Atomically sets the value of a variable to the result of
1352      * bitwise AND between the variable's current value and the {@code mask}
1353      * with the memory semantics of {@link #set} and returns the
1354      * variable's previous value, as accessed with the memory semantics of
1355      * {@link #getAcquire}.
1356      *
1357      * <p>If the variable type is the non-integral {@code boolean} type then a
1358      * logical AND is performed instead of a bitwise AND.
1359      *
1360      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1361      *
1362      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseAndAcquire}
1363      * must match the access mode type that is the result of calling
1364      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE)} on this
1365      * VarHandle.
1366      *
1367      * @param args the signature-polymorphic parameter list of the form
1368      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1369      * , statically represented using varargs.
1370      * @return the signature-polymorphic result that is the previous value of
1371      * the variable
1372      * , statically represented using {@code Object}.
1373      * @throws UnsupportedOperationException if the access mode is unsupported
1374      * for this VarHandle.
1375      * @throws WrongMethodTypeException if the access mode type does not
1376      * match the caller's symbolic type descriptor.
1377      * @throws ClassCastException if the access mode type matches the caller's
1378      * symbolic type descriptor, but a reference cast fails.
1379      * @see #set(Object...)
1380      * @see #getAcquire(Object...)
1381      */
1382     public final native
1383     @MethodHandle.PolymorphicSignature
1384     @HotSpotIntrinsicCandidate
1385     Object getAndBitwiseAndAcquire(Object... args);
1386 
1387     /**
1388      * Atomically sets the value of a variable to the result of
1389      * bitwise AND between the variable's current value and the {@code mask}
1390      * with the memory semantics of {@link #setRelease} and returns the
1391      * variable's previous value, as accessed with the memory semantics of
1392      * {@link #get}.
1393      *
1394      * <p>If the variable type is the non-integral {@code boolean} type then a
1395      * logical AND is performed instead of a bitwise AND.
1396      *
1397      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1398      *
1399      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseAndRelease}
1400      * must match the access mode type that is the result of calling
1401      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE)} on this
1402      * VarHandle.
1403      *
1404      * @param args the signature-polymorphic parameter list of the form
1405      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1406      * , statically represented using varargs.
1407      * @return the signature-polymorphic result that is the previous value of
1408      * the variable
1409      * , statically represented using {@code Object}.
1410      * @throws UnsupportedOperationException if the access mode is unsupported
1411      * for this VarHandle.
1412      * @throws WrongMethodTypeException if the access mode type does not
1413      * match the caller's symbolic type descriptor.
1414      * @throws ClassCastException if the access mode type matches the caller's
1415      * symbolic type descriptor, but a reference cast fails.
1416      * @see #setRelease(Object...)
1417      * @see #get(Object...)
1418      */
1419     public final native
1420     @MethodHandle.PolymorphicSignature
1421     @HotSpotIntrinsicCandidate
1422     Object getAndBitwiseAndRelease(Object... args);
1423 
1424     /**
1425      * Atomically sets the value of a variable to the result of
1426      * bitwise XOR between the variable's current value and the {@code mask}
1427      * with the memory semantics of {@link #setVolatile} and returns the
1428      * variable's previous value, as accessed with the memory semantics of
1429      * {@link #getVolatile}.
1430      *
1431      * <p>If the variable type is the non-integral {@code boolean} type then a
1432      * logical XOR is performed instead of a bitwise XOR.
1433      *
1434      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1435      *
1436      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseXor}
1437      * must match the access mode type that is the result of calling
1438      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_XOR)} on this
1439      * VarHandle.
1440      *
1441      * @param args the signature-polymorphic parameter list of the form
1442      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1443      * , statically represented using varargs.
1444      * @return the signature-polymorphic result that is the previous value of
1445      * the variable
1446      * , statically represented using {@code Object}.
1447      * @throws UnsupportedOperationException if the access mode is unsupported
1448      * for this VarHandle.
1449      * @throws WrongMethodTypeException if the access mode type does not
1450      * match the caller's symbolic type descriptor.
1451      * @throws ClassCastException if the access mode type matches the caller's
1452      * symbolic type descriptor, but a reference cast fails.
1453      * @see #setVolatile(Object...)
1454      * @see #getVolatile(Object...)
1455      */
1456     public final native
1457     @MethodHandle.PolymorphicSignature
1458     @HotSpotIntrinsicCandidate
1459     Object getAndBitwiseXor(Object... args);
1460 
1461     /**
1462      * Atomically sets the value of a variable to the result of
1463      * bitwise XOR between the variable's current value and the {@code mask}
1464      * with the memory semantics of {@link #set} and returns the
1465      * variable's previous value, as accessed with the memory semantics of
1466      * {@link #getAcquire}.
1467      *
1468      * <p>If the variable type is the non-integral {@code boolean} type then a
1469      * logical XOR is performed instead of a bitwise XOR.
1470      *
1471      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1472      *
1473      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseXorAcquire}
1474      * must match the access mode type that is the result of calling
1475      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE)} on this
1476      * VarHandle.
1477      *
1478      * @param args the signature-polymorphic parameter list of the form
1479      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1480      * , statically represented using varargs.
1481      * @return the signature-polymorphic result that is the previous value of
1482      * the variable
1483      * , statically represented using {@code Object}.
1484      * @throws UnsupportedOperationException if the access mode is unsupported
1485      * for this VarHandle.
1486      * @throws WrongMethodTypeException if the access mode type does not
1487      * match the caller's symbolic type descriptor.
1488      * @throws ClassCastException if the access mode type matches the caller's
1489      * symbolic type descriptor, but a reference cast fails.
1490      * @see #set(Object...)
1491      * @see #getAcquire(Object...)
1492      */
1493     public final native
1494     @MethodHandle.PolymorphicSignature
1495     @HotSpotIntrinsicCandidate
1496     Object getAndBitwiseXorAcquire(Object... args);
1497 
1498     /**
1499      * Atomically sets the value of a variable to the result of
1500      * bitwise XOR between the variable's current value and the {@code mask}
1501      * with the memory semantics of {@link #setRelease} and returns the
1502      * variable's previous value, as accessed with the memory semantics of
1503      * {@link #get}.
1504      *
1505      * <p>If the variable type is the non-integral {@code boolean} type then a
1506      * logical XOR is performed instead of a bitwise XOR.
1507      *
1508      * <p>The method signature is of the form {@code (CT1 ct1, ..., CTn ctn, T mask)T}.
1509      *
1510      * <p>The symbolic type descriptor at the call site of {@code getAndBitwiseXorRelease}
1511      * must match the access mode type that is the result of calling
1512      * {@code accessModeType(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE)} on this
1513      * VarHandle.
1514      *
1515      * @param args the signature-polymorphic parameter list of the form
1516      * {@code (CT1 ct1, ..., CTn ctn, T mask)}
1517      * , statically represented using varargs.
1518      * @return the signature-polymorphic result that is the previous value of
1519      * the variable
1520      * , statically represented using {@code Object}.
1521      * @throws UnsupportedOperationException if the access mode is unsupported
1522      * for this VarHandle.
1523      * @throws WrongMethodTypeException if the access mode type does not
1524      * match the caller's symbolic type descriptor.
1525      * @throws ClassCastException if the access mode type matches the caller's
1526      * symbolic type descriptor, but a reference cast fails.
1527      * @see #setRelease(Object...)
1528      * @see #get(Object...)
1529      */
1530     public final native
1531     @MethodHandle.PolymorphicSignature
1532     @HotSpotIntrinsicCandidate
1533     Object getAndBitwiseXorRelease(Object... args);
1534 
1535 
1536     enum AccessType {
1537         GET(Object.class),
1538         SET(void.class),
1539         COMPARE_AND_SET(boolean.class),
1540         COMPARE_AND_EXCHANGE(Object.class),
1541         GET_AND_UPDATE(Object.class);
1542 
1543         final Class<?> returnType;
1544         final boolean isMonomorphicInReturnType;
1545 
1546         AccessType(Class<?> returnType) {
1547             this.returnType = returnType;
1548             isMonomorphicInReturnType = returnType != Object.class;
1549         }
1550 
1551         MethodType accessModeType(Class<?> receiver, Class<?> value,
1552                                   Class<?>... intermediate) {
1553             Class<?>[] ps;
1554             int i;
1555             switch (this) {
1556                 case GET:
1557                     ps = allocateParameters(0, receiver, intermediate);
1558                     fillParameters(ps, receiver, intermediate);
1559                     return MethodType.methodType(value, ps);
1560                 case SET:
1561                     ps = allocateParameters(1, receiver, intermediate);
1562                     i = fillParameters(ps, receiver, intermediate);
1563                     ps[i] = value;
1564                     return MethodType.methodType(void.class, ps);
1565                 case COMPARE_AND_SET:
1566                     ps = allocateParameters(2, receiver, intermediate);
1567                     i = fillParameters(ps, receiver, intermediate);
1568                     ps[i++] = value;
1569                     ps[i] = value;
1570                     return MethodType.methodType(boolean.class, ps);
1571                 case COMPARE_AND_EXCHANGE:
1572                     ps = allocateParameters(2, receiver, intermediate);
1573                     i = fillParameters(ps, receiver, intermediate);
1574                     ps[i++] = value;
1575                     ps[i] = value;
1576                     return MethodType.methodType(value, ps);
1577                 case GET_AND_UPDATE:
1578                     ps = allocateParameters(1, receiver, intermediate);
1579                     i = fillParameters(ps, receiver, intermediate);
1580                     ps[i] = value;
1581                     return MethodType.methodType(value, ps);
1582                 default:
1583                     throw new InternalError("Unknown AccessType");
1584             }
1585         }
1586 
1587         private static Class<?>[] allocateParameters(int values,
1588                                                      Class<?> receiver, Class<?>... intermediate) {
1589             int size = ((receiver != null) ? 1 : 0) + intermediate.length + values;
1590             return new Class<?>[size];
1591         }
1592 
1593         private static int fillParameters(Class<?>[] ps,
1594                                           Class<?> receiver, Class<?>... intermediate) {
1595             int i = 0;
1596             if (receiver != null)
1597                 ps[i++] = receiver;
1598             for (int j = 0; j < intermediate.length; j++)
1599                 ps[i++] = intermediate[j];
1600             return i;
1601         }
1602     }
1603 
1604     /**
1605      * The set of access modes that specify how a variable, referenced by a
1606      * VarHandle, is accessed.
1607      */
1608     public enum AccessMode {
1609         /**
1610          * The access mode whose access is specified by the corresponding
1611          * method
1612          * {@link VarHandle#get VarHandle.get}
1613          */
1614         GET("get", AccessType.GET),
1615         /**
1616          * The access mode whose access is specified by the corresponding
1617          * method
1618          * {@link VarHandle#set VarHandle.set}
1619          */
1620         SET("set", AccessType.SET),
1621         /**
1622          * The access mode whose access is specified by the corresponding
1623          * method
1624          * {@link VarHandle#getVolatile VarHandle.getVolatile}
1625          */
1626         GET_VOLATILE("getVolatile", AccessType.GET),
1627         /**
1628          * The access mode whose access is specified by the corresponding
1629          * method
1630          * {@link VarHandle#setVolatile VarHandle.setVolatile}
1631          */
1632         SET_VOLATILE("setVolatile", AccessType.SET),
1633         /**
1634          * The access mode whose access is specified by the corresponding
1635          * method
1636          * {@link VarHandle#getAcquire VarHandle.getAcquire}
1637          */
1638         GET_ACQUIRE("getAcquire", AccessType.GET),
1639         /**
1640          * The access mode whose access is specified by the corresponding
1641          * method
1642          * {@link VarHandle#setRelease VarHandle.setRelease}
1643          */
1644         SET_RELEASE("setRelease", AccessType.SET),
1645         /**
1646          * The access mode whose access is specified by the corresponding
1647          * method
1648          * {@link VarHandle#getOpaque VarHandle.getOpaque}
1649          */
1650         GET_OPAQUE("getOpaque", AccessType.GET),
1651         /**
1652          * The access mode whose access is specified by the corresponding
1653          * method
1654          * {@link VarHandle#setOpaque VarHandle.setOpaque}
1655          */
1656         SET_OPAQUE("setOpaque", AccessType.SET),
1657         /**
1658          * The access mode whose access is specified by the corresponding
1659          * method
1660          * {@link VarHandle#compareAndSet VarHandle.compareAndSet}
1661          */
1662         COMPARE_AND_SET("compareAndSet", AccessType.COMPARE_AND_SET),
1663         /**
1664          * The access mode whose access is specified by the corresponding
1665          * method
1666          * {@link VarHandle#compareAndExchange VarHandle.compareAndExchange}
1667          */
1668         COMPARE_AND_EXCHANGE("compareAndExchange", AccessType.COMPARE_AND_EXCHANGE),
1669         /**
1670          * The access mode whose access is specified by the corresponding
1671          * method
1672          * {@link VarHandle#compareAndExchangeAcquire VarHandle.compareAndExchangeAcquire}
1673          */
1674         COMPARE_AND_EXCHANGE_ACQUIRE("compareAndExchangeAcquire", AccessType.COMPARE_AND_EXCHANGE),
1675         /**
1676          * The access mode whose access is specified by the corresponding
1677          * method
1678          * {@link VarHandle#compareAndExchangeRelease VarHandle.compareAndExchangeRelease}
1679          */
1680         COMPARE_AND_EXCHANGE_RELEASE("compareAndExchangeRelease", AccessType.COMPARE_AND_EXCHANGE),
1681         /**
1682          * The access mode whose access is specified by the corresponding
1683          * method
1684          * {@link VarHandle#weakCompareAndSetPlain VarHandle.weakCompareAndSetPlain}
1685          */
1686         WEAK_COMPARE_AND_SET_PLAIN("weakCompareAndSetPlain", AccessType.COMPARE_AND_SET),
1687         /**
1688          * The access mode whose access is specified by the corresponding
1689          * method
1690          * {@link VarHandle#weakCompareAndSet VarHandle.weakCompareAndSet}
1691          */
1692         WEAK_COMPARE_AND_SET("weakCompareAndSet", AccessType.COMPARE_AND_SET),
1693         /**
1694          * The access mode whose access is specified by the corresponding
1695          * method
1696          * {@link VarHandle#weakCompareAndSetAcquire VarHandle.weakCompareAndSetAcquire}
1697          */
1698         WEAK_COMPARE_AND_SET_ACQUIRE("weakCompareAndSetAcquire", AccessType.COMPARE_AND_SET),
1699         /**
1700          * The access mode whose access is specified by the corresponding
1701          * method
1702          * {@link VarHandle#weakCompareAndSetRelease VarHandle.weakCompareAndSetRelease}
1703          */
1704         WEAK_COMPARE_AND_SET_RELEASE("weakCompareAndSetRelease", AccessType.COMPARE_AND_SET),
1705         /**
1706          * The access mode whose access is specified by the corresponding
1707          * method
1708          * {@link VarHandle#getAndSet VarHandle.getAndSet}
1709          */
1710         GET_AND_SET("getAndSet", AccessType.GET_AND_UPDATE),
1711         /**
1712          * The access mode whose access is specified by the corresponding
1713          * method
1714          * {@link VarHandle#getAndSetAcquire VarHandle.getAndSetAcquire}
1715          */
1716         GET_AND_SET_ACQUIRE("getAndSetAcquire", AccessType.GET_AND_UPDATE),
1717         /**
1718          * The access mode whose access is specified by the corresponding
1719          * method
1720          * {@link VarHandle#getAndSetRelease VarHandle.getAndSetRelease}
1721          */
1722         GET_AND_SET_RELEASE("getAndSetRelease", AccessType.GET_AND_UPDATE),
1723         /**
1724          * The access mode whose access is specified by the corresponding
1725          * method
1726          * {@link VarHandle#getAndAdd VarHandle.getAndAdd}
1727          */
1728         GET_AND_ADD("getAndAdd", AccessType.GET_AND_UPDATE),
1729         /**
1730          * The access mode whose access is specified by the corresponding
1731          * method
1732          * {@link VarHandle#getAndAddAcquire VarHandle.getAndAddAcquire}
1733          */
1734         GET_AND_ADD_ACQUIRE("getAndAddAcquire", AccessType.GET_AND_UPDATE),
1735         /**
1736          * The access mode whose access is specified by the corresponding
1737          * method
1738          * {@link VarHandle#getAndAddRelease VarHandle.getAndAddRelease}
1739          */
1740         GET_AND_ADD_RELEASE("getAndAddRelease", AccessType.GET_AND_UPDATE),
1741         /**
1742          * The access mode whose access is specified by the corresponding
1743          * method
1744          * {@link VarHandle#getAndBitwiseOr VarHandle.getAndBitwiseOr}
1745          */
1746         GET_AND_BITWISE_OR("getAndBitwiseOr", AccessType.GET_AND_UPDATE),
1747         /**
1748          * The access mode whose access is specified by the corresponding
1749          * method
1750          * {@link VarHandle#getAndBitwiseOrRelease VarHandle.getAndBitwiseOrRelease}
1751          */
1752         GET_AND_BITWISE_OR_RELEASE("getAndBitwiseOrRelease", AccessType.GET_AND_UPDATE),
1753         /**
1754          * The access mode whose access is specified by the corresponding
1755          * method
1756          * {@link VarHandle#getAndBitwiseOrAcquire VarHandle.getAndBitwiseOrAcquire}
1757          */
1758         GET_AND_BITWISE_OR_ACQUIRE("getAndBitwiseOrAcquire", AccessType.GET_AND_UPDATE),
1759         /**
1760          * The access mode whose access is specified by the corresponding
1761          * method
1762          * {@link VarHandle#getAndBitwiseAnd VarHandle.getAndBitwiseAnd}
1763          */
1764         GET_AND_BITWISE_AND("getAndBitwiseAnd", AccessType.GET_AND_UPDATE),
1765         /**
1766          * The access mode whose access is specified by the corresponding
1767          * method
1768          * {@link VarHandle#getAndBitwiseAndRelease VarHandle.getAndBitwiseAndRelease}
1769          */
1770         GET_AND_BITWISE_AND_RELEASE("getAndBitwiseAndRelease", AccessType.GET_AND_UPDATE),
1771         /**
1772          * The access mode whose access is specified by the corresponding
1773          * method
1774          * {@link VarHandle#getAndBitwiseAndAcquire VarHandle.getAndBitwiseAndAcquire}
1775          */
1776         GET_AND_BITWISE_AND_ACQUIRE("getAndBitwiseAndAcquire", AccessType.GET_AND_UPDATE),
1777         /**
1778          * The access mode whose access is specified by the corresponding
1779          * method
1780          * {@link VarHandle#getAndBitwiseXor VarHandle.getAndBitwiseXor}
1781          */
1782         GET_AND_BITWISE_XOR("getAndBitwiseXor", AccessType.GET_AND_UPDATE),
1783         /**
1784          * The access mode whose access is specified by the corresponding
1785          * method
1786          * {@link VarHandle#getAndBitwiseXorRelease VarHandle.getAndBitwiseXorRelease}
1787          */
1788         GET_AND_BITWISE_XOR_RELEASE("getAndBitwiseXorRelease", AccessType.GET_AND_UPDATE),
1789         /**
1790          * The access mode whose access is specified by the corresponding
1791          * method
1792          * {@link VarHandle#getAndBitwiseXorAcquire VarHandle.getAndBitwiseXorAcquire}
1793          */
1794         GET_AND_BITWISE_XOR_ACQUIRE("getAndBitwiseXorAcquire", AccessType.GET_AND_UPDATE),
1795         ;
1796 
1797         static final Map<String, AccessMode> methodNameToAccessMode;
1798         static {
1799             AccessMode[] values = AccessMode.values();
1800             // Initial capacity of # values divided by the load factor is sufficient
1801             // to avoid resizes for the smallest table size (64)
1802             int initialCapacity = (int)(values.length / 0.75f) + 1;
1803             methodNameToAccessMode = new HashMap<>(initialCapacity);
1804             for (AccessMode am : values) {
1805                 methodNameToAccessMode.put(am.methodName, am);
1806             }
1807         }
1808 
1809         final String methodName;
1810         final AccessType at;
1811 
1812         AccessMode(final String methodName, AccessType at) {
1813             this.methodName = methodName;
1814             this.at = at;
1815         }
1816 
1817         /**
1818          * Returns the {@code VarHandle} signature-polymorphic method name
1819          * associated with this {@code AccessMode} value.
1820          *
1821          * @return the signature-polymorphic method name
1822          * @see #valueFromMethodName
1823          */
1824         public String methodName() {
1825             return methodName;
1826         }
1827 
1828         /**
1829          * Returns the {@code AccessMode} value associated with the specified
1830          * {@code VarHandle} signature-polymorphic method name.
1831          *
1832          * @param methodName the signature-polymorphic method name
1833          * @return the {@code AccessMode} value
1834          * @throws IllegalArgumentException if there is no {@code AccessMode}
1835          *         value associated with method name (indicating the method
1836          *         name does not correspond to a {@code VarHandle}
1837          *         signature-polymorphic method name).
1838          * @see #methodName()
1839          */
1840         public static AccessMode valueFromMethodName(String methodName) {
1841             AccessMode am = methodNameToAccessMode.get(methodName);
1842             if (am != null) return am;
1843             throw new IllegalArgumentException("No AccessMode value for method name " + methodName);
1844         }
1845 
1846         @ForceInline
1847         static MemberName getMemberName(int ordinal, VarForm vform) {
1848             return vform.memberName_table[ordinal];
1849         }
1850     }
1851 
1852     static final class AccessDescriptor {
1853         final MethodType symbolicMethodTypeErased;
1854         final MethodType symbolicMethodTypeInvoker;
1855         final Class<?> returnType;
1856         final int type;
1857         final int mode;
1858 
1859         public AccessDescriptor(MethodType symbolicMethodType, int type, int mode) {
1860             this.symbolicMethodTypeErased = symbolicMethodType.erase();
1861             this.symbolicMethodTypeInvoker = symbolicMethodType.insertParameterTypes(0, VarHandle.class);
1862             this.returnType = symbolicMethodType.returnType();
1863             this.type = type;
1864             this.mode = mode;
1865         }
1866     }
1867 
1868     /**
1869      * Returns a compact textual description of this {@linkplain VarHandle},
1870      * including the type of variable described, and a description of its coordinates.
1871      *
1872      * @return A compact textual description of this {@linkplain VarHandle}
1873      */
1874     @Override
1875     public final String toString() {
1876         return String.format("VarHandle[varType=%s, coord=%s]",
1877                              varType().getName(),
1878                              coordinateTypes());
1879     }
1880 
1881     /**
1882      * Returns the variable type of variables referenced by this VarHandle.
1883      *
1884      * @return the variable type of variables referenced by this VarHandle
1885      */
1886     public final Class<?> varType() {
1887         MethodType typeSet = accessModeType(AccessMode.SET);
1888         return typeSet.parameterType(typeSet.parameterCount() - 1);
1889     }
1890 
1891     /**
1892      * Returns the coordinate types for this VarHandle.
1893      *
1894      * @return the coordinate types for this VarHandle. The returned
1895      * list is unmodifiable
1896      */
1897     public final List<Class<?>> coordinateTypes() {
1898         MethodType typeGet = accessModeType(AccessMode.GET);
1899         return typeGet.parameterList();
1900     }
1901 
1902     /**
1903      * Obtains the access mode type for this VarHandle and a given access mode.
1904      *
1905      * <p>The access mode type's parameter types will consist of a prefix that
1906      * is the coordinate types of this VarHandle followed by further
1907      * types as defined by the access mode method.
1908      * The access mode type's return type is defined by the return type of the
1909      * access mode method.
1910      *
1911      * @param accessMode the access mode, corresponding to the
1912      * signature-polymorphic method of the same name
1913      * @return the access mode type for the given access mode
1914      */
1915     public final MethodType accessModeType(AccessMode accessMode) {
1916         TypesAndInvokers tis = getTypesAndInvokers();
1917         MethodType mt = tis.methodType_table[accessMode.at.ordinal()];
1918         if (mt == null) {
1919             mt = tis.methodType_table[accessMode.at.ordinal()] =
1920                     accessModeTypeUncached(accessMode);
1921         }
1922         return mt;
1923     }
1924     abstract MethodType accessModeTypeUncached(AccessMode accessMode);
1925 
1926     /**
1927      * Returns {@code true} if the given access mode is supported, otherwise
1928      * {@code false}.
1929      *
1930      * <p>The return of a {@code false} value for a given access mode indicates
1931      * that an {@code UnsupportedOperationException} is thrown on invocation
1932      * of the corresponding access mode method.
1933      *
1934      * @param accessMode the access mode, corresponding to the
1935      * signature-polymorphic method of the same name
1936      * @return {@code true} if the given access mode is supported, otherwise
1937      * {@code false}.
1938      */
1939     public final boolean isAccessModeSupported(AccessMode accessMode) {
1940         return AccessMode.getMemberName(accessMode.ordinal(), vform) != null;
1941     }
1942 
1943     /**
1944      * Obtains a method handle bound to this VarHandle and the given access
1945      * mode.
1946      *
1947      * @apiNote This method, for a VarHandle {@code vh} and access mode
1948      * {@code {access-mode}}, returns a method handle that is equivalent to
1949      * method handle {@code bmh} in the following code (though it may be more
1950      * efficient):
1951      * <pre>{@code
1952      * MethodHandle mh = MethodHandles.varHandleExactInvoker(
1953      *                       vh.accessModeType(VarHandle.AccessMode.{access-mode}));
1954      *
1955      * MethodHandle bmh = mh.bindTo(vh);
1956      * }</pre>
1957      *
1958      * @param accessMode the access mode, corresponding to the
1959      * signature-polymorphic method of the same name
1960      * @return a method handle bound to this VarHandle and the given access mode
1961      */
1962     public final MethodHandle toMethodHandle(AccessMode accessMode) {
1963         MemberName mn = AccessMode.getMemberName(accessMode.ordinal(), vform);
1964         if (mn != null) {
1965             MethodHandle mh = getMethodHandle(accessMode.ordinal());
1966             return mh.bindTo(this);
1967         }
1968         else {
1969             // Ensure an UnsupportedOperationException is thrown
1970             return MethodHandles.varHandleInvoker(accessMode, accessModeType(accessMode)).
1971                     bindTo(this);
1972         }
1973     }
1974 
1975     /**
1976      * Return a nominal descriptor for this instance, if one can be
1977      * constructed, or an empty {@link Optional} if one cannot be.
1978      *
1979      * @return An {@link Optional} containing the resulting nominal descriptor,
1980      * or an empty {@link Optional} if one cannot be constructed.
1981      * @since 12
1982      */
1983     @Override
1984     public Optional<VarHandleDesc> describeConstable() {
1985         // partial function for field and array only
1986         return Optional.empty();
1987     }
1988 
1989     @Stable
1990     TypesAndInvokers typesAndInvokers;
1991 
1992     static class TypesAndInvokers {
1993         final @Stable
1994         MethodType[] methodType_table =
1995                 new MethodType[VarHandle.AccessType.values().length];
1996 
1997         final @Stable
1998         MethodHandle[] methodHandle_table =
1999                 new MethodHandle[AccessMode.values().length];
2000     }
2001 
2002     @ForceInline
2003     private final TypesAndInvokers getTypesAndInvokers() {
2004         TypesAndInvokers tis = typesAndInvokers;
2005         if (tis == null) {
2006             tis = typesAndInvokers = new TypesAndInvokers();
2007         }
2008         return tis;
2009     }
2010 
2011     @ForceInline
2012     final MethodHandle getMethodHandle(int mode) {
2013         TypesAndInvokers tis = getTypesAndInvokers();
2014         MethodHandle mh = tis.methodHandle_table[mode];
2015         if (mh == null) {
2016             mh = tis.methodHandle_table[mode] = getMethodHandleUncached(mode);
2017         }
2018         return mh;
2019     }
2020     private final MethodHandle getMethodHandleUncached(int mode) {
2021         MethodType mt = accessModeType(AccessMode.values()[mode]).
2022                 insertParameterTypes(0, VarHandle.class);
2023         MemberName mn = vform.getMemberName(mode);
2024         DirectMethodHandle dmh = DirectMethodHandle.make(mn);
2025         // Such a method handle must not be publically exposed directly
2026         // otherwise it can be cracked, it must be transformed or rebound
2027         // before exposure
2028         MethodHandle mh = dmh.copyWith(mt, dmh.form);
2029         assert mh.type().erase() == mn.getMethodType().erase();
2030         return mh;
2031     }
2032 
2033 
2034     /*non-public*/
2035     final void updateVarForm(VarForm newVForm) {
2036         if (vform == newVForm) return;
2037         UNSAFE.putReference(this, VFORM_OFFSET, newVForm);
2038         UNSAFE.fullFence();
2039     }
2040 
2041     static final BiFunction<String, List<Integer>, ArrayIndexOutOfBoundsException>
2042             AIOOBE_SUPPLIER = Preconditions.outOfBoundsExceptionFormatter(
2043             new Function<String, ArrayIndexOutOfBoundsException>() {
2044                 @Override
2045                 public ArrayIndexOutOfBoundsException apply(String s) {
2046                     return new ArrayIndexOutOfBoundsException(s);
2047                 }
2048             });
2049 
2050     private static final long VFORM_OFFSET;
2051 
2052     static {
2053         VFORM_OFFSET = UNSAFE.objectFieldOffset(VarHandle.class, "vform");
2054 
2055         // The VarHandleGuards must be initialized to ensure correct
2056         // compilation of the guard methods
2057         UNSAFE.ensureClassInitialized(VarHandleGuards.class);
2058     }
2059 
2060 
2061     // Fence methods
2062 
2063     /**
2064      * Ensures that loads and stores before the fence will not be reordered
2065      * with
2066      * loads and stores after the fence.
2067      *
2068      * @apiNote Ignoring the many semantic differences from C and C++, this
2069      * method has memory ordering effects compatible with
2070      * {@code atomic_thread_fence(memory_order_seq_cst)}
2071      */
2072     @ForceInline
2073     public static void fullFence() {
2074         UNSAFE.fullFence();
2075     }
2076 
2077     /**
2078      * Ensures that loads before the fence will not be reordered with loads and
2079      * stores after the fence.
2080      *
2081      * @apiNote Ignoring the many semantic differences from C and C++, this
2082      * method has memory ordering effects compatible with
2083      * {@code atomic_thread_fence(memory_order_acquire)}
2084      */
2085     @ForceInline
2086     public static void acquireFence() {
2087         UNSAFE.loadFence();
2088     }
2089 
2090     /**
2091      * Ensures that loads and stores before the fence will not be
2092      * reordered with stores after the fence.
2093      *
2094      * @apiNote Ignoring the many semantic differences from C and C++, this
2095      * method has memory ordering effects compatible with
2096      * {@code atomic_thread_fence(memory_order_release)}
2097      */
2098     @ForceInline
2099     public static void releaseFence() {
2100         UNSAFE.storeFence();
2101     }
2102 
2103     /**
2104      * Ensures that loads before the fence will not be reordered with
2105      * loads after the fence.
2106      */
2107     @ForceInline
2108     public static void loadLoadFence() {
2109         UNSAFE.loadLoadFence();
2110     }
2111 
2112     /**
2113      * Ensures that stores before the fence will not be reordered with
2114      * stores after the fence.
2115      */
2116     @ForceInline
2117     public static void storeStoreFence() {
2118         UNSAFE.storeStoreFence();
2119     }
2120 
2121     /**
2122      * A <a href="package-summary.html#nominal">nominal descriptor</a> for a
2123      * {@link VarHandle} constant.
2124      *
2125      * @since 12
2126      */
2127     public static final class VarHandleDesc extends DynamicConstantDesc<VarHandle> {
2128 
2129         /**
2130          * Kinds of variable handle descs
2131          */
2132         private enum Kind {
2133             FIELD(ConstantDescs.BSM_VARHANDLE_FIELD),
2134             STATIC_FIELD(ConstantDescs.BSM_VARHANDLE_STATIC_FIELD),
2135             ARRAY(ConstantDescs.BSM_VARHANDLE_ARRAY);
2136 
2137             final DirectMethodHandleDesc bootstrapMethod;
2138 
2139             Kind(DirectMethodHandleDesc bootstrapMethod) {
2140                 this.bootstrapMethod = bootstrapMethod;
2141             }
2142 
2143             ConstantDesc[] toBSMArgs(ClassDesc declaringClass, ClassDesc varType) {
2144                 switch (this) {
2145                     case FIELD:
2146                     case STATIC_FIELD:
2147                         return new ConstantDesc[] {declaringClass, varType };
2148                     case ARRAY:
2149                         return new ConstantDesc[] {declaringClass };
2150                     default:
2151                         throw new InternalError("Cannot reach here");
2152                 }
2153             }
2154         }
2155 
2156         private final Kind kind;
2157         private final ClassDesc declaringClass;
2158         private final ClassDesc varType;
2159 
2160         /**
2161          * Construct a {@linkplain VarHandleDesc} given a kind, name, and declaring
2162          * class.
2163          *
2164          * @param kind the kind of the var handle
2165          * @param name the unqualified name of the field, for field var handles; otherwise ignored
2166          * @param declaringClass a {@link ClassDesc} describing the declaring class,
2167          *                       for field var handles
2168          * @param varType a {@link ClassDesc} describing the type of the variable
2169          * @throws NullPointerException if any required argument is null
2170          * @jvms 4.2.2 Unqualified Names
2171          */
2172         private VarHandleDesc(Kind kind, String name, ClassDesc declaringClass, ClassDesc varType) {
2173             super(kind.bootstrapMethod, name,
2174                   ConstantDescs.CD_VarHandle,
2175                   kind.toBSMArgs(declaringClass, varType));
2176             this.kind = kind;
2177             this.declaringClass = declaringClass;
2178             this.varType = varType;
2179         }
2180 
2181         /**
2182          * Returns a {@linkplain VarHandleDesc} corresponding to a {@link VarHandle}
2183          * for an instance field.
2184          *
2185          * @param name the unqualifed name of the field
2186          * @param declaringClass a {@link ClassDesc} describing the declaring class,
2187          *                       for field var handles
2188          * @param fieldType a {@link ClassDesc} describing the type of the field
2189          * @return the {@linkplain VarHandleDesc}
2190          * @throws NullPointerException if any of the arguments are null
2191          * @jvms 4.2.2 Unqualified Names
2192          */
2193         public static VarHandleDesc ofField(ClassDesc declaringClass, String name, ClassDesc fieldType) {
2194             Objects.requireNonNull(declaringClass);
2195             Objects.requireNonNull(name);
2196             Objects.requireNonNull(fieldType);
2197             return new VarHandleDesc(Kind.FIELD, name, declaringClass, fieldType);
2198         }
2199 
2200         /**
2201          * Returns a {@linkplain VarHandleDesc} corresponding to a {@link VarHandle}
2202          * for a static field.
2203          *
2204          * @param name the unqualified name of the field
2205          * @param declaringClass a {@link ClassDesc} describing the declaring class,
2206          *                       for field var handles
2207          * @param fieldType a {@link ClassDesc} describing the type of the field
2208          * @return the {@linkplain VarHandleDesc}
2209          * @throws NullPointerException if any of the arguments are null
2210          * @jvms 4.2.2 Unqualified Names
2211          */
2212         public static VarHandleDesc ofStaticField(ClassDesc declaringClass, String name, ClassDesc fieldType) {
2213             Objects.requireNonNull(declaringClass);
2214             Objects.requireNonNull(name);
2215             Objects.requireNonNull(fieldType);
2216             return new VarHandleDesc(Kind.STATIC_FIELD, name, declaringClass, fieldType);
2217         }
2218 
2219         /**
2220          * Returns a {@linkplain VarHandleDesc} corresponding to a {@link VarHandle}
2221          * for an array type.
2222          *
2223          * @param arrayClass a {@link ClassDesc} describing the type of the array
2224          * @return the {@linkplain VarHandleDesc}
2225          * @throws NullPointerException if any of the arguments are null
2226          */
2227         public static VarHandleDesc ofArray(ClassDesc arrayClass) {
2228             Objects.requireNonNull(arrayClass);
2229             if (!arrayClass.isArray())
2230                 throw new IllegalArgumentException("Array class argument not an array: " + arrayClass);
2231             return new VarHandleDesc(Kind.ARRAY, ConstantDescs.DEFAULT_NAME, arrayClass, arrayClass.componentType());
2232         }
2233 
2234         /**
2235          * Returns a {@link ClassDesc} describing the type of the variable described
2236          * by this descriptor.
2237          *
2238          * @return the variable type
2239          */
2240         public ClassDesc varType() {
2241             return varType;
2242         }
2243 
2244         @Override
2245         public VarHandle resolveConstantDesc(MethodHandles.Lookup lookup)
2246                 throws ReflectiveOperationException {
2247             switch (kind) {
2248                 case FIELD:
2249                     return lookup.findVarHandle((Class<?>) declaringClass.resolveConstantDesc(lookup),
2250                                                 constantName(),
2251                                                 (Class<?>) varType.resolveConstantDesc(lookup));
2252                 case STATIC_FIELD:
2253                     return lookup.findStaticVarHandle((Class<?>) declaringClass.resolveConstantDesc(lookup),
2254                                                       constantName(),
2255                                                       (Class<?>) varType.resolveConstantDesc(lookup));
2256                 case ARRAY:
2257                     return MethodHandles.arrayElementVarHandle((Class<?>) declaringClass.resolveConstantDesc(lookup));
2258                 default:
2259                     throw new InternalError("Cannot reach here");
2260             }
2261         }
2262 
2263         /**
2264          * Returns a compact textual description of this constant description.
2265          * For a field {@linkplain VarHandle}, includes the owner, name, and type
2266          * of the field, and whether it is static; for an array {@linkplain VarHandle},
2267          * the name of the component type.
2268          *
2269          * @return A compact textual description of this descriptor
2270          */
2271         @Override
2272         public String toString() {
2273             switch (kind) {
2274                 case FIELD:
2275                 case STATIC_FIELD:
2276                     return String.format("VarHandleDesc[%s%s.%s:%s]",
2277                                          (kind == Kind.STATIC_FIELD) ? "static " : "",
2278                                          declaringClass.displayName(), constantName(), varType.displayName());
2279                 case ARRAY:
2280                     return String.format("VarHandleDesc[%s[]]", declaringClass.displayName());
2281                 default:
2282                     throw new InternalError("Cannot reach here");
2283             }
2284         }
2285 
2286         @Override
2287         public Optional<? extends ConstantDesc> describeConstable() {
2288             return Optional.empty();
2289         }
2290     }
2291 
2292 }