Skip to content

Commit

Permalink
fix: replace non-precondition use of Preconditions (#539)
Browse files Browse the repository at this point in the history
* replace non-precondition use of Preconditions

* format
  • Loading branch information
elharo committed Jan 14, 2021
1 parent 75d85b3 commit f2ab4f1
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions oauth2_http/java/com/google/auth/oauth2/OAuth2Credentials.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -146,7 +148,10 @@ public Map<String, List<String>> getRequestMetadata(URI uri) throws IOException
if (shouldRefresh()) {
refresh();
}
return Preconditions.checkNotNull(requestMetadata, "requestMetadata");
if (requestMetadata == null) {
throw new NullPointerException("requestMetadata");
}
return requestMetadata;
}
}

Expand All @@ -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);
Expand Down Expand Up @@ -214,8 +221,10 @@ private boolean shouldRefresh() {
* <p>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(
Expand All @@ -230,7 +239,7 @@ public AccessToken refreshAccessToken() throws IOException {
* <p>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) {
Expand Down

0 comments on commit f2ab4f1

Please sign in to comment.