Skip to content

Commit

Permalink
Handle parameter annotations that are both PARAMETER and TYPE_USE.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eamonnmcmanus authored and Google Java Core Libraries committed Apr 17, 2023
1 parent 6015fc3 commit 9d455fa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.auto.factory.processor;

import static com.google.auto.common.MoreStreams.toImmutableList;
import static com.google.auto.common.MoreStreams.toImmutableSet;
import static com.google.auto.factory.processor.Mirrors.unwrapOptionalEquivalence;
import static com.google.auto.factory.processor.Mirrors.wrapOptionalInEquivalence;
import static com.google.common.base.Preconditions.checkArgument;
Expand Down Expand Up @@ -80,23 +81,29 @@ Optional<AnnotationMirror> nullable() {
}
private static Parameter forVariableElement(
VariableElement variable, TypeMirror type, Types types) {
ImmutableList<AnnotationMirror> annotations =
ImmutableList<AnnotationMirror> allAnnotations =
Stream.of(variable.getAnnotationMirrors(), type.getAnnotationMirrors())
.flatMap(List::stream)
.collect(toImmutableList());
Optional<AnnotationMirror> nullable =
annotations.stream().filter(Parameter::isNullable).findFirst();
ImmutableList<Equivalence.Wrapper<AnnotationMirror>> annotationWrappers =
allAnnotations.stream().filter(Parameter::isNullable).findFirst();
Key key = Key.create(type, allAnnotations, types);

ImmutableSet<Equivalence.Wrapper<AnnotationMirror>> typeAnnotationWrappers =
type.getAnnotationMirrors().stream()
.map(AnnotationMirrors.equivalence()::wrap)
.collect(toImmutableSet());
ImmutableList<Equivalence.Wrapper<AnnotationMirror>> parameterAnnotationWrappers =
variable.getAnnotationMirrors().stream()
.map(AnnotationMirrors.equivalence()::wrap)
.filter(annotation -> !typeAnnotationWrappers.contains(annotation))
.collect(toImmutableList());

Key key = Key.create(type, annotations, types);
return new AutoValue_Parameter(
MoreTypes.equivalence().wrap(type),
variable.getSimpleName().toString(),
key,
annotationWrappers,
parameterAnnotationWrappers,
wrapOptionalInEquivalence(AnnotationMirrors.equivalence(), nullable));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,24 @@ final class ParameterAnnotationsFactory {
this.fooProvider = checkNotNull(fooProvider, 1);
}

ParameterAnnotations create(@ParameterAnnotations.NullableParameter Integer bar, @ParameterAnnotations.Nullable Long baz, @ParameterAnnotations.NullableType Thread buh) {
return new ParameterAnnotations(checkNotNull(fooProvider.get(), 1), checkNotNull(bar, 2), baz, checkNotNull(buh, 4));
ParameterAnnotations create(
@ParameterAnnotations.NullableParameter Integer bar,
@ParameterAnnotations.Nullable Long baz,
@ParameterAnnotations.NullableType Thread buh,
@ParameterAnnotations.NullableParameterAndType String quux) {
return new ParameterAnnotations(
checkNotNull(fooProvider.get(), 1),
checkNotNull(bar, 2),
baz,
checkNotNull(buh, 4),
checkNotNull(quux, 5));
}

private static <T> T checkNotNull(T reference, int argumentIndex) {
if (reference == null) {
throw new NullPointerException("@AutoFactory method argument is null but is not marked @Nullable. Argument index: " + argumentIndex);
throw new NullPointerException(
"@AutoFactory method argument is null but is not marked @Nullable. Argument index: "
+ argumentIndex);
}
return reference;
}
Expand Down
7 changes: 6 additions & 1 deletion factory/src/test/resources/good/ParameterAnnotations.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ final class ParameterAnnotations {
@Target(TYPE_USE)
@interface NullableType {}

@Retention(RUNTIME)
@Target({PARAMETER, TYPE_USE})
@interface NullableParameterAndType {}

ParameterAnnotations(
@Provided @NullableParameter @NullableType String foo,
@NullableParameter Integer bar,
@Nullable Long baz,
@NullableType Thread buh) {}
@NullableType Thread buh,
@NullableParameterAndType String quux) {}
}

0 comments on commit 9d455fa

Please sign in to comment.