@@ -96,7 +96,7 @@ private static Object newCacheEntry(String host, String[] ips, long expiration)
9696 }
9797
9898 public static void removeInetAddressCache (String host )
99- throws NoSuchFieldException , IllegalAccessException {
99+ throws NoSuchFieldException , IllegalAccessException , ClassNotFoundException {
100100 host = host .toLowerCase ();
101101
102102 synchronized (getAddressCacheOfInetAddress ()) {
@@ -110,7 +110,7 @@ public static void removeInetAddressCache(String host)
110110 */
111111 @ GuardedBy ("getAddressCacheOfInetAddress()" )
112112 private static Map <String , Object > getCache ()
113- throws NoSuchFieldException , IllegalAccessException {
113+ throws NoSuchFieldException , IllegalAccessException , ClassNotFoundException {
114114 return getCacheOfInetAddress$Cache0 (getAddressCacheOfInetAddress ());
115115 }
116116
@@ -119,18 +119,31 @@ private static Map<String, Object> getCache()
119119 */
120120 @ GuardedBy ("getAddressCacheOfInetAddress()" )
121121 private static Map <String , Object > getNegativeCache ()
122- throws NoSuchFieldException , IllegalAccessException {
122+ throws NoSuchFieldException , IllegalAccessException , ClassNotFoundException {
123123 return getCacheOfInetAddress$Cache0 (getNegativeCacheOfInetAddress ());
124124 }
125125
126+
127+ /**
128+ * {@link InetAddress.Cache.cache}
129+ */
130+ private static volatile Field cacheMapFieldOfInetAddress$Cache = null ;
131+
126132 @ SuppressWarnings ("unchecked" )
127133 private static Map <String , Object > getCacheOfInetAddress$Cache0 (Object inetAddressCache )
128- throws NoSuchFieldException , IllegalAccessException {
129- Class <?> clazz = inetAddressCache .getClass ();
134+ throws NoSuchFieldException , IllegalAccessException , ClassNotFoundException {
135+ if (cacheMapFieldOfInetAddress$Cache == null ) {
136+ synchronized (InetAddressCacheUtilForJava8Minus .class ) {
137+ if (cacheMapFieldOfInetAddress$Cache == null ) { // double check
138+ final Class <?> clazz = Class .forName ("java.net.InetAddress$Cache" );
139+ final Field f = clazz .getDeclaredField ("cache" );
140+ f .setAccessible (true );
141+ cacheMapFieldOfInetAddress$Cache = f ;
142+ }
143+ }
144+ }
130145
131- final Field cacheMapField = clazz .getDeclaredField ("cache" );
132- cacheMapField .setAccessible (true );
133- return (Map <String , Object >) cacheMapField .get (inetAddressCache );
146+ return (Map <String , Object >) cacheMapFieldOfInetAddress$Cache .get (inetAddressCache );
134147 }
135148
136149 /**
@@ -177,7 +190,7 @@ private static Object[] getAddressCacheAndNegativeCacheOfInetAddress0()
177190
178191 @ Nullable
179192 public static DnsCacheEntry getInetAddressCache (String host )
180- throws NoSuchFieldException , IllegalAccessException {
193+ throws NoSuchFieldException , IllegalAccessException , ClassNotFoundException {
181194 host = host .toLowerCase ();
182195
183196 final Object cacheEntry ;
@@ -198,7 +211,7 @@ private static boolean isDnsCacheEntryExpired(String host) {
198211 }
199212
200213 public static DnsCache listInetAddressCache ()
201- throws NoSuchFieldException , IllegalAccessException {
214+ throws NoSuchFieldException , IllegalAccessException , ClassNotFoundException {
202215 final Map <String , Object > cache ;
203216 final Map <String , Object > negativeCache ;
204217 synchronized (getAddressCacheOfInetAddress ()) {
@@ -268,7 +281,7 @@ private static List<DnsCacheEntry> convert(Map<String, Object> cache) throws Ill
268281 return new DnsCacheEntry (host , ips , expiration );
269282 }
270283
271- public static void clearInetAddressCache () throws NoSuchFieldException , IllegalAccessException {
284+ public static void clearInetAddressCache () throws NoSuchFieldException , IllegalAccessException , ClassNotFoundException {
272285 synchronized (getAddressCacheOfInetAddress ()) {
273286 getCache ().clear ();
274287 getNegativeCache ().clear ();
0 commit comments