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