Skip to content

Commit

Permalink
Use actual annotations instead of comments. (google#169)
Browse files Browse the repository at this point in the history
Dependency on android.support.annotations is declared as "provided"
so it won't propagate to callers.

Note that by using the real @GuardedBy, ErrorProne now picks up on
places that a variable is accessed without a lock, so these had to
be fixed as well.
  • Loading branch information
jpd236 authored Apr 23, 2018
1 parent bdc8055 commit a842400
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 15 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ apply plugin: 'com.android.library'

repositories {
jcenter()
maven {
url 'https://maven.google.com/'
name 'Google'
}
}

group = 'com.android.volley'
Expand Down
4 changes: 4 additions & 0 deletions rules.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ tasks.withType(JavaCompile) {
options.compilerArgs << "-Werror"
}

dependencies {
provided "com.android.support:support-annotations:27.1.1"
}

// Check if the android plugin version supports unit testing.
if (configurations.findByName("testCompile")) {
dependencies {
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/com/android/volley/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.CallSuper;
import android.support.annotation.GuardedBy;
import android.text.TextUtils;
import com.android.volley.VolleyLog.MarkerLog;
import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -79,7 +81,7 @@ public interface Method {
private final Object mLock = new Object();

/** Listener interface for errors. */
// @GuardedBy("mLock")
@GuardedBy("mLock")
private Response.ErrorListener mErrorListener;

/** Sequence number of this request, used to enforce FIFO ordering. */
Expand All @@ -92,11 +94,11 @@ public interface Method {
private boolean mShouldCache = true;

/** Whether or not this request has been canceled. */
// @GuardedBy("mLock")
@GuardedBy("mLock")
private boolean mCanceled = false;

/** Whether or not a response has been delivered for this request yet. */
// @GuardedBy("mLock")
@GuardedBy("mLock")
private boolean mResponseDelivered = false;

/** Whether the request should be retried in the event of an HTTP 5xx (server) error. */
Expand All @@ -116,7 +118,7 @@ public interface Method {
private Object mTag;

/** Listener that will be notified when a response has been delivered. */
// @GuardedBy("mLock")
@GuardedBy("mLock")
private NetworkRequestCompleteListener mRequestCompleteListener;

/**
Expand Down Expand Up @@ -173,7 +175,9 @@ public Object getTag() {

/** @return this request's {@link com.android.volley.Response.ErrorListener}. */
public Response.ErrorListener getErrorListener() {
return mErrorListener;
synchronized (mLock) {
return mErrorListener;
}
}

/** @return A tag for use with {@link TrafficStats#setThreadStatsTag(int)} */
Expand Down Expand Up @@ -313,7 +317,7 @@ public Cache.Entry getCacheEntry() {
*
* <p>There are no guarantees if both of these conditions aren't met.
*/
// @CallSuper
@CallSuper
public void cancel() {
synchronized (mLock) {
mCanceled = true;
Expand Down Expand Up @@ -649,7 +653,7 @@ public int compareTo(Request<T> other) {
@Override
public String toString() {
String trafficStatsTag = "0x" + Integer.toHexString(getTrafficStatsTag());
return (mCanceled ? "[X] " : "[ ] ")
return (isCanceled() ? "[X] " : "[ ] ")
+ getUrl()
+ " "
+ trafficStatsTag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.VisibleForTesting;
import com.android.volley.AuthFailureError;

/**
Expand Down Expand Up @@ -61,7 +62,7 @@ public AndroidAuthenticator(
this(AccountManager.get(context), account, authTokenType, notifyAuthFailure);
}

// Visible for testing. Allows injection of a mock AccountManager.
@VisibleForTesting
AndroidAuthenticator(
AccountManager accountManager,
Account account,
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/android/volley/toolbox/DiskBasedCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.android.volley.toolbox;

import android.os.SystemClock;
import android.support.annotation.VisibleForTesting;
import android.text.TextUtils;
import com.android.volley.Cache;
import com.android.volley.Header;
Expand Down Expand Up @@ -482,7 +483,7 @@ boolean writeHeader(OutputStream os) {
}
}

// VisibleForTesting
@VisibleForTesting
static class CountingInputStream extends FilterInputStream {
private final long length;
private long bytesRead;
Expand Down Expand Up @@ -510,7 +511,7 @@ public int read(byte[] buffer, int offset, int count) throws IOException {
return result;
}

// VisibleForTesting
@VisibleForTesting
long bytesRead() {
return bytesRead;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/android/volley/toolbox/HurlStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.android.volley.toolbox;

import android.support.annotation.VisibleForTesting;
import com.android.volley.AuthFailureError;
import com.android.volley.Header;
import com.android.volley.Request;
Expand Down Expand Up @@ -106,7 +107,7 @@ public HttpResponse executeRequest(Request<?> request, Map<String, String> addit
inputStreamFromConnection(connection));
}

// VisibleForTesting
@VisibleForTesting
static List<Header> convertHeaders(Map<String, List<String>> responseHeaders) {
List<Header> headerList = new ArrayList<>(responseHeaders.size());
for (Map.Entry<String, List<String>> entry : responseHeaders.entrySet()) {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/android/volley/toolbox/ImageRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.support.annotation.GuardedBy;
import android.support.annotation.VisibleForTesting;
import android.widget.ImageView.ScaleType;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
Expand All @@ -41,8 +43,9 @@ public class ImageRequest extends Request<Bitmap> {
/** Lock to guard mListener as it is cleared on cancel() and read on delivery. */
private final Object mLock = new Object();

// @GuardedBy("mLock")
@GuardedBy("mLock")
private Response.Listener<Bitmap> mListener;

private final Config mDecodeConfig;
private final int mMaxWidth;
private final int mMaxHeight;
Expand Down Expand Up @@ -262,7 +265,7 @@ protected void deliverResponse(Bitmap response) {
* @param desiredWidth Desired width of the bitmap
* @param desiredHeight Desired height of the bitmap
*/
// Visible for testing.
@VisibleForTesting
static int findBestSampleSize(
int actualWidth, int actualHeight, int desiredWidth, int desiredHeight) {
double wr = (double) actualWidth / desiredWidth;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/android/volley/toolbox/JsonRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.android.volley.toolbox;

import android.support.annotation.GuardedBy;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
Expand All @@ -41,8 +42,9 @@ public abstract class JsonRequest<T> extends Request<T> {
/** Lock to guard mListener as it is cleared on cancel() and read on delivery. */
private final Object mLock = new Object();

// @GuardedBy("mLock")
@GuardedBy("mLock")
private Listener<T> mListener;

private final String mRequestBody;

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/android/volley/toolbox/StringRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.android.volley.toolbox;

import android.support.annotation.GuardedBy;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
Expand All @@ -29,7 +30,7 @@ public class StringRequest extends Request<String> {
/** Lock to guard mListener as it is cleared on cancel() and read on delivery. */
private final Object mLock = new Object();

// @GuardedBy("mLock")
@GuardedBy("mLock")
private Listener<String> mListener;

/**
Expand Down

0 comments on commit a842400

Please sign in to comment.