aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcpovirk <cpovirk@google.com>2021-06-30 11:50:27 -0700
committerGoogle Java Core Libraries <java-libraries-firehose+copybara@google.com>2021-06-30 11:50:59 -0700
commit3b1f4492eb61192fbb48a8927ae2e6e136c0f445 (patch)
tree3f0633819de384ab2b1ec8d505eb0a2fb9ee93ba
parent9d79ce1e183556146e4be44c9f2be28a84405c0f (diff)
downloadauto-3b1f4492eb61192fbb48a8927ae2e6e136c0f445.tar.gz
Avoid wildcards, which confuse our nullness checker.
Specifically, wildcards defeat our ability to recognize that `containsKey` guarantees that `get` will return non-null. See https://github.com/jspecify/nullness-checker-for-checker-framework/blob/a13e6921d6e23ec1349be007b5e9a6d1f279c91c/src/main/java/com/google/jspecify/nullness/NullSpecTransfer.java#L603 Without this CL, Auto-Common would break when I submit CL 382342656 to annotate the remaining immutable-map classes. Arguably we should have a specific issue open for this problem, but: - This may be the first time it's come up in practice. - _Everything_ about wildcards in our checker is weird, thanks to a combination of the Checker Framework's unusual handling of them (which they are fixing by implementing capture conversion) and our hacks on top of that (which we hope can go away). (The very general project of "remove our hacks" is tracked by https://github.com/jspecify/checker-framework/issues/4#issuecomment-763111013 and other issues there.) RELNOTES=n/a PiperOrigin-RevId: 382352859
-rw-r--r--common/src/main/java/com/google/auto/common/AnnotationMirrors.java6
1 files changed, 4 insertions, 2 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 bc6255d2..9ce5cd9b 100644
--- a/common/src/main/java/com/google/auto/common/AnnotationMirrors.java
+++ b/common/src/main/java/com/google/auto/common/AnnotationMirrors.java
@@ -18,6 +18,7 @@ package com.google.auto.common;
import static com.google.auto.common.MoreElements.isAnnotationPresent;
import static com.google.auto.common.MoreStreams.toImmutableSet;
import static com.google.common.base.Preconditions.checkNotNull;
+import static java.util.Collections.unmodifiableMap;
import com.google.common.base.Equivalence;
import com.google.common.collect.ImmutableMap;
@@ -94,9 +95,10 @@ public final class AnnotationMirrors {
public static ImmutableMap<ExecutableElement, AnnotationValue> getAnnotationValuesWithDefaults(
AnnotationMirror annotation) {
ImmutableMap.Builder<ExecutableElement, AnnotationValue> values = ImmutableMap.builder();
+ // Use unmodifiableMap to eliminate wildcards, which cause issues for our nullness checker.
@SuppressWarnings("GetElementValues")
- Map<? extends ExecutableElement, ? extends AnnotationValue> declaredValues =
- annotation.getElementValues();
+ Map<ExecutableElement, AnnotationValue> declaredValues =
+ unmodifiableMap(annotation.getElementValues());
for (ExecutableElement method :
ElementFilter.methodsIn(annotation.getAnnotationType().asElement().getEnclosedElements())) {
// Must iterate and put in this order, to ensure consistency in generated code.