Merge pull request #9405 from alimate/BAEL-4128
BAEL-4128: Adapting the VarHandle Code to Best Practices
This commit is contained in:
commit
d5c8ebe760
|
@ -5,7 +5,7 @@ import org.junit.Test;
|
|||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.VarHandle;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class VariableHandlesUnitTest {
|
||||
|
||||
|
@ -18,25 +18,23 @@ public class VariableHandlesUnitTest {
|
|||
|
||||
@Test
|
||||
public void whenVariableHandleForPublicVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException {
|
||||
VarHandle publicIntHandle = MethodHandles
|
||||
VarHandle PUBLIC_TEST_VARIABLE = MethodHandles
|
||||
.lookup()
|
||||
.in(VariableHandlesUnitTest.class)
|
||||
.findVarHandle(VariableHandlesUnitTest.class, "publicTestVariable", int.class);
|
||||
|
||||
assertThat(publicIntHandle.coordinateTypes().size() == 1);
|
||||
assertThat(publicIntHandle.coordinateTypes().get(0) == VariableHandlesUnitTest.class);
|
||||
|
||||
assertEquals(1, PUBLIC_TEST_VARIABLE.coordinateTypes().size());
|
||||
assertEquals(VariableHandlesUnitTest.class, PUBLIC_TEST_VARIABLE.coordinateTypes().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenVariableHandleForPrivateVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException {
|
||||
VarHandle privateIntHandle = MethodHandles
|
||||
VarHandle PRIVATE_TEST_VARIABLE = MethodHandles
|
||||
.privateLookupIn(VariableHandlesUnitTest.class, MethodHandles.lookup())
|
||||
.findVarHandle(VariableHandlesUnitTest.class, "privateTestVariable", int.class);
|
||||
|
||||
assertThat(privateIntHandle.coordinateTypes().size() == 1);
|
||||
assertThat(privateIntHandle.coordinateTypes().get(0) == VariableHandlesUnitTest.class);
|
||||
|
||||
assertEquals(1, PRIVATE_TEST_VARIABLE.coordinateTypes().size());
|
||||
assertEquals(VariableHandlesUnitTest.class, PRIVATE_TEST_VARIABLE.coordinateTypes().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -44,63 +42,64 @@ public class VariableHandlesUnitTest {
|
|||
VarHandle arrayVarHandle = MethodHandles
|
||||
.arrayElementVarHandle(int[].class);
|
||||
|
||||
assertThat(arrayVarHandle.coordinateTypes().size() == 2);
|
||||
assertThat(arrayVarHandle.coordinateTypes().get(0) == int[].class);
|
||||
assertEquals(2, arrayVarHandle.coordinateTypes().size());
|
||||
assertEquals(int[].class, arrayVarHandle.coordinateTypes().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVarHandle_whenGetIsInvoked_ThenValueOfVariableIsReturned() throws NoSuchFieldException, IllegalAccessException {
|
||||
VarHandle publicIntHandle = MethodHandles
|
||||
VarHandle PUBLIC_TEST_VARIABLE = MethodHandles
|
||||
.lookup()
|
||||
.in(VariableHandlesUnitTest.class)
|
||||
.findVarHandle(VariableHandlesUnitTest.class, "publicTestVariable", int.class);
|
||||
|
||||
assertThat((int) publicIntHandle.get(this) == 1);
|
||||
assertEquals(1, (int) PUBLIC_TEST_VARIABLE.get(this));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVarHandle_whenSetIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException {
|
||||
VarHandle publicIntHandle = MethodHandles
|
||||
VarHandle VARIABLE_TO_SET = MethodHandles
|
||||
.lookup()
|
||||
.in(VariableHandlesUnitTest.class)
|
||||
.findVarHandle(VariableHandlesUnitTest.class, "variableToSet", int.class);
|
||||
publicIntHandle.set(this, 15);
|
||||
|
||||
assertThat((int) publicIntHandle.get(this) == 15);
|
||||
VARIABLE_TO_SET.set(this, 15);
|
||||
assertEquals(15, (int) VARIABLE_TO_SET.get(this));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVarHandle_whenCompareAndSetIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException {
|
||||
VarHandle publicIntHandle = MethodHandles
|
||||
VarHandle VARIABLE_TO_COMPARE_AND_SET = MethodHandles
|
||||
.lookup()
|
||||
.in(VariableHandlesUnitTest.class)
|
||||
.findVarHandle(VariableHandlesUnitTest.class, "variableToCompareAndSet", int.class);
|
||||
publicIntHandle.compareAndSet(this, 1, 100);
|
||||
|
||||
assertThat((int) publicIntHandle.get(this) == 100);
|
||||
VARIABLE_TO_COMPARE_AND_SET.compareAndSet(this, 1, 100);
|
||||
assertEquals(100, (int) VARIABLE_TO_COMPARE_AND_SET.get(this));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVarHandle_whenGetAndAddIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException {
|
||||
VarHandle publicIntHandle = MethodHandles
|
||||
VarHandle VARIABLE_TO_GET_AND_ADD = MethodHandles
|
||||
.lookup()
|
||||
.in(VariableHandlesUnitTest.class)
|
||||
.findVarHandle(VariableHandlesUnitTest.class, "variableToGetAndAdd", int.class);
|
||||
int before = (int) publicIntHandle.getAndAdd(this, 200);
|
||||
|
||||
assertThat(before == 0);
|
||||
assertThat((int) publicIntHandle.get(this) == 200);
|
||||
int before = (int) VARIABLE_TO_GET_AND_ADD.getAndAdd(this, 200);
|
||||
|
||||
assertEquals(0, before);
|
||||
assertEquals(200, (int) VARIABLE_TO_GET_AND_ADD.get(this));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVarHandle_whenGetAndBitwiseOrIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException {
|
||||
VarHandle publicIntHandle = MethodHandles
|
||||
VarHandle VARIABLE_TO_BITWISE_OR = MethodHandles
|
||||
.lookup()
|
||||
.in(VariableHandlesUnitTest.class)
|
||||
.findVarHandle(VariableHandlesUnitTest.class, "variableToBitwiseOr", byte.class);
|
||||
byte before = (byte) publicIntHandle.getAndBitwiseOr(this, (byte) 127);
|
||||
byte before = (byte) VARIABLE_TO_BITWISE_OR.getAndBitwiseOr(this, (byte) 127);
|
||||
|
||||
assertThat(before == 0);
|
||||
assertThat(variableToBitwiseOr == 127);
|
||||
assertEquals(0, before);
|
||||
assertEquals(127, (byte) VARIABLE_TO_BITWISE_OR.get(this));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue