|
| 1 | +package com.baeldung.java9.varhandles; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import java.lang.invoke.MethodHandles; |
| 7 | +import java.lang.invoke.VarHandle; |
| 8 | + |
| 9 | +public class VariableHandlesTest { |
| 10 | + public int publicTestVariable = 1; |
| 11 | + private int privateTestVariable = 1; |
| 12 | + public int variableToSet = 1; |
| 13 | + public int variableToCompareAndSet = 1; |
| 14 | + public int variableToGetAndAdd = 0; |
| 15 | + public byte variableToBitwiseOr = 0; |
| 16 | + @Test |
| 17 | + public void whenVariableHandleForPublicVariableIsCreated_ThenItIsInitializedProperly() |
| 18 | + throws NoSuchFieldException, IllegalAccessException { |
| 19 | + VarHandle publicIntHandle = MethodHandles.lookup() |
| 20 | + .in(VariableHandlesTest.class) |
| 21 | + .findVarHandle(VariableHandlesTest.class,"publicTestVariable", int.class); |
| 22 | + Assert.assertEquals(1, publicIntHandle.coordinateTypes().size()); |
| 23 | + Assert.assertEquals(VariableHandlesTest.class, publicIntHandle.coordinateTypes().get(0)); |
| 24 | + |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void whenVariableHandleForPrivateVariableIsCreated_ThenItIsInitializedProperly() |
| 29 | + throws NoSuchFieldException, IllegalAccessException { |
| 30 | + VarHandle privateIntHandle = MethodHandles.privateLookupIn(VariableHandlesTest.class, MethodHandles.lookup()) |
| 31 | + .findVarHandle(VariableHandlesTest.class,"privateTestVariable", int.class); |
| 32 | + Assert.assertNotNull(privateIntHandle); |
| 33 | + Assert.assertEquals(1, privateIntHandle.coordinateTypes().size()); |
| 34 | + Assert.assertEquals(VariableHandlesTest.class, privateIntHandle.coordinateTypes().get(0)); |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void whenVariableHandleForArrayVariableIsCreated_ThenItIsInitializedProperly() |
| 40 | + throws NoSuchFieldException, IllegalAccessException { |
| 41 | + VarHandle arrayVarHandle = MethodHandles.arrayElementVarHandle(int[].class); |
| 42 | + Assert.assertEquals(2, arrayVarHandle.coordinateTypes().size()); |
| 43 | + Assert.assertEquals(int[].class, arrayVarHandle.coordinateTypes().get(0)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void givenVarHandle_whenGetIsInvoked_ThenValueOfVariableIsReturned() throws NoSuchFieldException, IllegalAccessException { |
| 48 | + VarHandle publicIntHandle = MethodHandles.lookup() |
| 49 | + .in(VariableHandlesTest.class) |
| 50 | + .findVarHandle(VariableHandlesTest.class,"publicTestVariable", int.class); |
| 51 | + Assert.assertEquals(1, publicIntHandle.get(this)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void givenVarHandle_whenSetIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException { |
| 56 | + VarHandle publicIntHandle = MethodHandles.lookup() |
| 57 | + .in(VariableHandlesTest.class) |
| 58 | + .findVarHandle(VariableHandlesTest.class,"variableToSet", int.class); |
| 59 | + publicIntHandle.set(this, 15); |
| 60 | + Assert.assertEquals(15, publicIntHandle.get(this)); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void givenVarHandle_whenCompareAndSetIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException { |
| 65 | + VarHandle publicIntHandle = MethodHandles.lookup() |
| 66 | + .in(VariableHandlesTest.class) |
| 67 | + .findVarHandle(VariableHandlesTest.class,"variableToCompareAndSet", int.class); |
| 68 | + publicIntHandle.compareAndSet(this, 1,100); |
| 69 | + Assert.assertEquals(100, publicIntHandle.get(this)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void givenVarHandle_whenGetAndAddIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException { |
| 74 | + VarHandle publicIntHandle = MethodHandles.lookup() |
| 75 | + .in(VariableHandlesTest.class) |
| 76 | + .findVarHandle(VariableHandlesTest.class,"variableToGetAndAdd", int.class); |
| 77 | + int before = (int) publicIntHandle.getAndAdd(this, 200); |
| 78 | + Assert.assertEquals(0, before); |
| 79 | + Assert.assertEquals(200, publicIntHandle.get(this)); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void givenVarHandle_whenGetAndBitwiseOrIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException { |
| 84 | + VarHandle publicIntHandle = MethodHandles.lookup() |
| 85 | + .in(VariableHandlesTest.class) |
| 86 | + .findVarHandle(VariableHandlesTest.class,"variableToBitwiseOr", byte.class); |
| 87 | + byte before = (byte) publicIntHandle.getAndBitwiseOr(this, (byte) 127); |
| 88 | + Assert.assertEquals(0, before); |
| 89 | + Assert.assertEquals(127, variableToBitwiseOr); |
| 90 | + } |
| 91 | +} |
0 commit comments