From 32ac6eef9b0371d1f78d581276318329f46b6b90 Mon Sep 17 00:00:00 2001
From: 515882294 <515882294@qq.com>
Date: Thu, 30 Sep 2021 23:19:04 +0800
Subject: [PATCH 1/7] Convert Byte Array to its Numeric Representation
---
.../core-java-arrays-convert/pom.xml | 5 +
.../ByteArrayToNumericRepresentation.java | 281 +++++++++++++
...eArrayToNumericRepresentationUnitTest.java | 391 ++++++++++++++++++
3 files changed, 677 insertions(+)
create mode 100644 core-java-modules/core-java-arrays-convert/src/main/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentation.java
create mode 100644 core-java-modules/core-java-arrays-convert/src/test/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentationUnitTest.java
diff --git a/core-java-modules/core-java-arrays-convert/pom.xml b/core-java-modules/core-java-arrays-convert/pom.xml
index 4cb2946ac9..6e001e12b0 100644
--- a/core-java-modules/core-java-arrays-convert/pom.xml
+++ b/core-java-modules/core-java-arrays-convert/pom.xml
@@ -19,6 +19,11 @@
guava
${guava.version}
+
+ org.apache.commons
+ commons-lang3
+ ${commons-lang3.version}
+
diff --git a/core-java-modules/core-java-arrays-convert/src/main/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentation.java b/core-java-modules/core-java-arrays-convert/src/main/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentation.java
new file mode 100644
index 0000000000..82d60e1666
--- /dev/null
+++ b/core-java-modules/core-java-arrays-convert/src/main/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentation.java
@@ -0,0 +1,281 @@
+package com.baeldung.array.conversions;
+
+import com.google.common.primitives.Ints;
+import com.google.common.primitives.Longs;
+import org.apache.commons.lang3.ArrayUtils;
+import org.apache.commons.lang3.Conversion;
+
+import java.math.BigInteger;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+class ByteArrayToNumericRepresentation {
+
+ static int convertByteArrayToIntUsingShiftOperator(byte[] bytes) {
+ int value = 0;
+ for (byte b : bytes) {
+ value = (value << 8) + (b & 0xFF);
+ }
+ return value;
+ }
+
+ static byte[] convertIntToByteArrayUsingShiftOperator(int value) {
+ byte[] bytes = new byte[Integer.BYTES];
+ int length = bytes.length;
+ for (int i = 0; i < length; i++) {
+ bytes[length - i - 1] = (byte) (value & 0xFF);
+ value >>= 8;
+ }
+ return bytes;
+ }
+
+ static long convertByteArrayToLongUsingShiftOperator(byte[] bytes) {
+ long value = 0;
+ for (byte b : bytes) {
+ value <<= 8;
+ value |= (b & 0xFF);
+ }
+ return value;
+ }
+
+ static byte[] convertLongToByteArrayUsingShiftOperator(long value) {
+ byte[] bytes = new byte[Long.BYTES];
+ int length = bytes.length;
+ for (int i = 0; i < length; i++) {
+ bytes[length - i - 1] = (byte) (value & 0xFF);
+ value >>= 8;
+ }
+ return bytes;
+ }
+
+ static float convertByteArrayToFloatUsingShiftOperator(byte[] bytes) {
+ // convert bytes to int
+ int intValue = 0;
+ for (byte b : bytes) {
+ intValue = (intValue << 8) + (b & 0xFF);
+ }
+
+ // convert int to float
+ return Float.intBitsToFloat(intValue);
+ }
+
+ static byte[] convertFloatToByteArrayUsingShiftOperator(float value) {
+ // convert float to int
+ int intValue = Float.floatToIntBits(value);
+
+ // convert int to bytes
+ byte[] bytes = new byte[Float.BYTES];
+ int length = bytes.length;
+ for (int i = 0; i < length; i++) {
+ bytes[length - i - 1] = (byte) (intValue & 0xFF);
+ intValue >>= 8;
+ }
+ return bytes;
+ }
+
+ static double convertingByteArrayToDoubleUsingShiftOperator(byte[] bytes) {
+ long longValue = 0;
+ for (byte b : bytes) {
+ longValue = (longValue << 8) + (b & 0xFF);
+ }
+
+ return Double.longBitsToDouble(longValue);
+ }
+
+ static byte[] convertDoubleToByteArrayUsingShiftOperator(double value) {
+ long longValue = Double.doubleToLongBits(value);
+
+ byte[] bytes = new byte[Double.BYTES];
+ int length = bytes.length;
+ for (int i = 0; i < length; i++) {
+ bytes[length - i - 1] = (byte) (longValue & 0xFF);
+ longValue >>= 8;
+ }
+ return bytes;
+ }
+
+ static int convertByteArrayToIntUsingByteBuffer(byte[] bytes) {
+ ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
+ buffer.put(bytes);
+ buffer.rewind();
+ return buffer.getInt();
+ }
+
+ static byte[] convertIntToByteArrayUsingByteBuffer(int value) {
+ ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
+ buffer.putInt(value);
+ buffer.rewind();
+ return buffer.array();
+ }
+
+ static long convertByteArrayToLongUsingByteBuffer(byte[] bytes) {
+ ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
+ buffer.put(bytes);
+ buffer.rewind();
+ return buffer.getLong();
+ }
+
+ static byte[] convertLongToByteArrayUsingByteBuffer(long value) {
+ ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
+ buffer.putLong(value);
+ buffer.rewind();
+ return buffer.array();
+ }
+
+ static float convertByteArrayToFloatUsingByteBuffer(byte[] bytes) {
+ ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES);
+ buffer.put(bytes);
+ buffer.rewind();
+ return buffer.getFloat();
+ }
+
+ static byte[] convertFloatToByteArrayUsingByteBuffer(float value) {
+ ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES);
+ buffer.putFloat(value);
+ buffer.rewind();
+ return buffer.array();
+ }
+
+ static double convertByteArrayToDoubleUsingByteBuffer(byte[] bytes) {
+ ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES);
+ buffer.put(bytes);
+ buffer.rewind();
+ return buffer.getDouble();
+ }
+
+ static byte[] convertDoubleToByteArrayUsingByteBuffer(double value) {
+ ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES);
+ buffer.putDouble(value);
+ buffer.rewind();
+ return buffer.array();
+ }
+
+ static int convertByteArrayToIntUsingBigInteger(byte[] bytes) {
+ return new BigInteger(bytes).intValue();
+ }
+
+ static byte[] convertIntToByteArrayUsingBigInteger(int value) {
+ return BigInteger.valueOf(value).toByteArray();
+ }
+
+ static long convertByteArrayToLongUsingBigInteger(byte[] bytes) {
+ return new BigInteger(bytes).longValue();
+ }
+
+ static byte[] convertLongToByteArrayUsingBigInteger(long value) {
+ return BigInteger.valueOf(value).toByteArray();
+ }
+
+ static float convertByteArrayToFloatUsingBigInteger(byte[] bytes) {
+ int intValue = new BigInteger(bytes).intValue();
+ return Float.intBitsToFloat(intValue);
+ }
+
+ static byte[] convertFloatToByteArrayUsingBigInteger(float value) {
+ int intValue = Float.floatToIntBits(value);
+ return BigInteger.valueOf(intValue).toByteArray();
+ }
+
+ static double convertByteArrayToDoubleUsingBigInteger(byte[] bytes) {
+ long longValue = new BigInteger(bytes).longValue();
+ return Double.longBitsToDouble(longValue);
+ }
+
+ static byte[] convertDoubleToByteArrayUsingBigInteger(double value) {
+ long longValue = Double.doubleToLongBits(value);
+ return BigInteger.valueOf(longValue).toByteArray();
+ }
+
+ static int convertingByteArrayToIntUsingGuava(byte[] bytes) {
+ return Ints.fromByteArray(bytes);
+ }
+
+ static byte[] convertIntToByteArrayUsingGuava(int value) {
+ return Ints.toByteArray(value);
+ }
+
+ static long convertByteArrayToLongUsingGuava(byte[] bytes) {
+ return Longs.fromByteArray(bytes);
+ }
+
+ static byte[] convertLongToByteArrayUsingGuava(long value) {
+ return Longs.toByteArray(value);
+ }
+
+ static float convertByteArrayToFloatUsingGuava(byte[] bytes) {
+ int intValue = Ints.fromByteArray(bytes);
+ return Float.intBitsToFloat(intValue);
+ }
+
+ static byte[] convertFloatToByteArrayUsingGuava(float value) {
+ int intValue = Float.floatToIntBits(value);
+ return Ints.toByteArray(intValue);
+ }
+
+ static double convertByteArrayToDoubleUsingGuava(byte[] bytes) {
+ long longValue = Longs.fromByteArray(bytes);
+ return Double.longBitsToDouble(longValue);
+ }
+
+ static byte[] convertDoubleToByteArrayUsingGuava(double value) {
+ long longValue = Double.doubleToLongBits(value);
+ return Longs.toByteArray(longValue);
+ }
+
+ static int convertByteArrayToIntUsingCommonsLang(byte[] bytes) {
+ byte[] copyBytes = Arrays.copyOf(bytes, bytes.length);
+ ArrayUtils.reverse(copyBytes);
+ return Conversion.byteArrayToInt(copyBytes, 0, 0, 0, copyBytes.length);
+ }
+
+ static byte[] convertIntToByteArrayUsingCommonsLang(int value) {
+ byte[] bytes = new byte[Integer.BYTES];
+ Conversion.intToByteArray(value, 0, bytes, 0, bytes.length);
+ ArrayUtils.reverse(bytes);
+ return bytes;
+ }
+
+ static long convertByteArrayToLongUsingCommonsLang(byte[] bytes) {
+ byte[] copyBytes = Arrays.copyOf(bytes, bytes.length);
+ ArrayUtils.reverse(copyBytes);
+ return Conversion.byteArrayToLong(copyBytes, 0, 0, 0, copyBytes.length);
+ }
+
+ static byte[] convertLongToByteArrayUsingCommonsLang(long value) {
+ byte[] bytes = new byte[Long.BYTES];
+ Conversion.longToByteArray(value, 0, bytes, 0, bytes.length);
+ ArrayUtils.reverse(bytes);
+ return bytes;
+ }
+
+ static float convertByteArrayToFloatUsingCommonsLang(byte[] bytes) {
+ byte[] copyBytes = Arrays.copyOf(bytes, bytes.length);
+ ArrayUtils.reverse(copyBytes);
+ int intValue = Conversion.byteArrayToInt(copyBytes, 0, 0, 0, copyBytes.length);
+ return Float.intBitsToFloat(intValue);
+ }
+
+ static byte[] convertFloatToByteArrayUsingCommonsLang(float value) {
+ int intValue = Float.floatToIntBits(value);
+ byte[] bytes = new byte[Float.BYTES];
+ Conversion.intToByteArray(intValue, 0, bytes, 0, bytes.length);
+ ArrayUtils.reverse(bytes);
+ return bytes;
+ }
+
+ static double convertByteArrayToDoubleUsingCommonsLang(byte[] bytes) {
+ byte[] copyBytes = Arrays.copyOf(bytes, bytes.length);
+ ArrayUtils.reverse(copyBytes);
+ long longValue = Conversion.byteArrayToLong(copyBytes, 0, 0, 0, copyBytes.length);
+ return Double.longBitsToDouble(longValue);
+ }
+
+ static byte[] convertDoubleToByteArrayUsingCommonsLang(double value) {
+ long longValue = Double.doubleToLongBits(value);
+ byte[] bytes = new byte[Long.BYTES];
+ Conversion.longToByteArray(longValue, 0, bytes, 0, bytes.length);
+ ArrayUtils.reverse(bytes);
+ return bytes;
+ }
+
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-arrays-convert/src/test/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentationUnitTest.java b/core-java-modules/core-java-arrays-convert/src/test/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentationUnitTest.java
new file mode 100644
index 0000000000..666b5a6be8
--- /dev/null
+++ b/core-java-modules/core-java-arrays-convert/src/test/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentationUnitTest.java
@@ -0,0 +1,391 @@
+package com.baeldung.array.conversions;
+
+import org.junit.Test;
+
+import java.util.Arrays;
+
+import static com.baeldung.array.conversions.ByteArrayToNumericRepresentation.*;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+public class ByteArrayToNumericRepresentationUnitTest {
+ private static final byte[] INT_BYTE_ARRAY = new byte[]{
+ (byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE
+ };
+ private static final int INT_VALUE = 0xCAFEBABE;
+
+
+ private static final byte[] FLOAT_BYTE_ARRAY = new byte[]{
+ (byte) 0x40, (byte) 0x48, (byte) 0xF5, (byte) 0xC3
+ };
+ private static final float FLOAT_VALUE = 3.14F;
+
+
+ private static final byte[] LONG_BYTE_ARRAY = new byte[]{
+ (byte) 0x01, (byte) 0x23, (byte) 0x45, (byte) 0x67,
+ (byte) 0x89, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF
+ };
+ private static final long LONG_VALUE = 0x0123456789ABCDEFL;
+
+
+ private static final byte[] DOUBLE_BYTE_ARRAY = new byte[]{
+ (byte) 0x3F, (byte) 0xE3, (byte) 0xC6, (byte) 0xA7,
+ (byte) 0xEF, (byte) 0x9D, (byte) 0xB2, (byte) 0x2D
+ };
+ private static final double DOUBLE_VALUE = 0.618D;
+
+
+ private int getIntValue() {
+ return INT_VALUE;
+ }
+
+ private byte[] getIntByteArray() {
+ return Arrays.copyOf(INT_BYTE_ARRAY, INT_BYTE_ARRAY.length);
+ }
+
+ private long getLongValue() {
+ return LONG_VALUE;
+ }
+
+ private byte[] getLongByteArray() {
+ return Arrays.copyOf(LONG_BYTE_ARRAY, LONG_BYTE_ARRAY.length);
+ }
+
+ private float getFloatValue() {
+ return FLOAT_VALUE;
+ }
+
+ private byte[] getFloatByteArray() {
+ return Arrays.copyOf(FLOAT_BYTE_ARRAY, FLOAT_BYTE_ARRAY.length);
+ }
+
+ private double getDoubleValue() {
+ return DOUBLE_VALUE;
+ }
+
+ private byte[] getDoubleByteArray() {
+ return Arrays.copyOf(DOUBLE_BYTE_ARRAY, DOUBLE_BYTE_ARRAY.length);
+ }
+
+
+ @Test
+ public void givenShiftOperator_whenConvertingByteArrayToInt_thenSuccess() {
+ byte[] bytes = getIntByteArray();
+ int value = convertByteArrayToIntUsingShiftOperator(bytes);
+
+ assertEquals(INT_VALUE, value);
+ }
+
+ @Test
+ public void givenShiftOperator_whenConvertingIntToByteArray_thenSuccess() {
+ int value = getIntValue();
+ byte[] bytes = convertIntToByteArrayUsingShiftOperator(value);
+
+ assertArrayEquals(INT_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenShiftOperator_whenConvertingByteArrayToLong_thenSuccess() {
+ byte[] bytes = getLongByteArray();
+ long value = convertByteArrayToLongUsingShiftOperator(bytes);
+
+ assertEquals(LONG_VALUE, value);
+ }
+
+ @Test
+ public void givenShiftOperator_whenConvertingLongToByteArray_thenSuccess() {
+ long value = getLongValue();
+ byte[] bytes = convertLongToByteArrayUsingShiftOperator(value);
+
+ assertArrayEquals(LONG_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenShiftOperator_whenConvertingByteArrayToFloat_thenSuccess() {
+ byte[] bytes = getFloatByteArray();
+ float value = convertByteArrayToFloatUsingShiftOperator(bytes);
+
+ assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
+ }
+
+ @Test
+ public void givenShiftOperator_whenConvertingFloatToByteArray_thenSuccess() {
+ float value = getFloatValue();
+ byte[] bytes = convertFloatToByteArrayUsingShiftOperator(value);
+
+ assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenShiftOperator_whenConvertingByteArrayToDouble_thenSuccess() {
+ byte[] bytes = getDoubleByteArray();
+ double value = convertingByteArrayToDoubleUsingShiftOperator(bytes);
+
+ assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
+ }
+
+ @Test
+ public void givenShiftOperator_whenConvertingDoubleToByteArray_thenSuccess() {
+ double value = getDoubleValue();
+ byte[] bytes = convertDoubleToByteArrayUsingShiftOperator(value);
+
+ assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenByteBuffer_whenConvertingByteArrayToInt_thenSuccess() {
+ byte[] bytes = getIntByteArray();
+ int value = convertByteArrayToIntUsingByteBuffer(bytes);
+
+ assertEquals(INT_VALUE, value);
+ }
+
+ @Test
+ public void givenByteBuffer_whenConvertingIntToByteArray_thenSuccess() {
+ int value = getIntValue();
+ byte[] bytes = convertIntToByteArrayUsingByteBuffer(value);
+
+ assertArrayEquals(INT_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenByteBuffer_whenConvertingByteArrayToLong_thenSuccess() {
+ byte[] bytes = getLongByteArray();
+ long value = convertByteArrayToLongUsingByteBuffer(bytes);
+
+ assertEquals(LONG_VALUE, value);
+ }
+
+ @Test
+ public void givenByteBuffer_whenConvertingLongToByteArray_thenSuccess() {
+ long value = getLongValue();
+ byte[] bytes = convertLongToByteArrayUsingByteBuffer(value);
+
+ assertArrayEquals(LONG_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenByteBuffer_whenConvertingByteArrayToFloat_thenSuccess() {
+ byte[] bytes = getFloatByteArray();
+ float value = convertByteArrayToFloatUsingByteBuffer(bytes);
+
+ assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
+ }
+
+ @Test
+ public void givenByteBuffer_whenConvertingFloatToByteArray_thenSuccess() {
+ float value = getFloatValue();
+ byte[] bytes = convertFloatToByteArrayUsingByteBuffer(value);
+
+ assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenByteBuffer_whenConvertingByteArrayToDouble_thenSuccess() {
+ byte[] bytes = getDoubleByteArray();
+ double value = convertByteArrayToDoubleUsingByteBuffer(bytes);
+
+ assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
+ }
+
+ @Test
+ public void givenByteBuffer_whenConvertingDoubleToByteArray_thenSuccess() {
+ double value = getDoubleValue();
+ byte[] bytes = convertDoubleToByteArrayUsingByteBuffer(value);
+
+ assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenBigInteger_whenConvertingByteArrayToInt_thenSuccess() {
+ byte[] bytes = getIntByteArray();
+ int value = convertByteArrayToIntUsingBigInteger(bytes);
+
+ assertEquals(INT_VALUE, value);
+ }
+
+ @Test
+ public void givenBigInteger_whenConvertingIntToByteArray_thenSuccess() {
+ int value = getIntValue();
+ byte[] bytes = convertIntToByteArrayUsingBigInteger(value);
+
+ assertArrayEquals(INT_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenBigInteger_whenConvertingByteArrayToLong_thenSuccess() {
+ byte[] bytes = getLongByteArray();
+ long value = convertByteArrayToLongUsingBigInteger(bytes);
+
+ assertEquals(LONG_VALUE, value);
+ }
+
+ @Test
+ public void givenBigInteger_whenConvertingLongToByteArray_thenSuccess() {
+ long value = getLongValue();
+ byte[] bytes = convertLongToByteArrayUsingBigInteger(value);
+
+ assertArrayEquals(LONG_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenBigInteger_whenConvertingByteArrayToFloat_thenSuccess() {
+ byte[] bytes = getFloatByteArray();
+ float value = convertByteArrayToFloatUsingBigInteger(bytes);
+
+ assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
+ }
+
+ @Test
+ public void givenBigInteger_whenConvertingFloatToByteArray_thenSuccess() {
+ float value = getFloatValue();
+ byte[] bytes = convertFloatToByteArrayUsingBigInteger(value);
+
+ assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenBigInteger_whenConvertingByteArrayToDouble_thenSuccess() {
+ byte[] bytes = getDoubleByteArray();
+ double value = convertByteArrayToDoubleUsingBigInteger(bytes);
+
+ assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
+ }
+
+ @Test
+ public void givenBigInteger_whenConvertingDoubleToByteArray_thenSuccess() {
+ double value = getDoubleValue();
+ byte[] bytes = convertDoubleToByteArrayUsingBigInteger(value);
+
+ assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenGuava_whenConvertingByteArrayToInt_thenSuccess() {
+ byte[] bytes = getIntByteArray();
+ int value = convertingByteArrayToIntUsingGuava(bytes);
+
+ assertEquals(INT_VALUE, value);
+ }
+
+ @Test
+ public void givenGuava_whenConvertingIntToByteArray_thenSuccess() {
+ int value = getIntValue();
+ byte[] bytes = convertIntToByteArrayUsingGuava(value);
+
+ assertArrayEquals(INT_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenGuava_whenConvertingByteArrayToLong_thenSuccess() {
+ byte[] bytes = getLongByteArray();
+ long value = convertByteArrayToLongUsingGuava(bytes);
+
+ assertEquals(LONG_VALUE, value);
+ }
+
+ @Test
+ public void givenGuava_whenConvertingLongToByteArray_thenSuccess() {
+ long value = getLongValue();
+ byte[] bytes = convertLongToByteArrayUsingGuava(value);
+
+ assertArrayEquals(LONG_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenGuava_whenConvertingByteArrayToFloat_thenSuccess() {
+ byte[] bytes = getFloatByteArray();
+ float value = convertByteArrayToFloatUsingGuava(bytes);
+
+ assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
+ }
+
+ @Test
+ public void givenGuava_whenConvertingFloatToByteArray_thenSuccess() {
+ float value = getFloatValue();
+ byte[] bytes = convertFloatToByteArrayUsingGuava(value);
+
+ assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenGuava_whenConvertingByteArrayToDouble_thenSuccess() {
+ byte[] bytes = getDoubleByteArray();
+ double value = convertByteArrayToDoubleUsingGuava(bytes);
+
+ assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
+ }
+
+ @Test
+ public void givenGuava_whenConvertingDoubleToByteArray_thenSuccess() {
+ double value = getDoubleValue();
+ byte[] bytes = convertDoubleToByteArrayUsingGuava(value);
+
+ assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenCommonsLang_whenConvertingByteArrayToInt_thenSuccess() {
+ byte[] bytes = getIntByteArray();
+ int value = convertByteArrayToIntUsingCommonsLang(bytes);
+
+ assertEquals(INT_VALUE, value);
+ }
+
+ @Test
+ public void givenCommonsLang_whenConvertingIntToByteArray_thenSuccess() {
+ int value = getIntValue();
+ byte[] bytes = convertIntToByteArrayUsingCommonsLang(value);
+
+ assertArrayEquals(INT_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenCommonsLang_whenConvertingByteArrayToLong_thenSuccess() {
+ byte[] bytes = getLongByteArray();
+ long value = convertByteArrayToLongUsingCommonsLang(bytes);
+
+ assertEquals(LONG_VALUE, value);
+ }
+
+ @Test
+ public void givenCommonsLang_whenConvertingLongToByteArray_thenSuccess() {
+ long value = getLongValue();
+ byte[] bytes = convertLongToByteArrayUsingCommonsLang(value);
+
+ assertArrayEquals(LONG_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenCommonsLang_whenConvertingByteArrayToFloat_thenSuccess() {
+ byte[] bytes = getFloatByteArray();
+ float value = convertByteArrayToFloatUsingCommonsLang(bytes);
+
+ assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
+ }
+
+ @Test
+ public void givenCommonsLang_whenConvertingFloatToByteArray_thenSuccess() {
+ float value = getFloatValue();
+ byte[] bytes = convertFloatToByteArrayUsingCommonsLang(value);
+
+ assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
+ }
+
+ @Test
+ public void givenCommonsLang_whenConvertingByteArrayToDouble_thenSuccess() {
+ byte[] bytes = getDoubleByteArray();
+ double value = convertByteArrayToDoubleUsingCommonsLang(bytes);
+
+ assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
+ }
+
+ @Test
+ public void givenCommonsLang_whenConvertingDoubleToByteArray_thenSuccess() {
+ double value = getDoubleValue();
+ byte[] bytes = convertDoubleToByteArrayUsingCommonsLang(value);
+
+ assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
+ }
+
+}
\ No newline at end of file
From 98f31e92d2e8fbeb110db157221af7bf1eccfd3a Mon Sep 17 00:00:00 2001
From: 515882294 <515882294@qq.com>
Date: Thu, 7 Oct 2021 14:43:13 +0800
Subject: [PATCH 2/7] Remove Redundant Getter Method
---
...eArrayToNumericRepresentationUnitTest.java | 155 +++++-------------
1 file changed, 40 insertions(+), 115 deletions(-)
diff --git a/core-java-modules/core-java-arrays-convert/src/test/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentationUnitTest.java b/core-java-modules/core-java-arrays-convert/src/test/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentationUnitTest.java
index 666b5a6be8..0fae765d9c 100644
--- a/core-java-modules/core-java-arrays-convert/src/test/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentationUnitTest.java
+++ b/core-java-modules/core-java-arrays-convert/src/test/java/com/baeldung/array/conversions/ByteArrayToNumericRepresentationUnitTest.java
@@ -2,8 +2,6 @@ package com.baeldung.array.conversions;
import org.junit.Test;
-import java.util.Arrays;
-
import static com.baeldung.array.conversions.ByteArrayToNumericRepresentation.*;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
@@ -35,355 +33,282 @@ public class ByteArrayToNumericRepresentationUnitTest {
private static final double DOUBLE_VALUE = 0.618D;
- private int getIntValue() {
- return INT_VALUE;
- }
-
- private byte[] getIntByteArray() {
- return Arrays.copyOf(INT_BYTE_ARRAY, INT_BYTE_ARRAY.length);
- }
-
- private long getLongValue() {
- return LONG_VALUE;
- }
-
- private byte[] getLongByteArray() {
- return Arrays.copyOf(LONG_BYTE_ARRAY, LONG_BYTE_ARRAY.length);
- }
-
- private float getFloatValue() {
- return FLOAT_VALUE;
- }
-
- private byte[] getFloatByteArray() {
- return Arrays.copyOf(FLOAT_BYTE_ARRAY, FLOAT_BYTE_ARRAY.length);
- }
-
- private double getDoubleValue() {
- return DOUBLE_VALUE;
- }
-
- private byte[] getDoubleByteArray() {
- return Arrays.copyOf(DOUBLE_BYTE_ARRAY, DOUBLE_BYTE_ARRAY.length);
- }
-
-
@Test
public void givenShiftOperator_whenConvertingByteArrayToInt_thenSuccess() {
- byte[] bytes = getIntByteArray();
- int value = convertByteArrayToIntUsingShiftOperator(bytes);
+ int value = convertByteArrayToIntUsingShiftOperator(INT_BYTE_ARRAY);
assertEquals(INT_VALUE, value);
}
@Test
public void givenShiftOperator_whenConvertingIntToByteArray_thenSuccess() {
- int value = getIntValue();
- byte[] bytes = convertIntToByteArrayUsingShiftOperator(value);
+ byte[] bytes = convertIntToByteArrayUsingShiftOperator(INT_VALUE);
assertArrayEquals(INT_BYTE_ARRAY, bytes);
}
@Test
public void givenShiftOperator_whenConvertingByteArrayToLong_thenSuccess() {
- byte[] bytes = getLongByteArray();
- long value = convertByteArrayToLongUsingShiftOperator(bytes);
+ long value = convertByteArrayToLongUsingShiftOperator(LONG_BYTE_ARRAY);
assertEquals(LONG_VALUE, value);
}
@Test
public void givenShiftOperator_whenConvertingLongToByteArray_thenSuccess() {
- long value = getLongValue();
- byte[] bytes = convertLongToByteArrayUsingShiftOperator(value);
+ byte[] bytes = convertLongToByteArrayUsingShiftOperator(LONG_VALUE);
assertArrayEquals(LONG_BYTE_ARRAY, bytes);
}
@Test
public void givenShiftOperator_whenConvertingByteArrayToFloat_thenSuccess() {
- byte[] bytes = getFloatByteArray();
- float value = convertByteArrayToFloatUsingShiftOperator(bytes);
+ float value = convertByteArrayToFloatUsingShiftOperator(FLOAT_BYTE_ARRAY);
assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
}
@Test
public void givenShiftOperator_whenConvertingFloatToByteArray_thenSuccess() {
- float value = getFloatValue();
- byte[] bytes = convertFloatToByteArrayUsingShiftOperator(value);
+ byte[] bytes = convertFloatToByteArrayUsingShiftOperator(FLOAT_VALUE);
assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
}
@Test
public void givenShiftOperator_whenConvertingByteArrayToDouble_thenSuccess() {
- byte[] bytes = getDoubleByteArray();
- double value = convertingByteArrayToDoubleUsingShiftOperator(bytes);
+ double value = convertingByteArrayToDoubleUsingShiftOperator(DOUBLE_BYTE_ARRAY);
assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
}
@Test
public void givenShiftOperator_whenConvertingDoubleToByteArray_thenSuccess() {
- double value = getDoubleValue();
- byte[] bytes = convertDoubleToByteArrayUsingShiftOperator(value);
+ byte[] bytes = convertDoubleToByteArrayUsingShiftOperator(DOUBLE_VALUE);
assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
}
@Test
public void givenByteBuffer_whenConvertingByteArrayToInt_thenSuccess() {
- byte[] bytes = getIntByteArray();
- int value = convertByteArrayToIntUsingByteBuffer(bytes);
+ int value = convertByteArrayToIntUsingByteBuffer(INT_BYTE_ARRAY);
assertEquals(INT_VALUE, value);
}
@Test
public void givenByteBuffer_whenConvertingIntToByteArray_thenSuccess() {
- int value = getIntValue();
- byte[] bytes = convertIntToByteArrayUsingByteBuffer(value);
+ byte[] bytes = convertIntToByteArrayUsingByteBuffer(INT_VALUE);
assertArrayEquals(INT_BYTE_ARRAY, bytes);
}
@Test
public void givenByteBuffer_whenConvertingByteArrayToLong_thenSuccess() {
- byte[] bytes = getLongByteArray();
- long value = convertByteArrayToLongUsingByteBuffer(bytes);
+ long value = convertByteArrayToLongUsingByteBuffer(LONG_BYTE_ARRAY);
assertEquals(LONG_VALUE, value);
}
@Test
public void givenByteBuffer_whenConvertingLongToByteArray_thenSuccess() {
- long value = getLongValue();
- byte[] bytes = convertLongToByteArrayUsingByteBuffer(value);
+ byte[] bytes = convertLongToByteArrayUsingByteBuffer(LONG_VALUE);
assertArrayEquals(LONG_BYTE_ARRAY, bytes);
}
@Test
public void givenByteBuffer_whenConvertingByteArrayToFloat_thenSuccess() {
- byte[] bytes = getFloatByteArray();
- float value = convertByteArrayToFloatUsingByteBuffer(bytes);
+ float value = convertByteArrayToFloatUsingByteBuffer(FLOAT_BYTE_ARRAY);
assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
}
@Test
public void givenByteBuffer_whenConvertingFloatToByteArray_thenSuccess() {
- float value = getFloatValue();
- byte[] bytes = convertFloatToByteArrayUsingByteBuffer(value);
+ byte[] bytes = convertFloatToByteArrayUsingByteBuffer(FLOAT_VALUE);
assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
}
@Test
public void givenByteBuffer_whenConvertingByteArrayToDouble_thenSuccess() {
- byte[] bytes = getDoubleByteArray();
- double value = convertByteArrayToDoubleUsingByteBuffer(bytes);
+ double value = convertByteArrayToDoubleUsingByteBuffer(DOUBLE_BYTE_ARRAY);
assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
}
@Test
public void givenByteBuffer_whenConvertingDoubleToByteArray_thenSuccess() {
- double value = getDoubleValue();
- byte[] bytes = convertDoubleToByteArrayUsingByteBuffer(value);
+ byte[] bytes = convertDoubleToByteArrayUsingByteBuffer(DOUBLE_VALUE);
assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
}
@Test
public void givenBigInteger_whenConvertingByteArrayToInt_thenSuccess() {
- byte[] bytes = getIntByteArray();
- int value = convertByteArrayToIntUsingBigInteger(bytes);
+ int value = convertByteArrayToIntUsingBigInteger(INT_BYTE_ARRAY);
assertEquals(INT_VALUE, value);
}
@Test
public void givenBigInteger_whenConvertingIntToByteArray_thenSuccess() {
- int value = getIntValue();
- byte[] bytes = convertIntToByteArrayUsingBigInteger(value);
+ byte[] bytes = convertIntToByteArrayUsingBigInteger(INT_VALUE);
assertArrayEquals(INT_BYTE_ARRAY, bytes);
}
@Test
public void givenBigInteger_whenConvertingByteArrayToLong_thenSuccess() {
- byte[] bytes = getLongByteArray();
- long value = convertByteArrayToLongUsingBigInteger(bytes);
+ long value = convertByteArrayToLongUsingBigInteger(LONG_BYTE_ARRAY);
assertEquals(LONG_VALUE, value);
}
@Test
public void givenBigInteger_whenConvertingLongToByteArray_thenSuccess() {
- long value = getLongValue();
- byte[] bytes = convertLongToByteArrayUsingBigInteger(value);
+ byte[] bytes = convertLongToByteArrayUsingBigInteger(LONG_VALUE);
assertArrayEquals(LONG_BYTE_ARRAY, bytes);
}
@Test
public void givenBigInteger_whenConvertingByteArrayToFloat_thenSuccess() {
- byte[] bytes = getFloatByteArray();
- float value = convertByteArrayToFloatUsingBigInteger(bytes);
+ float value = convertByteArrayToFloatUsingBigInteger(FLOAT_BYTE_ARRAY);
assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
}
@Test
public void givenBigInteger_whenConvertingFloatToByteArray_thenSuccess() {
- float value = getFloatValue();
- byte[] bytes = convertFloatToByteArrayUsingBigInteger(value);
+ byte[] bytes = convertFloatToByteArrayUsingBigInteger(FLOAT_VALUE);
assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
}
@Test
public void givenBigInteger_whenConvertingByteArrayToDouble_thenSuccess() {
- byte[] bytes = getDoubleByteArray();
- double value = convertByteArrayToDoubleUsingBigInteger(bytes);
+ double value = convertByteArrayToDoubleUsingBigInteger(DOUBLE_BYTE_ARRAY);
assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
}
@Test
public void givenBigInteger_whenConvertingDoubleToByteArray_thenSuccess() {
- double value = getDoubleValue();
- byte[] bytes = convertDoubleToByteArrayUsingBigInteger(value);
+ byte[] bytes = convertDoubleToByteArrayUsingBigInteger(DOUBLE_VALUE);
assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
}
@Test
public void givenGuava_whenConvertingByteArrayToInt_thenSuccess() {
- byte[] bytes = getIntByteArray();
- int value = convertingByteArrayToIntUsingGuava(bytes);
+ int value = convertingByteArrayToIntUsingGuava(INT_BYTE_ARRAY);
assertEquals(INT_VALUE, value);
}
@Test
public void givenGuava_whenConvertingIntToByteArray_thenSuccess() {
- int value = getIntValue();
- byte[] bytes = convertIntToByteArrayUsingGuava(value);
+ byte[] bytes = convertIntToByteArrayUsingGuava(INT_VALUE);
assertArrayEquals(INT_BYTE_ARRAY, bytes);
}
@Test
public void givenGuava_whenConvertingByteArrayToLong_thenSuccess() {
- byte[] bytes = getLongByteArray();
- long value = convertByteArrayToLongUsingGuava(bytes);
+ long value = convertByteArrayToLongUsingGuava(LONG_BYTE_ARRAY);
assertEquals(LONG_VALUE, value);
}
@Test
public void givenGuava_whenConvertingLongToByteArray_thenSuccess() {
- long value = getLongValue();
- byte[] bytes = convertLongToByteArrayUsingGuava(value);
+ byte[] bytes = convertLongToByteArrayUsingGuava(LONG_VALUE);
assertArrayEquals(LONG_BYTE_ARRAY, bytes);
}
@Test
public void givenGuava_whenConvertingByteArrayToFloat_thenSuccess() {
- byte[] bytes = getFloatByteArray();
- float value = convertByteArrayToFloatUsingGuava(bytes);
+ float value = convertByteArrayToFloatUsingGuava(FLOAT_BYTE_ARRAY);
assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
}
@Test
public void givenGuava_whenConvertingFloatToByteArray_thenSuccess() {
- float value = getFloatValue();
- byte[] bytes = convertFloatToByteArrayUsingGuava(value);
+ byte[] bytes = convertFloatToByteArrayUsingGuava(FLOAT_VALUE);
assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
}
@Test
public void givenGuava_whenConvertingByteArrayToDouble_thenSuccess() {
- byte[] bytes = getDoubleByteArray();
- double value = convertByteArrayToDoubleUsingGuava(bytes);
+ double value = convertByteArrayToDoubleUsingGuava(DOUBLE_BYTE_ARRAY);
assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
}
@Test
public void givenGuava_whenConvertingDoubleToByteArray_thenSuccess() {
- double value = getDoubleValue();
- byte[] bytes = convertDoubleToByteArrayUsingGuava(value);
+ byte[] bytes = convertDoubleToByteArrayUsingGuava(DOUBLE_VALUE);
assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
}
@Test
public void givenCommonsLang_whenConvertingByteArrayToInt_thenSuccess() {
- byte[] bytes = getIntByteArray();
- int value = convertByteArrayToIntUsingCommonsLang(bytes);
+ int value = convertByteArrayToIntUsingCommonsLang(INT_BYTE_ARRAY);
assertEquals(INT_VALUE, value);
}
@Test
public void givenCommonsLang_whenConvertingIntToByteArray_thenSuccess() {
- int value = getIntValue();
- byte[] bytes = convertIntToByteArrayUsingCommonsLang(value);
+ byte[] bytes = convertIntToByteArrayUsingCommonsLang(INT_VALUE);
assertArrayEquals(INT_BYTE_ARRAY, bytes);
}
@Test
public void givenCommonsLang_whenConvertingByteArrayToLong_thenSuccess() {
- byte[] bytes = getLongByteArray();
- long value = convertByteArrayToLongUsingCommonsLang(bytes);
+ long value = convertByteArrayToLongUsingCommonsLang(LONG_BYTE_ARRAY);
assertEquals(LONG_VALUE, value);
}
@Test
public void givenCommonsLang_whenConvertingLongToByteArray_thenSuccess() {
- long value = getLongValue();
- byte[] bytes = convertLongToByteArrayUsingCommonsLang(value);
+ byte[] bytes = convertLongToByteArrayUsingCommonsLang(LONG_VALUE);
assertArrayEquals(LONG_BYTE_ARRAY, bytes);
}
@Test
public void givenCommonsLang_whenConvertingByteArrayToFloat_thenSuccess() {
- byte[] bytes = getFloatByteArray();
- float value = convertByteArrayToFloatUsingCommonsLang(bytes);
+ float value = convertByteArrayToFloatUsingCommonsLang(FLOAT_BYTE_ARRAY);
assertEquals(Float.floatToIntBits(FLOAT_VALUE), Float.floatToIntBits(value));
}
@Test
public void givenCommonsLang_whenConvertingFloatToByteArray_thenSuccess() {
- float value = getFloatValue();
- byte[] bytes = convertFloatToByteArrayUsingCommonsLang(value);
+ byte[] bytes = convertFloatToByteArrayUsingCommonsLang(FLOAT_VALUE);
assertArrayEquals(FLOAT_BYTE_ARRAY, bytes);
}
@Test
public void givenCommonsLang_whenConvertingByteArrayToDouble_thenSuccess() {
- byte[] bytes = getDoubleByteArray();
- double value = convertByteArrayToDoubleUsingCommonsLang(bytes);
+ double value = convertByteArrayToDoubleUsingCommonsLang(DOUBLE_BYTE_ARRAY);
assertEquals(Double.doubleToLongBits(DOUBLE_VALUE), Double.doubleToLongBits(value));
}
@Test
public void givenCommonsLang_whenConvertingDoubleToByteArray_thenSuccess() {
- double value = getDoubleValue();
- byte[] bytes = convertDoubleToByteArrayUsingCommonsLang(value);
+ byte[] bytes = convertDoubleToByteArrayUsingCommonsLang(DOUBLE_VALUE);
assertArrayEquals(DOUBLE_BYTE_ARRAY, bytes);
}
From 615f407cdfe5fd764ad9a16325da2911ccd60369 Mon Sep 17 00:00:00 2001
From: 515882294 <515882294@qq.com>
Date: Tue, 2 Nov 2021 16:09:03 +0800
Subject: [PATCH 3/7] BAEL-4286 How to get the value of a bit at a certain
position from a byte
---
.../core-java-lang-operators-2/pom.xml | 5 +
.../GetABitFromIntegralValueUnitTest.java | 119 ++++++++++++++++++
2 files changed, 124 insertions(+)
create mode 100644 core-java-modules/core-java-lang-operators-2/src/test/java/com/baeldung/oroperators/GetABitFromIntegralValueUnitTest.java
diff --git a/core-java-modules/core-java-lang-operators-2/pom.xml b/core-java-modules/core-java-lang-operators-2/pom.xml
index 1779b7384c..2b81a797e0 100644
--- a/core-java-modules/core-java-lang-operators-2/pom.xml
+++ b/core-java-modules/core-java-lang-operators-2/pom.xml
@@ -15,6 +15,11 @@
+
+ org.apache.commons
+ commons-lang3
+ ${commons-lang3.version}
+
org.projectlombok
lombok
diff --git a/core-java-modules/core-java-lang-operators-2/src/test/java/com/baeldung/oroperators/GetABitFromIntegralValueUnitTest.java b/core-java-modules/core-java-lang-operators-2/src/test/java/com/baeldung/oroperators/GetABitFromIntegralValueUnitTest.java
new file mode 100644
index 0000000000..d39e3b3c47
--- /dev/null
+++ b/core-java-modules/core-java-lang-operators-2/src/test/java/com/baeldung/oroperators/GetABitFromIntegralValueUnitTest.java
@@ -0,0 +1,119 @@
+package com.baeldung.oroperators;
+
+import org.apache.commons.lang3.BitField;
+import org.junit.Test;
+
+import java.math.BigInteger;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class GetABitFromIntegralValueUnitTest {
+ @Test
+ public void givenAByte_whenUsingAHardCodedMask_thenGetBitValue() {
+ byte val1 = 0b0110_0100;
+ byte val2 = 0b0110_0010;
+ byte mask = 0b0000_0100;
+ boolean isSet1 = (val1 & mask) > 0;
+ boolean isSet2 = (val2 & mask) > 0;
+
+ assertTrue(isSet1);
+ assertFalse(isSet2);
+ }
+
+ @Test
+ public void givenAnIntValue_whenUsingACalculatedMask_thenGetBitValue() {
+ int val = 0b0110_0100;
+ int pos = 2;
+ int mask = 1 << pos;
+ boolean isSet = (val & mask) > 0;
+
+ assertTrue(isSet);
+ }
+
+ @Test
+ public void givenAnIntValue_whenUsingMultiPos_thenGetSameResult() {
+ int val = 0b0110_0100;
+ for (int i = 6; i < Integer.SIZE; i++) {
+ int pos = 1 << i | 2;
+ int mask = 1 << pos;
+ boolean isSet = (val & mask) > 0;
+
+ assertTrue(isSet);
+ }
+ }
+
+ @Test
+ public void givenALongValue_whenUsingACalculatedMask_thenGetBitValue() {
+ long val = 0b0110_0100;
+ int pos = 2;
+ long mask = 1L << pos;
+ boolean isSet = (val & mask) > 0;
+
+ assertTrue(isSet);
+ }
+
+ @Test
+ public void givenAnIntValue_whenUsingALeftShiftedValue_thenGetBitValue() {
+ int val = 0b0110_0100;
+ int pos = 2;
+ boolean isSet = ((val << ~pos) < 0);
+
+ assertTrue(isSet);
+ }
+
+ @Test
+ public void givenALongValue_whenUsingALeftShiftedValue_thenGetBitValue() {
+ long val = 0b0110_0100;
+ int pos = 2;
+ boolean isSet = ((val << ~pos) < 0);
+
+ assertTrue(isSet);
+ }
+
+ @Test
+ public void givenAnIntValue_whenUsingARightShiftedValue_thenGetBitValue() {
+ int val = 0b0110_0100;
+ int pos = 2;
+ boolean isSet = ((val >> pos) & 1) == 1;
+
+ assertTrue(isSet);
+ }
+
+ @Test
+ public void givenALongValue_whenUsingARightShiftedValue_thenGetBitValue() {
+ long val = 0b0110_0100;
+ int pos = 2;
+ boolean isSet = ((val >> pos) & 1) == 1;
+
+ assertTrue(isSet);
+ }
+
+ @Test
+ public void givenAnIntValue_whenUsingBigInteger_thenGetBitValue() {
+ int val = 0b0110_0100;
+ int pos = 2;
+ boolean isSet = BigInteger.valueOf(val).testBit(pos);
+
+ assertTrue(isSet);
+ }
+
+ @Test
+ public void givenALongValue_whenUsingBigInteger_thenGetBitValue() {
+ long val = 0b0110_0100;
+ int pos = 2;
+ boolean isSet = BigInteger.valueOf(val).testBit(pos);
+
+ assertTrue(isSet);
+ }
+
+ @Test
+ public void givenAnIntValue_whenUsingCommonsLang_thenGetBitValue() {
+ int val = 0b0110_0100;
+ int mask = 0b0000_1100;
+ BitField bitField = new BitField(mask);
+ boolean isSet = bitField.isSet(val);
+
+ assertTrue(isSet);
+ }
+}
From 40fb1e587255c421345ea2d336730a94b5c05de1 Mon Sep 17 00:00:00 2001
From: 515882294 <515882294@qq.com>
Date: Wed, 17 Nov 2021 19:18:53 +0800
Subject: [PATCH 4/7] BAEL-4286(update): remove redundant test methods
---
.../GetABitFromIntegralValueUnitTest.java | 55 +++----------------
1 file changed, 7 insertions(+), 48 deletions(-)
rename core-java-modules/core-java-lang-operators-2/src/test/java/com/baeldung/{oroperators => getbit}/GetABitFromIntegralValueUnitTest.java (51%)
diff --git a/core-java-modules/core-java-lang-operators-2/src/test/java/com/baeldung/oroperators/GetABitFromIntegralValueUnitTest.java b/core-java-modules/core-java-lang-operators-2/src/test/java/com/baeldung/getbit/GetABitFromIntegralValueUnitTest.java
similarity index 51%
rename from core-java-modules/core-java-lang-operators-2/src/test/java/com/baeldung/oroperators/GetABitFromIntegralValueUnitTest.java
rename to core-java-modules/core-java-lang-operators-2/src/test/java/com/baeldung/getbit/GetABitFromIntegralValueUnitTest.java
index d39e3b3c47..61f9885a91 100644
--- a/core-java-modules/core-java-lang-operators-2/src/test/java/com/baeldung/oroperators/GetABitFromIntegralValueUnitTest.java
+++ b/core-java-modules/core-java-lang-operators-2/src/test/java/com/baeldung/getbit/GetABitFromIntegralValueUnitTest.java
@@ -1,6 +1,5 @@
-package com.baeldung.oroperators;
+package com.baeldung.getbit;
-import org.apache.commons.lang3.BitField;
import org.junit.Test;
import java.math.BigInteger;
@@ -32,39 +31,26 @@ public class GetABitFromIntegralValueUnitTest {
}
@Test
- public void givenAnIntValue_whenUsingMultiPos_thenGetSameResult() {
+ public void givenAnIntValue_whenUsingALeftShiftedValue1_thenGetBitValue() {
int val = 0b0110_0100;
- for (int i = 6; i < Integer.SIZE; i++) {
- int pos = 1 << i | 2;
- int mask = 1 << pos;
- boolean isSet = (val & mask) > 0;
-
- assertTrue(isSet);
- }
- }
-
- @Test
- public void givenALongValue_whenUsingACalculatedMask_thenGetBitValue() {
- long val = 0b0110_0100;
int pos = 2;
- long mask = 1L << pos;
- boolean isSet = (val & mask) > 0;
+ boolean isSet = ((val << (31 - pos)) < 0);
assertTrue(isSet);
}
@Test
- public void givenAnIntValue_whenUsingALeftShiftedValue_thenGetBitValue() {
+ public void givenAnIntValue_whenUsingALeftShiftedValue2_thenGetBitValue() {
int val = 0b0110_0100;
int pos = 2;
- boolean isSet = ((val << ~pos) < 0);
+ boolean isSet = ((val << (~pos & 31)) < 0);
assertTrue(isSet);
}
@Test
- public void givenALongValue_whenUsingALeftShiftedValue_thenGetBitValue() {
- long val = 0b0110_0100;
+ public void givenAnIntValue_whenUsingALeftShiftedValue3_thenGetBitValue() {
+ int val = 0b0110_0100;
int pos = 2;
boolean isSet = ((val << ~pos) < 0);
@@ -80,15 +66,6 @@ public class GetABitFromIntegralValueUnitTest {
assertTrue(isSet);
}
- @Test
- public void givenALongValue_whenUsingARightShiftedValue_thenGetBitValue() {
- long val = 0b0110_0100;
- int pos = 2;
- boolean isSet = ((val >> pos) & 1) == 1;
-
- assertTrue(isSet);
- }
-
@Test
public void givenAnIntValue_whenUsingBigInteger_thenGetBitValue() {
int val = 0b0110_0100;
@@ -98,22 +75,4 @@ public class GetABitFromIntegralValueUnitTest {
assertTrue(isSet);
}
- @Test
- public void givenALongValue_whenUsingBigInteger_thenGetBitValue() {
- long val = 0b0110_0100;
- int pos = 2;
- boolean isSet = BigInteger.valueOf(val).testBit(pos);
-
- assertTrue(isSet);
- }
-
- @Test
- public void givenAnIntValue_whenUsingCommonsLang_thenGetBitValue() {
- int val = 0b0110_0100;
- int mask = 0b0000_1100;
- BitField bitField = new BitField(mask);
- boolean isSet = bitField.isSet(val);
-
- assertTrue(isSet);
- }
}
From 8896f641f72589448713c705ec48f730a0af5daf Mon Sep 17 00:00:00 2001
From: 515882294 <515882294@qq.com>
Date: Thu, 24 Feb 2022 11:45:01 +0800
Subject: [PATCH 5/7] BAEL-4151: Guide to ByteBuffer
---
.../bytebuffer/ByteBufferUnitTest.java | 339 ++++++++++++++++++
1 file changed, 339 insertions(+)
create mode 100644 core-java-modules/core-java-nio-2/src/test/java/com/baeldung/bytebuffer/ByteBufferUnitTest.java
diff --git a/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/bytebuffer/ByteBufferUnitTest.java b/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/bytebuffer/ByteBufferUnitTest.java
new file mode 100644
index 0000000000..5d108ba14a
--- /dev/null
+++ b/core-java-modules/core-java-nio-2/src/test/java/com/baeldung/bytebuffer/ByteBufferUnitTest.java
@@ -0,0 +1,339 @@
+package com.baeldung.bytebuffer;
+
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.nio.*;
+import java.nio.charset.StandardCharsets;
+
+import static org.junit.Assert.*;
+
+public class ByteBufferUnitTest {
+ @Test
+ public void givenBufferCreation_whenUsingAllocate_thenSuccess() {
+ ByteBuffer buffer = ByteBuffer.allocate(10);
+ assertNotNull(buffer);
+ }
+
+ @Test
+ public void givenBufferCreation_whenUsingAllocateDirect_thenSuccess() {
+ ByteBuffer buffer = ByteBuffer.allocateDirect(10);
+ assertNotNull(buffer);
+ }
+
+ @Test
+ public void givenBufferCreation_whenUsingWrap_thenSuccess() {
+ byte[] bytes = new byte[10];
+ ByteBuffer buffer = ByteBuffer.wrap(bytes);
+ assertNotNull(buffer);
+ }
+
+ @Test
+ public void givenBufferIndex_whenUsingAllocate_thenInitialIndices() {
+ // create instance using allocate
+ ByteBuffer buffer = ByteBuffer.allocate(10);
+
+ // get index
+ int position = buffer.position();
+ int limit = buffer.limit();
+ int capacity = buffer.capacity();
+
+ // assert
+ assertEquals(0, position);
+ assertEquals(10, limit);
+ assertEquals(10, capacity);
+ }
+
+ @Test
+ public void givenBufferIndex_whenChangingPositionAndLimit_thenSuccess() {
+ // create instance
+ ByteBuffer buffer = ByteBuffer.allocate(10);
+
+ // change index
+ buffer.position(2);
+ buffer.limit(5);
+
+ // assert
+ assertIndex(buffer, -1, 2, 5, 10);
+ }
+
+ @Test
+ public void givenBufferIndex_whenUsingWrap_thenInitialIndices() {
+ // create instance
+ byte[] bytes = new byte[10];
+ ByteBuffer buffer = ByteBuffer.wrap(bytes);
+
+ // assert
+ assertIndex(buffer, -1, 0, 10, 10);
+ }
+
+ @Test
+ public void givenBufferIndex_whenUsingWrapWithOffsetAndLength_thenInitialIndices() {
+ // create instance
+ byte[] bytes = new byte[10];
+ ByteBuffer buffer = ByteBuffer.wrap(bytes, 2, 6);
+
+ // assert
+ assertIndex(buffer, -1, 2, 8, 10);
+ }
+
+ @Test
+ public void givenBufferIndex_whenUsingMarkAndReset_thenOK() {
+ ByteBuffer buffer = ByteBuffer.allocate(10);
+ assertIndex(buffer, -1, 0, 10, 10);
+
+ buffer.position(2);
+ assertIndex(buffer, -1, 2, 10, 10);
+
+ buffer.mark();
+ assertIndex(buffer, 2, 2, 10, 10);
+
+ buffer.position(5);
+ assertIndex(buffer, 2, 5, 10, 10);
+
+ buffer.reset();
+ assertIndex(buffer, 2, 2, 10, 10);
+ }
+
+ @Test
+ public void givenBufferIndex_whenUsingClear_thenOK() {
+ // create instance
+ ByteBuffer buffer = ByteBuffer.allocate(10);
+ assertIndex(buffer, -1, 0, 10, 10);
+
+ // change index
+ buffer.position(2);
+ buffer.mark();
+ buffer.position(5);
+ buffer.limit(8);
+ assertIndex(buffer, 2, 5, 8, 10);
+
+ // clear
+ buffer.clear();
+ assertIndex(buffer, -1, 0, 10, 10);
+ }
+
+ @Test
+ public void givenBufferIndex_whenUsingFlip_thenOK() {
+ // create instance
+ ByteBuffer buffer = ByteBuffer.allocate(10);
+ assertIndex(buffer, -1, 0, 10, 10);
+
+ // change index
+ buffer.position(2);
+ buffer.mark();
+ buffer.position(5);
+ buffer.limit(8);
+ assertIndex(buffer, 2, 5, 8, 10);
+
+ // flip
+ buffer.flip();
+ assertIndex(buffer, -1, 0, 5, 10);
+ }
+
+ @Test
+ public void givenBufferIndex_whenUsingRewind_thenOK() {
+ // create instance
+ ByteBuffer buffer = ByteBuffer.allocate(10);
+ assertIndex(buffer, -1, 0, 10, 10);
+
+ // change index
+ buffer.position(2);
+ buffer.mark();
+ buffer.position(5);
+ buffer.limit(8);
+ assertIndex(buffer, 2, 5, 8, 10);
+
+ // rewind
+ buffer.rewind();
+ assertIndex(buffer, -1, 0, 8, 10);
+ }
+
+ @Test
+ public void givenBufferIndex_whenUsingCompact_thenOK() {
+ // create instance
+ ByteBuffer buffer = ByteBuffer.allocate(10);
+ assertIndex(buffer, -1, 0, 10, 10);
+
+ // change index
+ buffer.position(2);
+ buffer.mark();
+ buffer.position(5);
+ buffer.limit(8);
+ assertIndex(buffer, 2, 5, 8, 10);
+
+ // compact
+ buffer.compact();
+ assertIndex(buffer, -1, 3, 10, 10);
+ }
+
+ @Test
+ public void givenBufferIndex_whenUsingRemain_thenOK() {
+ // create instance
+ ByteBuffer buffer = ByteBuffer.allocate(10);
+
+ // change index
+ buffer.position(2);
+ buffer.limit(8);
+
+ // remain
+ boolean flag = buffer.hasRemaining();
+ int remaining = buffer.remaining();
+
+ // assert
+ assertTrue(flag);
+ assertEquals(6, remaining);
+ }
+
+ @Test(expected = BufferUnderflowException.class)
+ public void givenNotEnoughRemaining_WhenCallingGetInt_thenBufferUnderflowException() {
+ ByteBuffer buffer = ByteBuffer.allocate(2);
+ buffer.getInt();
+ }
+
+ @Test(expected = BufferOverflowException.class)
+ public void givenNotEnoughRemaining_WhenCallingPutInt_thenBufferOverflowException() {
+ ByteBuffer buffer = ByteBuffer.allocate(2);
+ buffer.putInt(10);
+ }
+
+ @Test
+ public void givenBufferView_whenUsingDuplicate_thenOK() {
+ // create instance
+ ByteBuffer buffer = ByteBuffer.allocate(10);
+ assertIndex(buffer, -1, 0, 10, 10);
+
+ // change index
+ buffer.position(2);
+ buffer.mark();
+ buffer.position(5);
+ buffer.limit(8);
+ assertIndex(buffer, 2, 5, 8, 10);
+
+ // view
+ ByteBuffer view = buffer.duplicate();
+ assertIndex(view, 2, 5, 8, 10);
+ }
+
+ @Test
+ public void givenBufferView_whenUsingSlice_thenOK() {
+ // create instance
+ ByteBuffer buffer = ByteBuffer.allocate(10);
+ assertIndex(buffer, -1, 0, 10, 10);
+
+ // change index
+ buffer.position(2);
+ buffer.mark();
+ buffer.position(5);
+ buffer.limit(8);
+ assertIndex(buffer, 2, 5, 8, 10);
+
+ // view
+ ByteBuffer view = buffer.slice();
+ assertIndex(view, -1, 0, 3, 3);
+ }
+
+ @Test
+ public void givenBufferView_whenUsingAsReaOnlyBuffer_thenOK() {
+ // create instance
+ ByteBuffer buffer = ByteBuffer.allocate(10);
+ assertIndex(buffer, -1, 0, 10, 10);
+
+ // change index
+ buffer.position(2);
+ buffer.mark();
+ buffer.position(5);
+ buffer.limit(8);
+ assertIndex(buffer, 2, 5, 8, 10);
+
+ // view
+ ByteBuffer view = buffer.asReadOnlyBuffer();
+ assertIndex(view, 2, 5, 8, 10);
+ }
+
+ @Test
+ public void givenBufferView_whenUsingAsIntBuffer_thenOK() {
+ // create instance
+ byte[] bytes = new byte[]{
+ (byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE, // CAFEBABE ---> cafebabe
+ (byte) 0xF0, (byte) 0x07, (byte) 0xBA, (byte) 0x11, // F007BA11 ---> football
+ (byte) 0x0F, (byte) 0xF1, (byte) 0xCE // 0FF1CE ---> office
+ };
+ ByteBuffer buffer = ByteBuffer.wrap(bytes);
+ assertIndex(buffer, -1, 0, 11, 11);
+
+ // view
+ IntBuffer intBuffer = buffer.asIntBuffer();
+ int capacity = intBuffer.capacity();
+ assertEquals(2, capacity);
+ assertIndex(intBuffer, -1, 0, 2, 2);
+ }
+
+ @Test
+ public void givenByteOrder_whenUsingBigEndian_thenOK() {
+ // create instance
+ byte[] bytes = new byte[]{(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE};
+ ByteBuffer buffer = ByteBuffer.wrap(bytes);
+
+ // change byte order
+ buffer.order(ByteOrder.BIG_ENDIAN);
+ int val = buffer.getInt();
+
+ // assert
+ assertEquals(0xCAFEBABE, val);
+ }
+
+ @Test
+ public void givenByteOrder_whenUsingLittleEndian_thenOK() {
+ // create instance
+ byte[] bytes = new byte[]{(byte) 0xCA, (byte) 0xFE, (byte) 0xBA, (byte) 0xBE};
+ ByteBuffer buffer = ByteBuffer.wrap(bytes);
+
+ // change byte order
+ buffer.order(ByteOrder.LITTLE_ENDIAN);
+ int val = buffer.getInt();
+
+ // assert
+ assertEquals(0xBEBAFECA, val);
+ }
+
+ @Test
+ public void givenComparing_whenUsingEqualsAndCompareTo_thenOK() {
+ // create instance
+ byte[] bytes1 = "World".getBytes(StandardCharsets.UTF_8);
+ byte[] bytes2 = "HelloWorld".getBytes(StandardCharsets.UTF_8);
+ ByteBuffer buffer1 = ByteBuffer.wrap(bytes1);
+ ByteBuffer buffer2 = ByteBuffer.wrap(bytes2);
+
+ // change index
+ buffer2.position(5);
+
+ // equals and compareTo
+ boolean equal = buffer1.equals(buffer2);
+ int result = buffer1.compareTo(buffer2);
+
+ // assert
+ assertTrue(equal);
+ assertEquals(0, result);
+ }
+
+ private void assertIndex(Buffer buffer, int mark, int position, int limit, int capacity) {
+ assertEquals(mark, getMark(buffer));
+ assertEquals(position, buffer.position());
+ assertEquals(limit, buffer.limit());
+ assertEquals(capacity, buffer.capacity());
+ }
+
+ private int getMark(Buffer buffer) {
+ try {
+ Class> clazz = Buffer.class;
+ Field f = clazz.getDeclaredField("mark");
+ f.setAccessible(true);
+ Object result = f.get(buffer);
+ return (int) result;
+ } catch (NoSuchFieldException | IllegalAccessException e) {
+ e.printStackTrace();
+ }
+ return -1;
+ }
+}
From fe6893e9c98e4b371eac6c75f20e848f2ef65da8 Mon Sep 17 00:00:00 2001
From: 515882294 <515882294@qq.com>
Date: Thu, 7 Apr 2022 22:06:01 +0800
Subject: [PATCH 6/7] BAEL-1699 - Java 9 illegal reflective access warning
---
.../A01-compile-and-package-baeldung-agent.sh | 11 +++
...-compile-and-package-baeldung-reflected.sh | 11 +++
...ompile-and-package-baeldung-intermedium.sh | 11 +++
...e-and-package-baeldung-reflecting-named.sh | 11 +++
...and-package-baeldung-reflecting-unnamed.sh | 11 +++
...eldung-reflecting-named-using-add-opens.sh | 4 ++
...ng-reflecting-named-using-forward-opens.sh | 4 ++
...ldung-reflecting-named-using-java-agent.sh | 4 ++
...aeldung-reflecting-unnamed-with-warning.sh | 2 +
...dung-reflecting-unnamed-using-add-opens.sh | 2 +
...ung-reflecting-unnamed-using-java-agent.sh | 2 +
.../C01-clean.sh | 4 ++
.../com/baeldung/agent/LoadTimeAgent.java | 69 +++++++++++++++++++
.../baeldung-agent/manifest.txt | 3 +
.../baeldung-agent/module-info.java | 3 +
.../com/baeldung/intermedium/ForwardOpen.java | 15 ++++
.../baeldung-intermedium/module-info.java | 3 +
.../exported/ExportedNonPublicClass.java | 11 +++
.../exported/ExportedPublicClass.java | 11 +++
.../internal/InternalNonPublicClass.java | 11 +++
.../internal/InternalPublicClass.java | 11 +++
.../opened/OpenedNonPublicClass.java | 11 +++
.../reflected/opened/OpenedPublicClass.java | 11 +++
.../baeldung-reflected/module-info.java | 4 ++
.../com/baeldung/reflecting/named/Main.java | 12 ++++
.../reflecting/named/MainWithForwardOpen.java | 18 +++++
.../module-info.java | 4 ++
.../com/baeldung/reflecting/unnamed/Main.java | 13 ++++
28 files changed, 287 insertions(+)
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A01-compile-and-package-baeldung-agent.sh
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A02-compile-and-package-baeldung-reflected.sh
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A03-compile-and-package-baeldung-intermedium.sh
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A04-compile-and-package-baeldung-reflecting-named.sh
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A05-compile-and-package-baeldung-reflecting-unnamed.sh
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B01-runtime-baeldung-reflecting-named-using-add-opens.sh
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B02-runtime-baeldung-reflecting-named-using-forward-opens.sh
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B03-runtime-baeldung-reflecting-named-using-java-agent.sh
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B04-runtime-baeldung-reflecting-unnamed-with-warning.sh
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B05-runtime-baeldung-reflecting-unnamed-using-add-opens.sh
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B06-runtime-baeldung-reflecting-unnamed-using-java-agent.sh
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/C01-clean.sh
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-agent/com/baeldung/agent/LoadTimeAgent.java
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-agent/manifest.txt
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-agent/module-info.java
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-intermedium/com/baeldung/intermedium/ForwardOpen.java
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-intermedium/module-info.java
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/exported/ExportedNonPublicClass.java
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/exported/ExportedPublicClass.java
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/internal/InternalNonPublicClass.java
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/internal/InternalPublicClass.java
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/opened/OpenedNonPublicClass.java
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/opened/OpenedPublicClass.java
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/module-info.java
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflecting-named/com/baeldung/reflecting/named/Main.java
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflecting-named/com/baeldung/reflecting/named/MainWithForwardOpen.java
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflecting-named/module-info.java
create mode 100644 core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflecting-unnamed/com/baeldung/reflecting/unnamed/Main.java
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A01-compile-and-package-baeldung-agent.sh b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A01-compile-and-package-baeldung-agent.sh
new file mode 100644
index 0000000000..959fa66ec4
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A01-compile-and-package-baeldung-agent.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+DIR=baeldung-agent
+
+# compile
+mkdir -p out/${DIR}
+javac -d out/${DIR} $(find ${DIR} -type f -name "*.java")
+
+# package
+mkdir -p mods
+jar --create --file=mods/${DIR}.jar --manifest=${DIR}/manifest.txt -C out/${DIR} .
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A02-compile-and-package-baeldung-reflected.sh b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A02-compile-and-package-baeldung-reflected.sh
new file mode 100644
index 0000000000..fa922c54df
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A02-compile-and-package-baeldung-reflected.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+DIR=baeldung-reflected
+
+# compile
+mkdir -p out/${DIR}
+javac -d out/${DIR} $(find ${DIR} -type f -name "*.java")
+
+# package
+mkdir -p mods
+jar --create --file=mods/${DIR}.jar -C out/${DIR} .
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A03-compile-and-package-baeldung-intermedium.sh b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A03-compile-and-package-baeldung-intermedium.sh
new file mode 100644
index 0000000000..af76ff5a2e
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A03-compile-and-package-baeldung-intermedium.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+DIR=baeldung-intermedium
+
+# compile
+mkdir -p out/${DIR}
+javac -d out/${DIR} $(find ${DIR} -type f -name "*.java")
+
+# package
+mkdir -p mods
+jar --create --file=mods/${DIR}.jar -C out/${DIR} .
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A04-compile-and-package-baeldung-reflecting-named.sh b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A04-compile-and-package-baeldung-reflecting-named.sh
new file mode 100644
index 0000000000..f3f91c9f04
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A04-compile-and-package-baeldung-reflecting-named.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+DIR=baeldung-reflecting-named
+
+# compile
+mkdir -p out/${DIR}
+javac -d out/${DIR} --module-path mods $(find ${DIR} -type f -name "*.java")
+
+# package
+mkdir -p mods
+jar --create --file=mods/${DIR}.jar -C out/${DIR} .
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A05-compile-and-package-baeldung-reflecting-unnamed.sh b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A05-compile-and-package-baeldung-reflecting-unnamed.sh
new file mode 100644
index 0000000000..deb0a8aaea
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/A05-compile-and-package-baeldung-reflecting-unnamed.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+DIR=baeldung-reflecting-unnamed
+
+# compile
+mkdir -p out/${DIR}
+javac -d out/${DIR} $(find ${DIR} -type f -name "*.java")
+
+# package
+mkdir -p mods
+jar --create --file=mods/${DIR}.jar -C out/${DIR} .
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B01-runtime-baeldung-reflecting-named-using-add-opens.sh b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B01-runtime-baeldung-reflecting-named-using-add-opens.sh
new file mode 100644
index 0000000000..720ad69003
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B01-runtime-baeldung-reflecting-named-using-add-opens.sh
@@ -0,0 +1,4 @@
+#!/bin/bash
+java --module-path mods \
+ --add-opens baeldung.reflected/com.baeldung.reflected.internal=baeldung.reflecting.named \
+ --module baeldung.reflecting.named/com.baeldung.reflecting.named.Main
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B02-runtime-baeldung-reflecting-named-using-forward-opens.sh b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B02-runtime-baeldung-reflecting-named-using-forward-opens.sh
new file mode 100644
index 0000000000..9b5f9cb094
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B02-runtime-baeldung-reflecting-named-using-forward-opens.sh
@@ -0,0 +1,4 @@
+#!/bin/bash
+java --module-path mods \
+ --add-opens baeldung.reflected/com.baeldung.reflected.internal=baeldung.intermedium \
+ --module baeldung.reflecting.named/com.baeldung.reflecting.named.MainWithForwardOpen
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B03-runtime-baeldung-reflecting-named-using-java-agent.sh b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B03-runtime-baeldung-reflecting-named-using-java-agent.sh
new file mode 100644
index 0000000000..cf66dd778f
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B03-runtime-baeldung-reflecting-named-using-java-agent.sh
@@ -0,0 +1,4 @@
+#!/bin/bash
+java --module-path mods \
+ -javaagent:mods/baeldung-agent.jar=com.baeldung.reflected.internal.InternalNonPublicClass,com.baeldung.reflecting.named.Main \
+ --module baeldung.reflecting.named/com.baeldung.reflecting.named.Main
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B04-runtime-baeldung-reflecting-unnamed-with-warning.sh b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B04-runtime-baeldung-reflecting-unnamed-with-warning.sh
new file mode 100644
index 0000000000..525b8dc58c
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B04-runtime-baeldung-reflecting-unnamed-with-warning.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+java -cp "mods/*" com.baeldung.reflecting.unnamed.Main
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B05-runtime-baeldung-reflecting-unnamed-using-add-opens.sh b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B05-runtime-baeldung-reflecting-unnamed-using-add-opens.sh
new file mode 100644
index 0000000000..f7f5f16237
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B05-runtime-baeldung-reflecting-unnamed-using-add-opens.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+java -cp "mods/*" --add-opens java.base/java.lang=ALL-UNNAMED com.baeldung.reflecting.unnamed.Main
\ No newline at end of file
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B06-runtime-baeldung-reflecting-unnamed-using-java-agent.sh b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B06-runtime-baeldung-reflecting-unnamed-using-java-agent.sh
new file mode 100644
index 0000000000..4b30ae1a5c
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/B06-runtime-baeldung-reflecting-unnamed-using-java-agent.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+java -cp "mods/*" -javaagent:mods/baeldung-agent.jar com.baeldung.reflecting.unnamed.Main
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/C01-clean.sh b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/C01-clean.sh
new file mode 100644
index 0000000000..98707f810f
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/C01-clean.sh
@@ -0,0 +1,4 @@
+#!/bin/bash
+
+rm -rf out
+rm -rf mods
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-agent/com/baeldung/agent/LoadTimeAgent.java b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-agent/com/baeldung/agent/LoadTimeAgent.java
new file mode 100644
index 0000000000..a2ec9c3d4a
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-agent/com/baeldung/agent/LoadTimeAgent.java
@@ -0,0 +1,69 @@
+package com.baeldung.agent;
+
+import java.lang.instrument.Instrumentation;
+import java.util.*;
+
+public class LoadTimeAgent {
+ public static void premain(String agentArgs, Instrumentation inst) {
+ System.out.println("agentArgs: " + agentArgs);
+
+ if (agentArgs != null) {
+ addExportsAndOpensByClassName(inst, agentArgs);
+ }
+ else {
+ addExportsAndOpensToUnnamedModule(inst);
+ }
+ }
+
+ private static void addExportsAndOpensByClassName(Instrumentation inst, String agentArgs) {
+ String[] array = agentArgs.split(",");
+ try {
+ String className1 = array[0];
+ String className2 = array[1];
+ Class> clazz1 = Class.forName(className1);
+ Class> clazz2 = Class.forName(className2);
+
+ Module srcModule = clazz1.getModule();
+ Module targetModule = clazz2.getModule();
+ redefineModule(inst, srcModule, targetModule);
+ } catch (ClassNotFoundException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private static void addExportsAndOpensToUnnamedModule(Instrumentation inst) {
+ Module unnamedModule = ClassLoader.getSystemClassLoader().getUnnamedModule();
+ Set modules = ModuleLayer.boot().modules();
+
+ for (Module m : modules) {
+ redefineModule(inst, m, unnamedModule);
+ }
+ }
+
+ private static void redefineModule(Instrumentation inst, Module src, Module target) {
+ // prepare extra reads
+ Set extraReads = Collections.singleton(target);
+
+ // prepare extra exports
+ Set packages = src.getPackages();
+ Map> extraExports = new HashMap<>();
+ for (String pkg : packages) {
+ extraExports.put(pkg, extraReads);
+ }
+
+ // prepare extra opens
+ Map> extraOpens = new HashMap<>();
+ for (String pkg : packages) {
+ extraOpens.put(pkg, extraReads);
+ }
+
+ // prepare extra uses
+ Set> extraUses = Collections.emptySet();
+
+ // prepare extra uses
+ Map, List>> extraProvides = Collections.emptyMap();
+
+ // redefine module
+ inst.redefineModule(src, extraReads, extraExports, extraOpens, extraUses, extraProvides);
+ }
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-agent/manifest.txt b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-agent/manifest.txt
new file mode 100644
index 0000000000..3dc9de7ba5
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-agent/manifest.txt
@@ -0,0 +1,3 @@
+Premain-Class: com.baeldung.agent.LoadTimeAgent
+Can-Redefine-Classes: true
+Can-Retransform-Classes: true
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-agent/module-info.java b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-agent/module-info.java
new file mode 100644
index 0000000000..c976de2881
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-agent/module-info.java
@@ -0,0 +1,3 @@
+module baeldung.agent {
+ requires java.instrument;
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-intermedium/com/baeldung/intermedium/ForwardOpen.java b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-intermedium/com/baeldung/intermedium/ForwardOpen.java
new file mode 100644
index 0000000000..db031e2cfc
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-intermedium/com/baeldung/intermedium/ForwardOpen.java
@@ -0,0 +1,15 @@
+package com.baeldung.intermedium;
+
+public class ForwardOpen {
+ public static void addOpens(Class> clazz1, Class> clazz2) {
+ Module currentModule = ForwardOpen.class.getModule();
+ Module srcModule = clazz1.getModule();
+ Module targetModule = clazz2.getModule();
+
+ System.out.println("current module: " + currentModule.getName());
+ System.out.println("source module: " + srcModule.getName());
+ System.out.println("target module: " + targetModule.getName());
+
+ srcModule.addOpens(clazz1.getPackageName(), targetModule);
+ }
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-intermedium/module-info.java b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-intermedium/module-info.java
new file mode 100644
index 0000000000..9ca6cbce43
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-intermedium/module-info.java
@@ -0,0 +1,3 @@
+module baeldung.intermedium {
+ exports com.baeldung.intermedium;
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/exported/ExportedNonPublicClass.java b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/exported/ExportedNonPublicClass.java
new file mode 100644
index 0000000000..c95e17ec1f
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/exported/ExportedNonPublicClass.java
@@ -0,0 +1,11 @@
+package com.baeldung.reflected.exported;
+
+class ExportedNonPublicClass {
+ public static void testPublicStaticMethod() {
+ System.out.println("ExportedNonPublicClass.testPublicStaticMethod()");
+ }
+
+ private static void testPrivateStaticMethod() {
+ System.out.println("ExportedNonPublicClass.testPrivateStaticMethod()");
+ }
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/exported/ExportedPublicClass.java b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/exported/ExportedPublicClass.java
new file mode 100644
index 0000000000..3bbfca209c
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/exported/ExportedPublicClass.java
@@ -0,0 +1,11 @@
+package com.baeldung.reflected.exported;
+
+public class ExportedPublicClass {
+ public static void testPublicStaticMethod() {
+ System.out.println("ExportedPublicClass.testPublicStaticMethod()");
+ }
+
+ private static void testPrivateStaticMethod() {
+ System.out.println("ExportedPublicClass.testPrivateStaticMethod()");
+ }
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/internal/InternalNonPublicClass.java b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/internal/InternalNonPublicClass.java
new file mode 100644
index 0000000000..052ecbea9e
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/internal/InternalNonPublicClass.java
@@ -0,0 +1,11 @@
+package com.baeldung.reflected.internal;
+
+class InternalNonPublicClass {
+ public static void testPublicStaticMethod() {
+ System.out.println("InternalNonPublicClass.testPublicStaticMethod()");
+ }
+
+ private static void testPrivateStaticMethod() {
+ System.out.println("InternalNonPublicClass.testPrivateStaticMethod()");
+ }
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/internal/InternalPublicClass.java b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/internal/InternalPublicClass.java
new file mode 100644
index 0000000000..8f7a20698f
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/internal/InternalPublicClass.java
@@ -0,0 +1,11 @@
+package com.baeldung.reflected.internal;
+
+public class InternalPublicClass {
+ public static void testPublicStaticMethod() {
+ System.out.println("InternalPublicClass.testPublicStaticMethod()");
+ }
+
+ private static void testPrivateStaticMethod() {
+ System.out.println("InternalPublicClass.testPrivateStaticMethod()");
+ }
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/opened/OpenedNonPublicClass.java b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/opened/OpenedNonPublicClass.java
new file mode 100644
index 0000000000..99f9850cc1
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/opened/OpenedNonPublicClass.java
@@ -0,0 +1,11 @@
+package com.baeldung.reflected.opened;
+
+class OpenedNonPublicClass {
+ public static void testPublicStaticMethod() {
+ System.out.println("OpenedNonPublicClass.testPublicStaticMethod()");
+ }
+
+ private static void testPrivateStaticMethod() {
+ System.out.println("OpenedNonPublicClass.testPrivateStaticMethod()");
+ }
+}
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/opened/OpenedPublicClass.java b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/opened/OpenedPublicClass.java
new file mode 100644
index 0000000000..f9a56ca26b
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/com/baeldung/reflected/opened/OpenedPublicClass.java
@@ -0,0 +1,11 @@
+package com.baeldung.reflected.opened;
+
+public class OpenedPublicClass {
+ public static void testPublicStaticMethod() {
+ System.out.println("OpenedPublicClass.testPublicStaticMethod()");
+ }
+
+ private static void testPrivateStaticMethod() {
+ System.out.println("OpenedPublicClass.testPrivateStaticMethod()");
+ }
+}
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/module-info.java b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/module-info.java
new file mode 100644
index 0000000000..e2164415cf
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflected/module-info.java
@@ -0,0 +1,4 @@
+module baeldung.reflected {
+ exports com.baeldung.reflected.exported;
+ opens com.baeldung.reflected.opened;
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflecting-named/com/baeldung/reflecting/named/Main.java b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflecting-named/com/baeldung/reflecting/named/Main.java
new file mode 100644
index 0000000000..c0fa4010ab
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflecting-named/com/baeldung/reflecting/named/Main.java
@@ -0,0 +1,12 @@
+package com.baeldung.reflecting.named;
+
+import java.lang.reflect.Method;
+
+public class Main {
+ public static void main(String[] args) throws Exception {
+ Class> clazz = Class.forName("com.baeldung.reflected.internal.InternalNonPublicClass");
+ Method method = clazz.getDeclaredMethod("testPrivateStaticMethod");
+ method.setAccessible(true);
+ method.invoke(null);
+ }
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflecting-named/com/baeldung/reflecting/named/MainWithForwardOpen.java b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflecting-named/com/baeldung/reflecting/named/MainWithForwardOpen.java
new file mode 100644
index 0000000000..fad8a9ddce
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflecting-named/com/baeldung/reflecting/named/MainWithForwardOpen.java
@@ -0,0 +1,18 @@
+package com.baeldung.reflecting.named;
+
+import com.baeldung.intermedium.ForwardOpen;
+
+import java.lang.reflect.Method;
+
+public class MainWithForwardOpen {
+ public static void main(String[] args) throws Exception {
+ Class> currentClass = Main.class;
+ Class> clazz = Class.forName("com.baeldung.reflected.internal.InternalNonPublicClass");
+
+ ForwardOpen.addOpens(clazz, currentClass);
+
+ Method method = clazz.getDeclaredMethod("testPrivateStaticMethod");
+ method.setAccessible(true);
+ method.invoke(null);
+ }
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflecting-named/module-info.java b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflecting-named/module-info.java
new file mode 100644
index 0000000000..936f53cd6e
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflecting-named/module-info.java
@@ -0,0 +1,4 @@
+module baeldung.reflecting.named {
+ requires baeldung.intermedium;
+ requires baeldung.reflected;
+}
\ No newline at end of file
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflecting-unnamed/com/baeldung/reflecting/unnamed/Main.java b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflecting-unnamed/com/baeldung/reflecting/unnamed/Main.java
new file mode 100644
index 0000000000..37bdc57eaf
--- /dev/null
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-reflecting-unnamed/com/baeldung/reflecting/unnamed/Main.java
@@ -0,0 +1,13 @@
+package com.baeldung.reflecting.unnamed;
+
+import java.lang.reflect.Field;
+
+public class Main {
+ public static void main(String[] args) throws Exception {
+ Class> clazz = StringBuilder.class;
+ Field f = clazz.getDeclaredField("serialVersionUID");
+ f.setAccessible(true);
+ Object value = f.get(null);
+ System.out.println(value);
+ }
+}
\ No newline at end of file
From 4329845b57fbd0e223b74507201918ee54bde612 Mon Sep 17 00:00:00 2001
From: 515882294 <515882294@qq.com>
Date: Tue, 12 Apr 2022 16:39:47 +0800
Subject: [PATCH 7/7] BAEL-1699 - modify code comment typo
---
.../baeldung-agent/com/baeldung/agent/LoadTimeAgent.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-agent/com/baeldung/agent/LoadTimeAgent.java b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-agent/com/baeldung/agent/LoadTimeAgent.java
index a2ec9c3d4a..33d9a231ea 100644
--- a/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-agent/com/baeldung/agent/LoadTimeAgent.java
+++ b/core-java-modules/core-java-9-jigsaw/src/modules/com.baeldung.reflective.access/baeldung-agent/com/baeldung/agent/LoadTimeAgent.java
@@ -60,7 +60,7 @@ public class LoadTimeAgent {
// prepare extra uses
Set> extraUses = Collections.emptySet();
- // prepare extra uses
+ // prepare extra provides
Map, List>> extraProvides = Collections.emptyMap();
// redefine module