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 640d557171..bc6255d2fb 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 static Map.Entry getAnnotationElement */ public static ImmutableSet getAnnotatedAnnotations( Element element, Class 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 92a72c4957..d951aaf472 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.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.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 @@ private ImmutableSetMultimap validElements(RoundEnvironmen // Look at the elements we've found and the new elements from this round and validate them. for (TypeElement annotationType : getSupportedAnnotationTypeElements()) { - Set roundElements = - (annotationType == null) - ? ImmutableSet.of() - : roundEnv.getElementsAnnotatedWith(annotationType); + Set roundElements = roundEnv.getElementsAnnotatedWith(annotationType); ImmutableSet prevRoundElements = deferredElementsByAnnotation.get(annotationType); for (Element element : Sets.union(roundElements, prevRoundElements)) { ElementName elementName = ElementName.forAnnotatedElement(element); @@ -478,7 +477,8 @@ private static class ProcessingStepAsStep implements Step { processingStep.annotations().stream() .collect( toImmutableMap( - Class::getCanonicalName, (Class aClass) -> aClass)); + c -> requireNonNull(c.getCanonicalName()), + (Class aClass) -> aClass)); } @Override @@ -499,8 +499,12 @@ private ImmutableSetMultimap, Element> toClassKeyedM elements .asMap() .forEach( - (annotation, annotatedElements) -> - builder.putAll(annotationsByName.get(annotation), annotatedElements)); + (annotationName, annotatedElements) -> { + Class annotation = annotationsByName.get(annotationName); + if (annotation != null) { // should not be null + builder.putAll(annotation, annotatedElements); + } + }); return builder.build(); } } @@ -558,7 +562,7 @@ Optional getElement(Elements elements) { } @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 c8a478a439..dfbbaeef14 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 static boolean isAnnotationPresent(Element element, String annotationName */ public static Optional getAnnotationMirror( Element element, Class 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 cf273c2d62..1a4906263d 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.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.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 @@ private static class ComparedElements { } @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 @@ private Set visitingSetPlus( } @SuppressWarnings("TypeEquals") - private static boolean equal(TypeMirror a, TypeMirror b, Set visiting) { + private static boolean equal( + @Nullable TypeMirror a, @Nullable TypeMirror b, Set 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 @@ private static boolean equal(TypeMirror a, TypeMirror b, Set v // 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 @@ private static boolean equal(TypeMirror a, TypeMirror b, Set v * this bug 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 static ImmutableSet referencedTypes(TypeMirror type) { } private static final class ReferencedTypes - extends SimpleTypeVisitor8> { + extends SimpleTypeVisitor8<@Nullable Void, ImmutableSet.Builder> { private static final ReferencedTypes INSTANCE = new ReferencedTypes(); @Override - public Void visitArray(ArrayType t, ImmutableSet.Builder p) { + public @Nullable Void visitArray(ArrayType t, ImmutableSet.Builder p) { t.getComponentType().accept(this, p); return null; } @Override - public Void visitDeclared(DeclaredType t, ImmutableSet.Builder p) { + public @Nullable Void visitDeclared(DeclaredType t, ImmutableSet.Builder p) { p.add(MoreElements.asType(t.asElement())); for (TypeMirror typeArgument : t.getTypeArguments()) { typeArgument.accept(this, p); @@ -480,14 +489,14 @@ public Void visitDeclared(DeclaredType t, ImmutableSet.Builder p) { } @Override - public Void visitTypeVariable(TypeVariable t, ImmutableSet.Builder p) { + public @Nullable Void visitTypeVariable(TypeVariable t, ImmutableSet.Builder p) { t.getLowerBound().accept(this, p); t.getUpperBound().accept(this, p); return null; } @Override - public Void visitWildcard(WildcardType t, ImmutableSet.Builder p) { + public @Nullable Void visitWildcard(WildcardType t, ImmutableSet.Builder 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 ccedc1d24e..775c304abf 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.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 @@ public boolean 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 @@ public boolean 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 @@ private boolean isSubsignature( * implements List}. 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 erasedParameterTypes(ExecutableElement method, TypeElement in) { if (method.getParameters().isEmpty()) { return ImmutableList.of(); @@ -242,6 +246,7 @@ private class TypeSubstVisitor extends SimpleTypeVisitor8 { */ private final Map typeBindings = Maps.newLinkedHashMap(); + @Nullable ImmutableList erasedParameterTypes(ExecutableElement method, TypeElement in) { if (method.getEnclosingElement().equals(in)) { ImmutableList.Builder params = ImmutableList.builder(); @@ -320,7 +325,7 @@ public TypeMirror visitArray(ArrayType t, Void p) { * 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 @@ ExecutableElement methodFromSuperclasses(TypeElement in, ExecutableElement metho * 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 @@ ExecutableElement methodFromSuperinterfaces(TypeElement in, ExecutableElement me * 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 params = erasedParameterTypes(method, type); if (params == null) { @@ -393,7 +399,7 @@ private ExecutableElement methodInType(TypeElement type, ExecutableElement metho 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 e6d1e67781..7952eb3748 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.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 String toString() { } @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 a1e170d0f2..36f4ad6df5 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 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 0000000000..22b0c45ac8 --- /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 11b98706ae..f9426527ba 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 @@ 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.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.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 boolean process(Set annotations, RoundEnvironment * 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 options, String packageToMask) + private String runProcessor(ImmutableList 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 CharSequence getCharContent(boolean ignoreEncodingErrors) */ 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 03fa69cfaf..b98b79b92e 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 @@ 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 @@ private ExecutableElement getMethod(Class c, String methodName, TypeMirror... } } assertWithMessage(methodName + Arrays.toString(parameterTypes)).that(found).isNotNull(); - return found; + return requireNonNull(found); } private abstract static class AbstractAbstractList extends AbstractList {} 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 5ecf779f67..b8e84e0859 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.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 void testReferencedTypes() { 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 referencedTypes( + ImmutableMap 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 R accept(TypeVisitor v, P p) { } @Override - public List getTypeArguments() { + public ImmutableList 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[] getAnnotationsByType(Class annotationType) { + public A @Nullable [] getAnnotationsByType(Class annotationType) { return null; } @Override - public A getAnnotation(Class annotationType) { + public @Nullable A getAnnotation(Class 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 85ccdcd798..c5ccc5f6ae 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 @@ 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.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 @@ private ExecutableElement getMethod(TypeElement in, String name, TypeKind... par } } 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 @@ private abstract static class StringToRangeConverter> extends Converter> { @Override protected String doBackward(Range b) { - return null; + return ""; } } @@ -519,7 +521,7 @@ public void methodFromSuperclasses() { 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 void methodFromSuperinterfaces() { 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 actual, List expected) { - assertThat(actual.size()).isEqualTo(expected.size()); - for (int i = 0; i < actual.size(); i++) { + private void assertTypeListsEqual(@Nullable List actual, List 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 b1679cdbdf..ea85365bae 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.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 void declaredType() { public void visitorMethod() { SimpleTypeAnnotationValue.of(objectType) .accept( - new SimpleAnnotationValueVisitor8() { + 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(); } },