import java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; class Main { void getInt() throws Throwable { MethodHandles.Lookup lookup = MethodHandles.lookup(); VarHandle handle = lookup.findStaticVarHandle(Test.class, "n", int.class); Test instance = new Test(); int exact = (int) handle.get(); Integer boxed = (Integer) handle.get(); Object object = (Object) handle.get(); String incompatible = (String) handle.get(); handle.get(instance); int exactV = (int) handle.getVolatile(); Integer boxedV = (Integer) handle.getVolatile(); Object objectV = (Object) handle.getVolatile(); String incompatibleV = (String) handle.getVolatile(); handle.getVolatile(instance); int exactO = (int) handle.getOpaque(); Integer boxedO = (Integer) handle.getOpaque(); Object objectO = (Object) handle.getOpaque(); String incompatibleO = (String) handle.getOpaque(); handle.getOpaque(instance); int exactA = (int) handle.getAcquire(); Integer boxedA = (Integer) handle.getAcquire(); Object objectA = (Object) handle.getAcquire(); String incompatibleA = (String) handle.getAcquire(); handle.getAcquire(instance); } void setInt() throws Throwable { MethodHandles.Lookup lookup = MethodHandles.lookup(); VarHandle handle = lookup.findStaticVarHandle(Test.class, "n", int.class); Test instance = new Test(); Object nullArg = null; Object textArg = "abc"; Object numberArg = 123; handle.set(1); handle.set(Integer.valueOf(2)); handle.set(numberArg); handle.set(textArg); handle.set(instance, 1); handle.set(nullArg); handle.set(); handle.setVolatile(1); handle.setVolatile(Integer.valueOf(2)); handle.setVolatile(numberArg); handle.setVolatile(textArg); handle.setVolatile(instance, 1); handle.setVolatile(nullArg); handle.setVolatile(); handle.setOpaque(1); handle.setOpaque(Integer.valueOf(2)); handle.setOpaque(numberArg); handle.setOpaque(textArg); handle.setOpaque(instance, 1); handle.setOpaque(nullArg); handle.setOpaque(); handle.setRelease(1); handle.setRelease(Integer.valueOf(2)); handle.setRelease(numberArg); handle.setRelease(textArg); handle.setRelease(instance, 1); handle.setRelease(nullArg); handle.setRelease(); } void getStr() throws Throwable { MethodHandles.Lookup lookup = MethodHandles.lookup(); final VarHandle handle = lookup.findStaticVarHandle(Test.class, "s", String.class); final Test instance = new Test(); String exact = (String) handle.get(); CharSequence parent = (CharSequence) handle.get(); Object object = (Object) handle.get(); Integer incompatible = (Integer) handle.get(); handle.get(instance); String exactV = (String) handle.getVolatile(); CharSequence parentV = (CharSequence) handle.getVolatile(); Object objectV = (Object) handle.getVolatile(); Integer incompatibleV = (Integer) handle.getVolatile(); handle.getVolatile(instance); String exactO = (String) handle.getOpaque(); CharSequence parentO = (CharSequence) handle.getOpaque(); Object objectO = (Object) handle.getOpaque(); Integer incompatibleO = (Integer) handle.getOpaque(); handle.getOpaque(instance); String exactA = (String) handle.getAcquire(); CharSequence parentA = (CharSequence) handle.getAcquire(); Object objectA = (Object) handle.getAcquire(); Integer incompatibleA = (Integer) handle.getAcquire(); handle.getAcquire(instance); } void setStr() throws Throwable { MethodHandles.Lookup lookup = MethodHandles.lookup(); final VarHandle handle = lookup.findStaticVarHandle(Test.class, "s", String.class); final Test instance = new Test(); final Object nullArg = null; final Object textArg = "abc"; final Object numberArg = 3; handle.set("a"); handle.set(charSequence()); handle.set(textArg); handle.set(nullArg); handle.set(numberArg); handle.set(instance, "abc"); handle.set(); handle.setVolatile("a"); handle.setVolatile(charSequence()); handle.setVolatile(textArg); handle.setVolatile(nullArg); handle.setVolatile(numberArg); handle.setVolatile(instance, "abc"); handle.setVolatile(); handle.setOpaque("a"); handle.setOpaque(charSequence()); handle.setOpaque(textArg); handle.setOpaque(nullArg); handle.setOpaque(numberArg); handle.setOpaque(instance, "abc"); handle.setOpaque(); handle.setRelease("a"); handle.setRelease(charSequence()); handle.setRelease(textArg); handle.setRelease(nullArg); handle.setRelease(numberArg); handle.setRelease(instance, "abc"); handle.setRelease(); } private void getAndAddInt() throws Throwable { MethodHandles.Lookup lookup = MethodHandles.lookup(); VarHandle handle = lookup.findStaticVarHandle(Test.class, "n", int.class); Test instance = new Test(); Object nullArg = null; int exact = (int) handle.getAndAdd(1); Integer boxed = (Integer) handle.getAndAdd(2); Object object = (Object) handle.getAndAdd(3); handle.getAndAdd(4); String incompatible = (String) handle.getAndAdd(0); int incompatibleArg = (int) handle.getAndSet("abc"); handle.getAndAdd(1, 2); handle.getAndAdd(); int exactA = (int) handle.getAndAddAcquire(1); ; Integer boxedA = (Integer) handle.getAndAddAcquire(2); Object objectA = (Object) handle.getAndAddAcquire(3); handle.getAndAddAcquire(4); String incompatibleA = (String) handle.getAndAddAcquire(instance, 0); int incompatibleArgA = (int) handle.getAndSetAcquire("abc"); handle.getAndAddAcquire(1, 2); handle.getAndAddAcquire(); int exactR = (int) handle.getAndAddRelease(1); ; Integer boxedR = (Integer) handle.getAndAddRelease(2); Object objectR = (Object) handle.getAndAddRelease(3); handle.getAndAddRelease(4); String incompatibleR = (String) handle.getAndAdd(0); int incompatibleArgR = (int) handle.getAndSetRelease("abc"); handle.getAndAddRelease(1, 2); handle.getAndAddRelease(); } private void getAndSetStr() throws Throwable { MethodHandles.Lookup lookup = MethodHandles.lookup(); final VarHandle handle = lookup.findStaticVarHandle(Test.class, "s", String.class); final Test instance = new Test(); final Object nullArg = null; String exact = (String) handle.getAndSet("a"); CharSequence parent = (CharSequence) handle.getAndSet("b"); Object object = (Object) handle.getAndSet("c"); String subclassArg = (String) handle.getAndSet(charSequence()); String withNullArg = (String) handle.getAndSet(nullArg); handle.getAndSet("d"); Integer incompatible = (Integer) handle.getAndSet("e"); String incompatibleArg = (String) handle.getAndSet(123); handle.getAndSet("a", "b"); handle.getAndSet(); String exactA = (String) handle.getAndSetAcquire("a"); CharSequence parentA = (CharSequence) handle.getAndSetAcquire("b"); Object objectA = (Object) handle.getAndSetAcquire("c");; String subclassArgA = (String) handle.getAndSetAcquire(charSequence()); String withNullArgA = (String) handle.getAndSetAcquire(nullArg); handle.getAndSetAcquire("d"); Integer incompatibleA = (Integer) handle.getAndSetAcquire("e"); String incompatibleArgA = (String) handle.getAndSetAcquire(123); handle.getAndSetAcquire("a", "b"); handle.getAndSetAcquire(); String exactR = (String) handle.getAndSetRelease("a"); CharSequence parentR = (CharSequence) handle.getAndSetRelease("b"); Object objectR = (Object) handle.getAndSetRelease("c");; String subclassArgR = (String) handle.getAndSetRelease(charSequence()); String withNullArgR = (String) handle.getAndSetRelease(nullArg); handle.getAndSetRelease("d"); Integer incompatibleR = (Integer) handle.getAndSetRelease("e"); String incompatibleArgR = (String) handle.getAndSetRelease(123); handle.getAndSetRelease("a", "b"); handle.getAndSetRelease(); } void getAndBitwise() throws Throwable { MethodHandles.Lookup lookup = MethodHandles.lookup(); VarHandle handle = lookup.findStaticVarHandle(Test.class, "n", int.class); Test instance = new Test(); int exactBitwiseAnd = (int) handle.getAndBitwiseAnd(1); int exactBitwiseAndA = (int) handle.getAndBitwiseAndAcquire(2); int exactBitwiseAndR = (int) handle.getAndBitwiseAndRelease(3); int wrongArgBitwiseAnd = (int) handle.getAndBitwiseAnd("a"); int wrongArgBitwiseAndA = (int) handle.getAndBitwiseAndAcquire("b"); int wrongArgBitwiseAndR = (int) handle.getAndBitwiseAndRelease("c"); String wrongResultBitwiseAnd = (String) handle.getAndBitwiseAnd(1); String wrongResultBitwiseAndA = (String) handle.getAndBitwiseAndAcquire(2); String wrongResultBitwiseAndR = (String) handle.getAndBitwiseAndRelease(3); int exactBitwiseOr = (int) handle.getAndBitwiseOr(1); int exactBitwiseOrA = (int) handle.getAndBitwiseOrAcquire(2); int exactBitwiseOrR = (int) handle.getAndBitwiseOrRelease(3); int wrongArgBitwiseOr = (int) handle.getAndBitwiseOr("a"); int wrongArgBitwiseOrA = (int) handle.getAndBitwiseOrAcquire("b"); int wrongArgBitwiseOrR = (int) handle.getAndBitwiseOrRelease("c"); String wrongResultBitwiseOr = (String) handle.getAndBitwiseOr(1); String wrongResultBitwiseOrA = (String) handle.getAndBitwiseOrAcquire(2); String wrongResultBitwiseOrR = (String) handle.getAndBitwiseOrRelease(3); int exactBitwiseXor = (int) handle.getAndBitwiseXor(1); int exactBitwiseXorA = (int) handle.getAndBitwiseXorAcquire(2); int exactBitwiseXorR = (int) handle.getAndBitwiseXorRelease(3); int wrongArgBitwiseXor = (int) handle.getAndBitwiseXor("a"); int wrongArgBitwiseXorA = (int) handle.getAndBitwiseXorAcquire("b"); int wrongArgBitwiseXorR = (int) handle.getAndBitwiseXorRelease("c"); String wrongResultBitwiseXor = (String) handle.getAndBitwiseXor(1); String wrongResultBitwiseXorA = (String) handle.getAndBitwiseXorAcquire(2); String wrongResultBitwiseXorR = (String) handle.getAndBitwiseXorRelease(3); } void compare() throws Throwable { MethodHandles.Lookup lookup = MethodHandles.lookup(); final VarHandle handle = lookup.findStaticVarHandle(Test.class, "s", String.class); final Test instance = new Test(); boolean exactCAS = handle.compareAndSet("a", "b"); String exactCAE = (String)handle.compareAndExchange("a", "b"); String exactCAEA = (String)handle.compareAndExchangeAcquire("a", "b"); String exactCAER = (String)handle.compareAndExchangeRelease("a", "b"); boolean badArg1CAS = handle.compareAndSet(1, "b"); String badArg1CAE = (String)handle.compareAndExchange(2, "b"); String badArg1CAEa = (String)handle.compareAndExchangeAcquire(3, "b"); String badArg1CAEr = (String)handle.compareAndExchangeRelease(4, "b"); boolean badArg2CAS = handle.compareAndSet("a", 1); String badArg2CAE = (String)handle.compareAndExchange("a", 2); String badArg2CAEa = (String)handle.compareAndExchangeAcquire("a", 3); String badArg2CAEr = (String)handle.compareAndExchangeRelease("a", 4); Integer badResultCAE = (Integer)handle.compareAndExchange("a", "b"); Integer badResultCAEa = (Integer)handle.compareAndExchangeAcquire("a", "b"); Integer badResultCAEr = (Integer)handle.compareAndExchangeRelease("a", "b"); } void weakCompare() throws Throwable { MethodHandles.Lookup lookup = MethodHandles.lookup(); final VarHandle handle = lookup.findStaticVarHandle(Test.class, "s", String.class); final Test instance = new Test(); boolean exactCAS = handle.weakCompareAndSet("a", "b"); boolean exactCASp = handle.weakCompareAndSetPlain("a", "b"); boolean exactCASa = handle.weakCompareAndSetAcquire("a", "b"); boolean exactCASr = handle.weakCompareAndSetRelease("a", "b"); boolean badArg1CAS = handle.weakCompareAndSet(1, "b"); boolean badArg1CASp = handle.weakCompareAndSetPlain(2, "b"); boolean badArg1CASa = handle.weakCompareAndSetAcquire(3, "b"); boolean badArg1CASr = handle.weakCompareAndSetRelease(4, "b"); boolean badArg2CAS = handle.weakCompareAndSet("a", 1); boolean badArg2CASp = handle.weakCompareAndSetPlain("a", 2); boolean badArg2CASa = handle.weakCompareAndSetAcquire("a", 3); boolean badArg2CASr = handle.weakCompareAndSetRelease("a", 4); } private static CharSequence charSequence() { return "abc"; } } class Test { public static int n; public static String s; }