Skip to content

Commit

Permalink
GH-35053: [Java] Fix MemoryUtil to support Java 21 (#36370)
Browse files Browse the repository at this point in the history
### 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 <dongjoon@apache.org>
Signed-off-by: David Li <li.davidm96@gmail.com>
  • Loading branch information
dongjoon-hyun committed Jun 29, 2023
1 parent 52d830e commit bea7a34
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit bea7a34

Please sign in to comment.