Working with variable handlers [JEP 193]
Variable handlers are typed references to variables and are governed by the java.lang.invoke.VarHandle
abstract class. TheVarHandle
method's signature is polymorphic. This provides for great variability in both method signatures and return types. Here is a code sample demonstrating how a VarHandle
might be used:
. . . class Example { int myInt; . . . } . . . class Sample { static final VarHandle VH_MYINT; static { try { VH_MYINT = MethodHandles.lookup().in(Example.class) .findVarHandle(Example.class, "myInt", int.class); } catch (Exception e) { throw new Error(e); } } } . . .
As you can see in the preceding code snippet, the VarHandle.lookup()
performs the same operation as those that are performed by a MethodHandle.lookup()
method.
The aim of this JEP was to standardize the way...