Skip to content

Commit

Permalink
Annotate some java.net classes, mostly related to cookies. (#108)
Browse files Browse the repository at this point in the history
Prompted by
google/xplat@18fd514.

Co-authored-by: Werner Dietl <[email protected]>
  • Loading branch information
cpovirk and wmdietl authored Dec 18, 2024
1 parent e07b421 commit e717491
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
8 changes: 6 additions & 2 deletions src/java.base/share/classes/java/net/CookieHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import java.util.List;
import java.io.IOException;

import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

/**
* A CookieHandler object provides a callback mechanism to hook up a
* HTTP state management policy implementation into the HTTP protocol
Expand All @@ -49,6 +52,7 @@
* @author Yingxian Wang
* @since 1.5
*/
@NullMarked
public abstract class CookieHandler {
/**
* Constructor for subclasses to call.
Expand All @@ -71,7 +75,7 @@ public CookieHandler() {}
* there is no system-wide cookie handler currently set.
* @see #setDefault(CookieHandler)
*/
public static synchronized CookieHandler getDefault() {
public static synchronized @Nullable CookieHandler getDefault() {
return cookieHandler;
}

Expand All @@ -84,7 +88,7 @@ public static synchronized CookieHandler getDefault() {
* {@code null} to unset.
* @see #getDefault()
*/
public static synchronized void setDefault(CookieHandler cHandler) {
public static synchronized void setDefault(@Nullable CookieHandler cHandler) {
cookieHandler = cHandler;
}

Expand Down
9 changes: 6 additions & 3 deletions src/java.base/share/classes/java/net/CookieManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.util.Comparator;
import java.io.IOException;
import sun.util.logging.PlatformLogger;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

/**
* CookieManager provides a concrete implementation of {@link CookieHandler},
Expand Down Expand Up @@ -114,6 +116,7 @@
* @author Edward Wang
* @since 1.6
*/
@NullMarked
public class CookieManager extends CookieHandler
{
/* ---------------- Fields -------------- */
Expand Down Expand Up @@ -149,8 +152,8 @@ public CookieManager() {
* if {@code null}, ACCEPT_ORIGINAL_SERVER will
* be used.
*/
public CookieManager(CookieStore store,
CookiePolicy cookiePolicy)
public CookieManager(@Nullable CookieStore store,
@Nullable CookiePolicy cookiePolicy)
{
// use default cookie policy if not specify one
policyCallback = (cookiePolicy == null) ? CookiePolicy.ACCEPT_ORIGINAL_SERVER
Expand All @@ -177,7 +180,7 @@ public CookieManager(CookieStore store,
* @param cookiePolicy the cookie policy. Can be {@code null}, which
* has no effects on current cookie policy.
*/
public void setCookiePolicy(CookiePolicy cookiePolicy) {
public void setCookiePolicy(@Nullable CookiePolicy cookiePolicy) {
if (cookiePolicy != null) policyCallback = cookiePolicy;
}

Expand Down
3 changes: 3 additions & 0 deletions src/java.base/share/classes/java/net/CookiePolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

package java.net;

import org.jspecify.annotations.NullMarked;

/**
* CookiePolicy implementations decide which cookies should be accepted
* and which should be rejected. Three pre-defined policy implementations
Expand All @@ -35,6 +37,7 @@
* @author Edward Wang
* @since 1.6
*/
@NullMarked
public interface CookiePolicy {
/**
* One pre-defined policy which accepts all cookies.
Expand Down
35 changes: 24 additions & 11 deletions src/java.base/share/classes/java/net/HttpCookie.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

package java.net;

import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.NullUnmarked;
import org.jspecify.annotations.Nullable;

import java.util.List;
Expand Down Expand Up @@ -60,6 +62,7 @@
* @author Edward Wang
* @since 1.6
*/
@NullMarked
public final class HttpCookie implements Cloneable {
// ---------------- Fields --------------

Expand Down Expand Up @@ -140,6 +143,7 @@ public final class HttpCookie implements Cloneable {
* @see #setValue
* @see #setVersion
*/
@NullUnmarked // TODO(cpovirk): Should `value` be @Nullable?
public HttpCookie(String name, String value) {
this(name, value, null /*header*/);
}
Expand Down Expand Up @@ -262,7 +266,7 @@ public boolean hasExpired() {
*
* @see #getComment
*/
public void setComment(String purpose) {
public void setComment(@Nullable String purpose) {
comment = purpose;
}

Expand All @@ -274,7 +278,7 @@ public void setComment(String purpose) {
*
* @see #setComment
*/
public String getComment() {
public @Nullable String getComment() {
return comment;
}

Expand All @@ -288,7 +292,7 @@ public String getComment() {
*
* @see #getCommentURL
*/
public void setCommentURL(String purpose) {
public void setCommentURL(@Nullable String purpose) {
commentURL = purpose;
}

Expand All @@ -301,7 +305,7 @@ public void setCommentURL(String purpose) {
*
* @see #setCommentURL
*/
public String getCommentURL() {
public @Nullable String getCommentURL() {
return commentURL;
}

Expand Down Expand Up @@ -339,7 +343,7 @@ public boolean getDiscard() {
*
* @see #getPortlist
*/
public void setPortlist(String ports) {
public void setPortlist(@Nullable String ports) {
portlist = ports;
}

Expand All @@ -350,7 +354,7 @@ public void setPortlist(String ports) {
*
* @see #setPortlist
*/
public String getPortlist() {
public @Nullable String getPortlist() {
return portlist;
}

Expand All @@ -370,7 +374,7 @@ public String getPortlist() {
*
* @see #getDomain
*/
public void setDomain(String pattern) {
public void setDomain(@Nullable String pattern) {
if (pattern != null)
domain = pattern.toLowerCase(Locale.ROOT);
else
Expand All @@ -385,7 +389,7 @@ public void setDomain(String pattern) {
*
* @see #setDomain
*/
public String getDomain() {
public @Nullable String getDomain() {
return domain;
}

Expand Down Expand Up @@ -442,7 +446,7 @@ public long getMaxAge() {
*
* @see #getPath
*/
public void setPath(String uri) {
public void setPath(@Nullable String uri) {
path = uri;
}

Expand All @@ -455,7 +459,7 @@ public void setPath(String uri) {
*
* @see #setPath
*/
public String getPath() {
public @Nullable String getPath() {
return path;
}

Expand Down Expand Up @@ -514,6 +518,7 @@ public String getName() {
*
* @see #getValue
*/
@NullUnmarked // TODO(cpovirk): Should `newValue` be @Nullable?
public void setValue(String newValue) {
value = newValue;
}
Expand All @@ -525,6 +530,7 @@ public void setValue(String newValue) {
*
* @see #setValue
*/
@NullUnmarked // TODO(cpovirk): Should the return type be @Nullable?
public String getValue() {
return value;
}
Expand Down Expand Up @@ -644,7 +650,14 @@ public void setHttpOnly(boolean httpOnly) {
*
* @return {@code true} if they domain-matches; {@code false} if not
*/
public static boolean domainMatches(String domain, String host) {
/*
* JSpecify: Passing null for either parameter seems likely to be at least suspicious. However,
* if we make the types non-null, we would change the null behavior under Kotlin from "return
* false" to "throw NullPointerException." And it's not clear whether any callers pass null in
* practice, even the one caller in the JDK, which passes cookie.getDomain() and uri.getHost(),
* both of which have nullable types.
*/
public static boolean domainMatches(@Nullable String domain, @Nullable String host) {
if (domain == null || host == null)
return false;

Expand Down
3 changes: 2 additions & 1 deletion src/java.base/share/classes/java/net/URISyntaxException.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package java.net;

import org.jspecify.annotations.NullMarked;

/**
* Checked exception thrown to indicate that a string could not be parsed as a
Expand All @@ -34,7 +35,7 @@
* @see URI
* @since 1.4
*/

@NullMarked
public class URISyntaxException
extends Exception
{
Expand Down

0 comments on commit e717491

Please sign in to comment.