Skip to content

Commit aa71e54

Browse files
dongjoon-hyunkou
authored andcommitted
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 openjdk/jdk@a56598f ### 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 netty/netty#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 <[email protected]> Signed-off-by: David Li <[email protected]>
1 parent ec032db commit aa71e54

File tree

1 file changed

+6
-1
lines changed
  • memory/memory-core/src/main/java/org/apache/arrow/memory/util

1 file changed

+6
-1
lines changed

memory/memory-core/src/main/java/org/apache/arrow/memory/util/MemoryUtil.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public class MemoryUtil {
5454
*/
5555
public static final boolean LITTLE_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN;
5656

57+
// Java 1.8, 9, 11, 17, 21 becomes 1, 9, 11, 17, and 21.
58+
private static final int majorVersion =
59+
Integer.parseInt(System.getProperty("java.specification.version").split("\\D+")[0]);
60+
5761
static {
5862
try {
5963
// try to get the unsafe object
@@ -94,7 +98,8 @@ public Object run() {
9498
@Override
9599
public Object run() {
96100
try {
97-
final Constructor<?> constructor =
101+
final Constructor<?> constructor = (majorVersion >= 21) ?
102+
direct.getClass().getDeclaredConstructor(long.class, long.class) :
98103
direct.getClass().getDeclaredConstructor(long.class, int.class);
99104
constructor.setAccessible(true);
100105
logger.debug("Constructor for direct buffer found and made accessible");

0 commit comments

Comments
 (0)