aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉamonn McManus <emcmanus@google.com>2021-06-30 08:43:10 -0700
committerGoogle Java Core Libraries <java-libraries-firehose+copybara@google.com>2021-06-30 08:43:49 -0700
commit9d79ce1e183556146e4be44c9f2be28a84405c0f (patch)
tree2a5aa30d300ae6b516d4f2c5458a08f872345ab9
parent2ee7f62aee5b486e0ed40e86909a00bf20ee203f (diff)
downloadauto-9d79ce1e183556146e4be44c9f2be28a84405c0f.tar.gz
Annotate auto-common for nullness.
RELNOTES=`com.google.auto.common` is annotated for null-safety. PiperOrigin-RevId: 382311770
-rw-r--r--common/src/main/java/com/google/auto/common/AnnotationMirrors.java6
-rw-r--r--common/src/main/java/com/google/auto/common/BasicAnnotationProcessor.java20
-rw-r--r--common/src/main/java/com/google/auto/common/MoreElements.java6
-rw-r--r--common/src/main/java/com/google/auto/common/MoreTypes.java31
-rw-r--r--common/src/main/java/com/google/auto/common/Overrides.java16
-rw-r--r--common/src/main/java/com/google/auto/common/SimpleAnnotationMirror.java3
-rw-r--r--common/src/main/java/com/google/auto/common/Visibility.java3
-rw-r--r--common/src/main/java/com/google/auto/common/package-info.java17
-rw-r--r--common/src/test/java/com/google/auto/common/GeneratedAnnotationsTest.java19
-rw-r--r--common/src/test/java/com/google/auto/common/MoreElementsTest.java3
-rw-r--r--common/src/test/java/com/google/auto/common/MoreTypesTest.java51
-rw-r--r--common/src/test/java/com/google/auto/common/OverridesTest.java22
-rw-r--r--common/src/test/java/com/google/auto/common/SimpleTypeAnnotationValueTest.java7
13 files changed, 131 insertions, 73 deletions
diff --git a/common/src/main/java/com/google/auto/common/AnnotationMirrors.java b/common/src/main/java/com/google/auto/common/AnnotationMirrors.java
index 640d5571..bc6255d2 100644
--- a/common/src/main/java/com/google/auto/common/AnnotationMirrors.java
+++ b/common/src/main/java/com/google/auto/common/AnnotationMirrors.java
@@ -159,7 +159,11 @@ public final class AnnotationMirrors {
*/
public static ImmutableSet<? extends AnnotationMirror> getAnnotatedAnnotations(
Element element, Class<? extends Annotation> annotationClass) {
- return getAnnotatedAnnotations(element, annotationClass.getCanonicalName());
+ String name = annotationClass.getCanonicalName();
+ if (name == null) {
+ return ImmutableSet.of();
+ }
+ return getAnnotatedAnnotations(element, name);
}
/**
diff --git a/common/src/main/java/com/google/auto/common/BasicAnnotationProcessor.java b/common/src/main/java/com/google/auto/common/BasicAnnotationProcessor.java
index 92a72c49..d951aaf4 100644
--- a/common/src/main/java/com/google/auto/common/BasicAnnotationProcessor.java
+++ b/common/src/main/java/com/google/auto/common/BasicAnnotationProcessor.java
@@ -24,6 +24,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Multimaps.filterKeys;
+import static java.util.Objects.requireNonNull;
import static javax.lang.model.element.ElementKind.PACKAGE;
import static javax.tools.Diagnostic.Kind.ERROR;
@@ -55,6 +56,7 @@ import javax.lang.model.element.TypeElement;
import javax.lang.model.type.ErrorType;
import javax.lang.model.util.Elements;
import javax.lang.model.util.SimpleElementVisitor8;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* An abstract {@link Processor} implementation that defers processing of {@link Element}s to later
@@ -286,10 +288,7 @@ public abstract class BasicAnnotationProcessor extends AbstractProcessor {
// Look at the elements we've found and the new elements from this round and validate them.
for (TypeElement annotationType : getSupportedAnnotationTypeElements()) {
- Set<? extends Element> roundElements =
- (annotationType == null)
- ? ImmutableSet.of()
- : roundEnv.getElementsAnnotatedWith(annotationType);
+ Set<? extends Element> roundElements = roundEnv.getElementsAnnotatedWith(annotationType);
ImmutableSet<Element> prevRoundElements = deferredElementsByAnnotation.get(annotationType);
for (Element element : Sets.union(roundElements, prevRoundElements)) {
ElementName elementName = ElementName.forAnnotatedElement(element);
@@ -478,7 +477,8 @@ public abstract class BasicAnnotationProcessor extends AbstractProcessor {
processingStep.annotations().stream()
.collect(
toImmutableMap(
- Class::getCanonicalName, (Class<? extends Annotation> aClass) -> aClass));
+ c -> requireNonNull(c.getCanonicalName()),
+ (Class<? extends Annotation> aClass) -> aClass));
}
@Override
@@ -499,8 +499,12 @@ public abstract class BasicAnnotationProcessor extends AbstractProcessor {
elements
.asMap()
.forEach(
- (annotation, annotatedElements) ->
- builder.putAll(annotationsByName.get(annotation), annotatedElements));
+ (annotationName, annotatedElements) -> {
+ Class<? extends Annotation> annotation = annotationsByName.get(annotationName);
+ if (annotation != null) { // should not be null
+ builder.putAll(annotation, annotatedElements);
+ }
+ });
return builder.build();
}
}
@@ -558,7 +562,7 @@ public abstract class BasicAnnotationProcessor extends AbstractProcessor {
}
@Override
- public boolean equals(Object object) {
+ public boolean equals(@Nullable Object object) {
if (!(object instanceof ElementName)) {
return false;
}
diff --git a/common/src/main/java/com/google/auto/common/MoreElements.java b/common/src/main/java/com/google/auto/common/MoreElements.java
index c8a478a4..dfbbaeef 100644
--- a/common/src/main/java/com/google/auto/common/MoreElements.java
+++ b/common/src/main/java/com/google/auto/common/MoreElements.java
@@ -253,7 +253,11 @@ public final class MoreElements {
*/
public static Optional<AnnotationMirror> getAnnotationMirror(
Element element, Class<? extends Annotation> annotationClass) {
- return getAnnotationMirror(element, annotationClass.getCanonicalName());
+ String name = annotationClass.getCanonicalName();
+ if (name == null) {
+ return Optional.absent();
+ }
+ return getAnnotationMirror(element, name);
}
/**
diff --git a/common/src/main/java/com/google/auto/common/MoreTypes.java b/common/src/main/java/com/google/auto/common/MoreTypes.java
index cf273c2d..1a490626 100644
--- a/common/src/main/java/com/google/auto/common/MoreTypes.java
+++ b/common/src/main/java/com/google/auto/common/MoreTypes.java
@@ -25,7 +25,6 @@ import static javax.lang.model.type.TypeKind.TYPEVAR;
import static javax.lang.model.type.TypeKind.WILDCARD;
import com.google.common.base.Equivalence;
-import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
@@ -55,6 +54,7 @@ import javax.lang.model.type.WildcardType;
import javax.lang.model.util.Elements;
import javax.lang.model.util.SimpleTypeVisitor8;
import javax.lang.model.util.Types;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Utilities related to {@link TypeMirror} instances.
@@ -140,7 +140,7 @@ public final class MoreTypes {
}
@Override
- public boolean equals(Object o) {
+ public boolean equals(@Nullable Object o) {
if (o instanceof ComparedElements) {
ComparedElements that = (ComparedElements) o;
int nArguments = aArguments.size();
@@ -297,7 +297,14 @@ public final class MoreTypes {
}
@SuppressWarnings("TypeEquals")
- private static boolean equal(TypeMirror a, TypeMirror b, Set<ComparedElements> visiting) {
+ private static boolean equal(
+ @Nullable TypeMirror a, @Nullable TypeMirror b, Set<ComparedElements> visiting) {
+ if (a == b) {
+ return true;
+ }
+ if (a == null || b == null) {
+ return false;
+ }
// TypeMirror.equals is not guaranteed to return true for types that are equal, but we can
// assume that if it does return true then the types are equal. This check also avoids getting
// stuck in infinite recursion when Eclipse decrees that the upper bound of the second K in
@@ -305,13 +312,15 @@ public final class MoreTypes {
// The javac implementation of ExecutableType, at least in some versions, does not take thrown
// exceptions into account in its equals implementation, so avoid this optimization for
// ExecutableType.
- if (Objects.equal(a, b) && !(a instanceof ExecutableType)) {
+ @SuppressWarnings("TypesEquals")
+ boolean equal = a.equals(b);
+ if (equal && !(a instanceof ExecutableType)) {
return true;
}
EqualVisitorParam p = new EqualVisitorParam();
p.type = b;
p.visiting = visiting;
- return (a == b) || (a != null && b != null && a.accept(EqualVisitor.INSTANCE, p));
+ return a.accept(EqualVisitor.INSTANCE, p);
}
/**
@@ -321,7 +330,7 @@ public final class MoreTypes {
* <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=508222">this bug</a> whereby
* the Eclipse compiler returns a value for static classes that is not NoType.
*/
- private static TypeMirror enclosingType(DeclaredType t) {
+ private static @Nullable TypeMirror enclosingType(DeclaredType t) {
TypeMirror enclosing = t.getEnclosingType();
if (enclosing.getKind().equals(TypeKind.NONE)
|| t.asElement().getModifiers().contains(Modifier.STATIC)) {
@@ -461,17 +470,17 @@ public final class MoreTypes {
}
private static final class ReferencedTypes
- extends SimpleTypeVisitor8<Void, ImmutableSet.Builder<TypeElement>> {
+ extends SimpleTypeVisitor8<@Nullable Void, ImmutableSet.Builder<TypeElement>> {
private static final ReferencedTypes INSTANCE = new ReferencedTypes();
@Override
- public Void visitArray(ArrayType t, ImmutableSet.Builder<TypeElement> p) {
+ public @Nullable Void visitArray(ArrayType t, ImmutableSet.Builder<TypeElement> p) {
t.getComponentType().accept(this, p);
return null;
}
@Override
- public Void visitDeclared(DeclaredType t, ImmutableSet.Builder<TypeElement> p) {
+ public @Nullable Void visitDeclared(DeclaredType t, ImmutableSet.Builder<TypeElement> p) {
p.add(MoreElements.asType(t.asElement()));
for (TypeMirror typeArgument : t.getTypeArguments()) {
typeArgument.accept(this, p);
@@ -480,14 +489,14 @@ public final class MoreTypes {
}
@Override
- public Void visitTypeVariable(TypeVariable t, ImmutableSet.Builder<TypeElement> p) {
+ public @Nullable Void visitTypeVariable(TypeVariable t, ImmutableSet.Builder<TypeElement> p) {
t.getLowerBound().accept(this, p);
t.getUpperBound().accept(this, p);
return null;
}
@Override
- public Void visitWildcard(WildcardType t, ImmutableSet.Builder<TypeElement> p) {
+ public @Nullable Void visitWildcard(WildcardType t, ImmutableSet.Builder<TypeElement> p) {
TypeMirror extendsBound = t.getExtendsBound();
if (extendsBound != null) {
extendsBound.accept(this, p);
diff --git a/common/src/main/java/com/google/auto/common/Overrides.java b/common/src/main/java/com/google/auto/common/Overrides.java
index ccedc1d2..775c304a 100644
--- a/common/src/main/java/com/google/auto/common/Overrides.java
+++ b/common/src/main/java/com/google/auto/common/Overrides.java
@@ -40,6 +40,7 @@ import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Elements;
import javax.lang.model.util.SimpleTypeVisitor8;
import javax.lang.model.util.Types;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Determines if one method overrides another. This class defines two ways of doing that:
@@ -142,7 +143,8 @@ abstract class Overrides {
// the enclosing elements rather than the methods themselves for the reason described
// at the start of the method.
ExecutableElement inherited = methodFromSuperclasses(in, overridden);
- return !overridden.getEnclosingElement().equals(inherited.getEnclosingElement());
+ return inherited != null
+ && !overridden.getEnclosingElement().equals(inherited.getEnclosingElement());
} else if (overriddenType.getKind().isInterface()) {
// ...overrides from C another method mI declared in interface I. We've already checked
// the conditions (assuming that the only alternative to mI being abstract or default is
@@ -158,7 +160,8 @@ abstract class Overrides {
// to methodFromSuperclasses above.
if (overrider.getModifiers().contains(Modifier.ABSTRACT)) {
ExecutableElement inherited = methodFromSuperinterfaces(in, overridden);
- return !overridden.getEnclosingElement().equals(inherited.getEnclosingElement());
+ return inherited != null
+ && !overridden.getEnclosingElement().equals(inherited.getEnclosingElement());
} else {
return true;
}
@@ -216,6 +219,7 @@ abstract class Overrides {
* implements List<E>}. The parameter types are erased since the purpose of this method is to
* determine whether two methods are candidates for one to override the other.
*/
+ @Nullable
ImmutableList<TypeMirror> erasedParameterTypes(ExecutableElement method, TypeElement in) {
if (method.getParameters().isEmpty()) {
return ImmutableList.of();
@@ -242,6 +246,7 @@ abstract class Overrides {
*/
private final Map<TypeParameterElement, TypeMirror> typeBindings = Maps.newLinkedHashMap();
+ @Nullable
ImmutableList<TypeMirror> erasedParameterTypes(ExecutableElement method, TypeElement in) {
if (method.getEnclosingElement().equals(in)) {
ImmutableList.Builder<TypeMirror> params = ImmutableList.builder();
@@ -320,7 +325,7 @@ abstract class Overrides {
* or the nearest override in a superclass of the given type, or null if the method is not
* found in the given type or any of its superclasses.
*/
- ExecutableElement methodFromSuperclasses(TypeElement in, ExecutableElement method) {
+ @Nullable ExecutableElement methodFromSuperclasses(TypeElement in, ExecutableElement method) {
for (TypeElement t = in; t != null; t = superclass(t)) {
ExecutableElement tMethod = methodInType(t, method);
if (tMethod != null) {
@@ -335,6 +340,7 @@ abstract class Overrides {
* itself, or the nearest override in a superinterface of the given type, or null if the method
* is not found in the given type or any of its transitive superinterfaces.
*/
+ @Nullable
ExecutableElement methodFromSuperinterfaces(TypeElement in, ExecutableElement method) {
TypeElement methodContainer = MoreElements.asType(method.getEnclosingElement());
Preconditions.checkArgument(methodContainer.getKind().isInterface());
@@ -371,7 +377,7 @@ abstract class Overrides {
* Returns the method from within the given type that has the same erased signature as the given
* method, or null if there is no such method.
*/
- private ExecutableElement methodInType(TypeElement type, ExecutableElement method) {
+ private @Nullable ExecutableElement methodInType(TypeElement type, ExecutableElement method) {
int nParams = method.getParameters().size();
List<TypeMirror> params = erasedParameterTypes(method, type);
if (params == null) {
@@ -393,7 +399,7 @@ abstract class Overrides {
return null;
}
- private TypeElement superclass(TypeElement type) {
+ private @Nullable TypeElement superclass(TypeElement type) {
TypeMirror sup = type.getSuperclass();
if (sup.getKind() == TypeKind.DECLARED) {
return MoreElements.asType(typeUtils.asElement(sup));
diff --git a/common/src/main/java/com/google/auto/common/SimpleAnnotationMirror.java b/common/src/main/java/com/google/auto/common/SimpleAnnotationMirror.java
index e6d1e677..7952eb37 100644
--- a/common/src/main/java/com/google/auto/common/SimpleAnnotationMirror.java
+++ b/common/src/main/java/com/google/auto/common/SimpleAnnotationMirror.java
@@ -32,6 +32,7 @@ import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* A simple implementation of the {@link AnnotationMirror} interface.
@@ -122,7 +123,7 @@ public final class SimpleAnnotationMirror implements AnnotationMirror {
}
@Override
- public boolean equals(Object other) {
+ public boolean equals(@Nullable Object other) {
return other instanceof AnnotationMirror
&& AnnotationMirrors.equivalence().equivalent(this, (AnnotationMirror) other);
}
diff --git a/common/src/main/java/com/google/auto/common/Visibility.java b/common/src/main/java/com/google/auto/common/Visibility.java
index a1e170d0..36f4ad6d 100644
--- a/common/src/main/java/com/google/auto/common/Visibility.java
+++ b/common/src/main/java/com/google/auto/common/Visibility.java
@@ -24,6 +24,7 @@ import java.util.Set;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Represents the visibility of a given {@link Element}: {@code public}, {@code protected},
@@ -41,7 +42,7 @@ public enum Visibility {
// TODO(ronshapiro): remove this and reference ElementKind.MODULE directly once we start building
// with -source 9
- private static final ElementKind MODULE =
+ private static final @Nullable ElementKind MODULE =
Enums.getIfPresent(ElementKind.class, "MODULE").orNull();
/**
diff --git a/common/src/main/java/com/google/auto/common/package-info.java b/common/src/main/java/com/google/auto/common/package-info.java
new file mode 100644
index 00000000..22b0c45a
--- /dev/null
+++ b/common/src/main/java/com/google/auto/common/package-info.java
@@ -0,0 +1,17 @@
+/*
+ * Copyright 2021 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.google.auto.common;
+
diff --git a/common/src/test/java/com/google/auto/common/GeneratedAnnotationsTest.java b/common/src/test/java/com/google/auto/common/GeneratedAnnotationsTest.java
index 11b98706..f9426527 100644
--- a/common/src/test/java/com/google/auto/common/GeneratedAnnotationsTest.java
+++ b/common/src/test/java/com/google/auto/common/GeneratedAnnotationsTest.java
@@ -18,6 +18,7 @@ package com.google.auto.common;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.Objects.requireNonNull;
import static org.junit.Assume.assumeTrue;
import com.google.common.collect.ImmutableList;
@@ -31,6 +32,7 @@ import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.URI;
import java.nio.file.Files;
+import java.util.Objects;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
@@ -44,6 +46,7 @@ import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;
+import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -99,12 +102,12 @@ public class GeneratedAnnotationsTest {
* Run {@link TestProcessor} in a compilation with the given {@code options}, and prevent the
* compilation from accessing classes with the qualified names in {@code maskFromClasspath}.
*/
- private String runProcessor(ImmutableList<String> options, String packageToMask)
+ private String runProcessor(ImmutableList<String> options, @Nullable String packageToMask)
throws IOException {
File tempDir = temporaryFolder.newFolder();
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager standardFileManager =
- compiler.getStandardFileManager(/* diagnostics= */ null, /* locale= */ null, UTF_8);
+ compiler.getStandardFileManager(/* diagnosticListener= */ null, /* locale= */ null, UTF_8);
standardFileManager.setLocation(StandardLocation.CLASS_OUTPUT, ImmutableList.of(tempDir));
StandardJavaFileManager proxyFileManager =
Reflection.newProxy(
@@ -142,18 +145,20 @@ public class GeneratedAnnotationsTest {
*/
private static class FileManagerInvocationHandler implements InvocationHandler {
private final StandardJavaFileManager fileManager;
- private final String packageToMask;
+ private final @Nullable String packageToMask;
- FileManagerInvocationHandler(StandardJavaFileManager fileManager, String packageToMask) {
+ FileManagerInvocationHandler(
+ StandardJavaFileManager fileManager, @Nullable String packageToMask) {
this.fileManager = fileManager;
this.packageToMask = packageToMask;
}
@Override
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ public Object invoke(Object proxy, Method method, @Nullable Object @Nullable [] args)
+ throws Throwable {
if (method.getName().equals("list")) {
- String packageName = (String) args[1];
- if (packageName.equals(packageToMask)) {
+ String packageName = (String) requireNonNull(args)[1];
+ if (Objects.equals(packageName, packageToMask)) {
return ImmutableList.of();
}
}
diff --git a/common/src/test/java/com/google/auto/common/MoreElementsTest.java b/common/src/test/java/com/google/auto/common/MoreElementsTest.java
index 03fa69cf..b98b79b9 100644
--- a/common/src/test/java/com/google/auto/common/MoreElementsTest.java
+++ b/common/src/test/java/com/google/auto/common/MoreElementsTest.java
@@ -18,6 +18,7 @@ package com.google.auto.common;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
+import static java.util.Objects.requireNonNull;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -479,7 +480,7 @@ public class MoreElementsTest {
}
}
assertWithMessage(methodName + Arrays.toString(parameterTypes)).that(found).isNotNull();
- return found;
+ return requireNonNull(found);
}
private abstract static class AbstractAbstractList extends AbstractList<String> {}
diff --git a/common/src/test/java/com/google/auto/common/MoreTypesTest.java b/common/src/test/java/com/google/auto/common/MoreTypesTest.java
index 5ecf779f..b8e84e08 100644
--- a/common/src/test/java/com/google/auto/common/MoreTypesTest.java
+++ b/common/src/test/java/com/google/auto/common/MoreTypesTest.java
@@ -16,6 +16,7 @@
package com.google.auto.common;
import static com.google.common.truth.Truth.assertThat;
+import static java.util.Objects.requireNonNull;
import static javax.lang.model.type.TypeKind.NONE;
import static javax.lang.model.type.TypeKind.VOID;
import static org.junit.Assert.assertThrows;
@@ -49,6 +50,7 @@ import javax.lang.model.type.WildcardType;
import javax.lang.model.util.ElementFilter;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
+import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -242,28 +244,27 @@ public class MoreTypesTest {
TypeElement charSequenceElement =
elements.getTypeElement(CharSequence.class.getCanonicalName());
- assertThat(MoreTypes.referencedTypes(fieldIndex.get("f1").asType()))
- .containsExactly(objectElement);
- assertThat(MoreTypes.referencedTypes(fieldIndex.get("f2").asType()))
- .containsExactly(setElement, stringElement);
- assertThat(MoreTypes.referencedTypes(fieldIndex.get("f3").asType()))
+ assertThat(referencedTypes(fieldIndex, "f1")).containsExactly(objectElement);
+ assertThat(referencedTypes(fieldIndex, "f2")).containsExactly(setElement, stringElement);
+ assertThat(referencedTypes(fieldIndex, "f3"))
.containsExactly(mapElement, stringElement, objectElement);
- assertThat(MoreTypes.referencedTypes(fieldIndex.get("f4").asType()))
- .containsExactly(integerElement);
- assertThat(MoreTypes.referencedTypes(fieldIndex.get("f5").asType()))
- .containsExactly(setElement);
- assertThat(MoreTypes.referencedTypes(fieldIndex.get("f6").asType()))
- .containsExactly(setElement, charSequenceElement);
- assertThat(MoreTypes.referencedTypes(fieldIndex.get("f7").asType()))
+ assertThat(referencedTypes(fieldIndex, "f4")).containsExactly(integerElement);
+ assertThat(referencedTypes(fieldIndex, "f5")).containsExactly(setElement);
+ assertThat(referencedTypes(fieldIndex, "f6")).containsExactly(setElement, charSequenceElement);
+ assertThat(referencedTypes(fieldIndex, "f7"))
.containsExactly(mapElement, stringElement, setElement, charSequenceElement);
- assertThat(MoreTypes.referencedTypes(fieldIndex.get("f8").asType()))
- .containsExactly(stringElement);
- assertThat(MoreTypes.referencedTypes(fieldIndex.get("f9").asType()))
- .containsExactly(stringElement);
- assertThat(MoreTypes.referencedTypes(fieldIndex.get("f10").asType())).isEmpty();
- assertThat(MoreTypes.referencedTypes(fieldIndex.get("f11").asType())).isEmpty();
- assertThat(MoreTypes.referencedTypes(fieldIndex.get("f12").asType()))
- .containsExactly(setElement, stringElement);
+ assertThat(referencedTypes(fieldIndex, "f8")).containsExactly(stringElement);
+ assertThat(referencedTypes(fieldIndex, "f9")).containsExactly(stringElement);
+ assertThat(referencedTypes(fieldIndex, "f10")).isEmpty();
+ assertThat(referencedTypes(fieldIndex, "f11")).isEmpty();
+ assertThat(referencedTypes(fieldIndex, "f12")).containsExactly(setElement, stringElement);
+ }
+
+ private static ImmutableSet<TypeElement> referencedTypes(
+ ImmutableMap<String, VariableElement> fieldIndex, String fieldName) {
+ VariableElement field = fieldIndex.get(fieldName);
+ requireNonNull(field, fieldName);
+ return MoreTypes.referencedTypes(field.asType());
}
@SuppressWarnings("unused") // types used in compiler tests
@@ -438,27 +439,27 @@ public class MoreTypesTest {
}
@Override
- public List<? extends TypeMirror> getTypeArguments() {
+ public ImmutableList<? extends TypeMirror> getTypeArguments() {
return ImmutableList.of();
}
@Override
- public TypeMirror getEnclosingType() {
+ public @Nullable TypeMirror getEnclosingType() {
return null;
}
@Override
- public Element asElement() {
+ public @Nullable Element asElement() {
return null;
}
@Override
- public <A extends Annotation> A[] getAnnotationsByType(Class<A> annotationType) {
+ public <A extends Annotation> A @Nullable [] getAnnotationsByType(Class<A> annotationType) {
return null;
}
@Override
- public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
+ public <A extends Annotation> @Nullable A getAnnotation(Class<A> annotationType) {
return null;
}
diff --git a/common/src/test/java/com/google/auto/common/OverridesTest.java b/common/src/test/java/com/google/auto/common/OverridesTest.java
index 85ccdcd7..c5ccc5f6 100644
--- a/common/src/test/java/com/google/auto/common/OverridesTest.java
+++ b/common/src/test/java/com/google/auto/common/OverridesTest.java
@@ -17,6 +17,7 @@ package com.google.auto.common;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.Objects.requireNonNull;
import static javax.lang.model.util.ElementFilter.methodsIn;
import com.google.common.base.Converter;
@@ -57,6 +58,7 @@ import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import org.eclipse.jdt.internal.compiler.tool.EclipseCompiler;
+import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -403,7 +405,7 @@ public class OverridesTest {
}
}
assertThat(found).isNotNull();
- return found;
+ return requireNonNull(found);
}
// These skeletal parallels to the real collection classes ensure that the test is independent
@@ -488,7 +490,7 @@ public class OverridesTest {
extends Converter<String, Range<T>> {
@Override
protected String doBackward(Range<T> b) {
- return null;
+ return "";
}
}
@@ -519,7 +521,7 @@ public class OverridesTest {
assertThat(addInAbstractStringList).isNull();
ExecutableElement addInStringList = explicitOverrides.methodFromSuperclasses(xStringList, add);
- assertThat(addInStringList.getEnclosingElement()).isEqualTo(xAbstractList);
+ assertThat(requireNonNull(addInStringList).getEnclosingElement()).isEqualTo(xAbstractList);
}
@Test
@@ -534,19 +536,21 @@ public class OverridesTest {
ExecutableElement addInAbstractStringList =
explicitOverrides.methodFromSuperinterfaces(xAbstractStringList, add);
- assertThat(addInAbstractStringList.getEnclosingElement()).isEqualTo(xCollection);
+ assertThat(requireNonNull(addInAbstractStringList).getEnclosingElement())
+ .isEqualTo(xCollection);
ExecutableElement addInNumberList =
explicitOverrides.methodFromSuperinterfaces(xNumberList, add);
- assertThat(addInNumberList.getEnclosingElement()).isEqualTo(xAbstractList);
+ assertThat(requireNonNull(addInNumberList).getEnclosingElement()).isEqualTo(xAbstractList);
ExecutableElement addInList = explicitOverrides.methodFromSuperinterfaces(xList, add);
- assertThat(addInList.getEnclosingElement()).isEqualTo(xCollection);
+ assertThat(requireNonNull(addInList).getEnclosingElement()).isEqualTo(xCollection);
}
- private void assertTypeListsEqual(List<TypeMirror> actual, List<TypeMirror> expected) {
- assertThat(actual.size()).isEqualTo(expected.size());
- for (int i = 0; i < actual.size(); i++) {
+ private void assertTypeListsEqual(@Nullable List<TypeMirror> actual, List<TypeMirror> expected) {
+ requireNonNull(actual);
+ assertThat(actual).hasSize(expected.size());
+ for (int i = 0; i < actual.size(); i++) {
assertThat(typeUtils.isSameType(actual.get(i), expected.get(i))).isTrue();
}
}
diff --git a/common/src/test/java/com/google/auto/common/SimpleTypeAnnotationValueTest.java b/common/src/test/java/com/google/auto/common/SimpleTypeAnnotationValueTest.java
index b1679cdb..ea85365b 100644
--- a/common/src/test/java/com/google/auto/common/SimpleTypeAnnotationValueTest.java
+++ b/common/src/test/java/com/google/auto/common/SimpleTypeAnnotationValueTest.java
@@ -28,6 +28,7 @@ import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.SimpleAnnotationValueVisitor8;
import javax.lang.model.util.Types;
+import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -72,15 +73,15 @@ public class SimpleTypeAnnotationValueTest {
public void visitorMethod() {
SimpleTypeAnnotationValue.of(objectType)
.accept(
- new SimpleAnnotationValueVisitor8<Void, Void>() {
+ new SimpleAnnotationValueVisitor8<@Nullable Void, @Nullable Void>() {
@Override
- public Void visitType(TypeMirror typeMirror, Void aVoid) {
+ public @Nullable Void visitType(TypeMirror typeMirror, @Nullable Void aVoid) {
// do nothing, expected case
return null;
}
@Override
- protected Void defaultAction(Object o, Void aVoid) {
+ protected @Nullable Void defaultAction(Object o, @Nullable Void aVoid) {
throw new AssertionError();
}
},