Skip to content

Commit 5c996fb

Browse files
committed
small improvement:
- rename internal method `isJdkAtMost8` to `isJavaVersionAtMost8` - improve javadoc/comments
1 parent a6b4715 commit 5c996fb

File tree

13 files changed

+59
-46
lines changed

13 files changed

+59
-46
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
@@ -16,7 +16,7 @@
1616
import java.util.regex.Pattern;
1717

1818
import static com.alibaba.dcm.internal.InetAddressCacheUtilCommons.NEVER_EXPIRATION;
19-
import static com.alibaba.dcm.internal.JavaVersionUtil.isJdkAtMost8;
19+
import static com.alibaba.dcm.internal.JavaVersionUtil.isJavaVersionAtMost8;
2020

2121

2222
/**
@@ -42,7 +42,7 @@ public final class DnsCacheManipulator {
4242
*/
4343
public static void setDnsCache(String host, String... ips) {
4444
try {
45-
if (isJdkAtMost8()) {
45+
if (isJavaVersionAtMost8()) {
4646
InetAddressCacheUtilForJava8Minus.setInetAddressCache(host, ips, NEVER_EXPIRATION);
4747
} else {
4848
InetAddressCacheUtilForJava9Plus.setInetAddressCache(host, ips, NEVER_EXPIRATION);
@@ -64,7 +64,7 @@ public static void setDnsCache(String host, String... ips) {
6464
*/
6565
public static void setDnsCache(long expireMillis, String host, String... ips) {
6666
try {
67-
if (isJdkAtMost8()) {
67+
if (isJavaVersionAtMost8()) {
6868
InetAddressCacheUtilForJava8Minus.setInetAddressCache(host, ips, expireMillis);
6969
} else {
7070
InetAddressCacheUtilForJava9Plus.setInetAddressCache(host, ips, expireMillis);
@@ -152,7 +152,7 @@ public static void loadDnsCacheConfig(String propertiesFileName) {
152152
@Nullable
153153
public static DnsCacheEntry getDnsCache(String host) {
154154
try {
155-
if (isJdkAtMost8()) {
155+
if (isJavaVersionAtMost8()) {
156156
return InetAddressCacheUtilForJava8Minus.getInetAddressCache(host);
157157
} else {
158158
return InetAddressCacheUtilForJava9Plus.getInetAddressCache(host);
@@ -174,7 +174,7 @@ public static DnsCacheEntry getDnsCache(String host) {
174174
*/
175175
public static DnsCache getWholeDnsCache() {
176176
try {
177-
if (isJdkAtMost8()) {
177+
if (isJavaVersionAtMost8()) {
178178
return InetAddressCacheUtilForJava8Minus.listInetAddressCache();
179179
} else {
180180
return InetAddressCacheUtilForJava9Plus.listInetAddressCache();
@@ -235,7 +235,7 @@ public static List<DnsCacheEntry> listDnsNegativeCache() {
235235
*/
236236
public static void removeDnsCache(String host) {
237237
try {
238-
if (isJdkAtMost8()) {
238+
if (isJavaVersionAtMost8()) {
239239
InetAddressCacheUtilForJava8Minus.removeInetAddressCache(host);
240240
} else {
241241
InetAddressCacheUtilForJava9Plus.removeInetAddressCache(host);
@@ -253,7 +253,7 @@ public static void removeDnsCache(String host) {
253253
*/
254254
public static void clearDnsCache() {
255255
try {
256-
if (isJdkAtMost8()) {
256+
if (isJavaVersionAtMost8()) {
257257
InetAddressCacheUtilForJava8Minus.clearInetAddressCache();
258258
} else {
259259
InetAddressCacheUtilForJava9Plus.clearInetAddressCache();

library/src/main/java/com/alibaba/dcm/internal/InetAddressCacheUtilCommons.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public final class InetAddressCacheUtilCommons {
2424
* {@code Long.MAX_VALUE(~9e18)} nanoseconds is ~292 years.
2525
* <p>
2626
* {@code Long.MAX_VALUE / 1e9 / 3600 / 24 / 365 ~= 292.47}.
27+
*
28+
* @see System#nanoTime()
2729
*/
2830
public static final long NEVER_EXPIRATION = Long.MAX_VALUE;
2931

library/src/main/java/com/alibaba/dcm/internal/InetAddressCacheUtilForJava8Minus.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public final class InetAddressCacheUtilForJava8Minus {
4343
/**
4444
* Need convert host to lowercase, see {@link InetAddress#cacheAddresses(String, InetAddress[], boolean)}.
4545
*/
46-
public static void setInetAddressCache(String host, String[] ips, long expireMillis) throws UnknownHostException,
47-
IllegalAccessException, InstantiationException, InvocationTargetException,
48-
ClassNotFoundException, NoSuchFieldException {
46+
public static void setInetAddressCache(String host, String[] ips, long expireMillis)
47+
throws UnknownHostException, IllegalAccessException, InstantiationException,
48+
InvocationTargetException, ClassNotFoundException, NoSuchFieldException {
4949
host = host.toLowerCase();
5050
long expiration = expireMillis == NEVER_EXPIRATION ? NEVER_EXPIRATION : System.currentTimeMillis() + expireMillis;
5151
Object entry = newCacheEntry(host, ips, expiration);
@@ -57,7 +57,8 @@ public static void setInetAddressCache(String host, String[] ips, long expireMil
5757
}
5858

5959
private static Object newCacheEntry(String host, String[] ips, long expiration)
60-
throws UnknownHostException, ClassNotFoundException, IllegalAccessException, InvocationTargetException, InstantiationException {
60+
throws UnknownHostException, ClassNotFoundException, IllegalAccessException,
61+
InvocationTargetException, InstantiationException {
6162
// InetAddress.CacheEntry has only one constructor
6263
return getConstructorOfInetAddress$CacheEntry().newInstance(toInetAddressArray(host, ips), expiration);
6364
}
@@ -223,7 +224,8 @@ public static DnsCache listInetAddressCache()
223224
return new DnsCache(convert(cache), convert(negativeCache));
224225
}
225226

226-
private static List<DnsCacheEntry> convert(Map<String, Object> cache) throws IllegalAccessException, ClassNotFoundException {
227+
private static List<DnsCacheEntry> convert(Map<String, Object> cache)
228+
throws IllegalAccessException, ClassNotFoundException {
227229
final List<DnsCacheEntry> ret = new ArrayList<>();
228230
for (Map.Entry<String, Object> entry : cache.entrySet()) {
229231
final String host = entry.getKey();
@@ -236,7 +238,8 @@ private static List<DnsCacheEntry> convert(Map<String, Object> cache) throws Ill
236238
return ret;
237239
}
238240

239-
private static DnsCacheEntry inetAddress$CacheEntry2DnsCacheEntry(String host, Object entry) throws IllegalAccessException, ClassNotFoundException {
241+
private static DnsCacheEntry inetAddress$CacheEntry2DnsCacheEntry(String host, Object entry)
242+
throws IllegalAccessException, ClassNotFoundException {
240243
initFieldsOfInetAddress$CacheEntry();
241244

242245
final long expiration = expirationFieldOfInetAddress$CacheEntry.getLong(entry);
@@ -262,7 +265,8 @@ private static List<DnsCacheEntry> convert(Map<String, Object> cache) throws Ill
262265
final Class<?> cacheEntryClass = Class.forName("java.net.InetAddress$CacheEntry");
263266
synchronized (InetAddressCacheUtilForJava8Minus.class) {
264267
// double check
265-
if (expirationFieldOfInetAddress$CacheEntry != null && addressesFieldOfInetAddress$CacheEntry != null) return;
268+
if (expirationFieldOfInetAddress$CacheEntry != null && addressesFieldOfInetAddress$CacheEntry != null)
269+
return;
266270

267271
// InetAddress.CacheEntry has 2 filed:
268272
// - for jdk 6, address and expiration
@@ -290,7 +294,8 @@ private static List<DnsCacheEntry> convert(Map<String, Object> cache) throws Ill
290294
}
291295
}
292296

293-
public static void clearInetAddressCache() throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException {
297+
public static void clearInetAddressCache()
298+
throws NoSuchFieldException, IllegalAccessException, ClassNotFoundException {
294299
synchronized (getAddressCacheOfInetAddress()) {
295300
getCache().clear();
296301
getNegativeCache().clear();

library/src/main/java/com/alibaba/dcm/internal/InetAddressCacheUtilForJava9Plus.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public final class InetAddressCacheUtilForJava9Plus {
4646
* </ul>
4747
*/
4848
public static void setInetAddressCache(String host, String[] ips, long expireMillis)
49-
throws UnknownHostException, IllegalAccessException, InstantiationException, InvocationTargetException, ClassNotFoundException, NoSuchFieldException {
49+
throws UnknownHostException, IllegalAccessException, InstantiationException,
50+
InvocationTargetException, ClassNotFoundException, NoSuchFieldException {
5051
long expiration = expireMillis == NEVER_EXPIRATION ? NEVER_EXPIRATION : getNanoTimeAfterMs(expireMillis);
5152
Object cachedAddresses = newCachedAddresses(host, ips, expiration);
5253

@@ -58,7 +59,8 @@ public static void setInetAddressCache(String host, String[] ips, long expireMil
5859
* {@link InetAddress.CachedAddresses#CachedAddresses(String, InetAddress[], long)}
5960
*/
6061
private static Object newCachedAddresses(String host, String[] ips, long expiration)
61-
throws ClassNotFoundException, UnknownHostException, IllegalAccessException, InvocationTargetException, InstantiationException {
62+
throws ClassNotFoundException, UnknownHostException, IllegalAccessException,
63+
InvocationTargetException, InstantiationException {
6264
// InetAddress.CachedAddresses has only one constructor
6365
return getConstructorOfInetAddress$CachedAddresses().newInstance(host, toInetAddressArray(host, ips), expiration);
6466
}
@@ -100,7 +102,8 @@ public static void removeInetAddressCache(String host) throws NoSuchFieldExcepti
100102
/**
101103
* @see #getExpirySetOfInetAddress()
102104
*/
103-
private static void removeHostFromExpirySetOfInetAddress(String host) throws NoSuchFieldException, IllegalAccessException {
105+
private static void removeHostFromExpirySetOfInetAddress(String host)
106+
throws NoSuchFieldException, IllegalAccessException {
104107
for (Iterator<Object> iterator = getExpirySetOfInetAddress().iterator(); iterator.hasNext(); ) {
105108
Object cachedAddresses = iterator.next();
106109
if (getHostOfInetAddress$CacheAddress(cachedAddresses).equals(host)) {
@@ -114,7 +117,8 @@ private static void removeHostFromExpirySetOfInetAddress(String host) throws NoS
114117
/**
115118
* {@link InetAddress.CachedAddresses.host}
116119
*/
117-
private static String getHostOfInetAddress$CacheAddress(Object cachedAddresses) throws NoSuchFieldException, IllegalAccessException {
120+
private static String getHostOfInetAddress$CacheAddress(Object cachedAddresses)
121+
throws NoSuchFieldException, IllegalAccessException {
118122
if (hostFieldOfInetAddress$CacheAddress == null) {
119123
synchronized (InetAddressCacheUtilForJava9Plus.class) {
120124
if (hostFieldOfInetAddress$CacheAddress == null) { // double check
@@ -140,7 +144,8 @@ private static void removeHostFromExpirySetOfInetAddress(String host) throws NoS
140144
* </ul>
141145
*/
142146
@SuppressWarnings("unchecked")
143-
private static ConcurrentMap<String, Object> getCacheOfInetAddress() throws NoSuchFieldException, IllegalAccessException {
147+
private static ConcurrentMap<String, Object> getCacheOfInetAddress()
148+
throws NoSuchFieldException, IllegalAccessException {
144149
return (ConcurrentMap<String, Object>) getCacheAndExpirySetOfInetAddress0()[0];
145150
}
146151

@@ -150,7 +155,8 @@ private static ConcurrentMap<String, Object> getCacheOfInetAddress() throws NoSu
150155
* type is {@code ConcurrentSkipListSet<CachedAddresses>} and thread-safe.
151156
*/
152157
@SuppressWarnings("unchecked")
153-
private static ConcurrentSkipListSet<Object> getExpirySetOfInetAddress() throws NoSuchFieldException, IllegalAccessException {
158+
private static ConcurrentSkipListSet<Object> getExpirySetOfInetAddress()
159+
throws NoSuchFieldException, IllegalAccessException {
154160
return (ConcurrentSkipListSet<Object>) getCacheAndExpirySetOfInetAddress0()[1];
155161
}
156162

@@ -159,7 +165,8 @@ private static ConcurrentSkipListSet<Object> getExpirySetOfInetAddress() throws
159165
/**
160166
* @return {@link InetAddress#cache} and {@link InetAddress#expirySet}
161167
*/
162-
private static Object[] getCacheAndExpirySetOfInetAddress0() throws NoSuchFieldException, IllegalAccessException {
168+
private static Object[] getCacheAndExpirySetOfInetAddress0()
169+
throws NoSuchFieldException, IllegalAccessException {
163170
if (ADDRESS_CACHE_AND_EXPIRY_SET != null) return ADDRESS_CACHE_AND_EXPIRY_SET;
164171

165172
synchronized (InetAddressCacheUtilForJava9Plus.class) {

library/src/main/java/com/alibaba/dcm/internal/JavaVersionUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @since 1.6.0
77
*/
88
public final class JavaVersionUtil {
9-
public static boolean isJdkAtMost8() {
9+
public static boolean isJavaVersionAtMost8() {
1010
return JAVA_SPECIFICATION_VERSION_AS_ENUM.atMost(JavaVersion.JAVA_1_8);
1111
}
1212

library/src/main/java/com/alibaba/dcm/internal/TimeUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* internal time util.
55
*
66
* @author Jerry Lee (oldratlee at gmail dot com)
7+
* @author antfling (ding_zhengang at hithinksoft dot com)
78
* @since 1.6.0
89
*/
910
final class TimeUtil {

library/src/main/java/com/alibaba/dcm/internal/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Internal helper class.
2+
* Internal helper classes.
33
*
44
* @author Jerry Lee (oldratlee at gmail dot com)
55
*/

library/src/test/java/com/alibaba/dcm/DnsCacheManipulatorTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class DnsCacheManipulatorTests : AnnotationSpec() {
244244
// 3. touch dns cache with external other host operation
245245
//////////////////////////////////////////////////
246246
EXISTED_DOMAIN.lookupIpByName()
247-
if (JavaVersionUtil.isJdkAtMost8()) {
247+
if (JavaVersionUtil.isJavaVersionAtMost8()) {
248248
shouldContainOnlyOneNegativeCacheWitchExpirationBetween(tick, tick + 1020)
249249
} else {
250250
DnsCacheManipulator.listDnsNegativeCache().shouldBeEmpty()

library/src/test/java/com/alibaba/dcm/Util.kt

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

3-
import com.alibaba.dcm.internal.JavaVersionUtil
3+
import com.alibaba.dcm.internal.JavaVersionUtil.isJavaVersionAtMost8
44
import com.alibaba.dcm.internal.TestTimeUtil.NEVER_EXPIRATION_NANO_TIME_TO_TIME_MILLIS
55
import io.kotest.assertions.fail
66
import io.kotest.assertions.throwables.shouldThrow
@@ -63,7 +63,7 @@ object Util {
6363
val actualExpiration = expiration.time
6464

6565
if (expectedExpiration == Long.MAX_VALUE) {
66-
if (JavaVersionUtil.isJdkAtMost8()) {
66+
if (isJavaVersionAtMost8()) {
6767
actualExpiration shouldBe expectedExpiration
6868
} else {
6969
// hard code test logic for jdk 9+
@@ -75,7 +75,7 @@ object Util {
7575
}
7676

7777
infix fun String.shouldBeEqualAsHostName(expected: String) {
78-
if (JavaVersionUtil.isJdkAtMost8()) {
78+
if (isJavaVersionAtMost8()) {
7979
// java 8-, host name is unified to lower case by InetAddress
8080
this shouldBeEqualIgnoringCase expected
8181
} else {

library/src/test/java/com/alibaba/dcm/agent/DcmAgentTests.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@ import java.nio.charset.StandardCharsets.UTF_8
1818
*/
1919
class DcmAgentTests : AnnotationSpec() {
2020
private lateinit var outputFile: File
21-
private lateinit var outputFilePath: String
2221

2322
@Before
2423
fun setUp() {
2524
outputFile = tempfile("dcm-output-", ".log")
2625
outputFile.length() shouldBe 0
2726
println("Prepared output file: " + outputFile.absolutePath)
2827

29-
outputFilePath = outputFile.absolutePath
30-
3128
DnsCacheManipulator.clearDnsCache()
3229
}
3330

@@ -38,7 +35,7 @@ class DcmAgentTests : AnnotationSpec() {
3835

3936
@Test
4037
fun test_agentmain_file() {
41-
DcmAgent.agentmain("file $outputFilePath")
38+
DcmAgent.agentmain("file ${outputFile.absolutePath}")
4239

4340
val content = outputFile.readLines(UTF_8)
4441
content.first() shouldContain "No action in agent argument, do nothing!"
@@ -53,7 +50,7 @@ class DcmAgentTests : AnnotationSpec() {
5350

5451
@Test
5552
fun test_agentmain_set_toFile() {
56-
DcmAgent.agentmain("set bing.com 1.2.3.4 file $outputFilePath")
53+
DcmAgent.agentmain("set bing.com 1.2.3.4 file ${outputFile.absolutePath}")
5754

5855
DnsCacheManipulator.getDnsCache("bing.com")!!.ip shouldBe "1.2.3.4"
5956

@@ -89,7 +86,7 @@ class DcmAgentTests : AnnotationSpec() {
8986
DnsCacheManipulator.setDnsCache("bing.com", "3.3.3.3")
9087
DnsCacheManipulator.getDnsCache("bing.com").shouldNotBeNull()
9188

92-
DcmAgent.agentmain("rm bing.com file $outputFilePath")
89+
DcmAgent.agentmain("rm bing.com file ${outputFile.absolutePath}")
9390

9491
DnsCacheManipulator.getDnsCache("bing.com").shouldBeNull()
9592
}
@@ -150,7 +147,7 @@ class DcmAgentTests : AnnotationSpec() {
150147
fun test_agentmain_actionNeedMoreArgument() {
151148
DnsCacheManipulator.setDnsNegativeCachePolicy(1110)
152149

153-
DcmAgent.agentmain(" setNegativePolicy file $outputFilePath")
150+
DcmAgent.agentmain(" setNegativePolicy file ${outputFile.absolutePath}")
154151

155152
DnsCacheManipulator.getDnsNegativeCachePolicy() shouldBe 1110
156153

@@ -163,7 +160,7 @@ class DcmAgentTests : AnnotationSpec() {
163160
fun test_agentmain_actionTooMoreArgument() {
164161
DnsCacheManipulator.setDnsNegativeCachePolicy(1111)
165162

166-
DcmAgent.agentmain(" setNegativePolicy 737 HaHa file $outputFilePath")
163+
DcmAgent.agentmain(" setNegativePolicy 737 HaHa file ${outputFile.absolutePath}")
167164

168165
DnsCacheManipulator.getDnsNegativeCachePolicy() shouldBe 1111
169166

@@ -174,7 +171,7 @@ class DcmAgentTests : AnnotationSpec() {
174171

175172
@Test
176173
fun test_agentmain_unknownAction() {
177-
DcmAgent.agentmain(" unknownAction arg1 arg2 file $outputFilePath")
174+
DcmAgent.agentmain(" unknownAction arg1 arg2 file ${outputFile.absolutePath}")
178175

179176
val content = outputFile.readLines(UTF_8)
180177
content.first() shouldContain "No action in agent argument, do nothing!"

0 commit comments

Comments
 (0)