From df35387dad20560690740061ba77d040af92225a Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Wed, 13 Jan 2021 11:52:33 -0500 Subject: [PATCH 1/2] replace non-precondition use of Preconditions --- .../google/auth/oauth2/OAuth2Credentials.java | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/oauth2_http/java/com/google/auth/oauth2/OAuth2Credentials.java b/oauth2_http/java/com/google/auth/oauth2/OAuth2Credentials.java index ab6c042da..f7bef6502 100644 --- a/oauth2_http/java/com/google/auth/oauth2/OAuth2Credentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/OAuth2Credentials.java @@ -37,7 +37,6 @@ import com.google.auth.http.AuthHttpConstants; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.MoreObjects; -import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import java.io.IOException; @@ -131,7 +130,10 @@ public void getRequestMetadata( super.getRequestMetadata(uri, executor, callback); return; } - metadata = Preconditions.checkNotNull(requestMetadata, "cached requestMetadata"); + if (requestMetadata == null) { + throw new NullPointerException("cached requestMetadata"); + } + metadata = requestMetadata; } callback.onSuccess(metadata); } @@ -146,7 +148,10 @@ public Map> getRequestMetadata(URI uri) throws IOException if (shouldRefresh()) { refresh(); } - return Preconditions.checkNotNull(requestMetadata, "requestMetadata"); + if (requestMetadata == null) { + throw new NullPointerException("requestMetadata"); + } + return requestMetadata; } } @@ -156,9 +161,11 @@ public void refresh() throws IOException { synchronized (lock) { requestMetadata = null; temporaryAccess = null; - useAccessToken( - Preconditions.checkNotNull(refreshAccessToken(), "new access token"), - getAdditionalHeaders()); + AccessToken accessToken = refreshAccessToken(); + if (accessToken == null) { + throw new NullPointerException("new access token"); + } + useAccessToken(accessToken, getAdditionalHeaders()); if (changeListeners != null) { for (CredentialsChangedListener listener : changeListeners) { listener.onChanged(this); @@ -214,8 +221,10 @@ private boolean shouldRefresh() { *

Throws IllegalStateException if not overridden since direct use of OAuth2Credentials is only * for temporary or non-refreshing access tokens. * - * @return Refreshed access token. - * @throws IOException from derived implementations + * @return never + * @throws IllegalStateException always. OAuth2Credentials does not support refreshing the + * access token. An instance with a new access token or a derived type + that supports refreshing should be used instead. */ public AccessToken refreshAccessToken() throws IOException { throw new IllegalStateException( @@ -230,7 +239,7 @@ public AccessToken refreshAccessToken() throws IOException { *

This is called when token content changes, such as when the access token is refreshed. This * is typically used by code caching the access token. * - * @param listener The listener to be added. + * @param listener the listener to be added */ public final void addChangeListener(CredentialsChangedListener listener) { synchronized (lock) { From cd9274e99e87fde85641c69a2b1c113c9e70a813 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Wed, 13 Jan 2021 12:55:19 -0500 Subject: [PATCH 2/2] format --- .../java/com/google/auth/oauth2/OAuth2Credentials.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/oauth2_http/java/com/google/auth/oauth2/OAuth2Credentials.java b/oauth2_http/java/com/google/auth/oauth2/OAuth2Credentials.java index f7bef6502..f22d3c74e 100644 --- a/oauth2_http/java/com/google/auth/oauth2/OAuth2Credentials.java +++ b/oauth2_http/java/com/google/auth/oauth2/OAuth2Credentials.java @@ -222,9 +222,9 @@ private boolean shouldRefresh() { * for temporary or non-refreshing access tokens. * * @return never - * @throws IllegalStateException always. OAuth2Credentials does not support refreshing the - * access token. An instance with a new access token or a derived type - that supports refreshing should be used instead. + * @throws IllegalStateException always. OAuth2Credentials does not support refreshing the access + * token. An instance with a new access token or a derived type that supports refreshing + * should be used instead. */ public AccessToken refreshAccessToken() throws IOException { throw new IllegalStateException(