Skip to content

Commit e717491

Browse files
cpovirkwmdietl
andauthored
Annotate some java.net classes, mostly related to cookies. (#108)
Prompted by google/xplat@18fd514. Co-authored-by: Werner Dietl <[email protected]>
1 parent e07b421 commit e717491

File tree

5 files changed

+41
-17
lines changed

5 files changed

+41
-17
lines changed

src/java.base/share/classes/java/net/CookieHandler.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
import java.util.List;
3030
import java.io.IOException;
3131

32+
import org.jspecify.annotations.NullMarked;
33+
import org.jspecify.annotations.Nullable;
34+
3235
/**
3336
* A CookieHandler object provides a callback mechanism to hook up a
3437
* HTTP state management policy implementation into the HTTP protocol
@@ -49,6 +52,7 @@
4952
* @author Yingxian Wang
5053
* @since 1.5
5154
*/
55+
@NullMarked
5256
public abstract class CookieHandler {
5357
/**
5458
* Constructor for subclasses to call.
@@ -71,7 +75,7 @@ public CookieHandler() {}
7175
* there is no system-wide cookie handler currently set.
7276
* @see #setDefault(CookieHandler)
7377
*/
74-
public static synchronized CookieHandler getDefault() {
78+
public static synchronized @Nullable CookieHandler getDefault() {
7579
return cookieHandler;
7680
}
7781

@@ -84,7 +88,7 @@ public static synchronized CookieHandler getDefault() {
8488
* {@code null} to unset.
8589
* @see #getDefault()
8690
*/
87-
public static synchronized void setDefault(CookieHandler cHandler) {
91+
public static synchronized void setDefault(@Nullable CookieHandler cHandler) {
8892
cookieHandler = cHandler;
8993
}
9094

src/java.base/share/classes/java/net/CookieManager.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.util.Comparator;
3131
import java.io.IOException;
3232
import sun.util.logging.PlatformLogger;
33+
import org.jspecify.annotations.NullMarked;
34+
import org.jspecify.annotations.Nullable;
3335

3436
/**
3537
* CookieManager provides a concrete implementation of {@link CookieHandler},
@@ -114,6 +116,7 @@
114116
* @author Edward Wang
115117
* @since 1.6
116118
*/
119+
@NullMarked
117120
public class CookieManager extends CookieHandler
118121
{
119122
/* ---------------- Fields -------------- */
@@ -149,8 +152,8 @@ public CookieManager() {
149152
* if {@code null}, ACCEPT_ORIGINAL_SERVER will
150153
* be used.
151154
*/
152-
public CookieManager(CookieStore store,
153-
CookiePolicy cookiePolicy)
155+
public CookieManager(@Nullable CookieStore store,
156+
@Nullable CookiePolicy cookiePolicy)
154157
{
155158
// use default cookie policy if not specify one
156159
policyCallback = (cookiePolicy == null) ? CookiePolicy.ACCEPT_ORIGINAL_SERVER
@@ -177,7 +180,7 @@ public CookieManager(CookieStore store,
177180
* @param cookiePolicy the cookie policy. Can be {@code null}, which
178181
* has no effects on current cookie policy.
179182
*/
180-
public void setCookiePolicy(CookiePolicy cookiePolicy) {
183+
public void setCookiePolicy(@Nullable CookiePolicy cookiePolicy) {
181184
if (cookiePolicy != null) policyCallback = cookiePolicy;
182185
}
183186

src/java.base/share/classes/java/net/CookiePolicy.java

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
package java.net;
2727

28+
import org.jspecify.annotations.NullMarked;
29+
2830
/**
2931
* CookiePolicy implementations decide which cookies should be accepted
3032
* and which should be rejected. Three pre-defined policy implementations
@@ -35,6 +37,7 @@
3537
* @author Edward Wang
3638
* @since 1.6
3739
*/
40+
@NullMarked
3841
public interface CookiePolicy {
3942
/**
4043
* One pre-defined policy which accepts all cookies.

src/java.base/share/classes/java/net/HttpCookie.java

+24-11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
package java.net;
2727

28+
import org.jspecify.annotations.NullMarked;
29+
import org.jspecify.annotations.NullUnmarked;
2830
import org.jspecify.annotations.Nullable;
2931

3032
import java.util.List;
@@ -60,6 +62,7 @@
6062
* @author Edward Wang
6163
* @since 1.6
6264
*/
65+
@NullMarked
6366
public final class HttpCookie implements Cloneable {
6467
// ---------------- Fields --------------
6568

@@ -140,6 +143,7 @@ public final class HttpCookie implements Cloneable {
140143
* @see #setValue
141144
* @see #setVersion
142145
*/
146+
@NullUnmarked // TODO(cpovirk): Should `value` be @Nullable?
143147
public HttpCookie(String name, String value) {
144148
this(name, value, null /*header*/);
145149
}
@@ -262,7 +266,7 @@ public boolean hasExpired() {
262266
*
263267
* @see #getComment
264268
*/
265-
public void setComment(String purpose) {
269+
public void setComment(@Nullable String purpose) {
266270
comment = purpose;
267271
}
268272

@@ -274,7 +278,7 @@ public void setComment(String purpose) {
274278
*
275279
* @see #setComment
276280
*/
277-
public String getComment() {
281+
public @Nullable String getComment() {
278282
return comment;
279283
}
280284

@@ -288,7 +292,7 @@ public String getComment() {
288292
*
289293
* @see #getCommentURL
290294
*/
291-
public void setCommentURL(String purpose) {
295+
public void setCommentURL(@Nullable String purpose) {
292296
commentURL = purpose;
293297
}
294298

@@ -301,7 +305,7 @@ public void setCommentURL(String purpose) {
301305
*
302306
* @see #setCommentURL
303307
*/
304-
public String getCommentURL() {
308+
public @Nullable String getCommentURL() {
305309
return commentURL;
306310
}
307311

@@ -339,7 +343,7 @@ public boolean getDiscard() {
339343
*
340344
* @see #getPortlist
341345
*/
342-
public void setPortlist(String ports) {
346+
public void setPortlist(@Nullable String ports) {
343347
portlist = ports;
344348
}
345349

@@ -350,7 +354,7 @@ public void setPortlist(String ports) {
350354
*
351355
* @see #setPortlist
352356
*/
353-
public String getPortlist() {
357+
public @Nullable String getPortlist() {
354358
return portlist;
355359
}
356360

@@ -370,7 +374,7 @@ public String getPortlist() {
370374
*
371375
* @see #getDomain
372376
*/
373-
public void setDomain(String pattern) {
377+
public void setDomain(@Nullable String pattern) {
374378
if (pattern != null)
375379
domain = pattern.toLowerCase(Locale.ROOT);
376380
else
@@ -385,7 +389,7 @@ public void setDomain(String pattern) {
385389
*
386390
* @see #setDomain
387391
*/
388-
public String getDomain() {
392+
public @Nullable String getDomain() {
389393
return domain;
390394
}
391395

@@ -442,7 +446,7 @@ public long getMaxAge() {
442446
*
443447
* @see #getPath
444448
*/
445-
public void setPath(String uri) {
449+
public void setPath(@Nullable String uri) {
446450
path = uri;
447451
}
448452

@@ -455,7 +459,7 @@ public void setPath(String uri) {
455459
*
456460
* @see #setPath
457461
*/
458-
public String getPath() {
462+
public @Nullable String getPath() {
459463
return path;
460464
}
461465

@@ -514,6 +518,7 @@ public String getName() {
514518
*
515519
* @see #getValue
516520
*/
521+
@NullUnmarked // TODO(cpovirk): Should `newValue` be @Nullable?
517522
public void setValue(String newValue) {
518523
value = newValue;
519524
}
@@ -525,6 +530,7 @@ public void setValue(String newValue) {
525530
*
526531
* @see #setValue
527532
*/
533+
@NullUnmarked // TODO(cpovirk): Should the return type be @Nullable?
528534
public String getValue() {
529535
return value;
530536
}
@@ -644,7 +650,14 @@ public void setHttpOnly(boolean httpOnly) {
644650
*
645651
* @return {@code true} if they domain-matches; {@code false} if not
646652
*/
647-
public static boolean domainMatches(String domain, String host) {
653+
/*
654+
* JSpecify: Passing null for either parameter seems likely to be at least suspicious. However,
655+
* if we make the types non-null, we would change the null behavior under Kotlin from "return
656+
* false" to "throw NullPointerException." And it's not clear whether any callers pass null in
657+
* practice, even the one caller in the JDK, which passes cookie.getDomain() and uri.getHost(),
658+
* both of which have nullable types.
659+
*/
660+
public static boolean domainMatches(@Nullable String domain, @Nullable String host) {
648661
if (domain == null || host == null)
649662
return false;
650663

src/java.base/share/classes/java/net/URISyntaxException.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
package java.net;
2727

28+
import org.jspecify.annotations.NullMarked;
2829

2930
/**
3031
* Checked exception thrown to indicate that a string could not be parsed as a
@@ -34,7 +35,7 @@
3435
* @see URI
3536
* @since 1.4
3637
*/
37-
38+
@NullMarked
3839
public class URISyntaxException
3940
extends Exception
4041
{

0 commit comments

Comments
 (0)