Skip to content

Commit 6260258

Browse files
netdpbCompile-Testing Team
authored andcommitted
Use @Nullable from the Checker Framework and fix a few null-correctness issues.
RELNOTES= Use `@Nullable` from the Checker Framework and fix a few null-correctness issues. PiperOrigin-RevId: 380043179
1 parent 620d236 commit 6260258

11 files changed

Lines changed: 186 additions & 176 deletions

pom.xml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,6 @@
6868
<artifactId>guava</artifactId>
6969
<version>30.1.1-jre</version>
7070
</dependency>
71-
<dependency>
72-
<groupId>com.google.code.findbugs</groupId>
73-
<artifactId>jsr305</artifactId>
74-
<version>3.0.2</version>
75-
<optional>true</optional>
76-
</dependency>
7771
<dependency>
7872
<groupId>com.google.errorprone</groupId>
7973
<artifactId>error_prone_annotations</artifactId>
@@ -90,6 +84,11 @@
9084
<artifactId>auto-common</artifactId>
9185
<version>1.0.1</version>
9286
</dependency>
87+
<dependency>
88+
<groupId>org.checkerframework</groupId>
89+
<artifactId>checker-qual</artifactId>
90+
<version>3.14.0</version>
91+
</dependency>
9392
</dependencies>
9493

9594
<build>

src/main/java/com/google/testing/compile/Compiler.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import javax.tools.JavaCompiler.CompilationTask;
4545
import javax.tools.JavaFileObject;
4646
import javax.tools.StandardLocation;
47+
import org.checkerframework.checker.nullness.qual.Nullable;
4748

4849
/** An object that can {@link #compile} Java source files. */
4950
@AutoValue
@@ -211,9 +212,10 @@ public final Compilation compile(Iterable<? extends JavaFileObject> files) {
211212
return compilation;
212213
}
213214

214-
@VisibleForTesting static final ClassLoader platformClassLoader = getPlatformClassLoader();
215+
@VisibleForTesting
216+
static final @Nullable ClassLoader platformClassLoader = getPlatformClassLoader();
215217

216-
private static ClassLoader getPlatformClassLoader() {
218+
private static @Nullable ClassLoader getPlatformClassLoader() {
217219
try {
218220
// JDK >= 9
219221
return (ClassLoader) ClassLoader.class.getMethod("getPlatformClassLoader").invoke(null);
@@ -229,12 +231,14 @@ private static ClassLoader getPlatformClassLoader() {
229231
* @throws IllegalArgumentException if the given classloader had classpaths which we could not
230232
* determine or use for compilation.
231233
*/
232-
private static ImmutableList<File> getClasspathFromClassloader(ClassLoader currentClassloader) {
234+
private static ImmutableList<File> getClasspathFromClassloader(ClassLoader classloader) {
233235
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
234236

235237
// Concatenate search paths from all classloaders in the hierarchy 'till the system classloader.
236238
Set<String> classpaths = new LinkedHashSet<>();
237-
while (true) {
239+
for (ClassLoader currentClassloader = classloader;
240+
;
241+
currentClassloader = currentClassloader.getParent()) {
238242
if (currentClassloader == systemClassLoader) {
239243
Iterables.addAll(
240244
classpaths,
@@ -263,7 +267,6 @@ private static ImmutableList<File> getClasspathFromClassloader(ClassLoader curre
263267
+ "since %s is not an instance of URLClassloader",
264268
currentClassloader));
265269
}
266-
currentClassloader = currentClassloader.getParent();
267270
}
268271

269272
return classpaths.stream().map(File::new).collect(toImmutableList());

src/main/java/com/google/testing/compile/InMemoryJavaFileManager.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import javax.tools.SimpleJavaFileObject;
4343
import javax.tools.StandardJavaFileManager;
4444
import javax.tools.StandardLocation;
45+
import org.checkerframework.checker.nullness.qual.Nullable;
4546

4647
/**
4748
* A file manager implementation that stores all output in memory.
@@ -90,8 +91,8 @@ public boolean isSameFile(FileObject a, FileObject b) {
9091
}
9192

9293
@Override
93-
public FileObject getFileForInput(Location location, String packageName,
94-
String relativeName) throws IOException {
94+
public @Nullable FileObject getFileForInput(
95+
Location location, String packageName, String relativeName) throws IOException {
9596
if (location.isOutputLocation()) {
9697
return inMemoryOutputs.getIfPresent(uriForFileObject(location, packageName, relativeName));
9798
}
@@ -103,8 +104,8 @@ public FileObject getFileForInput(Location location, String packageName,
103104
}
104105

105106
@Override
106-
public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind)
107-
throws IOException {
107+
public @Nullable JavaFileObject getJavaFileForInput(
108+
Location location, String className, Kind kind) throws IOException {
108109
if (location.isOutputLocation()) {
109110
return inMemoryOutputs.getIfPresent(uriForJavaFileObject(location, className, kind));
110111
}

src/main/java/com/google/testing/compile/JavaFileObjectSubject.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
import java.io.IOException;
3434
import java.nio.charset.Charset;
3535
import java.util.function.BiFunction;
36-
import javax.annotation.Nullable;
3736
import javax.tools.JavaFileObject;
37+
import org.checkerframework.checker.nullness.qual.Nullable;
3838

3939
/** Assertions about {@link JavaFileObject}s. */
4040
public final class JavaFileObjectSubject extends Subject {
@@ -72,6 +72,7 @@ protected String actualCustomStringRepresentation() {
7272
public void isEqualTo(@Nullable Object other) {
7373
if (!(other instanceof JavaFileObject)) {
7474
super.isEqualTo(other);
75+
return;
7576
}
7677

7778
JavaFileObject otherFile = (JavaFileObject) other;

src/main/java/com/google/testing/compile/JavaSourcesSubject.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@
5757
import java.util.List;
5858
import java.util.Map;
5959
import java.util.Optional;
60-
import javax.annotation.Nullable;
6160
import javax.annotation.processing.Processor;
6261
import javax.tools.Diagnostic;
6362
import javax.tools.Diagnostic.Kind;
6463
import javax.tools.JavaFileManager;
6564
import javax.tools.JavaFileObject;
65+
import org.checkerframework.checker.nullness.qual.Nullable;
6666

6767
/**
6868
* A <a href="https://github.com/truth0/truth">Truth</a> {@link Subject} that evaluates the result
@@ -213,7 +213,11 @@ public void parsesAs(JavaFileObject first, JavaFileObject... rest) {
213213
final CompilationUnitTree expectedTree = matchedTreePair.getKey();
214214
if (!matchedTreePair.getValue().isPresent()) {
215215
failNoCandidates(
216-
expectedTreeTypes.get(expectedTree), expectedTree, actualTreeTypes, actualTrees);
216+
// see comment above
217+
requireNonNull(expectedTreeTypes.get(expectedTree)),
218+
expectedTree,
219+
actualTreeTypes,
220+
actualTrees);
217221
} else {
218222
CompilationUnitTree actualTree = matchedTreePair.getValue().get();
219223
TreeDifference treeDifference = TreeDiffer.diffCompilationUnits(expectedTree, actualTree);

src/main/java/com/google/testing/compile/MoreTrees.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import com.sun.source.util.TreePathScanner;
3636
import java.util.Arrays;
3737
import java.util.Optional;
38-
import javax.annotation.Nullable;
38+
import org.checkerframework.checker.nullness.qual.Nullable;
3939

4040
/**
4141
* A class containing methods which are useful for gaining access to {@code Tree} instances from
@@ -128,10 +128,8 @@ static TreePath findSubtreePath(CompilationUnitTree root, Tree.Kind treeKind,
128128
return res.get();
129129
}
130130

131-
/**
132-
* A {@link TreePathScanner} to power the subtree searches in this class
133-
*/
134-
static final class SearchScanner extends TreePathScanner<Optional<TreePath>, Void> {
131+
/** A {@link TreePathScanner} to power the subtree searches in this class */
132+
static final class SearchScanner extends TreePathScanner<Optional<TreePath>, @Nullable Void> {
135133
private final Optional<String> identifier;
136134
private final Tree.Kind kindSought;
137135

@@ -178,7 +176,7 @@ private Optional<TreePath> absentIfNull(Optional<TreePath> ret) {
178176
}
179177

180178
@Override
181-
public Optional<TreePath> scan(Tree node, Void v) {
179+
public Optional<TreePath> scan(Tree node, @Nullable Void v) {
182180
if (node == null) {
183181
return Optional.empty();
184182
}
@@ -189,7 +187,7 @@ public Optional<TreePath> scan(Tree node, Void v) {
189187
}
190188

191189
@Override
192-
public Optional<TreePath> scan(Iterable<? extends Tree> nodes, Void v) {
190+
public Optional<TreePath> scan(Iterable<? extends Tree> nodes, @Nullable Void v) {
193191
return absentIfNull(super.scan(nodes, v));
194192
}
195193

@@ -200,7 +198,7 @@ public Optional<TreePath> reduce(Optional<TreePath> t1, Optional<TreePath> t2) {
200198
}
201199

202200
@Override
203-
public Optional<TreePath> visitBreak(@Nullable BreakTree node, Void v) {
201+
public Optional<TreePath> visitBreak(@Nullable BreakTree node, @Nullable Void v) {
204202
if (node == null) {
205203
return Optional.empty();
206204
}
@@ -209,7 +207,7 @@ public Optional<TreePath> visitBreak(@Nullable BreakTree node, Void v) {
209207
}
210208

211209
@Override
212-
public Optional<TreePath> visitClass(@Nullable ClassTree node, Void v) {
210+
public Optional<TreePath> visitClass(@Nullable ClassTree node, @Nullable Void v) {
213211
if (node == null) {
214212
return Optional.empty();
215213
} else if (isMatch(node, node.getSimpleName())) {
@@ -220,7 +218,7 @@ public Optional<TreePath> visitClass(@Nullable ClassTree node, Void v) {
220218
}
221219

222220
@Override
223-
public Optional<TreePath> visitContinue(@Nullable ContinueTree node, Void v) {
221+
public Optional<TreePath> visitContinue(@Nullable ContinueTree node, @Nullable Void v) {
224222
if (node == null) {
225223
return Optional.empty();
226224
} else if (isMatch(node, node.getLabel())) {
@@ -231,7 +229,7 @@ public Optional<TreePath> visitContinue(@Nullable ContinueTree node, Void v) {
231229
}
232230

233231
@Override
234-
public Optional<TreePath> visitIdentifier(@Nullable IdentifierTree node, Void v) {
232+
public Optional<TreePath> visitIdentifier(@Nullable IdentifierTree node, @Nullable Void v) {
235233
if (node == null) {
236234
return Optional.empty();
237235
} else if (isMatch(node, node.getName())) {
@@ -242,7 +240,8 @@ public Optional<TreePath> visitIdentifier(@Nullable IdentifierTree node, Void v)
242240
}
243241

244242
@Override
245-
public Optional<TreePath> visitLabeledStatement(@Nullable LabeledStatementTree node, Void v) {
243+
public Optional<TreePath> visitLabeledStatement(
244+
@Nullable LabeledStatementTree node, @Nullable Void v) {
246245
if (node == null) {
247246
return Optional.empty();
248247
} else if (isMatch(node, node.getLabel())) {
@@ -253,7 +252,7 @@ public Optional<TreePath> visitLabeledStatement(@Nullable LabeledStatementTree n
253252
}
254253

255254
@Override
256-
public Optional<TreePath> visitLiteral(@Nullable LiteralTree node, Void v) {
255+
public Optional<TreePath> visitLiteral(@Nullable LiteralTree node, @Nullable Void v) {
257256
if (node == null) {
258257
return Optional.empty();
259258
} else if (isMatch(node, node.getValue())) {
@@ -264,7 +263,7 @@ public Optional<TreePath> visitLiteral(@Nullable LiteralTree node, Void v) {
264263
}
265264

266265
@Override
267-
public Optional<TreePath> visitMethod(@Nullable MethodTree node, Void v) {
266+
public Optional<TreePath> visitMethod(@Nullable MethodTree node, @Nullable Void v) {
268267
if (node == null) {
269268
return Optional.empty();
270269
} else if (isMatch(node, node.getName())) {
@@ -275,7 +274,7 @@ public Optional<TreePath> visitMethod(@Nullable MethodTree node, Void v) {
275274
}
276275

277276
@Override
278-
public Optional<TreePath> visitMemberSelect(@Nullable MemberSelectTree node, Void v) {
277+
public Optional<TreePath> visitMemberSelect(@Nullable MemberSelectTree node, @Nullable Void v) {
279278
if (node == null) {
280279
return Optional.empty();
281280
} else if (isMatch(node, node.getIdentifier())) {
@@ -286,7 +285,8 @@ public Optional<TreePath> visitMemberSelect(@Nullable MemberSelectTree node, Voi
286285
}
287286

288287
@Override
289-
public Optional<TreePath> visitTypeParameter(@Nullable TypeParameterTree node, Void v) {
288+
public Optional<TreePath> visitTypeParameter(
289+
@Nullable TypeParameterTree node, @Nullable Void v) {
290290
if (node == null) {
291291
return Optional.empty();
292292
} else if (isMatch(node, node.getName())) {
@@ -297,7 +297,7 @@ public Optional<TreePath> visitTypeParameter(@Nullable TypeParameterTree node, V
297297
}
298298

299299
@Override
300-
public Optional<TreePath> visitVariable(@Nullable VariableTree node, Void v) {
300+
public Optional<TreePath> visitVariable(@Nullable VariableTree node, @Nullable Void v) {
301301
if (node == null) {
302302
return Optional.empty();
303303
} else if (isMatch(node, node.getName())) {

src/main/java/com/google/testing/compile/ProcessedCompileTesterFactory.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.google.testing.compile;
1717

1818
import java.io.File;
19-
import javax.annotation.CheckReturnValue;
2019
import javax.annotation.processing.Processor;
2120

2221
/**
@@ -25,7 +24,6 @@
2524
*
2625
* @author Gregory Kick
2726
*/
28-
@CheckReturnValue
2927
public interface ProcessedCompileTesterFactory {
3028

3129
/**

0 commit comments

Comments
 (0)