Skip to content

Commit

Permalink
Prepare code for more JDK nullness annotations.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 569590313
  • Loading branch information
cpovirk authored and Compile-Testing Team committed Sep 29, 2023
1 parent 7fb694b commit 7573767
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/google/testing/compile/Compilation.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static com.google.common.base.Preconditions.checkState;
import static com.google.testing.compile.JavaFileObjects.asByteSource;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;
import static javax.tools.Diagnostic.Kind.ERROR;
Expand Down Expand Up @@ -155,9 +156,8 @@ public Optional<JavaFileObject> generatedFile(Location location, String path) {
// We're relying on the implementation of location.getName() to be equivalent to the first
// part of the path.
String expectedFilename = String.format("%s/%s", location.getName(), path);
return generatedFiles()
.stream()
.filter(generated -> generated.toUri().getPath().endsWith(expectedFilename))
return generatedFiles().stream()
.filter(generated -> requireNonNull(generated.toUri().getPath()).endsWith(expectedFilename))
.findFirst();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static com.google.testing.compile.Compilation.Status.SUCCESS;
import static com.google.testing.compile.JavaFileObjectSubject.javaFileObjects;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -338,7 +339,7 @@ private JavaFileObjectSubject checkGeneratedFile(
facts.add(fact("in location", location.getName()));
facts.add(simpleFact("it generated:"));
for (JavaFileObject generated : actualNotNull().generatedFiles()) {
if (generated.toUri().getPath().contains(location.getName())) {
if (requireNonNull(generated.toUri().getPath()).contains(location.getName())) {
facts.add(simpleFact(" " + generated.toUri().getPath()));
}
}
Expand Down Expand Up @@ -421,7 +422,8 @@ private ImmutableList<Diagnostic<? extends JavaFileObject>> findDiagnosticsInFil
filterDiagnostics(
diagnostic -> {
JavaFileObject source = diagnostic.getSource();
return source != null && source.toUri().getPath().equals(expectedFilePath);
return source != null
&& requireNonNull(source.toUri().getPath()).equals(expectedFilePath);
});
if (diagnosticsInFile.isEmpty()) {
failExpectingMatchingDiagnostic(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.testing.compile;

import static com.google.common.collect.MoreCollectors.toOptional;
import static java.util.Objects.requireNonNull;

import com.google.common.base.MoreObjects;
import com.google.common.cache.CacheBuilder;
Expand Down Expand Up @@ -129,7 +130,7 @@ private Optional<JavaFileObject> findInMemoryInput(String packageName, String re
String suffix =
packageName.isEmpty() ? relativeName : packageName.replace('.', '/') + "/" + relativeName;
return inMemoryInputs.entrySet().stream()
.filter(entry -> entry.getKey().getPath().endsWith(suffix))
.filter(entry -> requireNonNull(entry.getKey().getPath()).endsWith(suffix))
.map(Map.Entry::getValue)
.collect(toOptional()); // Might have problems if more than one input file matches.
}
Expand All @@ -151,7 +152,8 @@ public JavaFileObject getJavaFileForOutput(Location location, String className,
ImmutableList<JavaFileObject> getGeneratedSources() {
ImmutableList.Builder<JavaFileObject> result = ImmutableList.builder();
for (Map.Entry<URI, JavaFileObject> entry : inMemoryOutputs.asMap().entrySet()) {
if (entry.getKey().getPath().startsWith("/" + StandardLocation.SOURCE_OUTPUT.name())
if (requireNonNull(entry.getKey().getPath())
.startsWith("/" + StandardLocation.SOURCE_OUTPUT.name())
&& (entry.getValue().getKind() == Kind.SOURCE)) {
result.add(entry.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static com.google.testing.compile.TreeDiffer.diffCompilationUnits;
import static com.google.testing.compile.TreeDiffer.matchCompilationUnits;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;

import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteSource;
Expand Down Expand Up @@ -62,7 +63,7 @@ public static JavaFileObjectSubject assertThat(@Nullable JavaFileObject actual)

@Override
protected String actualCustomStringRepresentation() {
return actualNotNull().toUri().getPath();
return requireNonNull(actualNotNull().toUri().getPath());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;
import static javax.tools.JavaFileObject.Kind.SOURCE;

import com.google.common.base.CharMatcher;
Expand Down Expand Up @@ -162,7 +163,7 @@ public static JavaFileObject forResource(String resourceName) {
}

static Kind deduceKind(URI uri) {
String path = uri.getPath();
String path = requireNonNull(uri.getPath());
for (Kind kind : Kind.values()) {
if (path.endsWith(kind.extension)) {
return kind;
Expand Down

0 comments on commit 7573767

Please sign in to comment.