Skip to content

Commit 0c59b08

Browse files
committed
fix(auth): refine JSpecify nullability annotations for core ADC credentials
Refine JSpecify nullability annotations across core ADC credential types: - Annotate optional fields, getters, and builder setters/fields with @nullable across AccessToken, ComputeEngineCredentials, AppEngineCredentials, DefaultCredentialsProvider, ImpersonatedCredentials, GdchCredentials, CloudShellCredentials, DownscopedCredentials, IdTokenCredentials, and IdTokenProvider. - Annotate EnvironmentProvider and SystemEnvironmentProvider getEnv methods with @nullable. - Remove redundant final modifiers on private methods in DefaultCredentialsProvider. - Ensure correct JSpecify type-use annotation placements on fields, return types, and parameters. - Migrate Preconditions imports to com.google.common.base.Preconditions. - Ensure builder copy constructors properly preserve all fields and chain super(credentials).
1 parent a51bb8d commit 0c59b08

15 files changed

Lines changed: 181 additions & 140 deletions

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public class AppEngineCredentials extends GoogleCredentials implements ServiceAc
6868

6969
private transient AppIdentityService appIdentityService;
7070

71-
private AppEngineCredentials(Collection<String> scopes, AppIdentityService appIdentityService) {
71+
private AppEngineCredentials(
72+
@Nullable Collection<String> scopes, @Nullable AppIdentityService appIdentityService) {
7273
this.scopes = scopes == null ? ImmutableSet.<String>of() : ImmutableList.copyOf(scopes);
7374
this.appIdentityService =
7475
appIdentityService != null
@@ -96,7 +97,7 @@ public boolean createScopedRequired() {
9697
}
9798

9899
@Override
99-
public GoogleCredentials createScoped(Collection<String> scopes) {
100+
public GoogleCredentials createScoped(@Nullable Collection<String> scopes) {
100101
return new AppEngineCredentials(scopes, appIdentityService);
101102
}
102103

@@ -175,36 +176,44 @@ public Builder toBuilder() {
175176

176177
public static class Builder extends GoogleCredentials.Builder {
177178

178-
private Collection<String> scopes;
179-
private AppIdentityService appIdentityService;
179+
private @Nullable Collection<String> scopes;
180+
private @Nullable AppIdentityService appIdentityService;
180181

181182
protected Builder() {}
182183

183184
protected Builder(AppEngineCredentials credentials) {
185+
super(credentials);
184186
this.scopes = credentials.scopes;
185187
this.appIdentityService = credentials.appIdentityService;
186188
}
187189

188190
@CanIgnoreReturnValue
189-
public Builder setScopes(Collection<String> scopes) {
191+
public Builder setScopes(@Nullable Collection<String> scopes) {
190192
this.scopes = scopes;
191193
return this;
192194
}
193195

194196
@CanIgnoreReturnValue
195-
public Builder setAppIdentityService(AppIdentityService appIdentityService) {
197+
public Builder setAppIdentityService(@Nullable AppIdentityService appIdentityService) {
196198
this.appIdentityService = appIdentityService;
197199
return this;
198200
}
199201

200-
public Collection<String> getScopes() {
202+
public @Nullable Collection<String> getScopes() {
201203
return scopes;
202204
}
203205

204-
public AppIdentityService getAppIdentityService() {
206+
public @Nullable AppIdentityService getAppIdentityService() {
205207
return appIdentityService;
206208
}
207209

210+
@Override
211+
@CanIgnoreReturnValue
212+
public Builder setQuotaProjectId(@Nullable String quotaProjectId) {
213+
super.setQuotaProjectId(quotaProjectId);
214+
return this;
215+
}
216+
208217
@Override
209218
public AppEngineCredentials build() {
210219
return new AppEngineCredentials(scopes, appIdentityService);

google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/AccessToken.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class AccessToken implements Serializable {
5656
* @param tokenValue String representation of the access token.
5757
* @param expirationTime Time when access token will expire.
5858
*/
59-
public AccessToken(String tokenValue, Date expirationTime) {
59+
public AccessToken(String tokenValue, @Nullable Date expirationTime) {
6060
this.tokenValue = tokenValue;
6161
this.expirationTimeMillis = (expirationTime == null) ? null : expirationTime.getTime();
6262
this.scopes = new ArrayList<>();
@@ -108,7 +108,7 @@ public String getTokenValue() {
108108
return new Date(expirationTimeMillis);
109109
}
110110

111-
Long getExpirationTimeMillis() {
111+
@Nullable Long getExpirationTimeMillis() {
112112
return expirationTimeMillis;
113113
}
114114

@@ -145,8 +145,8 @@ public boolean equals(@Nullable Object obj) {
145145
}
146146

147147
public static class Builder {
148-
private String tokenValue;
149-
private Date expirationTime;
148+
private @Nullable String tokenValue;
149+
private @Nullable Date expirationTime;
150150
private List<String> scopes = new ArrayList<>();
151151

152152
protected Builder() {}
@@ -157,34 +157,34 @@ protected Builder(AccessToken accessToken) {
157157
this.scopes = accessToken.getScopes();
158158
}
159159

160-
public String getTokenValue() {
160+
public @Nullable String getTokenValue() {
161161
return this.tokenValue;
162162
}
163163

164164
public List<String> getScopes() {
165165
return this.scopes;
166166
}
167167

168-
public Date getExpirationTime() {
168+
public @Nullable Date getExpirationTime() {
169169
return this.expirationTime;
170170
}
171171

172172
@CanIgnoreReturnValue
173-
public Builder setTokenValue(String tokenValue) {
173+
public Builder setTokenValue(@Nullable String tokenValue) {
174174
this.tokenValue = tokenValue;
175175
return this;
176176
}
177177

178178
@CanIgnoreReturnValue
179-
public Builder setScopes(String scopes) {
179+
public Builder setScopes(@Nullable String scopes) {
180180
if (scopes != null && scopes.trim().length() > 0) {
181181
this.scopes = Arrays.asList(scopes.split(" "));
182182
}
183183
return this;
184184
}
185185

186186
@CanIgnoreReturnValue
187-
public Builder setScopes(List<String> scopes) {
187+
public Builder setScopes(@Nullable List<String> scopes) {
188188
if (scopes == null) {
189189
this.scopes = new ArrayList<>();
190190
} else {
@@ -195,7 +195,7 @@ public Builder setScopes(List<String> scopes) {
195195
}
196196

197197
@CanIgnoreReturnValue
198-
public Builder setExpirationTime(Date expirationTime) {
198+
public Builder setExpirationTime(@Nullable Date expirationTime) {
199199
this.expirationTime = expirationTime;
200200
return this;
201201
}

google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/AppEngineCredentials.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ class AppEngineCredentials extends GoogleCredentials implements ServiceAccountSi
8080
private transient Method getExpirationTime;
8181
private transient Method signForApp;
8282
private transient Method getSignature;
83-
private transient String account;
83+
private transient @Nullable String account;
8484

85-
AppEngineCredentials(Collection<String> scopes, Collection<String> defaultScopes)
85+
AppEngineCredentials(
86+
@Nullable Collection<String> scopes, @Nullable Collection<String> defaultScopes)
8687
throws IOException {
8788
// Use defaultScopes only when scopes don't exist.
8889
if (scopes == null || scopes.isEmpty()) {
@@ -96,7 +97,7 @@ class AppEngineCredentials extends GoogleCredentials implements ServiceAccountSi
9697
}
9798

9899
AppEngineCredentials(
99-
Collection<String> scopes,
100+
@Nullable Collection<String> scopes,
100101
@Nullable Collection<String> defaultScopes,
101102
AppEngineCredentials unscoped) {
102103
this.appIdentityService = unscoped.appIdentityService;
@@ -164,13 +165,13 @@ public boolean createScopedRequired() {
164165
}
165166

166167
@Override
167-
public GoogleCredentials createScoped(Collection<String> scopes) {
168+
public GoogleCredentials createScoped(@Nullable Collection<String> scopes) {
168169
return new AppEngineCredentials(scopes, null, this);
169170
}
170171

171172
@Override
172173
public GoogleCredentials createScoped(
173-
Collection<String> scopes, Collection<String> defaultScopes) {
174+
@Nullable Collection<String> scopes, @Nullable Collection<String> defaultScopes) {
174175
return new AppEngineCredentials(scopes, defaultScopes, this);
175176
}
176177

google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/CloudShellCredentials.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public static class Builder extends GoogleCredentials.Builder {
135135
protected Builder() {}
136136

137137
protected Builder(CloudShellCredentials credentials) {
138+
super(credentials);
138139
this.authPort = credentials.authPort;
139140
}
140141

@@ -144,9 +145,10 @@ public Builder setAuthPort(int authPort) {
144145
return this;
145146
}
146147

148+
@Override
147149
@CanIgnoreReturnValue
148-
public Builder setQuotaProjectId(String quotaProjectId) {
149-
super.quotaProjectId = quotaProjectId;
150+
public Builder setQuotaProjectId(@Nullable String quotaProjectId) {
151+
super.setQuotaProjectId(quotaProjectId);
150152
return this;
151153
}
152154

0 commit comments

Comments
 (0)