Skip to content

Commit 24d01e7

Browse files
mk868diemol
authored andcommitted
[java] Nullness annotations for Cookie and Platform (SeleniumHQ#15062)
Co-authored-by: Diego Molina <[email protected]>
1 parent c88bf9d commit 24d01e7

File tree

2 files changed

+73
-57
lines changed

2 files changed

+73
-57
lines changed

java/src/org/openqa/selenium/Cookie.java

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,21 @@
2424
import java.util.Objects;
2525
import java.util.TimeZone;
2626
import java.util.TreeMap;
27+
import org.jspecify.annotations.NullMarked;
28+
import org.jspecify.annotations.Nullable;
2729

30+
@NullMarked
2831
public class Cookie implements Serializable {
2932
private static final long serialVersionUID = 4115876353625612383L;
3033

3134
private final String name;
3235
private final String value;
3336
private final String path;
34-
private final String domain;
35-
private final Date expiry;
37+
private final @Nullable String domain;
38+
private final @Nullable Date expiry;
3639
private final boolean isSecure;
3740
private final boolean isHttpOnly;
38-
private final String sameSite;
41+
private final @Nullable String sameSite;
3942

4043
/**
4144
* Creates an insecure non-httpOnly cookie with no domain specified.
@@ -47,7 +50,7 @@ public class Cookie implements Serializable {
4750
* @param expiry The cookie's expiration date; may be null.
4851
* @see #Cookie(String, String, String, String, Date)
4952
*/
50-
public Cookie(String name, String value, String path, Date expiry) {
53+
public Cookie(String name, String value, @Nullable String path, @Nullable Date expiry) {
5154
this(name, value, null, path, expiry);
5255
}
5356

@@ -62,7 +65,12 @@ public Cookie(String name, String value, String path, Date expiry) {
6265
* @param expiry The cookie's expiration date; may be null.
6366
* @see #Cookie(String, String, String, String, Date, boolean)
6467
*/
65-
public Cookie(String name, String value, String domain, String path, Date expiry) {
68+
public Cookie(
69+
String name,
70+
String value,
71+
@Nullable String domain,
72+
@Nullable String path,
73+
@Nullable Date expiry) {
6674
this(name, value, domain, path, expiry, false);
6775
}
6876

@@ -78,7 +86,12 @@ public Cookie(String name, String value, String domain, String path, Date expiry
7886
* @param isSecure Whether this cookie requires a secure connection.
7987
*/
8088
public Cookie(
81-
String name, String value, String domain, String path, Date expiry, boolean isSecure) {
89+
String name,
90+
String value,
91+
@Nullable String domain,
92+
@Nullable String path,
93+
@Nullable Date expiry,
94+
boolean isSecure) {
8295
this(name, value, domain, path, expiry, isSecure, false);
8396
}
8497

@@ -97,9 +110,9 @@ public Cookie(
97110
public Cookie(
98111
String name,
99112
String value,
100-
String domain,
101-
String path,
102-
Date expiry,
113+
@Nullable String domain,
114+
@Nullable String path,
115+
@Nullable Date expiry,
103116
boolean isSecure,
104117
boolean isHttpOnly) {
105118
this(name, value, domain, path, expiry, isSecure, isHttpOnly, null);
@@ -121,12 +134,12 @@ public Cookie(
121134
public Cookie(
122135
String name,
123136
String value,
124-
String domain,
125-
String path,
126-
Date expiry,
137+
@Nullable String domain,
138+
@Nullable String path,
139+
@Nullable Date expiry,
127140
boolean isSecure,
128141
boolean isHttpOnly,
129-
String sameSite) {
142+
@Nullable String sameSite) {
130143
this.name = name;
131144
this.value = value;
132145
this.path = path == null || path.isEmpty() ? "/" : path;
@@ -174,7 +187,7 @@ public String getValue() {
174187
return value;
175188
}
176189

177-
public String getDomain() {
190+
public @Nullable String getDomain() {
178191
return domain;
179192
}
180193

@@ -190,15 +203,15 @@ public boolean isHttpOnly() {
190203
return isHttpOnly;
191204
}
192205

193-
public Date getExpiry() {
206+
public @Nullable Date getExpiry() {
194207
return expiry == null ? null : new Date(expiry.getTime());
195208
}
196209

197-
public String getSameSite() {
210+
public @Nullable String getSameSite() {
198211
return sameSite;
199212
}
200213

201-
private static String stripPort(String domain) {
214+
private static @Nullable String stripPort(@Nullable String domain) {
202215
return (domain == null) ? null : domain.split(":")[0];
203216
}
204217

@@ -270,7 +283,7 @@ public String toString() {
270283

271284
/** Two cookies are equal if the name and value match */
272285
@Override
273-
public boolean equals(Object o) {
286+
public boolean equals(@Nullable Object o) {
274287
if (this == o) {
275288
return true;
276289
}
@@ -295,29 +308,29 @@ public static class Builder {
295308

296309
private final String name;
297310
private final String value;
298-
private String path;
299-
private String domain;
300-
private Date expiry;
311+
private @Nullable String path;
312+
private @Nullable String domain;
313+
private @Nullable Date expiry;
301314
private boolean secure;
302315
private boolean httpOnly;
303-
private String sameSite;
316+
private @Nullable String sameSite;
304317

305318
public Builder(String name, String value) {
306319
this.name = name;
307320
this.value = value;
308321
}
309322

310-
public Builder domain(String host) {
323+
public Builder domain(@Nullable String host) {
311324
this.domain = stripPort(host);
312325
return this;
313326
}
314327

315-
public Builder path(String path) {
328+
public Builder path(@Nullable String path) {
316329
this.path = path;
317330
return this;
318331
}
319332

320-
public Builder expiresOn(Date expiry) {
333+
public Builder expiresOn(@Nullable Date expiry) {
321334
this.expiry = expiry == null ? null : new Date(expiry.getTime());
322335
return this;
323336
}
@@ -332,7 +345,7 @@ public Builder isHttpOnly(boolean httpOnly) {
332345
return this;
333346
}
334347

335-
public Builder sameSite(String sameSite) {
348+
public Builder sameSite(@Nullable String sameSite) {
336349
this.sameSite = sameSite;
337350
return this;
338351
}

0 commit comments

Comments
 (0)