Skip to content

Commit eacd14a

Browse files
committed
Merge pull request spotify#407 from spotify/dxia/checkstyle-final-vars
checkstyle: Ensure local variables are final
2 parents 1c333d0 + b552399 commit eacd14a

32 files changed

+140
-131
lines changed

checkstyle.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ page at http://checkstyle.sourceforge.net/config.html -->
138138
<!-- Validates identifiers for local variables against the
139139
expression "^[a-z][a-zA-Z0-9]*$". -->
140140
</module>
141-
141+
<module name="FinalLocalVariable">
142+
<!-- Checks that local variables that never have their values changed are declared final. -->
143+
<property name="tokens" value="VARIABLE_DEF"/>
144+
<property name="validateEnhancedForLoopVariable" value="true"/>
145+
</module>
142146

143147
<!--
144148

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@
219219
<plugin>
220220
<groupId>org.apache.maven.plugins</groupId>
221221
<artifactId>maven-checkstyle-plugin</artifactId>
222-
<version>2.16</version>
222+
<version>2.17</version>
223223
<executions>
224224
<execution>
225225
<id>validate</id>

src/main/java/com/spotify/docker/client/ApacheUnixSocket.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private void setSocketOption(final SocketOptionSetter s) throws SocketException
147147
}
148148

149149
private void setAllSocketOptions() throws SocketException {
150-
for (SocketOptionSetter s : optionsToSet) {
150+
for (final SocketOptionSetter s : optionsToSet) {
151151
s.run();
152152
}
153153
}

src/main/java/com/spotify/docker/client/CompressedDirectory.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ static ImmutableList<DockerIgnorePathMatcher> parseDockerIgnore(Path dockerIgnor
157157
}
158158

159159
private static String createPattern(String line) {
160-
String pattern = line.trim();
160+
final String pattern = line.trim();
161161
if (pattern.startsWith("#")) {
162162
return null;
163163
}
@@ -287,12 +287,9 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
287287
* @return <code>true</code> if the given path should be excluded, <code>false</code> otherwise
288288
*/
289289
private static boolean exclude(ImmutableList<DockerIgnorePathMatcher> matchers, Path path) {
290-
for (DockerIgnorePathMatcher matcher : matchers) {
290+
for (final DockerIgnorePathMatcher matcher : matchers) {
291291
if (matcher.matches(path)) {
292-
if (matcher.isExclude()) {
293-
return true;
294-
}
295-
return false;
292+
return matcher.isExclude();
296293
}
297294
}
298295
return false;
@@ -315,12 +312,15 @@ private static int getPosixFileMode(Path file) throws IOException {
315312
final Set<PosixFilePermission> perm = attr.permissions();
316313

317314
// retain permissions, note these values are octal
315+
//noinspection OctalInteger
318316
int mode = 0100000;
317+
//noinspection OctalInteger
319318
mode += 0100 * getModeFromPermissions(
320319
perm.contains(PosixFilePermission.OWNER_READ),
321320
perm.contains(PosixFilePermission.OWNER_WRITE),
322321
perm.contains(PosixFilePermission.OWNER_EXECUTE));
323322

323+
//noinspection OctalInteger
324324
mode += 010 * getModeFromPermissions(
325325
perm.contains(PosixFilePermission.GROUP_READ),
326326
perm.contains(PosixFilePermission.GROUP_WRITE),

src/main/java/com/spotify/docker/client/DefaultDockerClient.java

+27-28
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ protected DefaultDockerClient(final Builder builder) {
277277

278278
@VisibleForTesting
279279
DefaultDockerClient(final Builder builder, Supplier<ClientBuilder> clientBuilderSupplier) {
280-
URI originalUri = checkNotNull(builder.uri, "uri");
280+
final URI originalUri = checkNotNull(builder.uri, "uri");
281281
this.apiVersion = builder.apiVersion();
282282

283283
if ((builder.dockerCertificates != null) && !originalUri.getScheme().equals("https")) {
@@ -400,7 +400,7 @@ public List<Container> listContainers(final ListContainersParam... params)
400400
.path("containers").path("json");
401401

402402
final Map<String, List<String>> filters = newHashMap();
403-
for (ListContainersParam param : params) {
403+
for (final ListContainersParam param : params) {
404404
if (param instanceof ListContainersFilterParam) {
405405
List<String> filterValueList;
406406
if (filters.containsKey(param.name())) {
@@ -467,9 +467,9 @@ public List<Image> listImages(final ListImagesParam... params)
467467
.path("images").path("json");
468468

469469
final Map<String, List<String>> filters = newHashMap();
470-
for (ListImagesParam param : params) {
470+
for (final ListImagesParam param : params) {
471471
if (param instanceof ListImagesFilterParam) {
472-
List<String> filterValueList;
472+
final List<String> filterValueList;
473473
if (filters.containsKey(param.name())) {
474474
filterValueList = filters.get(param.name());
475475
} else {
@@ -686,7 +686,7 @@ public InputStream copyContainer(String containerId, String path)
686686
.path("containers").path(containerId).path("copy");
687687

688688
// Internal JSON object; not worth it to create class for this
689-
JsonNodeFactory nf = JsonNodeFactory.instance;
689+
final JsonNodeFactory nf = JsonNodeFactory.instance;
690690
final JsonNode params = nf.objectNode().set("Resource", nf.textNode(path));
691691

692692
return request(POST, InputStream.class, resource,
@@ -704,11 +704,9 @@ public void copyToContainer(final Path directory, String containerId, String pat
704704
.queryParam("noOverwriteDirNonDir", true)
705705
.queryParam("path", path);
706706

707-
CompressedDirectory compressedDirectory
708-
= CompressedDirectory.create(directory);
707+
final CompressedDirectory compressedDirectory = CompressedDirectory.create(directory);
709708

710-
final InputStream fileStream =
711-
Files.newInputStream(compressedDirectory.file());
709+
final InputStream fileStream = Files.newInputStream(compressedDirectory.file());
712710

713711
request(PUT, String.class, resource,
714712
resource.request(APPLICATION_OCTET_STREAM_TYPE),
@@ -845,9 +843,10 @@ public void load(final String image, final InputStream imagePayload,
845843
.queryParam("fromSrc", "-")
846844
.queryParam("tag", image);
847845

848-
LoadProgressHandler loadProgressHandler = new LoadProgressHandler(handler);
849-
Entity<InputStream> entity = Entity.entity(imagePayload, MediaType.APPLICATION_OCTET_STREAM);
850-
try (ProgressStream load =
846+
final LoadProgressHandler loadProgressHandler = new LoadProgressHandler(handler);
847+
final Entity<InputStream> entity = Entity.entity(imagePayload,
848+
MediaType.APPLICATION_OCTET_STREAM);
849+
try (final ProgressStream load =
851850
request(POST, ProgressStream.class, resource,
852851
resource
853852
.request(APPLICATION_JSON_TYPE)
@@ -870,7 +869,7 @@ public InputStream save(final String image)
870869
@Override
871870
public InputStream save(final String image, final AuthConfig authConfig)
872871
throws DockerException, IOException, InterruptedException {
873-
WebTarget resource = resource().path("images").path(image).path("get");
872+
final WebTarget resource = resource().path("images").path(image).path("get");
874873

875874
return request(
876875
GET,
@@ -1108,7 +1107,7 @@ public LogStream logs(final String containerId, final LogsParam... params)
11081107
.path("containers").path(containerId)
11091108
.path("logs");
11101109

1111-
for (LogsParam param : params) {
1110+
for (final LogsParam param : params) {
11121111
resource = resource.queryParam(param.name(), param.value());
11131112
}
11141113

@@ -1120,7 +1119,7 @@ public EventStream events(EventsParam... params)
11201119
throws DockerException, InterruptedException {
11211120
WebTarget resource = noTimeoutResource().path("events");
11221121
final Map<String, String> filters = newHashMap();
1123-
for (EventsParam param : params) {
1122+
for (final EventsParam param : params) {
11241123
if (param instanceof EventsFilterParam) {
11251124
filters.put(param.name(), param.value());
11261125
} else {
@@ -1133,7 +1132,7 @@ public EventStream events(EventsParam... params)
11331132
final StringWriter writer = new StringWriter();
11341133
final JsonGenerator generator = objectMapper().getFactory().createGenerator(writer);
11351134
generator.writeStartObject();
1136-
for (Map.Entry<String, String> entry : filters.entrySet()) {
1135+
for (final Map.Entry<String, String> entry : filters.entrySet()) {
11371136
generator.writeArrayFieldStart(entry.getKey());
11381137
generator.writeString(entry.getValue());
11391138
generator.writeEndArray();
@@ -1149,9 +1148,9 @@ public EventStream events(EventsParam... params)
11491148
}
11501149

11511150
try {
1152-
CloseableHttpClient client = (CloseableHttpClient) ApacheConnectorProvider
1151+
final CloseableHttpClient client = (CloseableHttpClient) ApacheConnectorProvider
11531152
.getHttpClient(noTimeoutClient);
1154-
CloseableHttpResponse response = client.execute(new HttpGet(resource.getUri()));
1153+
final CloseableHttpResponse response = client.execute(new HttpGet(resource.getUri()));
11551154
return new EventStream(response, objectMapper());
11561155
} catch (IOException exception) {
11571156
throw new DockerException(exception);
@@ -1192,7 +1191,7 @@ public String execCreate(final String containerId,
11921191
final String[] cmd,
11931192
final ExecCreateParam... params)
11941193
throws DockerException, InterruptedException {
1195-
WebTarget resource = resource().path("containers").path(containerId).path("exec");
1194+
final WebTarget resource = resource().path("containers").path(containerId).path("exec");
11961195

11971196
final StringWriter writer = new StringWriter();
11981197
try {
@@ -1233,7 +1232,7 @@ public String execCreate(final String containerId,
12331232
}
12341233

12351234
try {
1236-
JsonNode json = objectMapper().readTree(response);
1235+
final JsonNode json = objectMapper().readTree(response);
12371236
return json.findValue("Id").textValue();
12381237
} catch (IOException e) {
12391238
throw new DockerException(e);
@@ -1244,14 +1243,14 @@ public String execCreate(final String containerId,
12441243
@Override
12451244
public LogStream execStart(final String execId, final ExecStartParameter... params)
12461245
throws DockerException, InterruptedException {
1247-
WebTarget resource = resource().path("exec").path(execId).path("start");
1246+
final WebTarget resource = resource().path("exec").path(execId).path("start");
12481247

12491248
final StringWriter writer = new StringWriter();
12501249
try {
12511250
final JsonGenerator generator = objectMapper().getFactory().createGenerator(writer);
12521251
generator.writeStartObject();
12531252

1254-
for (ExecStartParameter param : params) {
1253+
for (final ExecStartParameter param : params) {
12551254
generator.writeBooleanField(param.getName(), true);
12561255
}
12571256

@@ -1277,7 +1276,7 @@ public LogStream execStart(final String execId, final ExecStartParameter... para
12771276

12781277
@Override
12791278
public ExecState execInspect(final String execId) throws DockerException, InterruptedException {
1280-
WebTarget resource = resource().path("exec").path(execId).path("json");
1279+
final WebTarget resource = resource().path("exec").path(execId).path("json");
12811280

12821281
try {
12831282
return request(GET, ExecState.class, resource, resource.request(APPLICATION_JSON_TYPE));
@@ -1370,9 +1369,9 @@ private void manageNetworkConnection(String containerId, String methodname, Stri
13701369
throws DockerException, InterruptedException {
13711370
final WebTarget resource = resource().path("networks").path(networkId).path(methodname);
13721371

1373-
Map<String, String> request = new HashMap<>();
1372+
final Map<String, String> request = new HashMap<>();
13741373
request.put("Container", containerId);
1375-
Response response =
1374+
final Response response =
13761375
request(POST, Response.class, resource, resource.request(APPLICATION_JSON_TYPE),
13771376
Entity.json(request));
13781377
switch (response.getStatus()) {
@@ -1444,9 +1443,9 @@ private void request(final String method,
14441443
}
14451444

14461445
private Invocation.Builder headers(final Invocation.Builder request) {
1447-
Set<Map.Entry<String, Object>> entries = headers.entrySet();
1446+
final Set<Map.Entry<String, Object>> entries = headers.entrySet();
14481447

1449-
for (Map.Entry<String, Object> entry : entries) {
1448+
for (final Map.Entry<String, Object> entry : entries) {
14501449
request.header(entry.getKey(), entry.getValue());
14511450
}
14521451

@@ -1537,7 +1536,7 @@ private String authRegistryHeader(final AuthRegistryConfig authRegistryConfig)
15371536
}
15381537

15391538
log.debug("Registry Config Json {}", authRegistryJson);
1540-
String authRegistryEncoded = Base64.encodeAsString(authRegistryJson);
1539+
final String authRegistryEncoded = Base64.encodeAsString(authRegistryJson);
15411540
log.debug("Registry Config Encoded {}", authRegistryEncoded);
15421541
return authRegistryEncoded;
15431542
} catch (JsonProcessingException | InterruptedException ex) {

src/main/java/com/spotify/docker/client/DockerClient.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ public boolean equals(Object o) {
538538
return false;
539539
}
540540

541-
BuildParam that = (BuildParam) o;
541+
final BuildParam that = (BuildParam) o;
542542

543543
if (name != null ? !name.equals(that.name) : that.name != null) {
544544
return false;

src/main/java/com/spotify/docker/client/LogStream.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void close() {
7777
}
7878

7979
public String readFully() {
80-
StringBuilder stringBuilder = new StringBuilder();
80+
final StringBuilder stringBuilder = new StringBuilder();
8181
while (hasNext()) {
8282
stringBuilder.append(UTF_8.decode(next().content()));
8383
}

src/main/java/com/spotify/docker/client/VersionCompare.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ private VersionCompare() {
3737
* "1.10.0".
3838
*/
3939
public static int compareVersion(String str1, String str2) {
40-
String[] vals1 = str1.split("\\.");
41-
String[] vals2 = str2.split("\\.");
40+
final String[] vals1 = str1.split("\\.");
41+
final String[] vals2 = str2.split("\\.");
4242
int i = 0;
4343
// set index to first non-equal ordinal or length of shortest version string
4444
while (i < vals1.length && i < vals2.length && vals1[i].equals(vals2[i])) {
4545
i++;
4646
}
4747
// compare first non-equal ordinal number
4848
if (i < vals1.length && i < vals2.length) {
49-
int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
49+
final int diff = Integer.valueOf(vals1[i]).compareTo(Integer.valueOf(vals2[i]));
5050
return Integer.signum(diff);
5151
} else {
5252
// the strings are equal or one string is a substring of the other

src/main/java/com/spotify/docker/client/messages/AttachedNetwork.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public boolean equals(Object o) {
8080
return false;
8181
}
8282

83-
AttachedNetwork that = (AttachedNetwork) o;
83+
final AttachedNetwork that = (AttachedNetwork) o;
8484

8585
return Objects.equals(this.endpointId, that.endpointId) &&
8686
Objects.equals(this.gateway, that.gateway) &&

src/main/java/com/spotify/docker/client/messages/ContainerInfo.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ public boolean equals(Object o) {
329329
if (o == null || getClass() != o.getClass()) {
330330
return false;
331331
}
332-
Node node = (Node) o;
332+
final Node node = (Node) o;
333333
return Objects.equals(id, node.id) &&
334334
Objects.equals(ip, node.ip) &&
335335
Objects.equals(addr, node.addr) &&

src/main/java/com/spotify/docker/client/messages/ContainerStats.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public boolean equals(Object obj) {
8484
if (getClass() != obj.getClass()) {
8585
return false;
8686
}
87-
ContainerStats other = (ContainerStats) obj;
87+
final ContainerStats other = (ContainerStats) obj;
8888
if (cpuStats == null) {
8989
if (other.cpuStats != null) {
9090
return false;

src/main/java/com/spotify/docker/client/messages/CpuStats.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public boolean equals(Object obj) {
5959
if (getClass() != obj.getClass()) {
6060
return false;
6161
}
62-
CpuStats other = (CpuStats) obj;
62+
final CpuStats other = (CpuStats) obj;
6363
if (cpuUsage == null) {
6464
if (other.cpuUsage != null) {
6565
return false;

src/main/java/com/spotify/docker/client/messages/CpuUsage.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public boolean equals(Object obj) {
7272
if (getClass() != obj.getClass()) {
7373
return false;
7474
}
75-
CpuUsage other = (CpuUsage) obj;
75+
final CpuUsage other = (CpuUsage) obj;
7676
if (percpuUsage == null) {
7777
if (other.percpuUsage != null) {
7878
return false;

src/main/java/com/spotify/docker/client/messages/HostConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ public boolean equals(Object o) {
434434
return false;
435435
}
436436

437-
RestartPolicy that = (RestartPolicy) o;
437+
final RestartPolicy that = (RestartPolicy) o;
438438

439439
if (name != null ? !name.equals(that.name) : that.name != null) {
440440
return false;

src/main/java/com/spotify/docker/client/messages/Image.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public boolean equals(Object o) {
7979
return false;
8080
}
8181

82-
Image that = (Image) o;
82+
final Image that = (Image) o;
8383

8484
return Objects.equals(this.created, that.created) &&
8585
Objects.equals(this.id, that.id) &&

src/main/java/com/spotify/docker/client/messages/Info.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public boolean equals(Object o) {
156156
return false;
157157
}
158158

159-
Info info = (Info) o;
159+
final Info info = (Info) o;
160160

161161
if (containers != info.containers) {
162162
return false;

src/main/java/com/spotify/docker/client/messages/Ipam.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public boolean equals(Object o) {
6161
return false;
6262
}
6363

64-
Ipam that = (Ipam) o;
64+
final Ipam that = (Ipam) o;
6565

6666
return Objects.equals(this.driver, that.driver) && Objects.equals(this.config, that.config);
6767
}
@@ -93,7 +93,7 @@ public Builder driver(final String driver) {
9393
}
9494

9595
public Builder config(final String subnet, final String ipRange, final String gateway) {
96-
IpamConfig config = new IpamConfig();
96+
final IpamConfig config = new IpamConfig();
9797
config.subnet(subnet);
9898
config.ipRange(ipRange);
9999
config.gateway(gateway);

src/main/java/com/spotify/docker/client/messages/IpamConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public boolean equals(Object o) {
6767
return false;
6868
}
6969

70-
IpamConfig that = (IpamConfig) o;
70+
final IpamConfig that = (IpamConfig) o;
7171

7272
return Objects.equals(this.subnet, that.subnet) &&
7373
Objects.equals(this.ipRange, that.ipRange) &&

0 commit comments

Comments
 (0)