3939 */
4040@ ParametersAreNonnullByDefault
4141@ ReturnValuesAreNonnullByDefault
42- public final class InetAddressCacheUtilForJava8Minus {
42+ public final class InetAddressCacheUtilForJava8 {
4343 /**
4444 * Need convert host to lowercase, see {@link InetAddress#cacheAddresses(String, InetAddress[], boolean)}.
4545 */
@@ -80,7 +80,7 @@ private static Object newCacheEntry(String host, String[] ips, long expiration)
8080
8181 // InetAddress.CacheEntry has only one constructor:
8282 // - for jdk 6, constructor signature is CacheEntry(Object address, long expiration)
83- // - for jdk 7/8, constructor signature is CacheEntry(InetAddress[] addresses, long expiration)
83+ // - for jdk 7/8+ , constructor signature is CacheEntry(InetAddress[] addresses, long expiration)
8484 //
8585 // code in jdk 6:
8686 // https://hg.openjdk.java.net/jdk6/jdk6/jdk/file/8deef18bb749/src/share/classes/java/net/InetAddress.java#l739
@@ -134,7 +134,7 @@ private static Map<String, Object> getNegativeCache()
134134 private static Map <String , Object > getCacheOfInetAddress$Cache0 (Object inetAddressCache )
135135 throws NoSuchFieldException , IllegalAccessException , ClassNotFoundException {
136136 if (cacheMapFieldOfInetAddress$Cache == null ) {
137- synchronized (InetAddressCacheUtilForJava8Minus .class ) {
137+ synchronized (InetAddressCacheUtilForJava8 .class ) {
138138 if (cacheMapFieldOfInetAddress$Cache == null ) { // double check
139139 final Class <?> clazz = Class .forName ("java.net.InetAddress$Cache" );
140140 final Field f = clazz .getDeclaredField ("cache" );
@@ -172,7 +172,7 @@ private static Object[] getAddressCacheAndNegativeCacheOfInetAddress0()
172172 throws NoSuchFieldException , IllegalAccessException {
173173 if (ADDRESS_CACHE_AND_NEGATIVE_CACHE != null ) return ADDRESS_CACHE_AND_NEGATIVE_CACHE ;
174174
175- synchronized (InetAddressCacheUtilForJava8Minus .class ) {
175+ synchronized (InetAddressCacheUtilForJava8 .class ) {
176176 // double check
177177 if (ADDRESS_CACHE_AND_NEGATIVE_CACHE != null ) return ADDRESS_CACHE_AND_NEGATIVE_CACHE ;
178178
@@ -225,7 +225,7 @@ public static DnsCache listInetAddressCache()
225225 }
226226
227227 private static List <DnsCacheEntry > convert (Map <String , Object > cache )
228- throws IllegalAccessException , ClassNotFoundException {
228+ throws IllegalAccessException , ClassNotFoundException , NoSuchFieldException {
229229 final List <DnsCacheEntry > ret = new ArrayList <>();
230230 for (Map .Entry <String , Object > entry : cache .entrySet ()) {
231231 final String host = entry .getKey ();
@@ -239,7 +239,7 @@ private static List<DnsCacheEntry> convert(Map<String, Object> cache)
239239 }
240240
241241 private static DnsCacheEntry inetAddress$CacheEntry2DnsCacheEntry (String host , Object entry )
242- throws IllegalAccessException , ClassNotFoundException {
242+ throws IllegalAccessException , ClassNotFoundException , NoSuchFieldException {
243243 initFieldsOfInetAddress$CacheEntry ();
244244
245245 final long expiration = expirationFieldOfInetAddress$CacheEntry .getLong (entry );
@@ -255,42 +255,37 @@ private static List<DnsCacheEntry> convert(Map<String, Object> cache)
255255 */
256256 private static volatile Field expirationFieldOfInetAddress$CacheEntry = null ;
257257 /**
258- * {@link InetAddress.CacheEntry.expiration }
258+ * {@link InetAddress.CacheEntry.addresses }
259259 */
260260 private static volatile Field addressesFieldOfInetAddress$CacheEntry = null ;
261261
262- private static void initFieldsOfInetAddress$CacheEntry () throws ClassNotFoundException {
262+ private static void initFieldsOfInetAddress$CacheEntry () throws ClassNotFoundException , NoSuchFieldException {
263263 if (expirationFieldOfInetAddress$CacheEntry != null && addressesFieldOfInetAddress$CacheEntry != null ) return ;
264264
265- final Class <?> cacheEntryClass = Class . forName ( "java.net.InetAddress$CacheEntry" );
266- synchronized ( InetAddressCacheUtilForJava8Minus . class ) {
265+ synchronized ( InetAddressCacheUtilForJava8 . class ) {
266+ final Class <?> cacheEntryClass = Class . forName ( "java.net.InetAddress$CacheEntry" );
267267 // double check
268268 if (expirationFieldOfInetAddress$CacheEntry != null && addressesFieldOfInetAddress$CacheEntry != null )
269269 return ;
270270
271271 // InetAddress.CacheEntry has 2 filed:
272272 // - for jdk 6, address and expiration
273273 // - for jdk 7+, addresses(*renamed*!) and expiration
274+ //
274275 // code in jdk 6:
275276 // https://hg.openjdk.java.net/jdk6/jdk6/jdk/file/8deef18bb749/src/share/classes/java/net/InetAddress.java#l739
276277 // code in jdk 7:
277278 // https://hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/4dd5e486620d/src/share/classes/java/net/InetAddress.java#l742
278279 // code in jdk 8:
279280 // https://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/45e4e636b757/src/share/classes/java/net/InetAddress.java#l748
280- final Field [] fields = cacheEntryClass .getDeclaredFields ();
281- for (Field field : fields ) {
282- final String name = field .getName ();
283- if (name .equals ("expiration" )) {
284- field .setAccessible (true );
285- expirationFieldOfInetAddress$CacheEntry = field ;
286- } else if (name .startsWith ("address" )) { // use startWith so works for jdk 6 and jdk 7+
287- field .setAccessible (true );
288- addressesFieldOfInetAddress$CacheEntry = field ;
289- } else {
290- throw new IllegalStateException ("JDK add new Field " + name +
291- " for class InetAddress.CacheEntry, report issue for dns-cache-manipulator lib!" );
292- }
293- }
281+
282+ Field expirationField = cacheEntryClass .getDeclaredField ("expiration" );
283+ expirationField .setAccessible (true );
284+ expirationFieldOfInetAddress$CacheEntry = expirationField ;
285+
286+ Field addressesField = cacheEntryClass .getDeclaredField ("addresses" );
287+ addressesField .setAccessible (true );
288+ addressesFieldOfInetAddress$CacheEntry = addressesField ;
294289 }
295290 }
296291
@@ -302,6 +297,6 @@ public static void clearInetAddressCache()
302297 }
303298 }
304299
305- private InetAddressCacheUtilForJava8Minus () {
300+ private InetAddressCacheUtilForJava8 () {
306301 }
307302}
0 commit comments