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