Skip to content

Commit 536e281

Browse files
alexey-anufrievoldratlee
authored andcommitted
feat: support for jdk 21 (#265)
1 parent 85469de commit 536e281

File tree

7 files changed

+30
-7
lines changed

7 files changed

+30
-7
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
matrix:
1313
os: [ ubuntu-latest, windows-latest ]
14-
java: [ 8.0.345, 8, 11, 17, 20 ]
14+
java: [ 8.0.345, 8, 11, 17, 20, 21 ]
1515
fail-fast: false
1616
max-parallel: 64
1717
name: Fast CI on Java ${{ matrix.java }} OS ${{ matrix.os }}

.github/workflows/strong_ci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
11
2828
17
2929
20
30+
21
3031
distribution: zulu
3132
cache: maven
3233

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
Java Dns Cache Manipulator(`DCM`) contains 2 subprojects:
2828

2929
- [**`DCM` Library**](library)
30-
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`.
30+
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`.
3131
- [**`DCM` Tool**](tool)
3232
A tiny tool for setting/viewing dns of running JVM processes.
3333

library/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<description>
1515
🌏 a tiny 0-dependency thread-safe Java™ lib
1616
for setting/viewing dns programmatically without touching host file,
17-
make unit/integration test portable. support Java 8~20, support IPv6.
17+
make unit/integration test portable. support Java 8~21, support IPv6.
1818
</description>
1919
<url>https://github.com/alibaba/java-dns-cache-manipulator</url>
2020
<inceptionYear>2015</inceptionYear>

library/src/main/java/com/alibaba/dcm/internal/InetAddressCacheUtilForNew.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,15 @@ private static Object newCachedAddresses(String host, String[] ips, long expirat
8080
// double check
8181
if (constructorOfInetAddress$CachedAddresses != null) return constructorOfInetAddress$CachedAddresses;
8282

83-
final Class<?> clazz = Class.forName(inetAddress$CachedAddresses_ClassName);
83+
Class<?> clazz;
84+
85+
try {
86+
clazz = Class.forName(inetAddress$CachedAddresses_ClassName);
87+
} catch (ClassNotFoundException e) {
88+
// jdk 21 support
89+
// due to https://github.com/openjdk/jdk/commit/8b127262a3dff9c4420945e902f6a688f8d05e2e
90+
clazz = Class.forName(inetAddress$CachedLookup_ClassName);
91+
}
8492

8593
// InetAddress.CacheEntry has only one constructor:
8694
//
@@ -89,6 +97,8 @@ private static Object newCachedAddresses(String host, String[] ips, long expirat
8997
// https://hg.openjdk.java.net/jdk9/jdk9/jdk/file/65464a307408/src/java.base/share/classes/java/net/InetAddress.java#l783
9098
// code in jdk 11:
9199
// https://hg.openjdk.java.net/jdk/jdk11/file/1ddf9a99e4ad/src/java.base/share/classes/java/net/InetAddress.java#l787
100+
// code in jdk 21:
101+
// https://github.com/openjdk/jdk/blob/jdk-21-ga/src/java.base/share/classes/java/net/InetAddress.java#L979
92102
final Constructor<?> constructor = clazz.getDeclaredConstructors()[0];
93103
constructor.setAccessible(true);
94104

@@ -235,7 +245,10 @@ public static DnsCache listInetAddressCache()
235245

236246
final InetAddress[] inetAddresses;
237247
final long expiration;
238-
if (addressesClassName.equals(inetAddress$CachedAddresses_ClassName)) {
248+
if (addressesClassName.equals(inetAddress$CachedAddresses_ClassName)
249+
// jdk 21 support
250+
|| addressesClassName.equals(inetAddress$CachedLookup_ClassName)) {
251+
239252
inetAddresses = (InetAddress[]) inetAddressesFieldOfInetAddress$CacheAddress.get(addresses);
240253

241254
long expiryTimeNanos = expiryTimeFieldOfInetAddress$CacheAddress.getLong(addresses);
@@ -254,6 +267,7 @@ public static DnsCache listInetAddressCache()
254267
}
255268

256269
private static final String inetAddress$CachedAddresses_ClassName = "java.net.InetAddress$CachedAddresses";
270+
private static final String inetAddress$CachedLookup_ClassName = "java.net.InetAddress$CachedLookup";
257271
private static final String inetAddress$NameServiceAddresses_ClassName = "java.net.InetAddress$NameServiceAddresses";
258272

259273
// Fields of InetAddress$CachedAddresses
@@ -275,7 +289,14 @@ private static void initFieldsOfAddresses() throws ClassNotFoundException, NoSuc
275289
///////////////////////////////////////////////
276290
// Fields of InetAddress$CachedAddresses
277291
///////////////////////////////////////////////
278-
final Class<?> cachedAddresses_Class = Class.forName(inetAddress$CachedAddresses_ClassName);
292+
Class<?> cachedAddresses_Class;
293+
294+
try {
295+
cachedAddresses_Class = Class.forName(inetAddress$CachedAddresses_ClassName);
296+
} catch (ClassNotFoundException e) {
297+
// jdk 21 support
298+
cachedAddresses_Class = Class.forName(inetAddress$CachedLookup_ClassName);
299+
}
279300

280301
final Field inetAddressesFiled = cachedAddresses_Class.getDeclaredField("inetAddresses");
281302
inetAddressesFiled.setAccessible(true);

scripts/integration_test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ readonly CI_JDKS=(
5555
"$default_build_jdk_version"
5656
17
5757
20
58+
21
5859
)
5960

6061
# here use `install` and `-D performRelease` intended

tool/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<name>Java Dns Cache Manipulator(DCM) Tool</name>
1414
<description>
1515
🌏 a tiny tool for setting/viewing dns of running JVM process.
16-
support Java 8~20, support IPv6.
16+
support Java 8~21, support IPv6.
1717
</description>
1818
<url>https://github.com/alibaba/java-dns-cache-manipulator</url>
1919
<inceptionYear>2015</inceptionYear>

0 commit comments

Comments
 (0)