Skip to content

Commit 9d455fa

Browse files
eamonnmcmanusGoogle Java Core Libraries
authored andcommitted
Handle parameter annotations that are both PARAMETER and TYPE_USE.
For example, `org.jetbrains.annotations.NotNull`. Previously, we would end up putting `@NotNull` in the source code twice, once on the parameter and once on the type. Of course this just ends up looking like `@NotNull @NotNull String` and the compiler rejects it. RELNOTES=AutoFactory now correctly handles the case where a parameter annotation has `@Target` with both `PARAMETER` and `TYPE_USE`. PiperOrigin-RevId: 524844110
1 parent 6015fc3 commit 9d455fa

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

factory/src/main/java/com/google/auto/factory/processor/Parameter.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.google.auto.factory.processor;
1717

1818
import static com.google.auto.common.MoreStreams.toImmutableList;
19+
import static com.google.auto.common.MoreStreams.toImmutableSet;
1920
import static com.google.auto.factory.processor.Mirrors.unwrapOptionalEquivalence;
2021
import static com.google.auto.factory.processor.Mirrors.wrapOptionalInEquivalence;
2122
import static com.google.common.base.Preconditions.checkArgument;
@@ -80,23 +81,29 @@ Optional<AnnotationMirror> nullable() {
8081
}
8182
private static Parameter forVariableElement(
8283
VariableElement variable, TypeMirror type, Types types) {
83-
ImmutableList<AnnotationMirror> annotations =
84+
ImmutableList<AnnotationMirror> allAnnotations =
8485
Stream.of(variable.getAnnotationMirrors(), type.getAnnotationMirrors())
8586
.flatMap(List::stream)
8687
.collect(toImmutableList());
8788
Optional<AnnotationMirror> nullable =
88-
annotations.stream().filter(Parameter::isNullable).findFirst();
89-
ImmutableList<Equivalence.Wrapper<AnnotationMirror>> annotationWrappers =
89+
allAnnotations.stream().filter(Parameter::isNullable).findFirst();
90+
Key key = Key.create(type, allAnnotations, types);
91+
92+
ImmutableSet<Equivalence.Wrapper<AnnotationMirror>> typeAnnotationWrappers =
93+
type.getAnnotationMirrors().stream()
94+
.map(AnnotationMirrors.equivalence()::wrap)
95+
.collect(toImmutableSet());
96+
ImmutableList<Equivalence.Wrapper<AnnotationMirror>> parameterAnnotationWrappers =
9097
variable.getAnnotationMirrors().stream()
9198
.map(AnnotationMirrors.equivalence()::wrap)
99+
.filter(annotation -> !typeAnnotationWrappers.contains(annotation))
92100
.collect(toImmutableList());
93101

94-
Key key = Key.create(type, annotations, types);
95102
return new AutoValue_Parameter(
96103
MoreTypes.equivalence().wrap(type),
97104
variable.getSimpleName().toString(),
98105
key,
99-
annotationWrappers,
106+
parameterAnnotationWrappers,
100107
wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), nullable));
101108
}
102109

factory/src/test/resources/expected/ParameterAnnotationsFactory.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,24 @@ final class ParameterAnnotationsFactory {
3131
this.fooProvider = checkNotNull(fooProvider, 1);
3232
}
3333

34-
ParameterAnnotations create(@ParameterAnnotations.NullableParameter Integer bar, @ParameterAnnotations.Nullable Long baz, @ParameterAnnotations.NullableType Thread buh) {
35-
return new ParameterAnnotations(checkNotNull(fooProvider.get(), 1), checkNotNull(bar, 2), baz, checkNotNull(buh, 4));
34+
ParameterAnnotations create(
35+
@ParameterAnnotations.NullableParameter Integer bar,
36+
@ParameterAnnotations.Nullable Long baz,
37+
@ParameterAnnotations.NullableType Thread buh,
38+
@ParameterAnnotations.NullableParameterAndType String quux) {
39+
return new ParameterAnnotations(
40+
checkNotNull(fooProvider.get(), 1),
41+
checkNotNull(bar, 2),
42+
baz,
43+
checkNotNull(buh, 4),
44+
checkNotNull(quux, 5));
3645
}
3746

3847
private static <T> T checkNotNull(T reference, int argumentIndex) {
3948
if (reference == null) {
40-
throw new NullPointerException("@AutoFactory method argument is null but is not marked @Nullable. Argument index: " + argumentIndex);
49+
throw new NullPointerException(
50+
"@AutoFactory method argument is null but is not marked @Nullable. Argument index: "
51+
+ argumentIndex);
4152
}
4253
return reference;
4354
}

factory/src/test/resources/good/ParameterAnnotations.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@ final class ParameterAnnotations {
3939
@Target(TYPE_USE)
4040
@interface NullableType {}
4141

42+
@Retention(RUNTIME)
43+
@Target({PARAMETER, TYPE_USE})
44+
@interface NullableParameterAndType {}
45+
4246
ParameterAnnotations(
4347
@Provided @NullableParameter @NullableType String foo,
4448
@NullableParameter Integer bar,
4549
@Nullable Long baz,
46-
@NullableType Thread buh) {}
50+
@NullableType Thread buh,
51+
@NullableParameterAndType String quux) {}
4752
}

0 commit comments

Comments
 (0)