Skip to content

Commit 81c5461

Browse files
authored
Merge pull request #101 from diffplug/issue-86
Added significantly more early null checks throughout Spotless.
2 parents f8836ae + 2f1664e commit 81c5461

File tree

59 files changed

+497
-188
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+497
-188
lines changed

lib-extra/src/main/java/com/diffplug/spotless/extra/GitAttributesLineEndings.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.spotless.extra;
1717

18+
import static com.diffplug.spotless.extra.LibExtraPreconditions.requireElementsNonNull;
19+
1820
import java.io.File;
1921
import java.io.FileInputStream;
2022
import java.io.IOException;
@@ -81,8 +83,8 @@ static class Policy extends LazyForwardingEquality<FileState> implements LineEnd
8183
final transient Supplier<Iterable<File>> toFormat;
8284

8385
Policy(File projectDir, Supplier<Iterable<File>> toFormat) {
84-
this.projectDir = Objects.requireNonNull(projectDir);
85-
this.toFormat = Objects.requireNonNull(toFormat);
86+
this.projectDir = Objects.requireNonNull(projectDir, "projectDir");
87+
this.toFormat = Objects.requireNonNull(toFormat, "toFormat");
8688
}
8789

8890
@Override
@@ -126,8 +128,7 @@ static class FileState implements Serializable {
126128

127129
@SuppressFBWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON")
128130
FileState(File projectDir, Iterable<File> toFormat) throws IOException {
129-
Objects.requireNonNull(projectDir);
130-
Objects.requireNonNull(toFormat);
131+
requireElementsNonNull(toFormat);
131132
/////////////////////////////////
132133
// USER AND SYSTEM-WIDE VALUES //
133134
/////////////////////////////////
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.extra;
17+
18+
import java.util.Objects;
19+
20+
final class LibExtraPreconditions {
21+
// prevent direct instantiation
22+
private LibExtraPreconditions() {}
23+
24+
static <T, I extends Iterable<T>> I requireElementsNonNull(I elements) {
25+
Objects.requireNonNull(elements);
26+
for (Object element : elements) {
27+
Objects.requireNonNull(element);
28+
}
29+
return elements;
30+
}
31+
}

lib-extra/src/main/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStep.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@
3131

3232
/** Formatter step which calls out to the Groovy-Eclipse formatter. */
3333
public class GrEclipseFormatterStep {
34-
public static final String defaultVersion() {
35-
return DEFAULT_VERSION;
36-
}
37-
3834
private static final String NAME = "groovy-eclipse formatter";
3935
private static final String FORMATTER_CLASS = "com.diffplug.gradle.spotless.groovy.eclipse.GrEclipseFormatterStepImpl";
4036
private static final String DEFAULT_VERSION = "2.3.0";
@@ -54,6 +50,10 @@ public static FormatterStep create(String version, Iterable<File> settingsFiles,
5450
State::createFormat);
5551
}
5652

53+
public static String defaultVersion() {
54+
return DEFAULT_VERSION;
55+
}
56+
5757
private static class State implements Serializable {
5858
private static final long serialVersionUID = 1L;
5959

lib-extra/src/main/java/com/diffplug/spotless/extra/java/EclipseFormatterStep.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.io.File;
1919
import java.io.Serializable;
2020
import java.lang.reflect.Method;
21-
import java.util.Arrays;
21+
import java.util.Collections;
2222
import java.util.Objects;
2323
import java.util.Properties;
2424

@@ -45,7 +45,7 @@ private EclipseFormatterStep() {}
4545
* to many files. Use {@link #create(Iterable, Provisioner)} instead.*/
4646
@Deprecated
4747
public static FormatterStep create(File settingsFile, Provisioner provisioner) {
48-
return create(Arrays.asList(settingsFile), provisioner);
48+
return create(Collections.singletonList(settingsFile), provisioner);
4949
}
5050

5151
/** Creates a formatter step for the given version and settings file. */
@@ -58,11 +58,14 @@ public static FormatterStep create(Iterable<File> settingsFiles, Provisioner pro
5858
* to many files. Use {@link #create(String, Iterable, Provisioner)} instead.*/
5959
@Deprecated
6060
public static FormatterStep create(String version, File settingsFile, Provisioner provisioner) {
61-
return create(version, Arrays.asList(settingsFile), provisioner);
61+
return create(version, Collections.singletonList(settingsFile), provisioner);
6262
}
6363

6464
/** Creates a formatter step for the given version and settings files. */
6565
public static FormatterStep create(String version, Iterable<File> settingsFiles, Provisioner provisioner) {
66+
Objects.requireNonNull(version, "version");
67+
Objects.requireNonNull(settingsFiles, "settingsFiles");
68+
Objects.requireNonNull(provisioner, "provisioner");
6669
return FormatterStep.createLazy(NAME,
6770
() -> new State(JarState.from(MAVEN_COORDINATE + version, provisioner), settingsFiles),
6871
State::createFormat);
@@ -81,7 +84,7 @@ private static class State implements Serializable {
8184
final FileSignature settings;
8285

8386
State(JarState jar, final Iterable<File> settingsFiles) throws Exception {
84-
this.jarState = Objects.requireNonNull(jar);
87+
this.jarState = jar;
8588
this.settings = FileSignature.signAsList(settingsFiles);
8689
}
8790

lib/src/main/java/com/diffplug/spotless/FileSignature.java

+4-35
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
*/
1616
package com.diffplug.spotless;
1717

18+
import static com.diffplug.spotless.MoreIterables.toNullHostileList;
19+
import static com.diffplug.spotless.MoreIterables.toSortedSet;
20+
1821
import java.io.File;
1922
import java.io.IOException;
2023
import java.io.Serializable;
21-
import java.util.ArrayList;
2224
import java.util.Arrays;
2325
import java.util.Collection;
2426
import java.util.Collections;
25-
import java.util.Iterator;
2627
import java.util.List;
2728

2829
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -66,7 +67,7 @@ public static FileSignature signAsList(File... files) throws IOException {
6667

6768
/** Creates file signature whereas order of the files remains unchanged. */
6869
public static FileSignature signAsList(Iterable<File> files) throws IOException {
69-
return new FileSignature(asNonNullList(files));
70+
return new FileSignature(toNullHostileList(files));
7071
}
7172

7273
/** Creates file signature whereas order of the files remains unchanged. */
@@ -109,36 +110,4 @@ public File getOnlyFile() {
109110
}
110111
}
111112

112-
/** Sorts "toBeSorted" and removes duplicates. */
113-
static <T extends Comparable<T>> List<T> toSortedSet(Iterable<T> raw) {
114-
List<T> toBeSorted = asNonNullList(raw);
115-
// sort it
116-
Collections.sort(toBeSorted);
117-
// remove any duplicates (normally there won't be any)
118-
if (toBeSorted.size() > 1) {
119-
Iterator<T> iter = toBeSorted.iterator();
120-
T last = iter.next();
121-
while (iter.hasNext()) {
122-
T next = iter.next();
123-
if (next.compareTo(last) == 0) {
124-
iter.remove();
125-
} else {
126-
last = next;
127-
}
128-
}
129-
}
130-
return toBeSorted;
131-
}
132-
133-
/** Returns a shallow copy of input elements omitting null elements */
134-
private static <T> List<T> asNonNullList(Iterable<T> input) {
135-
List<T> shallowCopy = (input instanceof Collection)
136-
? new ArrayList<T>(((Collection<?>) input).size())
137-
: new ArrayList<T>();
138-
input.forEach(element -> {
139-
if (null != element)
140-
shallowCopy.add(element);
141-
});
142-
return shallowCopy;
143-
}
144113
}

lib/src/main/java/com/diffplug/spotless/FilterByFileFormatterStep.java

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public String getName() {
3636

3737
@Override
3838
public @Nullable String format(String raw, File file) throws Exception {
39+
Objects.requireNonNull(raw, "raw");
40+
Objects.requireNonNull(file, "file");
3941
if (filter.accept(file)) {
4042
return delegateStep.format(raw, file);
4143
} else {

lib/src/main/java/com/diffplug/spotless/FormatExceptionPolicy.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/** A policy for handling exceptions in the format. */
2121
public interface FormatExceptionPolicy extends Serializable, NoLambda {
2222
/** Called for every error in the formatter. */
23-
void handleError(Throwable e, FormatterStep step, String relativePath);
23+
public void handleError(Throwable e, FormatterStep step, String relativePath);
2424

2525
/**
2626
* Returns a byte array representation of everything inside this `FormatExceptionPolicy`.

lib/src/main/java/com/diffplug/spotless/FormatExceptionPolicyStrict.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.diffplug.spotless;
1717

18+
import java.util.Objects;
1819
import java.util.Set;
1920
import java.util.TreeSet;
2021

@@ -30,16 +31,19 @@ public class FormatExceptionPolicyStrict extends NoLambda.EqualityBasedOnSeriali
3031

3132
/** Adds a step name to exclude. */
3233
public void excludeStep(String stepName) {
33-
excludeSteps.add(stepName);
34+
excludeSteps.add(Objects.requireNonNull(stepName));
3435
}
3536

3637
/** Adds a realtive pathx to exclude. */
3738
public void excludePath(String relativePath) {
38-
excludePaths.add(relativePath);
39+
excludePaths.add(Objects.requireNonNull(relativePath));
3940
}
4041

4142
@Override
4243
public void handleError(Throwable e, FormatterStep step, String relativePath) {
44+
Objects.requireNonNull(e, "e");
45+
Objects.requireNonNull(step, "step");
46+
Objects.requireNonNull(relativePath, "relativePath");
4347
if (excludeSteps.contains(step.getName())) {
4448
FormatExceptionPolicyLegacy.warning(e, step, relativePath);
4549
} else {

lib/src/main/java/com/diffplug/spotless/Formatter.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.spotless;
1717

18+
import static com.diffplug.spotless.LibPreconditions.requireElementsNonNull;
19+
1820
import java.io.File;
1921
import java.io.IOException;
2022
import java.io.ObjectInputStream;
@@ -47,7 +49,7 @@ private Formatter(LineEnding.Policy lineEndingsPolicy, Charset encoding, Path ro
4749
this.lineEndingsPolicy = Objects.requireNonNull(lineEndingsPolicy, "lineEndingsPolicy");
4850
this.encoding = Objects.requireNonNull(encoding, "encoding");
4951
this.rootDir = Objects.requireNonNull(rootDirectory, "rootDir");
50-
this.steps = new ArrayList<>(Objects.requireNonNull(steps, "steps"));
52+
this.steps = requireElementsNonNull(new ArrayList<>(steps));
5153
this.exceptionPolicy = Objects.requireNonNull(exceptionPolicy, "exceptionPolicy");
5254
}
5355

@@ -143,6 +145,8 @@ public Formatter build() {
143145

144146
/** Returns true iff the given file's formatting is up-to-date. */
145147
public boolean isClean(File file) throws IOException {
148+
Objects.requireNonNull(file);
149+
146150
String raw = new String(Files.readAllBytes(file.toPath()), encoding);
147151
String unix = LineEnding.toUnix(raw);
148152

@@ -178,6 +182,8 @@ public void applyTo(File file) throws IOException {
178182
* formatted result with unix newlines if it was not.
179183
*/
180184
public @Nullable String applyToAndReturnResultIfDirty(File file) throws IOException {
185+
Objects.requireNonNull(file);
186+
181187
byte[] rawBytes = Files.readAllBytes(file.toPath());
182188
String raw = new String(rawBytes, encoding);
183189
String rawUnix = LineEnding.toUnix(raw);
@@ -199,6 +205,9 @@ public void applyTo(File file) throws IOException {
199205

200206
/** Applies the appropriate line endings to the given unix content. */
201207
public String computeLineEndings(String unix, File file) {
208+
Objects.requireNonNull(unix, "unix");
209+
Objects.requireNonNull(file, "file");
210+
202211
String ending = lineEndingsPolicy.getEndingFor(file);
203212
if (!ending.equals(LineEnding.UNIX.str())) {
204213
return unix.replace(LineEnding.UNIX.str(), ending);
@@ -213,6 +222,9 @@ public String computeLineEndings(String unix, File file) {
213222
* is guaranteed to also have unix line endings.
214223
*/
215224
public String compute(String unix, File file) {
225+
Objects.requireNonNull(unix, "unix");
226+
Objects.requireNonNull(file, "file");
227+
216228
for (FormatterStep step : steps) {
217229
try {
218230
String formatted = step.format(unix, file);

lib/src/main/java/com/diffplug/spotless/FormatterFunc.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.spotless;
1717

18+
import java.util.Objects;
19+
1820
/** A `Function<String, String>` which can throw an exception. */
1921
public interface FormatterFunc extends ThrowingEx.Function<String, String> {
2022
/** A `Function<String, String>` whose implementation requires a resource which should be released when the function is no longer needed. */
@@ -24,6 +26,8 @@ interface Closeable extends FormatterFunc, AutoCloseable {
2426

2527
/** Creates a {@link Closeable} from an AutoCloseable and a function. */
2628
public static Closeable of(AutoCloseable closeable, FormatterFunc function) {
29+
Objects.requireNonNull(closeable, "closeable");
30+
Objects.requireNonNull(function, "function");
2731
return new Closeable() {
2832
@Override
2933
public void close() {
@@ -32,7 +36,7 @@ public void close() {
3236

3337
@Override
3438
public String apply(String input) throws Exception {
35-
return function.apply(input);
39+
return function.apply(Objects.requireNonNull(input));
3640
}
3741
};
3842
}

0 commit comments

Comments
 (0)