From ec95d9b208cae1d187b8428bbbe94f8982a3c504 Mon Sep 17 00:00:00 2001 From: Dominik Date: Sat, 16 Dec 2017 19:37:43 +0100 Subject: [PATCH] BAEL1367: VarHandles Article --- .../java9/varhandles/VariableHandlesTest.java | 101 ++++++++++-------- 1 file changed, 58 insertions(+), 43 deletions(-) diff --git a/core-java-9/src/test/java/com/baeldung/java9/varhandles/VariableHandlesTest.java b/core-java-9/src/test/java/com/baeldung/java9/varhandles/VariableHandlesTest.java index 210c4f0654..50766502ec 100644 --- a/core-java-9/src/test/java/com/baeldung/java9/varhandles/VariableHandlesTest.java +++ b/core-java-9/src/test/java/com/baeldung/java9/varhandles/VariableHandlesTest.java @@ -1,91 +1,106 @@ package com.baeldung.java9.varhandles; -import org.junit.Assert; import org.junit.Test; import java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; +import static org.assertj.core.api.Assertions.assertThat; + public class VariableHandlesTest { + public int publicTestVariable = 1; private int privateTestVariable = 1; public int variableToSet = 1; public int variableToCompareAndSet = 1; public int variableToGetAndAdd = 0; public byte variableToBitwiseOr = 0; + @Test - public void whenVariableHandleForPublicVariableIsCreated_ThenItIsInitializedProperly() - throws NoSuchFieldException, IllegalAccessException { - VarHandle publicIntHandle = MethodHandles.lookup() - .in(VariableHandlesTest.class) - .findVarHandle(VariableHandlesTest.class,"publicTestVariable", int.class); - Assert.assertEquals(1, publicIntHandle.coordinateTypes().size()); - Assert.assertEquals(VariableHandlesTest.class, publicIntHandle.coordinateTypes().get(0)); + public void whenVariableHandleForPublicVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException { + VarHandle publicIntHandle = MethodHandles + .lookup() + .in(VariableHandlesTest.class) + .findVarHandle(VariableHandlesTest.class, "publicTestVariable", int.class); + + assertThat(publicIntHandle.coordinateTypes().size() == 1); + assertThat(publicIntHandle.coordinateTypes().get(0) == VariableHandles.class); } @Test - public void whenVariableHandleForPrivateVariableIsCreated_ThenItIsInitializedProperly() - throws NoSuchFieldException, IllegalAccessException { - VarHandle privateIntHandle = MethodHandles.privateLookupIn(VariableHandlesTest.class, MethodHandles.lookup()) - .findVarHandle(VariableHandlesTest.class,"privateTestVariable", int.class); - Assert.assertNotNull(privateIntHandle); - Assert.assertEquals(1, privateIntHandle.coordinateTypes().size()); - Assert.assertEquals(VariableHandlesTest.class, privateIntHandle.coordinateTypes().get(0)); + public void whenVariableHandleForPrivateVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException { + VarHandle privateIntHandle = MethodHandles + .privateLookupIn(VariableHandlesTest.class, MethodHandles.lookup()) + .findVarHandle(VariableHandlesTest.class, "privateTestVariable", int.class); + + assertThat(privateIntHandle.coordinateTypes().size() == 1); + assertThat(privateIntHandle.coordinateTypes().get(0) == VariableHandlesTest.class); } @Test - public void whenVariableHandleForArrayVariableIsCreated_ThenItIsInitializedProperly() - throws NoSuchFieldException, IllegalAccessException { - VarHandle arrayVarHandle = MethodHandles.arrayElementVarHandle(int[].class); - Assert.assertEquals(2, arrayVarHandle.coordinateTypes().size()); - Assert.assertEquals(int[].class, arrayVarHandle.coordinateTypes().get(0)); + public void whenVariableHandleForArrayVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException { + VarHandle arrayVarHandle = MethodHandles + .arrayElementVarHandle(int[].class); + + assertThat(arrayVarHandle.coordinateTypes().size() == 2); + assertThat(arrayVarHandle.coordinateTypes().get(0) == int[].class); } @Test public void givenVarHandle_whenGetIsInvoked_ThenValueOfVariableIsReturned() throws NoSuchFieldException, IllegalAccessException { - VarHandle publicIntHandle = MethodHandles.lookup() - .in(VariableHandlesTest.class) - .findVarHandle(VariableHandlesTest.class,"publicTestVariable", int.class); - Assert.assertEquals(1, publicIntHandle.get(this)); + VarHandle publicIntHandle = MethodHandles + .lookup() + .in(VariableHandlesTest.class) + .findVarHandle(VariableHandlesTest.class, "publicTestVariable", int.class); + + assertThat((int) publicIntHandle.get(this) == 1); } @Test public void givenVarHandle_whenSetIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException { - VarHandle publicIntHandle = MethodHandles.lookup() - .in(VariableHandlesTest.class) - .findVarHandle(VariableHandlesTest.class,"variableToSet", int.class); + VarHandle publicIntHandle = MethodHandles + .lookup() + .in(VariableHandlesTest.class) + .findVarHandle(VariableHandlesTest.class, "variableToSet", int.class); publicIntHandle.set(this, 15); - Assert.assertEquals(15, publicIntHandle.get(this)); + + assertThat((int) publicIntHandle.get(this) == 15); } @Test public void givenVarHandle_whenCompareAndSetIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException { - VarHandle publicIntHandle = MethodHandles.lookup() - .in(VariableHandlesTest.class) - .findVarHandle(VariableHandlesTest.class,"variableToCompareAndSet", int.class); - publicIntHandle.compareAndSet(this, 1,100); - Assert.assertEquals(100, publicIntHandle.get(this)); + VarHandle publicIntHandle = MethodHandles + .lookup() + .in(VariableHandlesTest.class) + .findVarHandle(VariableHandlesTest.class, "variableToCompareAndSet", int.class); + publicIntHandle.compareAndSet(this, 1, 100); + + assertThat((int) publicIntHandle.get(this) == 100); } @Test public void givenVarHandle_whenGetAndAddIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException { - VarHandle publicIntHandle = MethodHandles.lookup() - .in(VariableHandlesTest.class) - .findVarHandle(VariableHandlesTest.class,"variableToGetAndAdd", int.class); + VarHandle publicIntHandle = MethodHandles + .lookup() + .in(VariableHandlesTest.class) + .findVarHandle(VariableHandlesTest.class, "variableToGetAndAdd", int.class); int before = (int) publicIntHandle.getAndAdd(this, 200); - Assert.assertEquals(0, before); - Assert.assertEquals(200, publicIntHandle.get(this)); + + assertThat(before == 0); + assertThat((int) publicIntHandle.get(this) == 200); } @Test public void givenVarHandle_whenGetAndBitwiseOrIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException { - VarHandle publicIntHandle = MethodHandles.lookup() - .in(VariableHandlesTest.class) - .findVarHandle(VariableHandlesTest.class,"variableToBitwiseOr", byte.class); + VarHandle publicIntHandle = MethodHandles + .lookup() + .in(VariableHandlesTest.class) + .findVarHandle(VariableHandlesTest.class, "variableToBitwiseOr", byte.class); byte before = (byte) publicIntHandle.getAndBitwiseOr(this, (byte) 127); - Assert.assertEquals(0, before); - Assert.assertEquals(127, variableToBitwiseOr); + + assertThat(before == 0); + assertThat(variableToBitwiseOr == 127); } }