Skip to content

Commit 3e10703

Browse files
authored
Bumped ErrorProne version and enabled NullAway for "uri" package (#1882)
* Bump error prone version * Enabled NullAway for uri package
1 parent de8d88b commit 3e10703

File tree

5 files changed

+71
-62
lines changed

5 files changed

+71
-62
lines changed

client/src/main/java/org/asynchttpclient/uri/Uri.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.asynchttpclient.uri;
1717

1818
import org.asynchttpclient.util.StringBuilderPool;
19+
import org.jetbrains.annotations.Nullable;
1920

2021
import java.net.URI;
2122
import java.net.URISyntaxException;
@@ -31,17 +32,17 @@ public class Uri {
3132
public static final String WS = "ws";
3233
public static final String WSS = "wss";
3334
private final String scheme;
34-
private final String userInfo;
35+
private final @Nullable String userInfo;
3536
private final String host;
3637
private final int port;
37-
private final String query;
38+
private final @Nullable String query;
3839
private final String path;
39-
private final String fragment;
40-
private String url;
40+
private final @Nullable String fragment;
41+
private @Nullable String url;
4142
private final boolean secured;
4243
private final boolean webSocket;
4344

44-
public Uri(String scheme, String userInfo, String host, int port, String path, String query, String fragment) {
45+
public Uri(String scheme, @Nullable String userInfo, String host, int port, String path, @Nullable String query, @Nullable String fragment) {
4546
this.scheme = assertNotEmpty(scheme, "scheme");
4647
this.userInfo = userInfo;
4748
this.host = assertNotEmpty(host, "host");
@@ -57,10 +58,8 @@ public static Uri create(String originalUrl) {
5758
return create(null, originalUrl);
5859
}
5960

60-
public static Uri create(Uri context, final String originalUrl) {
61-
UriParser parser = new UriParser();
62-
parser.parse(context, originalUrl);
63-
61+
public static Uri create(@Nullable Uri context, final String originalUrl) {
62+
UriParser parser = UriParser.parse(context, originalUrl);
6463
if (isEmpty(parser.scheme)) {
6564
throw new IllegalArgumentException(originalUrl + " could not be parsed into a proper Uri, missing scheme");
6665
}
@@ -71,15 +70,15 @@ public static Uri create(Uri context, final String originalUrl) {
7170
return new Uri(parser.scheme, parser.userInfo, parser.host, parser.port, parser.path, parser.query, parser.fragment);
7271
}
7372

74-
public String getQuery() {
73+
public @Nullable String getQuery() {
7574
return query;
7675
}
7776

7877
public String getPath() {
7978
return path;
8079
}
8180

82-
public String getUserInfo() {
81+
public @Nullable String getUserInfo() {
8382
return userInfo;
8483
}
8584

@@ -95,7 +94,7 @@ public String getHost() {
9594
return host;
9695
}
9796

98-
public String getFragment() {
97+
public @Nullable String getFragment() {
9998
return fragment;
10099
}
101100

@@ -203,7 +202,7 @@ public Uri withNewScheme(String newScheme) {
203202
return new Uri(newScheme, userInfo, host, port, path, query, fragment);
204203
}
205204

206-
public Uri withNewQuery(String newQuery) {
205+
public Uri withNewQuery(@Nullable String newQuery) {
207206
return new Uri(scheme, userInfo, host, port, path, newQuery, fragment);
208207
}
209208

client/src/main/java/org/asynchttpclient/uri/UriParser.java

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,29 @@
1515
*/
1616
package org.asynchttpclient.uri;
1717

18+
import org.jetbrains.annotations.Nullable;
19+
1820
import static org.asynchttpclient.util.Assertions.assertNotNull;
1921
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
2022

2123
final class UriParser {
2224

23-
public String scheme;
24-
public String host;
25+
public @Nullable String scheme;
26+
public @Nullable String host;
2527
public int port = -1;
26-
public String query;
27-
public String fragment;
28-
private String authority;
29-
public String path;
30-
public String userInfo;
28+
public @Nullable String query;
29+
public @Nullable String fragment;
30+
private @Nullable String authority;
31+
public String path = "";
32+
public @Nullable String userInfo;
3133

32-
private String originalUrl;
34+
private final String originalUrl;
3335
private int start, end, currentIndex;
3436

37+
private UriParser(final String originalUrl) {
38+
this.originalUrl = originalUrl;
39+
}
40+
3541
private void trimLeft() {
3642
while (start < end && originalUrl.charAt(start) <= ' ') {
3743
start++;
@@ -86,7 +92,7 @@ private void computeInitialScheme() {
8692
}
8793
}
8894

89-
private boolean overrideWithContext(Uri context) {
95+
private boolean overrideWithContext(@Nullable Uri context) {
9096

9197
boolean isRelative = false;
9298

@@ -126,7 +132,9 @@ private void trimFragment() {
126132
}
127133
}
128134

129-
private void inheritContextQuery(Uri context, boolean isRelative) {
135+
// isRelative can be true only when context is not null
136+
@SuppressWarnings("NullAway")
137+
private void inheritContextQuery(@Nullable Uri context, boolean isRelative) {
130138
// see RFC2396 5.2.2: query and fragment inheritance
131139
if (isRelative && currentIndex == end) {
132140
query = context.getQuery();
@@ -156,7 +164,7 @@ private boolean currentPositionStartsWith2Slashes() {
156164
return originalUrl.regionMatches(currentIndex, "//", 0, 2);
157165
}
158166

159-
private void computeAuthority() {
167+
private String computeAuthority() {
160168
int authorityEndPosition = findWithinCurrentRange('/');
161169
if (authorityEndPosition == -1) {
162170
authorityEndPosition = findWithinCurrentRange('?');
@@ -166,59 +174,60 @@ private void computeAuthority() {
166174
}
167175
host = authority = originalUrl.substring(currentIndex, authorityEndPosition);
168176
currentIndex = authorityEndPosition;
177+
return authority;
169178
}
170179

171-
private void computeUserInfo() {
172-
int atPosition = authority.indexOf('@');
180+
private void computeUserInfo(String nonNullAuthority) {
181+
int atPosition = nonNullAuthority.indexOf('@');
173182
if (atPosition != -1) {
174-
userInfo = authority.substring(0, atPosition);
175-
host = authority.substring(atPosition + 1);
183+
userInfo = nonNullAuthority.substring(0, atPosition);
184+
host = nonNullAuthority.substring(atPosition + 1);
176185
} else {
177186
userInfo = null;
178187
}
179188
}
180189

181-
private boolean isMaybeIPV6() {
190+
private boolean isMaybeIPV6(String nonNullHost) {
182191
// If the host is surrounded by [ and ] then its an IPv6
183192
// literal address as specified in RFC2732
184-
return host.length() > 0 && host.charAt(0) == '[';
193+
return nonNullHost.length() > 0 && nonNullHost.charAt(0) == '[';
185194
}
186195

187-
private void computeIPV6() {
188-
int positionAfterClosingSquareBrace = host.indexOf(']') + 1;
196+
private void computeIPV6(String nonNullHost) {
197+
int positionAfterClosingSquareBrace = nonNullHost.indexOf(']') + 1;
189198
if (positionAfterClosingSquareBrace > 1) {
190199

191200
port = -1;
192201

193-
if (host.length() > positionAfterClosingSquareBrace) {
194-
if (host.charAt(positionAfterClosingSquareBrace) == ':') {
202+
if (nonNullHost.length() > positionAfterClosingSquareBrace) {
203+
if (nonNullHost.charAt(positionAfterClosingSquareBrace) == ':') {
195204
// see RFC2396: port can be null
196205
int portPosition = positionAfterClosingSquareBrace + 1;
197-
if (host.length() > portPosition) {
198-
port = Integer.parseInt(host.substring(portPosition));
206+
if (nonNullHost.length() > portPosition) {
207+
port = Integer.parseInt(nonNullHost.substring(portPosition));
199208
}
200209
} else {
201210
throw new IllegalArgumentException("Invalid authority field: " + authority);
202211
}
203212
}
204213

205-
host = host.substring(0, positionAfterClosingSquareBrace);
214+
host = nonNullHost.substring(0, positionAfterClosingSquareBrace);
206215

207216
} else {
208217
throw new IllegalArgumentException("Invalid authority field: " + authority);
209218
}
210219
}
211220

212-
private void computeRegularHostPort() {
213-
int colonPosition = host.indexOf(':');
221+
private void computeRegularHostPort(String nonNullHost) {
222+
int colonPosition = nonNullHost.indexOf(':');
214223
port = -1;
215224
if (colonPosition >= 0) {
216225
// see RFC2396: port can be null
217226
int portPosition = colonPosition + 1;
218-
if (host.length() > portPosition) {
219-
port = Integer.parseInt(host.substring(portPosition));
227+
if (nonNullHost.length() > portPosition) {
228+
port = Integer.parseInt(nonNullHost.substring(portPosition));
220229
}
221-
host = host.substring(0, colonPosition);
230+
host = nonNullHost.substring(0, colonPosition);
222231
}
223232
}
224233

@@ -293,14 +302,15 @@ private void parseAuthority() {
293302
if (!currentPositionStartsWith4Slashes() && currentPositionStartsWith2Slashes()) {
294303
currentIndex += 2;
295304

296-
computeAuthority();
297-
computeUserInfo();
305+
String nonNullAuthority = computeAuthority();
306+
computeUserInfo(nonNullAuthority);
298307

299308
if (host != null) {
300-
if (isMaybeIPV6()) {
301-
computeIPV6();
309+
String nonNullHost = host;
310+
if (isMaybeIPV6(nonNullHost)) {
311+
computeIPV6(nonNullHost);
302312
} else {
303-
computeRegularHostPort();
313+
computeRegularHostPort(nonNullHost);
304314
}
305315
}
306316

@@ -336,17 +346,12 @@ private void computePath(boolean queryOnly) {
336346
// Parse the file path if any
337347
if (currentIndex < end) {
338348
computeRegularPath();
339-
} else if (queryOnly && path != null) {
349+
} else if (queryOnly) {
340350
computeQueryOnlyPath();
341-
} else if (path == null) {
342-
path = "";
343351
}
344352
}
345353

346-
public void parse(Uri context, final String originalUrl) {
347-
348-
assertNotNull(originalUrl, "originalUrl");
349-
this.originalUrl = originalUrl;
354+
private void parse(@Nullable Uri context) {
350355
end = originalUrl.length();
351356

352357
trimLeft();
@@ -362,4 +367,11 @@ public void parse(Uri context, final String originalUrl) {
362367
parseAuthority();
363368
computePath(queryOnly);
364369
}
370+
371+
public static UriParser parse(@Nullable Uri context, final String originalUrl) {
372+
assertNotNull(originalUrl, "originalUrl");
373+
final UriParser parser = new UriParser(originalUrl);
374+
parser.parse(context);
375+
return parser;
376+
}
365377
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public static boolean isNonEmpty(@Nullable String string) {
3232
return !isEmpty(string);
3333
}
3434

35+
@Contract(value = "null -> true", pure = true)
3536
public static boolean isEmpty(@Nullable String string) {
3637
return string == null || string.isEmpty();
3738
}

client/src/test/java/org/asynchttpclient/uri/UriParserTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@ private static void assertUriEquals(UriParser parser, URI uri) {
3333
}
3434

3535
private static void validateAgainstAbsoluteURI(String url) {
36-
UriParser parser = new UriParser();
37-
parser.parse(null, url);
36+
final UriParser parser = UriParser.parse(null, url);
3837
assertUriEquals(parser, URI.create(url));
3938
}
4039

4140
private static void validateAgainstRelativeURI(Uri uriContext, String urlContext, String url) {
42-
UriParser parser = new UriParser();
43-
parser.parse(uriContext, url);
41+
final UriParser parser = UriParser.parse(uriContext, url);
4442
assertUriEquals(parser, URI.create(urlContext).resolve(URI.create(url)));
4543
}
4644

@@ -56,9 +54,8 @@ public void testFragmentTryingToTrickAuthorityAsBasicAuthCredentials() {
5654

5755
@RepeatedIfExceptionsTest(repeats = 5)
5856
public void testUrlHasLeadingAndTrailingWhiteSpace() {
59-
UriParser parser = new UriParser();
6057
String url = " http://user@example.com:8080/test?q=1 ";
61-
parser.parse(null, url);
58+
final UriParser parser = UriParser.parse(null, url);
6259
assertUriEquals(parser, URI.create(url.trim()));
6360
}
6461

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@
233233
-Xep:NullOptional:ERROR
234234
-XepExcludedPaths:.*/src/test/java/.*
235235
-XepOpt:NullAway:AnnotatedPackages=org.asynchttpclient
236-
-XepOpt:NullAway:UnannotatedSubPackages=org.asynchttpclient.netty,org.asynchttpclient.ntlm,org.asynchttpclient.request,org.asynchttpclient.spnego,org.asynchttpclient.uri,org.asynchttpclient.util,org.asynchttpclient.webdav,org.asynchttpclient.ws
236+
-XepOpt:NullAway:UnannotatedSubPackages=org.asynchttpclient.netty,org.asynchttpclient.ntlm,org.asynchttpclient.request,org.asynchttpclient.spnego,org.asynchttpclient.util,org.asynchttpclient.webdav,org.asynchttpclient.ws
237237
-XepOpt:NullAway:AcknowledgeRestrictiveAnnotations=true
238238
-Xep:NullAway:ERROR
239239
</arg>
@@ -242,7 +242,7 @@
242242
<path>
243243
<groupId>com.google.errorprone</groupId>
244244
<artifactId>error_prone_core</artifactId>
245-
<version>2.18.0</version>
245+
<version>2.19.1</version>
246246
</path>
247247
<path>
248248
<groupId>com.uber.nullaway</groupId>

0 commit comments

Comments
 (0)