Skip to content

Commit f6f24d4

Browse files
authored
feat(auth): add JSpecify Null annotations to Auth (#13842)
## Summary This PR migrates the Google Auth Library for Java (`google-auth-library-java`) to use JSpecify 1.0.0 nullability annotations (`@NullMarked` and `@Nullable`), replacing the previous `javax.annotation` references. ## Key Changes ### JSpecify 1.0.0 Annotation Adoption in Auth: - **NullMarked Semantics**: Applied `@NullMarked` at the class and package levels across core auth classes (in `credentials`, `oauth2_http`, `appengine`, and `cab-token-generator` modules) to define non-null by default semantics. - **Annotation Replacement**: Replaced imports of `javax.annotation.Nullable` with `org.jspecify.annotations.Nullable`. - **Dependency Management**: Updated Maven `pom.xml` files and Bazel `BUILD.bazel` configurations to declare and manage the `org.jspecify:jspecify` dependency. ## Verification Results - https://docs.google.com/document/d/1YSg2toS7wXT6gC8whKAxx71_BFkfPVOX8a3s2OlZ25E/edit?resourcekey=0-g0wyO_jvBgHigUg6Pjo1CA&tab=t.pv38dpfp09db
1 parent ddf9add commit f6f24d4

111 files changed

Lines changed: 389 additions & 129 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

google-auth-library-java/appengine/java/com/google/auth/appengine/AppEngineCredentials.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,16 @@
4848
import java.util.Collection;
4949
import java.util.Date;
5050
import java.util.Objects;
51+
import org.jspecify.annotations.NullMarked;
52+
import org.jspecify.annotations.Nullable;
5153

5254
/**
5355
* OAuth2 credentials representing the built-in service account for Google App Engine. You should
5456
* only use this class if you are running on AppEngine and are using urlfetch.
5557
*
5658
* <p>Fetches access tokens from the App Identity service.
5759
*/
60+
@NullMarked
5861
public class AppEngineCredentials extends GoogleCredentials implements ServiceAccountSigner {
5962

6063
private static final long serialVersionUID = -2627708355455064660L;
@@ -122,7 +125,7 @@ public String toString() {
122125
}
123126

124127
@Override
125-
public boolean equals(Object obj) {
128+
public boolean equals(@Nullable Object obj) {
126129
if (!(obj instanceof AppEngineCredentials)) {
127130
return false;
128131
}

google-auth-library-java/appengine/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,9 @@
9393
<artifactId>error_prone_annotations</artifactId>
9494
<scope>compile</scope>
9595
</dependency>
96+
<dependency>
97+
<groupId>org.jspecify</groupId>
98+
<artifactId>jspecify</artifactId>
99+
</dependency>
96100
</dependencies>
97101
</project>

google-auth-library-java/cab-token-generator/java/com/google/auth/credentialaccessboundary/ClientSideCredentialAccessBoundaryFactory.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
import java.util.Date;
7979
import java.util.List;
8080
import java.util.concurrent.ExecutionException;
81+
import org.jspecify.annotations.NullMarked;
82+
import org.jspecify.annotations.Nullable;
8183

8284
/**
8385
* A factory for generating downscoped access tokens using a client-side approach.
@@ -136,6 +138,7 @@
136138
* token, allowing for automatic token refreshes by providing a {@link
137139
* OAuth2CredentialsWithRefresh.OAuth2RefreshHandler}.
138140
*/
141+
@NullMarked
139142
public class ClientSideCredentialAccessBoundaryFactory {
140143
static final Duration DEFAULT_REFRESH_MARGIN = Duration.ofMinutes(45);
141144
static final Duration DEFAULT_MINIMUM_TOKEN_LIFETIME = Duration.ofMinutes(30);
@@ -144,9 +147,9 @@ public class ClientSideCredentialAccessBoundaryFactory {
144147
private final String tokenExchangeEndpoint;
145148
private final Duration minimumTokenLifetime;
146149
private final Duration refreshMargin;
147-
private RefreshTask refreshTask;
150+
private @Nullable RefreshTask refreshTask;
148151
private final Object refreshLock = new byte[0];
149-
private volatile IntermediateCredentials intermediateCredentials = null;
152+
private volatile @Nullable IntermediateCredentials intermediateCredentials = null;
150153
private final Clock clock;
151154
private final CelCompiler celCompiler;
152155

google-auth-library-java/cab-token-generator/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
<groupId>com.google.crypto.tink</groupId>
6161
<artifactId>tink</artifactId>
6262
</dependency>
63+
<dependency>
64+
<groupId>org.jspecify</groupId>
65+
<artifactId>jspecify</artifactId>
66+
</dependency>
6367

6468
<dependency>
6569
<groupId>junit</groupId>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
java_library(
22
name = "credentials",
33
srcs = glob(["java/**/*.java"]),
4+
deps = [
5+
"@org_jspecify_jspecify//jar",
6+
],
47
visibility = ["//visibility:public"],
58
)

google-auth-library-java/credentials/java/com/google/auth/ApiKeyCredentials.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import java.util.Collections;
3636
import java.util.List;
3737
import java.util.Map;
38+
import org.jspecify.annotations.NullMarked;
39+
import org.jspecify.annotations.Nullable;
3840

3941
/**
4042
* Credentials class for calling Google APIs using an API key.
@@ -49,6 +51,7 @@
4951
* Credentials credentials = ApiKeyCredentials.create("your api key");
5052
* </code></pre>
5153
*/
54+
@NullMarked
5255
public class ApiKeyCredentials extends Credentials {
5356
static final String API_KEY_HEADER_KEY = "x-goog-api-key";
5457
private final String apiKey;
@@ -70,7 +73,7 @@ public String getAuthenticationType() {
7073
}
7174

7275
@Override
73-
public Map<String, List<String>> getRequestMetadata(URI uri) throws IOException {
76+
public Map<String, List<String>> getRequestMetadata(@Nullable URI uri) throws IOException {
7477
return Collections.singletonMap(API_KEY_HEADER_KEY, Collections.singletonList(apiKey));
7578
}
7679

google-auth-library-java/credentials/java/com/google/auth/CredentialTypeForMetrics.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
package com.google.auth;
3333

34+
import org.jspecify.annotations.NullMarked;
35+
3436
/**
3537
* Defines the different types of credentials that can be used for metrics.
3638
*
@@ -44,6 +46,7 @@
4446
*
4547
* @see #getLabel()
4648
*/
49+
@NullMarked
4750
public enum CredentialTypeForMetrics {
4851
USER_CREDENTIALS("u"),
4952
SERVICE_ACCOUNT_CREDENTIALS_AT("sa"),

google-auth-library-java/credentials/java/com/google/auth/Credentials.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@
3737
import java.util.List;
3838
import java.util.Map;
3939
import java.util.concurrent.Executor;
40+
import org.jspecify.annotations.NullMarked;
41+
import org.jspecify.annotations.Nullable;
4042

4143
/** Represents an abstract authorized identity instance. */
44+
@NullMarked
4245
public abstract class Credentials implements Serializable {
4346

4447
private static final long serialVersionUID = 808575179767517313L;
@@ -161,7 +164,8 @@ protected final void blockingGetToCallback(URI uri, RequestMetadataCallback call
161164
* implement {@link Retryable} and {@code isRetryable()} will return true if the operation may
162165
* be retried.
163166
*/
164-
public abstract Map<String, List<String>> getRequestMetadata(URI uri) throws IOException;
167+
public abstract Map<String, List<String>> getRequestMetadata(@Nullable URI uri)
168+
throws IOException;
165169

166170
/**
167171
* Whether the credentials have metadata entries that should be added to each request.

google-auth-library-java/credentials/java/com/google/auth/RequestMetadataCallback.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333

3434
import java.util.List;
3535
import java.util.Map;
36+
import org.jspecify.annotations.NullMarked;
3637

3738
/**
3839
* The callback that receives the result of the asynchronous {@link
3940
* Credentials#getRequestMetadata(java.net.URI, java.util.concurrent.Executor,
4041
* RequestMetadataCallback)}. Exactly one method should be called.
4142
*/
43+
@NullMarked
4244
public interface RequestMetadataCallback {
4345
/**
4446
* Called when metadata is successfully produced.

google-auth-library-java/credentials/java/com/google/auth/Retryable.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131

3232
package com.google.auth;
3333

34+
import org.jspecify.annotations.NullMarked;
35+
3436
// an interface to identify retryable errors
37+
@NullMarked
3538
public interface Retryable {
3639
/**
3740
* A flag indicating whether the error is retryable

0 commit comments

Comments
 (0)