Skip to content

Commit

Permalink
Integrate Volley builds with errorprone. (#162)
Browse files Browse the repository at this point in the history
All error prone warnings+errors now lead to compile failures.
Existing failures have been fixed or suppressed.

Fixes #100
  • Loading branch information
jpd236 authored Apr 9, 2018
1 parent fa58602 commit 5307293
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 20 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ buildscript {
}
}

plugins {
id "net.ltgt.errorprone" version "0.0.13"
}

apply plugin: 'com.android.library'

repositories {
Expand Down
4 changes: 4 additions & 0 deletions rules.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ android {
}
}

tasks.withType(JavaCompile) {
options.compilerArgs << "-Werror"
}

// Check if the android plugin version supports unit testing.
if (configurations.findByName("testCompile")) {
dependencies {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/android/volley/DefaultRetryPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public float getBackoffMultiplier() {
@Override
public void retry(VolleyError error) throws VolleyError {
mCurrentRetryCount++;
mCurrentTimeoutMs += (mCurrentTimeoutMs * mBackoffMultiplier);
mCurrentTimeoutMs += (int) (mCurrentTimeoutMs * mBackoffMultiplier);
if (!hasAttemptRemaining()) {
throw error;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/android/volley/ExecutorDelivery.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void postError(Request<?> request, VolleyError error) {
* main thread.
*/
@SuppressWarnings("rawtypes")
private class ResponseDeliveryRunnable implements Runnable {
private static class ResponseDeliveryRunnable implements Runnable {
private final Request mRequest;
private final Response mResponse;
private final Runnable mRunnable;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/android/volley/toolbox/ByteArrayPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

/**
Expand Down Expand Up @@ -53,7 +52,7 @@
*/
public class ByteArrayPool {
/** The buffer pool, arranged both by last use and by buffer size */
private final List<byte[]> mBuffersByLastUse = new LinkedList<byte[]>();
private final List<byte[]> mBuffersByLastUse = new ArrayList<>();
private final List<byte[]> mBuffersBySize = new ArrayList<byte[]>(64);

/** The total size of the buffers in the pool */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static Cache.Entry parseCacheHeaders(NetworkResponse response) {
headerValue = headers.get("Cache-Control");
if (headerValue != null) {
hasCacheControl = true;
String[] tokens = headerValue.split(",");
String[] tokens = headerValue.split(",", 0);
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i].trim();
if (token.equals("no-cache") || token.equals("no-store")) {
Expand Down Expand Up @@ -170,9 +170,9 @@ private static SimpleDateFormat newRfc1123Formatter() {
public static String parseCharset(Map<String, String> headers, String defaultCharset) {
String contentType = headers.get(HEADER_CONTENT_TYPE);
if (contentType != null) {
String[] params = contentType.split(";");
String[] params = contentType.split(";", 0);
for (int i = 1; i < params.length; i++) {
String[] pair = params[i].trim().split("=");
String[] pair = params[i].trim().split("=", 0);
if (pair.length == 2) {
if (pair[0].equals("charset")) {
return pair[1];
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/android/volley/toolbox/ImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
import android.os.Looper;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
* Helper that handles loading and caching images from remote URLs.
Expand Down Expand Up @@ -387,7 +389,7 @@ public String getRequestUrl() {
* Wrapper class used to map a Request to the set of active ImageContainer objects that are
* interested in its results.
*/
private class BatchedImageRequest {
private static class BatchedImageRequest {
/** The request being tracked */
private final Request<?> mRequest;

Expand All @@ -398,7 +400,7 @@ private class BatchedImageRequest {
private VolleyError mError;

/** List of all of the active ImageContainers that are interested in the request */
private final LinkedList<ImageContainer> mContainers = new LinkedList<ImageContainer>();
private final List<ImageContainer> mContainers = new ArrayList<>();

/**
* Constructs a new BatchedImageRequest object
Expand Down Expand Up @@ -433,7 +435,7 @@ public void addContainer(ImageContainer container) {
}

/**
* Detatches the bitmap container from the request and cancels the request if no one is
* Detaches the bitmap container from the request and cancels the request if no one is
* left listening.
* @param container The container to remove from the list
* @return True if the request was canceled, false otherwise.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public void finalize() {
/**
* Ensures there is enough space in the buffer for the given number of additional bytes.
*/
@SuppressWarnings("UnsafeFinalization")
private void expand(int i) {
/* Can the buffer handle @i more bytes, if not expand it */
if (count + i <= buf.length) {
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/android/volley/toolbox/RequestFuture.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.android.volley.toolbox;

import android.os.SystemClock;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
Expand Down Expand Up @@ -108,9 +110,16 @@ private synchronized T doGet(Long timeoutMs)
}

if (timeoutMs == null) {
wait(0);
while (!isDone()) {
wait(0);
}
} else if (timeoutMs > 0) {
wait(timeoutMs);
long nowMs = SystemClock.uptimeMillis();
long deadlineMs = nowMs + timeoutMs;
while (!isDone() && nowMs < deadlineMs) {
wait(deadlineMs - nowMs);
nowMs = SystemClock.uptimeMillis();
}
}

if (mException != null) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/android/volley/toolbox/StringRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,15 @@ protected void deliverResponse(String response) {
}

@Override
@SuppressWarnings("DefaultCharset")
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String parsed;
try {
parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
// Since minSdkVersion = 8, we can't call
// new String(response.data, Charset.defaultCharset())
// So suppress the warning instead.
parsed = new String(response.data);
}
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/com/android/volley/NetworkDispatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import static org.junit.Assert.*;
Expand All @@ -40,7 +41,8 @@ public class NetworkDispatcherTest {
private MockCache mCache;
private MockRequest mRequest;

private static final byte[] CANNED_DATA = "Ceci n'est pas une vraie reponse".getBytes();
private static final byte[] CANNED_DATA =
"Ceci n'est pas une vraie reponse".getBytes(StandardCharsets.UTF_8);
private static final long TIMEOUT_MILLIS = 5000;

@Before public void setUp() throws Exception {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/android/volley/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class RequestTest {
assertTrue(immediate.compareTo(high) < 0);
}

private class TestRequest extends Request<Object> {
private static class TestRequest extends Request<Object> {
private Priority mPriority = Priority.NORMAL;
public TestRequest(Priority priority) {
super(Request.Method.GET, "", null);
Expand Down Expand Up @@ -80,7 +80,7 @@ protected Response<Object> parseNetworkResponse(NetworkResponse response) {
assertFalse(0 == goodProtocol.getTrafficStatsTag());
}

private class UrlParseRequest extends Request<Object> {
private static class UrlParseRequest extends Request<Object> {
public UrlParseRequest(String url) {
super(Request.Method.GET, url, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -68,7 +69,7 @@ public class BasicNetworkTest {
@Test public void headersAndPostParams() throws Exception {
MockHttpStack mockHttpStack = new MockHttpStack();
InputStream responseStream =
new ByteArrayInputStream("foobar".getBytes());
new ByteArrayInputStream("foobar".getBytes(StandardCharsets.UTF_8));
HttpResponse fakeResponse =
new HttpResponse(200, Collections.<Header>emptyList(), 6, responseStream);
mockHttpStack.setResponseToReturn(fakeResponse);
Expand All @@ -83,7 +84,8 @@ public class BasicNetworkTest {
assertEquals("foobar", mockHttpStack.getLastHeaders().get("If-None-Match"));
assertEquals("Sat, 19 Aug 2017 00:20:02 GMT",
mockHttpStack.getLastHeaders().get("If-Modified-Since"));
assertEquals("requestpost=foo&", new String(mockHttpStack.getLastPostBody()));
assertEquals("requestpost=foo&",
new String(mockHttpStack.getLastPostBody(), StandardCharsets.UTF_8));
}

@Test public void notModified() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import static org.junit.Assert.*;

Expand All @@ -45,7 +46,7 @@ public class ImageRequestTest {
// "file:" + name in its lookaside map. I write all this because it will
// probably break mysteriously at some point and I feel terrible about your
// having to debug it.
byte[] jpegBytes = "file:fake".getBytes();
byte[] jpegBytes = "file:fake".getBytes(StandardCharsets.UTF_8);
ShadowBitmapFactory.provideWidthAndHeightHints("fake", 1024, 500);
NetworkResponse jpeg = new NetworkResponse(jpegBytes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class NetworkImageViewTest {
// // instrumentation test. Write this test once it's figured out.
// }

private class MockImageLoader extends ImageLoader {
private static class MockImageLoader extends ImageLoader {
public MockImageLoader() {
super(null, null);
}
Expand All @@ -61,6 +61,7 @@ public MockImageLoader() {
public int lastMaxWidth;
public int lastMaxHeight;

@Override
public ImageContainer get(String requestUrl, ImageListener imageListener, int maxWidth,
int maxHeight, ScaleType scaleType) {
lastRequestUrl = requestUrl;
Expand Down

0 comments on commit 5307293

Please sign in to comment.