Skip to content

Commit

Permalink
! fix method_complexity `InetAddressCacheUtilForJava8Minus#inetAddres…
Browse files Browse the repository at this point in the history
…s$CacheEntry2DnsCacheEntry` #65
  • Loading branch information
oldratlee committed Nov 25, 2021
1 parent 43d11b2 commit 5ddb68f
Showing 1 changed file with 46 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public static DnsCache listInetAddressCache()
return new DnsCache(convert(cache), convert(negativeCache));
}

private static List<DnsCacheEntry> convert(Map<String, Object> cache) throws IllegalAccessException {
private static List<DnsCacheEntry> convert(Map<String, Object> cache) throws IllegalAccessException, ClassNotFoundException {
final List<DnsCacheEntry> ret = new ArrayList<DnsCacheEntry>();
for (Map.Entry<String, Object> entry : cache.entrySet()) {
final String host = entry.getKey();
Expand All @@ -234,41 +234,8 @@ private static List<DnsCacheEntry> convert(Map<String, Object> cache) throws Ill
return ret;
}


private static volatile Field expirationFieldOfInetAddress$CacheEntry = null;
private static volatile Field addressesFieldOfInetAddress$CacheEntry = null;

private static DnsCacheEntry inetAddress$CacheEntry2DnsCacheEntry(String host, Object entry) throws IllegalAccessException {
if (expirationFieldOfInetAddress$CacheEntry == null || addressesFieldOfInetAddress$CacheEntry == null) {
synchronized (InetAddressCacheUtilForJava8Minus.class) {
if (expirationFieldOfInetAddress$CacheEntry == null || addressesFieldOfInetAddress$CacheEntry == null) { // double check
Class<?> cacheEntryClass = entry.getClass();
// InetAddress.CacheEntry has 2 filed:
// - for jdk 6, address and expiration
// - for jdk 7+, addresses(*renamed*!) and expiration
// code in jdk 6:
// https://hg.openjdk.java.net/jdk6/jdk6/jdk/file/8deef18bb749/src/share/classes/java/net/InetAddress.java#l739
// code in jdk 7:
// https://hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/4dd5e486620d/src/share/classes/java/net/InetAddress.java#l742
// code in jdk 8:
// https://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/45e4e636b757/src/share/classes/java/net/InetAddress.java#l748
final Field[] fields = cacheEntryClass.getDeclaredFields();
for (Field field : fields) {
final String name = field.getName();
if (name.equals("expiration")) {
field.setAccessible(true);
expirationFieldOfInetAddress$CacheEntry = field;
} else if (name.startsWith("address")) { // use startWith so works for jdk 6 and jdk 7+
field.setAccessible(true);
addressesFieldOfInetAddress$CacheEntry = field;
} else {
throw new IllegalStateException("JDK add new Field " + name +
" for class InetAddress.CacheEntry, report issue for dns-cache-manipulator lib!");
}
}
}
}
}
private static DnsCacheEntry inetAddress$CacheEntry2DnsCacheEntry(String host, Object entry) throws IllegalAccessException, ClassNotFoundException {
initFieldsOfInetAddress$CacheEntry();

final long expiration = expirationFieldOfInetAddress$CacheEntry.getLong(entry);

Expand All @@ -278,6 +245,49 @@ private static List<DnsCacheEntry> convert(Map<String, Object> cache) throws Ill
return new DnsCacheEntry(host, ips, expiration);
}

/**
* {@link InetAddress.CacheEntry.expiration}
*/
private static volatile Field expirationFieldOfInetAddress$CacheEntry = null;
/**
* {@link InetAddress.CacheEntry.expiration}
*/
private static volatile Field addressesFieldOfInetAddress$CacheEntry = null;

private static void initFieldsOfInetAddress$CacheEntry() throws ClassNotFoundException {
if (expirationFieldOfInetAddress$CacheEntry != null && addressesFieldOfInetAddress$CacheEntry != null) return;

synchronized (InetAddressCacheUtilForJava8Minus.class) {
// double check
if (expirationFieldOfInetAddress$CacheEntry != null && addressesFieldOfInetAddress$CacheEntry != null) return;

final Class<?> cacheEntryClass = Class.forName("java.net.InetAddress$CacheEntry");
// InetAddress.CacheEntry has 2 filed:
// - for jdk 6, address and expiration
// - for jdk 7+, addresses(*renamed*!) and expiration
// code in jdk 6:
// https://hg.openjdk.java.net/jdk6/jdk6/jdk/file/8deef18bb749/src/share/classes/java/net/InetAddress.java#l739
// code in jdk 7:
// https://hg.openjdk.java.net/jdk7u/jdk7u/jdk/file/4dd5e486620d/src/share/classes/java/net/InetAddress.java#l742
// code in jdk 8:
// https://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/45e4e636b757/src/share/classes/java/net/InetAddress.java#l748
final Field[] fields = cacheEntryClass.getDeclaredFields();
for (Field field : fields) {
final String name = field.getName();
if (name.equals("expiration")) {
field.setAccessible(true);
expirationFieldOfInetAddress$CacheEntry = field;
} else if (name.startsWith("address")) { // use startWith so works for jdk 6 and jdk 7+
field.setAccessible(true);
addressesFieldOfInetAddress$CacheEntry = field;
} else {
throw new IllegalStateException("JDK add new Field " + name +
" for class InetAddress.CacheEntry, report issue for dns-cache-manipulator lib!");
}
}
}
}

public static void clearInetAddressCache() throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException {
synchronized (getAddressCacheOfInetAddress()) {
getCache().clear();
Expand Down

0 comments on commit 5ddb68f

Please sign in to comment.