Skip to content

Commit 8da2b18

Browse files
authored
Added nullable annotations to org.asynchttpclient.config, org.asynchttpclient.cookie and org.asynchttpclient.exception packages (#1874)
1 parent f25d32b commit 8da2b18

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed

client/src/main/java/org/asynchttpclient/config/AsyncHttpClientConfigDefaults.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.asynchttpclient.config;
1717

18+
import org.jetbrains.annotations.Nullable;
19+
1820
import java.io.IOException;
1921
import java.io.InputStream;
2022
import java.time.Duration;
@@ -157,11 +159,11 @@ public static String defaultUserAgent() {
157159
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getString(ASYNC_CLIENT_CONFIG_ROOT + USER_AGENT_CONFIG);
158160
}
159161

160-
public static String[] defaultEnabledProtocols() {
162+
public static @Nullable String[] defaultEnabledProtocols() {
161163
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getStringArray(ASYNC_CLIENT_CONFIG_ROOT + ENABLED_PROTOCOLS_CONFIG);
162164
}
163165

164-
public static String[] defaultEnabledCipherSuites() {
166+
public static @Nullable String[] defaultEnabledCipherSuites() {
165167
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getStringArray(ASYNC_CLIENT_CONFIG_ROOT + ENABLED_CIPHER_SUITES_CONFIG);
166168
}
167169

client/src/main/java/org/asynchttpclient/config/AsyncHttpClientConfigHelper.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.asynchttpclient.config;
1717

18+
import org.jetbrains.annotations.Nullable;
19+
1820
import java.io.IOException;
1921
import java.io.InputStream;
2022
import java.time.Duration;
@@ -23,7 +25,7 @@
2325

2426
public final class AsyncHttpClientConfigHelper {
2527

26-
private static volatile Config config;
28+
private static volatile @Nullable Config config;
2729

2830
private AsyncHttpClientConfigHelper() {
2931
}
@@ -41,8 +43,9 @@ public static Config getAsyncHttpClientConfig() {
4143
* getAsyncHttpClientConfig() to get the new property values.
4244
*/
4345
public static void reloadProperties() {
44-
if (config != null) {
45-
config.reload();
46+
final Config localInstance = config;
47+
if (localInstance != null) {
48+
localInstance.reload();
4649
}
4750
}
4851

@@ -90,7 +93,7 @@ public String getString(String key) {
9093
});
9194
}
9295

93-
public String[] getStringArray(String key) {
96+
public @Nullable String[] getStringArray(String key) {
9497
String s = getString(key);
9598
s = s.trim();
9699
if (s.isEmpty()) {

client/src/main/java/org/asynchttpclient/cookie/ThreadSafeCookieStore.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.asynchttpclient.uri.Uri;
2020
import org.asynchttpclient.util.Assertions;
2121
import org.asynchttpclient.util.MiscUtils;
22+
import org.jetbrains.annotations.NotNull;
23+
import org.jetbrains.annotations.Nullable;
2224

2325
import java.util.AbstractMap;
2426
import java.util.ArrayList;
@@ -119,7 +121,7 @@ private static String requestPath(Uri requestUri) {
119121

120122
// rfc6265#section-5.2.3
121123
// Let cookie-domain be the attribute-value without the leading %x2E (".") character.
122-
private static AbstractMap.SimpleEntry<String, Boolean> cookieDomain(String cookieDomain, String requestDomain) {
124+
private static AbstractMap.SimpleEntry<String, Boolean> cookieDomain(@Nullable String cookieDomain, String requestDomain) {
123125
if (cookieDomain != null) {
124126
String normalizedCookieDomain = cookieDomain.toLowerCase();
125127
return new AbstractMap.SimpleEntry<>(
@@ -132,7 +134,7 @@ private static AbstractMap.SimpleEntry<String, Boolean> cookieDomain(String cook
132134
}
133135

134136
// rfc6265#section-5.2.4
135-
private static String cookiePath(String rawCookiePath, String requestPath) {
137+
private static String cookiePath(@Nullable String rawCookiePath, String requestPath) {
136138
if (MiscUtils.isNonEmpty(rawCookiePath) && rawCookiePath.charAt(0) == '/') {
137139
return rawCookiePath;
138140
} else {
@@ -242,7 +244,7 @@ private static class CookieKey implements Comparable<CookieKey> {
242244
}
243245

244246
@Override
245-
public int compareTo(CookieKey o) {
247+
public int compareTo(@NotNull CookieKey o) {
246248
Assertions.assertNotNull(o, "Parameter can't be null");
247249
int result;
248250
if ((result = name.compareTo(o.name)) == 0) {
@@ -291,7 +293,7 @@ public String toString() {
291293
public static final class DomainUtils {
292294
private static final char DOT = '.';
293295

294-
public static String getSubDomain(String domain) {
296+
public static @Nullable String getSubDomain(@Nullable String domain) {
295297
if (domain == null || domain.isEmpty()) {
296298
return null;
297299
}

client/src/main/java/org/asynchttpclient/util/MiscUtils.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
*/
1313
package org.asynchttpclient.util;
1414

15+
import org.jetbrains.annotations.Contract;
16+
import org.jetbrains.annotations.Nullable;
17+
1518
import java.io.Closeable;
1619
import java.io.IOException;
1720
import java.util.Collection;
@@ -23,35 +26,37 @@ private MiscUtils() {
2326
// Prevent outside initialization
2427
}
2528

26-
public static boolean isNonEmpty(String string) {
29+
// NullAway is not powerful enough to recognise that if the values has passed the check, it's not null
30+
@Contract(value = "null -> false", pure = true)
31+
public static boolean isNonEmpty(@Nullable String string) {
2732
return !isEmpty(string);
2833
}
2934

30-
public static boolean isEmpty(String string) {
35+
public static boolean isEmpty(@Nullable String string) {
3136
return string == null || string.isEmpty();
3237
}
3338

34-
public static boolean isNonEmpty(Object[] array) {
39+
public static boolean isNonEmpty(@Nullable Object[] array) {
3540
return array != null && array.length != 0;
3641
}
3742

38-
public static boolean isNonEmpty(byte[] array) {
43+
public static boolean isNonEmpty(byte @Nullable [] array) {
3944
return array != null && array.length != 0;
4045
}
4146

42-
public static boolean isNonEmpty(Collection<?> collection) {
47+
public static boolean isNonEmpty(@Nullable Collection<?> collection) {
4348
return collection != null && !collection.isEmpty();
4449
}
4550

46-
public static boolean isNonEmpty(Map<?, ?> map) {
51+
public static boolean isNonEmpty(@Nullable Map<?, ?> map) {
4752
return map != null && !map.isEmpty();
4853
}
4954

50-
public static <T> T withDefault(T value, T def) {
55+
public static <T> T withDefault(@Nullable T value, T def) {
5156
return value == null ? def : value;
5257
}
5358

54-
public static void closeSilently(Closeable closeable) {
59+
public static void closeSilently(@Nullable Closeable closeable) {
5560
if (closeable != null) {
5661
try {
5762
closeable.close();

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@
231231
-Xep:NullablePrimitive:ERROR
232232
-Xep:NullOptional:ERROR
233233
-XepExcludedPaths:.*/src/test/java/.*
234-
-XepOpt:NullAway:AnnotatedPackages=org.asynchttpclient.channel
234+
-XepOpt:NullAway:AnnotatedPackages=org.asynchttpclient.channel,org.asynchttpclient.config,org.asynchttpclient.cookie,org.asynchttpclient.exception
235235
-XepOpt:NullAway:AcknowledgeRestrictiveAnnotations=true
236236
-Xep:NullAway:ERROR
237237
</arg>

0 commit comments

Comments
 (0)