From bea7a34e2671bf8084a1abac71745a47e28c9081 Mon Sep 17 00:00:00 2001 From: Dongjoon Hyun Date: Thu, 29 Jun 2023 10:37:27 -0700 Subject: [PATCH] GH-35053: [Java] Fix MemoryUtil to support Java 21 (#36370) ### Rationale for this change Java 21 switched `DirectByteBuffer(long,int)` constructor to `DirectByteBuffer(long,long)` via https://github.com/openjdk/jdk/commit/a56598f5a534cc9223367e7faa8433ea38661db9 ### What changes are included in this PR? In order to avoid `NoSuchMethodException` error in Java 21 environment, this PR aims to choose one of constructors based on the Java version like https://github.com/netty/netty/pull/13366 . ### Are these changes tested? ``` $ java -version openjdk version "21-ea" 2023-09-19 OpenJDK Runtime Environment (build 21-ea+28-2377) OpenJDK 64-Bit Server VM (build 21-ea+28-2377, mixed mode, sharing) $ cd java $ mvn clean package --am --pl memory/memory-core ... [INFO] Apache Arrow Java Root POM ......................... SUCCESS [ 5.693 s] [INFO] Arrow Memory ....................................... SUCCESS [ 1.703 s] [INFO] Arrow Memory - Core ................................ SUCCESS [ 7.050 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 14.630 s [INFO] Finished at: 2023-06-28T20:43:29-07:00 [INFO] ------------------------------------------------------------------------ ``` ### Are there any user-facing changes? * Closes: #35053 Authored-by: Dongjoon Hyun Signed-off-by: David Li --- .../main/java/org/apache/arrow/memory/util/MemoryUtil.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/MemoryUtil.java b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/MemoryUtil.java index 70e1a2586f2e0..b83adf9271d4b 100644 --- a/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/MemoryUtil.java +++ b/java/memory/memory-core/src/main/java/org/apache/arrow/memory/util/MemoryUtil.java @@ -54,6 +54,10 @@ public class MemoryUtil { */ public static final boolean LITTLE_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN; + // Java 1.8, 9, 11, 17, 21 becomes 1, 9, 11, 17, and 21. + private static final int majorVersion = + Integer.parseInt(System.getProperty("java.specification.version").split("\\D+")[0]); + static { try { // try to get the unsafe object @@ -94,7 +98,8 @@ public Object run() { @Override public Object run() { try { - final Constructor constructor = + final Constructor constructor = (majorVersion >= 21) ? + direct.getClass().getDeclaredConstructor(long.class, long.class) : direct.getClass().getDeclaredConstructor(long.class, int.class); constructor.setAccessible(true); logger.debug("Constructor for direct buffer found and made accessible");