Skip to content

Commit c8c44d9

Browse files
authored
Code style cleanup (#571)
* Code clarity fixes from import * Java suggestion: UnknownHostException is subtype of IOException * Java suggestion: params is only assigned during initialization * Java suggestion: simpler equivalent code * Java suggestion: empty array is faster than presized array * Java suggestion: JDK case-related methods depend dangerously on implicit default locale * Java suggestion: method list type can be inferred * Java suggestion: remove redundant toString() call on string * Java suggestion: use immutable collections API instead of singleton collection APIs * Java suggestion: make final tracer/idGenerator only assigned during initialization * Java suggestion: make final tracer only assigned during initialization * Java style: group overloads together * Update maven-checkstyle-plugin for Java 7 syntax
1 parent 132edb7 commit c8c44d9

File tree

12 files changed

+78
-57
lines changed

12 files changed

+78
-57
lines changed

google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ApacheHttpResponse.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ public String getStatusLine() {
8989
return statusLine == null ? null : statusLine.toString();
9090
}
9191

92-
public String getHeaderValue(String name) {
93-
return response.getLastHeader(name).getValue();
94-
}
95-
9692
@Override
9793
public int getHeaderCount() {
9894
return allHeaders.length;
@@ -103,6 +99,10 @@ public String getHeaderName(int index) {
10399
return allHeaders[index].getName();
104100
}
105101

102+
public String getHeaderValue(String name) {
103+
return response.getLastHeader(name).getValue();
104+
}
105+
106106
@Override
107107
public String getHeaderValue(int index) {
108108
return allHeaders[index].getValue();

google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ApacheHttpTransport.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -131,23 +131,34 @@ public ApacheHttpTransport(HttpClient httpClient) {
131131
params.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
132132
}
133133

134+
/** Returns a new instance of the default HTTP parameters we use. */
135+
static HttpParams newDefaultHttpParams() {
136+
HttpParams params = new BasicHttpParams();
137+
// Turn off stale checking. Our connections break all the time anyway,
138+
// and it's not worth it to pay the penalty of checking every time.
139+
HttpConnectionParams.setStaleCheckingEnabled(params, false);
140+
HttpConnectionParams.setSocketBufferSize(params, 8192);
141+
ConnManagerParams.setMaxTotalConnections(params, 200);
142+
ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(20));
143+
return params;
144+
}
145+
134146
/**
135-
* Creates a new instance of the Apache HTTP client that is used by the
136-
* {@link #ApacheHttpTransport()} constructor.
147+
* Creates a new instance of the Apache HTTP client that is used by the {@link
148+
* #ApacheHttpTransport()} constructor.
149+
*
150+
* <p>Use this constructor if you want to customize the default Apache HTTP client. Settings:
137151
*
138-
* <p>
139-
* Use this constructor if you want to customize the default Apache HTTP client. Settings:
140-
* </p>
141152
* <ul>
142-
* <li>The client connection manager is set to {@link ThreadSafeClientConnManager}.</li>
143-
* <li>The socket buffer size is set to 8192 using
144-
* {@link HttpConnectionParams#setSocketBufferSize}.</li>
145-
* <li><The retry mechanism is turned off by setting
146-
* {@code new DefaultHttpRequestRetryHandler(0, false)}.</li>
147-
* <li>The route planner uses {@link ProxySelectorRoutePlanner} with
148-
* {@link ProxySelector#getDefault()}, which uses the proxy settings from <a
149-
* href="http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html">system
150-
* properties</a>.</li>
153+
* <li>The client connection manager is set to {@link ThreadSafeClientConnManager}.</li>
154+
* <li>The socket buffer size is set to 8192 using {@link
155+
* HttpConnectionParams#setSocketBufferSize}.
156+
* <li>The retry mechanism is turned off by setting {@code new
157+
* DefaultHttpRequestRetryHandler(0, false)}.
158+
* <li>The route planner uses {@link ProxySelectorRoutePlanner} with {@link
159+
* ProxySelector#getDefault()}, which uses the proxy settings from <a
160+
* href="http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html">system
161+
* properties</a>.
151162
* </ul>
152163
*
153164
* @return new instance of the Apache HTTP client
@@ -158,18 +169,6 @@ public static DefaultHttpClient newDefaultHttpClient() {
158169
SSLSocketFactory.getSocketFactory(), newDefaultHttpParams(), ProxySelector.getDefault());
159170
}
160171

161-
/** Returns a new instance of the default HTTP parameters we use. */
162-
static HttpParams newDefaultHttpParams() {
163-
HttpParams params = new BasicHttpParams();
164-
// Turn off stale checking. Our connections break all the time anyway,
165-
// and it's not worth it to pay the penalty of checking every time.
166-
HttpConnectionParams.setStaleCheckingEnabled(params, false);
167-
HttpConnectionParams.setSocketBufferSize(params, 8192);
168-
ConnManagerParams.setMaxTotalConnections(params, 200);
169-
ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(20));
170-
return params;
171-
}
172-
173172
/**
174173
* Creates a new instance of the Apache HTTP client that is used by the
175174
* {@link #ApacheHttpTransport()} constructor.
@@ -258,7 +257,7 @@ public static final class Builder {
258257
private SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
259258

260259
/** HTTP parameters. */
261-
private HttpParams params = newDefaultHttpParams();
260+
private final HttpParams params = newDefaultHttpParams();
262261

263262
/**
264263
* HTTP proxy selector to use {@link ProxySelectorRoutePlanner} or {@code null} for

google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/ContentEntity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,27 @@ final class ContentEntity extends AbstractHttpEntity {
4141
this.streamingContent = Preconditions.checkNotNull(streamingContent);
4242
}
4343

44+
@Override
4445
public InputStream getContent() {
4546
throw new UnsupportedOperationException();
4647
}
4748

49+
@Override
4850
public long getContentLength() {
4951
return contentLength;
5052
}
5153

54+
@Override
5255
public boolean isRepeatable() {
5356
return false;
5457
}
5558

59+
@Override
5660
public boolean isStreaming() {
5761
return true;
5862
}
5963

64+
@Override
6065
public void writeTo(OutputStream out) throws IOException {
6166
if (contentLength != 0) {
6267
streamingContent.writeTo(out);

google-http-client-apache-legacy/src/main/java/com/google/api/client/http/apache/SSLSocketFactoryExtension.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import java.io.IOException;
1818
import java.net.Socket;
19-
import java.net.UnknownHostException;
2019
import java.security.KeyManagementException;
2120
import java.security.KeyStore;
2221
import java.security.KeyStoreException;
@@ -53,7 +52,7 @@ public Socket createSocket() throws IOException {
5352

5453
@Override
5554
public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
56-
throws IOException, UnknownHostException {
55+
throws IOException {
5756
SSLSocket sslSocket = (SSLSocket) socketFactory.createSocket(socket, host, port, autoClose);
5857
getHostnameVerifier().verify(host, sslSocket);
5958
return sslSocket;

google-http-client-apache/src/main/java/com/google/api/client/http/apache/ContentEntity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,27 @@ final class ContentEntity extends AbstractHttpEntity {
4141
this.streamingContent = Preconditions.checkNotNull(streamingContent);
4242
}
4343

44+
@Override
4445
public InputStream getContent() {
4546
throw new UnsupportedOperationException();
4647
}
4748

49+
@Override
4850
public long getContentLength() {
4951
return contentLength;
5052
}
5153

54+
@Override
5255
public boolean isRepeatable() {
5356
return false;
5457
}
5558

59+
@Override
5660
public boolean isStreaming() {
5761
return true;
5862
}
5963

64+
@Override
6065
public void writeTo(OutputStream out) throws IOException {
6166
if (contentLength != 0) {
6267
streamingContent.writeTo(out);

google-http-client/src/main/java/com/google/api/client/http/HttpBackOffUnsuccessfulResponseHandler.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.google.api.client.util.Beta;
2020
import com.google.api.client.util.Preconditions;
2121
import com.google.api.client.util.Sleeper;
22-
2322
import java.io.IOException;
2423

2524
/**
@@ -131,13 +130,12 @@ public HttpBackOffUnsuccessfulResponseHandler setSleeper(Sleeper sleeper) {
131130
/**
132131
* {@inheritDoc}
133132
*
134-
* <p>
135-
* Handles the request with {@link BackOff}. That means that if back-off is required a call to
133+
* <p>Handles the request with {@link BackOff}. That means that if back-off is required a call to
136134
* {@link Sleeper#sleep(long)} will be made.
137-
* </p>
138135
*/
139-
public boolean handleResponse(
140-
HttpRequest request, HttpResponse response, boolean supportsRetry) throws IOException {
136+
@Override
137+
public boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry)
138+
throws IOException {
141139
if (!supportsRetry) {
142140
return false;
143141
}

google-http-client/src/main/java/com/google/api/client/http/HttpRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ static String executeAndGetValueOfSomeCustomHeader(HttpRequest request) {
222222
private Sleeper sleeper = Sleeper.DEFAULT;
223223

224224
/** OpenCensus tracing component. */
225-
private Tracer tracer = OpenCensusUtils.getTracer();
225+
private final Tracer tracer = OpenCensusUtils.getTracer();
226226

227227
/**
228228
* @param transport HTTP transport

google-http-client/src/main/java/com/google/api/client/http/OpenCensusUtils.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.api.client.util.Preconditions;
1919
import com.google.common.annotations.VisibleForTesting;
2020

21+
import com.google.common.collect.ImmutableList;
2122
import io.opencensus.contrib.http.util.HttpPropagationUtil;
2223
import io.opencensus.trace.BlankSpan;
2324
import io.opencensus.trace.EndSpanOptions;
@@ -29,7 +30,6 @@
2930
import io.opencensus.trace.Tracing;
3031
import io.opencensus.trace.propagation.TextFormat;
3132

32-
import java.util.Collections;
3333
import java.util.concurrent.atomic.AtomicLong;
3434
import java.util.logging.Level;
3535
import java.util.logging.Logger;
@@ -57,12 +57,12 @@ public class OpenCensusUtils {
5757
* OpenCensus tracing component. When no OpenCensus implementation is provided, it will return a
5858
* no-op tracer.
5959
*/
60-
private static Tracer tracer = Tracing.getTracer();
60+
private static final Tracer tracer = Tracing.getTracer();
6161

6262
/**
6363
* Sequence id generator for message event.
6464
*/
65-
private static AtomicLong idGenerator = new AtomicLong();
65+
private static final AtomicLong idGenerator = new AtomicLong();
6666

6767
/**
6868
* Whether spans should be recorded locally. Defaults to true.
@@ -253,8 +253,9 @@ public void put(HttpHeaders carrier, String key, String value) {
253253
}
254254

255255
try {
256-
Tracing.getExportComponent().getSampledSpanStore().registerSpanNamesForCollection(
257-
Collections.<String>singletonList(SPAN_NAME_HTTP_REQUEST_EXECUTE));
256+
Tracing.getExportComponent()
257+
.getSampledSpanStore()
258+
.registerSpanNamesForCollection(ImmutableList.of(SPAN_NAME_HTTP_REQUEST_EXECUTE));
258259
} catch (Exception e) {
259260
logger.log(
260261
Level.WARNING, "Cannot register default OpenCensus span names for collection.", e);

google-http-client/src/main/java/com/google/api/client/http/UriTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ public static String expand(String pathUri, Object parameters,
360360

361361
private static String getSimpleValue(String name, String value, CompositeOutput compositeOutput) {
362362
if (compositeOutput.requiresVarAssignment()) {
363-
return String.format("%s=%s", name, compositeOutput.getEncodedValue(value.toString()));
363+
return String.format("%s=%s", name, compositeOutput.getEncodedValue(value));
364364
}
365365
return compositeOutput.getEncodedValue(value);
366366
}

google-http-client/src/main/java/com/google/api/client/util/FieldInfo.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.api.client.util;
1616

17+
import com.google.common.base.Ascii;
1718
import java.lang.reflect.Field;
1819
import java.lang.reflect.InvocationTargetException;
1920
import java.lang.reflect.Method;
@@ -141,14 +142,14 @@ public static FieldInfo of(Field field) {
141142
* Creates list of setter methods for a field only in declaring class.
142143
*/
143144
private Method[] settersMethodForField(Field field) {
144-
List<Method> methods = new ArrayList<Method>();
145+
List<Method> methods = new ArrayList<>();
145146
for (Method method : field.getDeclaringClass().getDeclaredMethods()) {
146-
if (method.getName().toLowerCase().equals("set" + field.getName().toLowerCase())
147+
if (Ascii.toLowerCase(method.getName()).equals("set" + field.getName().toLowerCase())
147148
&& method.getParameterTypes().length == 1) {
148149
methods.add(method);
149150
}
150151
}
151-
return methods.toArray(new Method[methods.size()]);
152+
return methods.toArray(new Method[0]);
152153
}
153154

154155
/**
@@ -231,9 +232,7 @@ public void setValue(Object obj, Object value) {
231232
try {
232233
method.invoke(obj, value);
233234
return;
234-
} catch (IllegalAccessException e) {
235-
// try to set field directly
236-
} catch (InvocationTargetException e) {
235+
} catch (IllegalAccessException | InvocationTargetException e) {
237236
// try to set field directly
238237
}
239238
}

pom.xml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,9 @@
361361
</configuration>
362362
</plugin>
363363
<plugin>
364+
<groupId>org.apache.maven.plugins</groupId>
364365
<artifactId>maven-checkstyle-plugin</artifactId>
365-
<version>2.6</version>
366+
<version>3.0.0</version>
366367
</plugin>
367368
<plugin>
368369
<groupId>org.codehaus.mojo</groupId>
@@ -482,9 +483,9 @@
482483
<groupId>org.apache.maven.plugins</groupId>
483484
<artifactId>maven-checkstyle-plugin</artifactId>
484485
<configuration>
485-
<configLocation>checkstyle.xml</configLocation>
486+
<configLocation>${project.root-directory}/checkstyle.xml</configLocation>
486487
<consoleOutput>true</consoleOutput>
487-
<suppressionsLocation>${basedir}/../checkstyle-suppressions.xml</suppressionsLocation>
488+
<suppressionsLocation>${project.root-directory}/checkstyle-suppressions.xml</suppressionsLocation>
488489
</configuration>
489490
<executions>
490491
<execution>
@@ -576,6 +577,7 @@
576577
<project.datanucleus-rdbms.version>3.2.1</project.datanucleus-rdbms.version>
577578
<project.datanucleus-maven-plugin.version>4.0.3</project.datanucleus-maven-plugin.version>
578579
<project.opencensus.version>0.18.0</project.opencensus.version>
580+
<project.root-directory>..</project.root-directory>
579581
</properties>
580582

581583
<profiles>
@@ -612,5 +614,17 @@
612614
</plugins>
613615
</build>
614616
</profile>
617+
<!-- set project.root-directory property based on where we are -->
618+
<profile>
619+
<id>root-directory</id>
620+
<activation>
621+
<file>
622+
<exists>checkstyle-suppressions.xml</exists>
623+
</file>
624+
</activation>
625+
<properties>
626+
<project.root-directory>.</project.root-directory>
627+
</properties>
628+
</profile>
615629
</profiles>
616630
</project>

samples/dailymotion-simple-cmdline-sample/pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@
3535
</plugin>
3636
<plugin>
3737
<artifactId>maven-checkstyle-plugin</artifactId>
38-
<version>2.6</version>
38+
<version>3.0.0</version>
3939
<configuration>
40-
<configLocation>../checkstyle.xml</configLocation>
40+
<configLocation>../../checkstyle.xml</configLocation>
4141
<consoleOutput>true</consoleOutput>
4242
<failOnViolation>false</failOnViolation>
43+
<suppressionsLocation>../../checkstyle-suppressions.xml</suppressionsLocation>
4344
</configuration>
4445
<executions>
4546
<execution>

0 commit comments

Comments
 (0)