Skip to content

Commit

Permalink
Use @Nullable from the Checker Framework and fix a few null-correct…
Browse files Browse the repository at this point in the history
…ness issues.

RELNOTES= Use `@Nullable` from the Checker Framework and fix a few null-correctness issues.
PiperOrigin-RevId: 380043179
  • Loading branch information
netdpb authored and Compile-Testing Team committed Jun 17, 2021
1 parent 620d236 commit 6260258
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 176 deletions.
11 changes: 5 additions & 6 deletions pom.xml
Expand Up @@ -68,12 +68,6 @@
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotations</artifactId>
Expand All @@ -90,6 +84,11 @@
<artifactId>auto-common</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.checkerframework</groupId>
<artifactId>checker-qual</artifactId>
<version>3.14.0</version>
</dependency>
</dependencies>

<build>
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/com/google/testing/compile/Compiler.java
Expand Up @@ -44,6 +44,7 @@
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;
import org.checkerframework.checker.nullness.qual.Nullable;

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

@VisibleForTesting static final ClassLoader platformClassLoader = getPlatformClassLoader();
@VisibleForTesting
static final @Nullable ClassLoader platformClassLoader = getPlatformClassLoader();

private static ClassLoader getPlatformClassLoader() {
private static @Nullable ClassLoader getPlatformClassLoader() {
try {
// JDK >= 9
return (ClassLoader) ClassLoader.class.getMethod("getPlatformClassLoader").invoke(null);
Expand All @@ -229,12 +231,14 @@ private static ClassLoader getPlatformClassLoader() {
* @throws IllegalArgumentException if the given classloader had classpaths which we could not
* determine or use for compilation.
*/
private static ImmutableList<File> getClasspathFromClassloader(ClassLoader currentClassloader) {
private static ImmutableList<File> getClasspathFromClassloader(ClassLoader classloader) {
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();

// Concatenate search paths from all classloaders in the hierarchy 'till the system classloader.
Set<String> classpaths = new LinkedHashSet<>();
while (true) {
for (ClassLoader currentClassloader = classloader;
;
currentClassloader = currentClassloader.getParent()) {
if (currentClassloader == systemClassLoader) {
Iterables.addAll(
classpaths,
Expand Down Expand Up @@ -263,7 +267,6 @@ private static ImmutableList<File> getClasspathFromClassloader(ClassLoader curre
+ "since %s is not an instance of URLClassloader",
currentClassloader));
}
currentClassloader = currentClassloader.getParent();
}

return classpaths.stream().map(File::new).collect(toImmutableList());
Expand Down
Expand Up @@ -42,6 +42,7 @@
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import org.checkerframework.checker.nullness.qual.Nullable;

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

@Override
public FileObject getFileForInput(Location location, String packageName,
String relativeName) throws IOException {
public @Nullable FileObject getFileForInput(
Location location, String packageName, String relativeName) throws IOException {
if (location.isOutputLocation()) {
return inMemoryOutputs.getIfPresent(uriForFileObject(location, packageName, relativeName));
}
Expand All @@ -103,8 +104,8 @@ public FileObject getFileForInput(Location location, String packageName,
}

@Override
public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind)
throws IOException {
public @Nullable JavaFileObject getJavaFileForInput(
Location location, String className, Kind kind) throws IOException {
if (location.isOutputLocation()) {
return inMemoryOutputs.getIfPresent(uriForJavaFileObject(location, className, kind));
}
Expand Down
Expand Up @@ -33,8 +33,8 @@
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.function.BiFunction;
import javax.annotation.Nullable;
import javax.tools.JavaFileObject;
import org.checkerframework.checker.nullness.qual.Nullable;

/** Assertions about {@link JavaFileObject}s. */
public final class JavaFileObjectSubject extends Subject {
Expand Down Expand Up @@ -72,6 +72,7 @@ protected String actualCustomStringRepresentation() {
public void isEqualTo(@Nullable Object other) {
if (!(other instanceof JavaFileObject)) {
super.isEqualTo(other);
return;
}

JavaFileObject otherFile = (JavaFileObject) other;
Expand Down
Expand Up @@ -57,12 +57,12 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Nullable;
import javax.annotation.processing.Processor;
import javax.tools.Diagnostic;
import javax.tools.Diagnostic.Kind;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* A <a href="https://github.com/truth0/truth">Truth</a> {@link Subject} that evaluates the result
Expand Down Expand Up @@ -213,7 +213,11 @@ public void parsesAs(JavaFileObject first, JavaFileObject... rest) {
final CompilationUnitTree expectedTree = matchedTreePair.getKey();
if (!matchedTreePair.getValue().isPresent()) {
failNoCandidates(
expectedTreeTypes.get(expectedTree), expectedTree, actualTreeTypes, actualTrees);
// see comment above
requireNonNull(expectedTreeTypes.get(expectedTree)),
expectedTree,
actualTreeTypes,
actualTrees);
} else {
CompilationUnitTree actualTree = matchedTreePair.getValue().get();
TreeDifference treeDifference = TreeDiffer.diffCompilationUnits(expectedTree, actualTree);
Expand Down
34 changes: 17 additions & 17 deletions src/main/java/com/google/testing/compile/MoreTrees.java
Expand Up @@ -35,7 +35,7 @@
import com.sun.source.util.TreePathScanner;
import java.util.Arrays;
import java.util.Optional;
import javax.annotation.Nullable;
import org.checkerframework.checker.nullness.qual.Nullable;

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

/**
* A {@link TreePathScanner} to power the subtree searches in this class
*/
static final class SearchScanner extends TreePathScanner<Optional<TreePath>, Void> {
/** A {@link TreePathScanner} to power the subtree searches in this class */
static final class SearchScanner extends TreePathScanner<Optional<TreePath>, @Nullable Void> {
private final Optional<String> identifier;
private final Tree.Kind kindSought;

Expand Down Expand Up @@ -178,7 +176,7 @@ private Optional<TreePath> absentIfNull(Optional<TreePath> ret) {
}

@Override
public Optional<TreePath> scan(Tree node, Void v) {
public Optional<TreePath> scan(Tree node, @Nullable Void v) {
if (node == null) {
return Optional.empty();
}
Expand All @@ -189,7 +187,7 @@ public Optional<TreePath> scan(Tree node, Void v) {
}

@Override
public Optional<TreePath> scan(Iterable<? extends Tree> nodes, Void v) {
public Optional<TreePath> scan(Iterable<? extends Tree> nodes, @Nullable Void v) {
return absentIfNull(super.scan(nodes, v));
}

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

@Override
public Optional<TreePath> visitBreak(@Nullable BreakTree node, Void v) {
public Optional<TreePath> visitBreak(@Nullable BreakTree node, @Nullable Void v) {
if (node == null) {
return Optional.empty();
}
Expand All @@ -209,7 +207,7 @@ public Optional<TreePath> visitBreak(@Nullable BreakTree node, Void v) {
}

@Override
public Optional<TreePath> visitClass(@Nullable ClassTree node, Void v) {
public Optional<TreePath> visitClass(@Nullable ClassTree node, @Nullable Void v) {
if (node == null) {
return Optional.empty();
} else if (isMatch(node, node.getSimpleName())) {
Expand All @@ -220,7 +218,7 @@ public Optional<TreePath> visitClass(@Nullable ClassTree node, Void v) {
}

@Override
public Optional<TreePath> visitContinue(@Nullable ContinueTree node, Void v) {
public Optional<TreePath> visitContinue(@Nullable ContinueTree node, @Nullable Void v) {
if (node == null) {
return Optional.empty();
} else if (isMatch(node, node.getLabel())) {
Expand All @@ -231,7 +229,7 @@ public Optional<TreePath> visitContinue(@Nullable ContinueTree node, Void v) {
}

@Override
public Optional<TreePath> visitIdentifier(@Nullable IdentifierTree node, Void v) {
public Optional<TreePath> visitIdentifier(@Nullable IdentifierTree node, @Nullable Void v) {
if (node == null) {
return Optional.empty();
} else if (isMatch(node, node.getName())) {
Expand All @@ -242,7 +240,8 @@ public Optional<TreePath> visitIdentifier(@Nullable IdentifierTree node, Void v)
}

@Override
public Optional<TreePath> visitLabeledStatement(@Nullable LabeledStatementTree node, Void v) {
public Optional<TreePath> visitLabeledStatement(
@Nullable LabeledStatementTree node, @Nullable Void v) {
if (node == null) {
return Optional.empty();
} else if (isMatch(node, node.getLabel())) {
Expand All @@ -253,7 +252,7 @@ public Optional<TreePath> visitLabeledStatement(@Nullable LabeledStatementTree n
}

@Override
public Optional<TreePath> visitLiteral(@Nullable LiteralTree node, Void v) {
public Optional<TreePath> visitLiteral(@Nullable LiteralTree node, @Nullable Void v) {
if (node == null) {
return Optional.empty();
} else if (isMatch(node, node.getValue())) {
Expand All @@ -264,7 +263,7 @@ public Optional<TreePath> visitLiteral(@Nullable LiteralTree node, Void v) {
}

@Override
public Optional<TreePath> visitMethod(@Nullable MethodTree node, Void v) {
public Optional<TreePath> visitMethod(@Nullable MethodTree node, @Nullable Void v) {
if (node == null) {
return Optional.empty();
} else if (isMatch(node, node.getName())) {
Expand All @@ -275,7 +274,7 @@ public Optional<TreePath> visitMethod(@Nullable MethodTree node, Void v) {
}

@Override
public Optional<TreePath> visitMemberSelect(@Nullable MemberSelectTree node, Void v) {
public Optional<TreePath> visitMemberSelect(@Nullable MemberSelectTree node, @Nullable Void v) {
if (node == null) {
return Optional.empty();
} else if (isMatch(node, node.getIdentifier())) {
Expand All @@ -286,7 +285,8 @@ public Optional<TreePath> visitMemberSelect(@Nullable MemberSelectTree node, Voi
}

@Override
public Optional<TreePath> visitTypeParameter(@Nullable TypeParameterTree node, Void v) {
public Optional<TreePath> visitTypeParameter(
@Nullable TypeParameterTree node, @Nullable Void v) {
if (node == null) {
return Optional.empty();
} else if (isMatch(node, node.getName())) {
Expand All @@ -297,7 +297,7 @@ public Optional<TreePath> visitTypeParameter(@Nullable TypeParameterTree node, V
}

@Override
public Optional<TreePath> visitVariable(@Nullable VariableTree node, Void v) {
public Optional<TreePath> visitVariable(@Nullable VariableTree node, @Nullable Void v) {
if (node == null) {
return Optional.empty();
} else if (isMatch(node, node.getName())) {
Expand Down
Expand Up @@ -16,7 +16,6 @@
package com.google.testing.compile;

import java.io.File;
import javax.annotation.CheckReturnValue;
import javax.annotation.processing.Processor;

/**
Expand All @@ -25,7 +24,6 @@
*
* @author Gregory Kick
*/
@CheckReturnValue
public interface ProcessedCompileTesterFactory {

/**
Expand Down

0 comments on commit 6260258

Please sign in to comment.