Skip to content

Commit ba1beab

Browse files
authored
feature: Support JDK21 (and drop JDK7 support) (#765)
* feature: Support JDK21 * JDK21 doesn't support -source 1.7 option any more * Upgrade to Scala 2.13.12, which supports JDK21 * Use DirectByteBuffer(long, long) in JDK21
1 parent c2c3a8f commit ba1beab

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

.github/workflows/CI.yml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ jobs:
2727
- uses: actions/checkout@v2
2828
- name: jcheckstyle
2929
run: ./sbt jcheckStyle
30+
test_jdk21:
31+
name: Test JDK21
32+
runs-on: ubuntu-latest
33+
steps:
34+
- uses: actions/checkout@v2
35+
- uses: actions/setup-java@v3
36+
with:
37+
distribution: 'zulu'
38+
java-version: '21'
39+
- uses: actions/cache@v2
40+
with:
41+
path: ~/.cache
42+
key: ${{ runner.os }}-jdk21-${{ hashFiles('**/*.sbt') }}
43+
restore-keys: ${{ runner.os }}-jdk21-
44+
- name: Test
45+
run: ./sbt test
46+
- name: Universal Buffer Test
47+
run: ./sbt test -J-Dmsgpack.universal-buffer=true
3048
test_jdk17:
3149
name: Test JDK17
3250
runs-on: ubuntu-latest
@@ -39,7 +57,7 @@ jobs:
3957
- uses: actions/cache@v2
4058
with:
4159
path: ~/.cache
42-
key: ${{ runner.os }}-jdk11-${{ hashFiles('**/*.sbt') }}
60+
key: ${{ runner.os }}-jdk17-${{ hashFiles('**/*.sbt') }}
4361
restore-keys: ${{ runner.os }}-jdk17-
4462
- name: Test
4563
run: ./sbt test

build.sbt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ val buildSettings = Seq[Setting[_]](
1717
organizationName := "MessagePack",
1818
organizationHomepage := Some(new URL("http://msgpack.org/")),
1919
description := "MessagePack for Java",
20-
scalaVersion := "2.13.6",
20+
scalaVersion := "2.13.12",
2121
Test / logBuffered := false,
2222
// msgpack-java should be a pure-java library, so remove Scala specific configurations
2323
autoScalaLibrary := false,
@@ -26,11 +26,11 @@ val buildSettings = Seq[Setting[_]](
2626
// JVM options for building
2727
scalacOptions ++= Seq("-encoding", "UTF-8", "-deprecation", "-unchecked", "-feature"),
2828
Test / javaOptions ++= Seq("-ea"),
29-
javacOptions ++= Seq("-source", "1.7", "-target", "1.7"),
29+
javacOptions ++= Seq("-source", "1.8", "-target", "1.8"),
3030
Compile / compile / javacOptions ++= Seq("-encoding", "UTF-8", "-Xlint:unchecked", "-Xlint:deprecation"),
3131
// Use lenient validation mode when generating Javadoc (for Java8)
3232
doc / javacOptions := {
33-
val opts = Seq("-source", "1.7")
33+
val opts = Seq("-source", "1.8")
3434
if (scala.util.Properties.isJavaAtLeast("1.8")) {
3535
opts ++ Seq("-Xdoclint:none")
3636
} else {
@@ -92,7 +92,7 @@ lazy val msgpackCore = Project(id = "msgpack-core", base = file("msgpack-core"))
9292
"org.msgpack" % "msgpack" % "0.6.12" % "test",
9393
// For integration test with Akka
9494
"com.typesafe.akka" %% "akka-actor" % "2.6.20" % "test",
95-
"org.scala-lang.modules" %% "scala-collection-compat" % "2.11.0" % "test"
95+
"org.scala-lang.modules" %% "scala-collection-compat" % "2.11.0" % "test"
9696
)
9797
)
9898

msgpack-core/src/main/java/org/msgpack/core/buffer/DirectBufferAccess.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ private DirectBufferAccess()
3737

3838
enum DirectBufferConstructorType
3939
{
40+
ARGS_LONG_LONG,
4041
ARGS_LONG_INT_REF,
4142
ARGS_LONG_INT,
4243
ARGS_INT_INT,
@@ -64,28 +65,35 @@ enum DirectBufferConstructorType
6465
DirectBufferConstructorType constructorType = null;
6566
Method mbWrap = null;
6667
try {
67-
// TODO We should use MethodHandle for Java7, which can avoid the cost of boxing with JIT optimization
68-
directByteBufferConstructor = directByteBufferClass.getDeclaredConstructor(long.class, int.class, Object.class);
69-
constructorType = DirectBufferConstructorType.ARGS_LONG_INT_REF;
68+
// JDK21 DirectByteBuffer(long, long)
69+
directByteBufferConstructor = directByteBufferClass.getDeclaredConstructor(long.class, long.class);
70+
constructorType = DirectBufferConstructorType.ARGS_LONG_LONG;
7071
}
71-
catch (NoSuchMethodException e0) {
72+
catch (NoSuchMethodException e00) {
7273
try {
73-
// https://android.googlesource.com/platform/libcore/+/master/luni/src/main/java/java/nio/DirectByteBuffer.java
74-
// DirectByteBuffer(long address, int capacity)
75-
directByteBufferConstructor = directByteBufferClass.getDeclaredConstructor(long.class, int.class);
76-
constructorType = DirectBufferConstructorType.ARGS_LONG_INT;
74+
// TODO We should use MethodHandle for Java7, which can avoid the cost of boxing with JIT optimization
75+
directByteBufferConstructor = directByteBufferClass.getDeclaredConstructor(long.class, int.class, Object.class);
76+
constructorType = DirectBufferConstructorType.ARGS_LONG_INT_REF;
7777
}
78-
catch (NoSuchMethodException e1) {
78+
catch (NoSuchMethodException e0) {
7979
try {
80-
directByteBufferConstructor = directByteBufferClass.getDeclaredConstructor(int.class, int.class);
81-
constructorType = DirectBufferConstructorType.ARGS_INT_INT;
80+
// https://android.googlesource.com/platform/libcore/+/master/luni/src/main/java/java/nio/DirectByteBuffer.java
81+
// DirectByteBuffer(long address, int capacity)
82+
directByteBufferConstructor = directByteBufferClass.getDeclaredConstructor(long.class, int.class);
83+
constructorType = DirectBufferConstructorType.ARGS_LONG_INT;
8284
}
83-
catch (NoSuchMethodException e2) {
84-
Class<?> aClass = Class.forName("java.nio.MemoryBlock");
85-
mbWrap = aClass.getDeclaredMethod("wrapFromJni", int.class, long.class);
86-
mbWrap.setAccessible(true);
87-
directByteBufferConstructor = directByteBufferClass.getDeclaredConstructor(aClass, int.class, int.class);
88-
constructorType = DirectBufferConstructorType.ARGS_MB_INT_INT;
85+
catch (NoSuchMethodException e1) {
86+
try {
87+
directByteBufferConstructor = directByteBufferClass.getDeclaredConstructor(int.class, int.class);
88+
constructorType = DirectBufferConstructorType.ARGS_INT_INT;
89+
}
90+
catch (NoSuchMethodException e2) {
91+
Class<?> aClass = Class.forName("java.nio.MemoryBlock");
92+
mbWrap = aClass.getDeclaredMethod("wrapFromJni", int.class, long.class);
93+
mbWrap.setAccessible(true);
94+
directByteBufferConstructor = directByteBufferClass.getDeclaredConstructor(aClass, int.class, int.class);
95+
constructorType = DirectBufferConstructorType.ARGS_MB_INT_INT;
96+
}
8997
}
9098
}
9199
}
@@ -281,6 +289,8 @@ static ByteBuffer newByteBuffer(long address, int index, int length, ByteBuffer
281289
}
282290
try {
283291
switch (directBufferConstructorType) {
292+
case ARGS_LONG_LONG:
293+
return (ByteBuffer) byteBufferConstructor.newInstance(address + index, (long) length);
284294
case ARGS_LONG_INT_REF:
285295
return (ByteBuffer) byteBufferConstructor.newInstance(address + index, length, reference);
286296
case ARGS_LONG_INT:

0 commit comments

Comments
 (0)