@@ -212,10 +212,36 @@ public static DnsCache listInetAddressCache()
212212 return new DnsCache (retCache , retNegativeCache );
213213 }
214214
215+ /**
216+ * convert {@link InetAddress.Addresses} to {@link DnsCacheEntry}
217+ */
218+ private static DnsCacheEntry inetAddress$Addresses2DnsCacheEntry (String host , Object addresses )
219+ throws NoSuchFieldException , IllegalAccessException , ClassNotFoundException {
220+ final String addressesClassName = addresses .getClass ().getName ();
215221
216- //////////////////////////////////////////////////////////////////////////////
217- // getters of fields of InetAddress$CachedAddresses / NameServiceAddresses
218- //////////////////////////////////////////////////////////////////////////////
222+ initFieldsOfAddresses ();
223+
224+ final InetAddress [] inetAddresses ;
225+ final long expiration ;
226+ if (addressesClassName .equals (inetAddress$CachedAddresses_ClassName )) {
227+ inetAddresses = (InetAddress []) inetAddressesFieldOfInetAddress$CacheAddress .get (addresses );
228+
229+ long expiryTimeNanos = expiryTimeFieldOfInetAddress$CacheAddress .getLong (addresses );
230+ expiration = convertNanoTimeToTimeMillis (expiryTimeNanos );
231+ } else if (addressesClassName .equals (inetAddress$NameServiceAddresses_ClassName )) {
232+ InetAddress inetAddress = (InetAddress ) reqAddrFieldOfInetAddress$NameServiceAddress .get (addresses );
233+ inetAddresses = new InetAddress []{inetAddress };
234+
235+ expiration = NEVER_EXPIRATION ;
236+ } else {
237+ throw new IllegalStateException ("JDK add new child class " + addressesClassName +
238+ " for class InetAddress.Addresses, report issue for dns-cache-manipulator lib!" );
239+ }
240+
241+ final String [] ips = getIpFromInetAddress (inetAddresses );
242+
243+ return new DnsCacheEntry (host , ips , expiration );
244+ }
219245
220246 private static final String inetAddress$CachedAddresses_ClassName = "java.net.InetAddress$CachedAddresses" ;
221247 private static final String inetAddress$NameServiceAddresses_ClassName = "java.net.InetAddress$NameServiceAddresses" ;
@@ -236,13 +262,7 @@ public static DnsCache listInetAddressCache()
236262 */
237263 private static volatile Field reqAddrFieldOfInetAddress$NameServiceAddress = null ;
238264
239- /**
240- * convert {@link InetAddress.Addresses} to {@link DnsCacheEntry}
241- */
242- private static DnsCacheEntry inetAddress$Addresses2DnsCacheEntry (String host , Object addresses )
243- throws NoSuchFieldException , IllegalAccessException , ClassNotFoundException {
244- final String addressesClassName = addresses .getClass ().getName ();
245-
265+ private static void initFieldsOfAddresses () throws ClassNotFoundException , NoSuchFieldException {
246266 if (reqAddrFieldOfInetAddress$NameServiceAddress == null ) {
247267 synchronized (InetAddressCacheUtilForJava9Plus .class ) {
248268 if (reqAddrFieldOfInetAddress$NameServiceAddress == null ) { // double check
@@ -270,32 +290,8 @@ public static DnsCache listInetAddressCache()
270290 }
271291 }
272292 }
273-
274- final InetAddress [] inetAddresses ;
275- final long expiration ;
276- if (addressesClassName .equals (inetAddress$CachedAddresses_ClassName )) {
277- inetAddresses = (InetAddress []) inetAddressesFieldOfInetAddress$CacheAddress .get (addresses );
278-
279- long expiryTimeNanos = expiryTimeFieldOfInetAddress$CacheAddress .getLong (addresses );
280- expiration = convertNanoTimeToTimeMillis (expiryTimeNanos );
281- } else if (addressesClassName .equals (inetAddress$NameServiceAddresses_ClassName )) {
282- InetAddress inetAddress = (InetAddress ) reqAddrFieldOfInetAddress$NameServiceAddress .get (addresses );
283- inetAddresses = new InetAddress []{inetAddress };
284-
285- expiration = NEVER_EXPIRATION ;
286- } else {
287- throw new IllegalStateException ("JDK add new child class " + addressesClassName +
288- " for class InetAddress.Addresses, report issue for dns-cache-manipulator lib!" );
289- }
290-
291- final String [] ips = getIpFromInetAddress (inetAddresses );
292-
293- return new DnsCacheEntry (host , ips , expiration );
294293 }
295294
296- //////////////////////////////////////////////////////////////////////////////
297-
298-
299295 public static void clearInetAddressCache () throws NoSuchFieldException , IllegalAccessException {
300296 getCacheOfInetAddress ().clear ();
301297 getExpirySetOfInetAddress ().clear ();
0 commit comments