import java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; public class ArrayVarHandle { void array() { VarHandle handle = MethodHandles.arrayElementVarHandle(Test[].class); Test[] array = new Test[]{new Test(), new Test(), new SubTest()}; Test instance = new Test(); Test exactGet = (Test)handle.get(array, 0); Super superGet = (Super) handle.get(array, 1); Object objectGet = handle.get(array, 2); Object missingIndexGet = handle.get(array); Object incompatibleReceiverGet = handle.get(instance, 1); Object incompatibleIndexGet = handle.get(array, "abc"); String incompatibleResultGet = (String)handle.get(instance, 1); handle.set(array, 0, instance); handle.set(array, 1, new SubTest()); Super superItemSet = new Test(); handle.set(array, 1, superItemSet); Object objectItemSet = new Test(); handle.set(array, 2, objectItemSet); handle.set(array, instance, 0); handle.set(array, instance); handle.set(instance, array, 0); handle.set(array, 0); handle.set(array, 0, "abc"); handle.set("abc"); Test exactGAS = (Test)handle.getAndSet(array, 0, instance); SubTest subGAS = (SubTest)handle.getAndSet(array, 1, instance); Super superGAS = (Super)handle.getAndSet(array, 2, instance); Object objectGAS = handle.getAndSet(array, 0, instance); Object tooManyArgumentsGAS = handle.getAndSet(array, 0, instance, 1); Object wrongArgumentGAS = handle.getAndSet(array, instance, 0); Test tooFewArgumentsGAS = (Test)handle.getAndSet(array, instance); boolean exactCAS = handle.compareAndSet(array, 0, instance, new Test()); boolean subCAS = handle.compareAndSet(array, 0, new SubTest(), instance); boolean tooManyArgumentsCAS = handle.compareAndSet(array, 0, instance, new Test(), new Test()); boolean wrongArgumentCAS = handle.compareAndSet(array, instance, new Test(), 2); boolean tooFewArgumentsCAS = handle.compareAndSet(array, 0, instance); Test exactCAE = (Test)handle.compareAndExchange(array, 0, instance, new Test()); Super superCAE = (Super)handle.compareAndExchange(array, 0, new SubTest(), instance); Test tooManyArgumentsCAE = (Test)handle.compareAndExchange(array, 0, instance, new Test(), new Test()); Test wrongArgumentCAE = (Test)handle.compareAndExchange(array, instance, new Test(), 2); Test tooFewArgumentsCAE = (Test)handle.compareAndExchange(array, 0, instance); } void array2() { VarHandle handle = MethodHandles.arrayElementVarHandle(Test[][].class); Test[] array0 = new Test[1]; Test[][] array = new Test[][]{new Test[1]}; Test instance = new Test(); Test[] exactGet = (Test[])handle.get(array, 0); Object incompatibleReceiverGet = handle.get(array0, 1); Test incompatibleResultGet = (Test)handle.get(array, 1); handle.set(array, 0, array0); handle.set(array, 0, instance); Test[] exactGAS = (Test[])handle.getAndSet(array, 0, array0); Object wrongArgumentGAS = handle.getAndSet(array, 0, instance); boolean exactCAS = handle.compareAndSet(array, 0, array0, new Test[0]); boolean wrongArgumentCAS = handle.compareAndSet(array, 0, array0, instance); Object exactCAE = (Object)handle.compareAndExchange(array, 0, new Test[0], array0); Test[] wrongArgumentCAE = (Test[])handle.compareAndExchange(array, 0, instance, array0); Test wrongResultCAE = (Test)handle.compareAndExchange(array, 0, array0, new Test[0]); } } class Super {} class Test extends Super {} class SubTest extends Test {}