Skip to content

Commit fe31d2f

Browse files
committed
refactor: remove java 6 logic since upgrade to java 8
1 parent 951e7b6 commit fe31d2f

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

library/src/main/java/com/alibaba/dcm/DnsCacheManipulator.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.alibaba.dcm;
22

33
import com.alibaba.dcm.internal.InetAddressCacheUtilCommons;
4-
import com.alibaba.dcm.internal.InetAddressCacheUtilForJava8Minus;
4+
import com.alibaba.dcm.internal.InetAddressCacheUtilForJava8;
55
import com.alibaba.dcm.internal.InetAddressCacheUtilForJava9Plus;
66
import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault;
77
import sun.net.InetAddressCachePolicy;
@@ -43,7 +43,7 @@ public final class DnsCacheManipulator {
4343
public static void setDnsCache(String host, String... ips) {
4444
try {
4545
if (isJavaVersionAtMost8()) {
46-
InetAddressCacheUtilForJava8Minus.setInetAddressCache(host, ips, NEVER_EXPIRATION);
46+
InetAddressCacheUtilForJava8.setInetAddressCache(host, ips, NEVER_EXPIRATION);
4747
} else {
4848
InetAddressCacheUtilForJava9Plus.setInetAddressCache(host, ips, NEVER_EXPIRATION);
4949
}
@@ -65,7 +65,7 @@ public static void setDnsCache(String host, String... ips) {
6565
public static void setDnsCache(long expireMillis, String host, String... ips) {
6666
try {
6767
if (isJavaVersionAtMost8()) {
68-
InetAddressCacheUtilForJava8Minus.setInetAddressCache(host, ips, expireMillis);
68+
InetAddressCacheUtilForJava8.setInetAddressCache(host, ips, expireMillis);
6969
} else {
7070
InetAddressCacheUtilForJava9Plus.setInetAddressCache(host, ips, expireMillis);
7171
}
@@ -153,7 +153,7 @@ public static void loadDnsCacheConfig(String propertiesFileName) {
153153
public static DnsCacheEntry getDnsCache(String host) {
154154
try {
155155
if (isJavaVersionAtMost8()) {
156-
return InetAddressCacheUtilForJava8Minus.getInetAddressCache(host);
156+
return InetAddressCacheUtilForJava8.getInetAddressCache(host);
157157
} else {
158158
return InetAddressCacheUtilForJava9Plus.getInetAddressCache(host);
159159
}
@@ -175,7 +175,7 @@ public static DnsCacheEntry getDnsCache(String host) {
175175
public static DnsCache getWholeDnsCache() {
176176
try {
177177
if (isJavaVersionAtMost8()) {
178-
return InetAddressCacheUtilForJava8Minus.listInetAddressCache();
178+
return InetAddressCacheUtilForJava8.listInetAddressCache();
179179
} else {
180180
return InetAddressCacheUtilForJava9Plus.listInetAddressCache();
181181
}
@@ -236,7 +236,7 @@ public static List<DnsCacheEntry> listDnsNegativeCache() {
236236
public static void removeDnsCache(String host) {
237237
try {
238238
if (isJavaVersionAtMost8()) {
239-
InetAddressCacheUtilForJava8Minus.removeInetAddressCache(host);
239+
InetAddressCacheUtilForJava8.removeInetAddressCache(host);
240240
} else {
241241
InetAddressCacheUtilForJava9Plus.removeInetAddressCache(host);
242242
}
@@ -254,7 +254,7 @@ public static void removeDnsCache(String host) {
254254
public static void clearDnsCache() {
255255
try {
256256
if (isJavaVersionAtMost8()) {
257-
InetAddressCacheUtilForJava8Minus.clearInetAddressCache();
257+
InetAddressCacheUtilForJava8.clearInetAddressCache();
258258
} else {
259259
InetAddressCacheUtilForJava9Plus.clearInetAddressCache();
260260
}

library/src/main/java/com/alibaba/dcm/internal/InetAddressCacheUtilForJava8Minus.java renamed to library/src/main/java/com/alibaba/dcm/internal/InetAddressCacheUtilForJava8.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
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

Comments
 (0)