Class VarHandle
java.lang.Object
java.lang.invoke.VarHandle
public class VarHandle
extends java.lang.Object
A stub for the VarHandle class.
This stub is used to allow Java 8 release compilation to work as expected. The sole limit of this stub is that since
This stub is used to allow Java 8 release compilation to work as expected. The sole limit of this stub is that since
java.lang.invoke is a privileged package
it cannot be used at runtime (e.g. loaded by a classloader).
For example, if SomeClass is loaded at runtime:
class SomeClass {
private static final VarHandle VH = // ... obtained somehow;
public static void storeStoreFence() {
if (VH == null) {
return;
}
VH.storeStoreFence();
}
}
this is not going to work on Java 8.To fix it is possible to use an holder class (which won't be loaded at runtime):
class SomeClass {
static class Holder {
private static final VarHandle VH = // ... obtained somehow;
}
public static void storeStoreFence() {
VarHandle vh = Holder.VH;
if (vh == null) {
return;
}
vh.storeStoreFence();
}
}
Or:
class SomeClass {
private static final Object VH = // ... obtained somehow;
public static void storeStoreFence() {
if (VH == null) {
return;
}
((VarHandle)VH).storeStoreFence();
}
}
The reason why the methods on the stub are declared as native is to allow
MethodHandle.PolymorphicSignature to work as expected,
see JLS 15.12.3:A method is signature polymorphic if all of the following are true: - It is declared in the java.lang.invoke.MethodHandle class or the java.lang.invoke.VarHandle class. - It has a single variable arity parameter (ยง8.4.1) whose declared type is Object[]. - It is native.This seems counter-intuitive since this stub is not going to be used at runtime, but it is required to allow Java 8 compilation to produce
VarHandle's method invocations with parameters and result types with
the types of the formal ones of the compile-time declaration.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancompareAndSet(java.lang.Object... args) java.lang.Objectget(java.lang.Object... args) java.lang.ObjectgetAcquire(java.lang.Object... args) java.lang.ObjectgetAndAdd(java.lang.Object... args) voidset(java.lang.Object... args) voidsetRelease(java.lang.Object... args) static voidMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
VarHandle
public VarHandle()
-
-
Method Details
-
get
public java.lang.Object get(java.lang.Object... args) -
getAcquire
public java.lang.Object getAcquire(java.lang.Object... args) -
set
public void set(java.lang.Object... args) -
setRelease
public void setRelease(java.lang.Object... args) -
getAndAdd
public java.lang.Object getAndAdd(java.lang.Object... args) -
compareAndSet
public boolean compareAndSet(java.lang.Object... args) -
storeStoreFence
public static void storeStoreFence()
-