feat: support for jdk 21 (#265)

This commit is contained in:
Alexey Anufriev 2024-06-21 07:10:41 +02:00 committed by Jerry Lee
parent 85469defc6
commit 536e281d1e
7 changed files with 30 additions and 7 deletions

View File

@ -11,7 +11,7 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest, windows-latest ]
java: [ 8.0.345, 8, 11, 17, 20 ]
java: [ 8.0.345, 8, 11, 17, 20, 21 ]
fail-fast: false
max-parallel: 64
name: Fast CI on Java ${{ matrix.java }} OS ${{ matrix.os }}

View File

@ -27,6 +27,7 @@ jobs:
11
17
20
21
distribution: zulu
cache: maven

View File

@ -27,7 +27,7 @@
Java Dns Cache Manipulator(`DCM`) contains 2 subprojects:
- [**`DCM` Library**](library)
A tiny 0-dependency thread-safe lib for setting/viewing dns programmatically without touching host file, make unit/integration test portable. Support `Java 8~20`, support `IPv6`.
A tiny 0-dependency thread-safe lib for setting/viewing dns programmatically without touching host file, make unit/integration test portable. Support `Java 8~21`, support `IPv6`.
- [**`DCM` Tool**](tool)
A tiny tool for setting/viewing dns of running JVM processes.

View File

@ -14,7 +14,7 @@
<description>
🌏 a tiny 0-dependency thread-safe Java™ lib
for setting/viewing dns programmatically without touching host file,
make unit/integration test portable. support Java 8~20, support IPv6.
make unit/integration test portable. support Java 8~21, support IPv6.
</description>
<url>https://github.com/alibaba/java-dns-cache-manipulator</url>
<inceptionYear>2015</inceptionYear>

View File

@ -80,7 +80,15 @@ public final class InetAddressCacheUtilForNew {
// double check
if (constructorOfInetAddress$CachedAddresses != null) return constructorOfInetAddress$CachedAddresses;
final Class<?> clazz = Class.forName(inetAddress$CachedAddresses_ClassName);
Class<?> clazz;
try {
clazz = Class.forName(inetAddress$CachedAddresses_ClassName);
} catch (ClassNotFoundException e) {
// jdk 21 support
// due to https://github.com/openjdk/jdk/commit/8b127262a3dff9c4420945e902f6a688f8d05e2e
clazz = Class.forName(inetAddress$CachedLookup_ClassName);
}
// InetAddress.CacheEntry has only one constructor:
//
@ -89,6 +97,8 @@ public final class InetAddressCacheUtilForNew {
// https://hg.openjdk.java.net/jdk9/jdk9/jdk/file/65464a307408/src/java.base/share/classes/java/net/InetAddress.java#l783
// code in jdk 11:
// https://hg.openjdk.java.net/jdk/jdk11/file/1ddf9a99e4ad/src/java.base/share/classes/java/net/InetAddress.java#l787
// code in jdk 21:
// https://github.com/openjdk/jdk/blob/jdk-21-ga/src/java.base/share/classes/java/net/InetAddress.java#L979
final Constructor<?> constructor = clazz.getDeclaredConstructors()[0];
constructor.setAccessible(true);
@ -235,7 +245,10 @@ public final class InetAddressCacheUtilForNew {
final InetAddress[] inetAddresses;
final long expiration;
if (addressesClassName.equals(inetAddress$CachedAddresses_ClassName)) {
if (addressesClassName.equals(inetAddress$CachedAddresses_ClassName)
// jdk 21 support
|| addressesClassName.equals(inetAddress$CachedLookup_ClassName)) {
inetAddresses = (InetAddress[]) inetAddressesFieldOfInetAddress$CacheAddress.get(addresses);
long expiryTimeNanos = expiryTimeFieldOfInetAddress$CacheAddress.getLong(addresses);
@ -254,6 +267,7 @@ public final class InetAddressCacheUtilForNew {
}
private static final String inetAddress$CachedAddresses_ClassName = "java.net.InetAddress$CachedAddresses";
private static final String inetAddress$CachedLookup_ClassName = "java.net.InetAddress$CachedLookup";
private static final String inetAddress$NameServiceAddresses_ClassName = "java.net.InetAddress$NameServiceAddresses";
// Fields of InetAddress$CachedAddresses
@ -275,7 +289,14 @@ public final class InetAddressCacheUtilForNew {
///////////////////////////////////////////////
// Fields of InetAddress$CachedAddresses
///////////////////////////////////////////////
final Class<?> cachedAddresses_Class = Class.forName(inetAddress$CachedAddresses_ClassName);
Class<?> cachedAddresses_Class;
try {
cachedAddresses_Class = Class.forName(inetAddress$CachedAddresses_ClassName);
} catch (ClassNotFoundException e) {
// jdk 21 support
cachedAddresses_Class = Class.forName(inetAddress$CachedLookup_ClassName);
}
final Field inetAddressesFiled = cachedAddresses_Class.getDeclaredField("inetAddresses");
inetAddressesFiled.setAccessible(true);

View File

@ -55,6 +55,7 @@ readonly CI_JDKS=(
"$default_build_jdk_version"
17
20
21
)
# here use `install` and `-D performRelease` intended

View File

@ -13,7 +13,7 @@
<name>Java Dns Cache Manipulator(DCM) Tool</name>
<description>
🌏 a tiny tool for setting/viewing dns of running JVM process.
support Java 8~20, support IPv6.
support Java 8~21, support IPv6.
</description>
<url>https://github.com/alibaba/java-dns-cache-manipulator</url>
<inceptionYear>2015</inceptionYear>