Skip to content

[java] Nullness annotations for Cookie and Platform #15062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 39 additions & 26 deletions java/src/org/openqa/selenium/Cookie.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,21 @@
import java.util.Objects;
import java.util.TimeZone;
import java.util.TreeMap;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
public class Cookie implements Serializable {
private static final long serialVersionUID = 4115876353625612383L;

private final String name;
private final String value;
private final String path;
private final String domain;
private final Date expiry;
private final @Nullable String domain;
private final @Nullable Date expiry;
private final boolean isSecure;
private final boolean isHttpOnly;
private final String sameSite;
private final @Nullable String sameSite;

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

Expand All @@ -62,7 +65,12 @@ public Cookie(String name, String value, String path, Date expiry) {
* @param expiry The cookie's expiration date; may be null.
* @see #Cookie(String, String, String, String, Date, boolean)
*/
public Cookie(String name, String value, String domain, String path, Date expiry) {
public Cookie(
String name,
String value,
@Nullable String domain,
@Nullable String path,
@Nullable Date expiry) {
this(name, value, domain, path, expiry, false);
}

Expand All @@ -78,7 +86,12 @@ public Cookie(String name, String value, String domain, String path, Date expiry
* @param isSecure Whether this cookie requires a secure connection.
*/
public Cookie(
String name, String value, String domain, String path, Date expiry, boolean isSecure) {
String name,
String value,
@Nullable String domain,
@Nullable String path,
@Nullable Date expiry,
boolean isSecure) {
this(name, value, domain, path, expiry, isSecure, false);
}

Expand All @@ -97,9 +110,9 @@ public Cookie(
public Cookie(
String name,
String value,
String domain,
String path,
Date expiry,
@Nullable String domain,
@Nullable String path,
@Nullable Date expiry,
boolean isSecure,
boolean isHttpOnly) {
this(name, value, domain, path, expiry, isSecure, isHttpOnly, null);
Expand All @@ -121,12 +134,12 @@ public Cookie(
public Cookie(
String name,
String value,
String domain,
String path,
Date expiry,
@Nullable String domain,
@Nullable String path,
@Nullable Date expiry,
boolean isSecure,
boolean isHttpOnly,
String sameSite) {
@Nullable String sameSite) {
this.name = name;
this.value = value;
this.path = path == null || path.isEmpty() ? "/" : path;
Expand Down Expand Up @@ -174,7 +187,7 @@ public String getValue() {
return value;
}

public String getDomain() {
public @Nullable String getDomain() {
return domain;
}

Expand All @@ -190,15 +203,15 @@ public boolean isHttpOnly() {
return isHttpOnly;
}

public Date getExpiry() {
public @Nullable Date getExpiry() {
return expiry == null ? null : new Date(expiry.getTime());
}

public String getSameSite() {
public @Nullable String getSameSite() {
return sameSite;
}

private static String stripPort(String domain) {
private static @Nullable String stripPort(@Nullable String domain) {
return (domain == null) ? null : domain.split(":")[0];
}

Expand Down Expand Up @@ -270,7 +283,7 @@ public String toString() {

/** Two cookies are equal if the name and value match */
@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
Expand All @@ -295,29 +308,29 @@ public static class Builder {

private final String name;
private final String value;
private String path;
private String domain;
private Date expiry;
private @Nullable String path;
private @Nullable String domain;
private @Nullable Date expiry;
private boolean secure;
private boolean httpOnly;
private String sameSite;
private @Nullable String sameSite;

public Builder(String name, String value) {
this.name = name;
this.value = value;
}

public Builder domain(String host) {
public Builder domain(@Nullable String host) {
this.domain = stripPort(host);
return this;
}

public Builder path(String path) {
public Builder path(@Nullable String path) {
this.path = path;
return this;
}

public Builder expiresOn(Date expiry) {
public Builder expiresOn(@Nullable Date expiry) {
this.expiry = expiry == null ? null : new Date(expiry.getTime());
return this;
}
Expand All @@ -332,7 +345,7 @@ public Builder isHttpOnly(boolean httpOnly) {
return this;
}

public Builder sameSite(String sameSite) {
public Builder sameSite(@Nullable String sameSite) {
this.sameSite = sameSite;
return this;
}
Expand Down
Loading