aboutsummaryrefslogtreecommitdiff
path: root/value
diff options
context:
space:
mode:
Diffstat (limited to 'value')
-rw-r--r--value/src/main/java/com/google/auto/value/processor/AutoValueOrOneOfProcessor.java21
-rw-r--r--value/src/main/java/com/google/auto/value/processor/Nullables.java26
-rw-r--r--value/src/test/java/com/google/auto/value/processor/NullablesTest.java5
3 files changed, 21 insertions, 31 deletions
diff --git a/value/src/main/java/com/google/auto/value/processor/AutoValueOrOneOfProcessor.java b/value/src/main/java/com/google/auto/value/processor/AutoValueOrOneOfProcessor.java
index 4b545bc0..55c2d0bf 100644
--- a/value/src/main/java/com/google/auto/value/processor/AutoValueOrOneOfProcessor.java
+++ b/value/src/main/java/com/google/auto/value/processor/AutoValueOrOneOfProcessor.java
@@ -433,7 +433,7 @@ abstract class AutoValueOrOneOfProcessor extends AbstractProcessor {
vars.toString = methodsToGenerate.containsKey(ObjectMethod.TO_STRING);
vars.equals = methodsToGenerate.containsKey(ObjectMethod.EQUALS);
vars.hashCode = methodsToGenerate.containsKey(ObjectMethod.HASH_CODE);
- Optional<DeclaredType> nullable = Nullables.nullableMentionedInMethods(methods);
+ Optional<AnnotationMirror> nullable = Nullables.nullableMentionedInMethods(methods);
vars.equalsParameterType = equalsParameterType(methodsToGenerate, nullable);
vars.serialVersionUID = getSerialVersionUID(type);
}
@@ -710,7 +710,7 @@ abstract class AutoValueOrOneOfProcessor extends AbstractProcessor {
* @param nullable the type of a {@code @Nullable} type annotation that we have found, if any
*/
static String equalsParameterType(
- Map<ObjectMethod, ExecutableElement> methodsToGenerate, Optional<DeclaredType> nullable) {
+ Map<ObjectMethod, ExecutableElement> methodsToGenerate, Optional<AnnotationMirror> nullable) {
ExecutableElement equals = methodsToGenerate.get(ObjectMethod.EQUALS);
if (equals == null) {
return ""; // this will not be referenced because no equals method will be generated
@@ -721,26 +721,11 @@ abstract class AutoValueOrOneOfProcessor extends AbstractProcessor {
// then that might be a type annotation or an annotation on the parameter.
ImmutableList<AnnotationMirror> extraAnnotations =
nullable.isPresent() && !nullableAnnotationFor(equals, parameterType).isPresent()
- ? ImmutableList.of(annotationMirror(nullable.get()))
+ ? ImmutableList.of(nullable.get())
: ImmutableList.of();
return TypeEncoder.encodeWithAnnotations(parameterType, extraAnnotations, ImmutableSet.of());
}
- private static AnnotationMirror annotationMirror(DeclaredType annotationType) {
- return new AnnotationMirror() {
- @Override
- public DeclaredType getAnnotationType() {
- return annotationType;
- }
-
- @Override
- public ImmutableMap<? extends ExecutableElement, ? extends AnnotationValue>
- getElementValues() {
- return ImmutableMap.of();
- }
- };
- }
-
/**
* Returns the subset of all abstract methods in the given set of methods. A given method
* signature is only mentioned once, even if it is inherited on more than one path. If any of the
diff --git a/value/src/main/java/com/google/auto/value/processor/Nullables.java b/value/src/main/java/com/google/auto/value/processor/Nullables.java
index 9c5e309c..3df23222 100644
--- a/value/src/main/java/com/google/auto/value/processor/Nullables.java
+++ b/value/src/main/java/com/google/auto/value/processor/Nullables.java
@@ -39,7 +39,8 @@ class Nullables {
* Returns the type of a {@code @Nullable} type-annotation, if one is found anywhere in the
* signatures of the given methods.
*/
- static Optional<DeclaredType> nullableMentionedInMethods(Collection<ExecutableElement> methods) {
+ static Optional<AnnotationMirror> nullableMentionedInMethods(
+ Collection<ExecutableElement> methods) {
return methods.stream()
.flatMap(
method ->
@@ -52,18 +53,19 @@ class Nullables {
.orElse(Optional.empty());
}
- private static Optional<DeclaredType> nullableIn(TypeMirror type) {
+ private static Optional<AnnotationMirror> nullableIn(TypeMirror type) {
return new NullableFinder().visit(type);
}
- private static Optional<DeclaredType> nullableIn(List<? extends AnnotationMirror> annotations) {
+ private static Optional<AnnotationMirror> nullableIn(
+ List<? extends AnnotationMirror> annotations) {
return annotations.stream()
- .map(AnnotationMirror::getAnnotationType)
- .filter(t -> t.asElement().getSimpleName().contentEquals("Nullable"))
+ .filter(a -> a.getAnnotationType().asElement().getSimpleName().contentEquals("Nullable"))
+ .map(a -> (AnnotationMirror) a) // get rid of the pesky wildcard
.findFirst();
}
- private static class NullableFinder extends SimpleTypeVisitor8<Optional<DeclaredType>, Void> {
+ private static class NullableFinder extends SimpleTypeVisitor8<Optional<AnnotationMirror>, Void> {
private final TypeMirrorSet visiting = new TypeMirrorSet();
NullableFinder() {
@@ -73,7 +75,7 @@ class Nullables {
// Primitives can't be @Nullable so we don't check that.
@Override
- public Optional<DeclaredType> visitDeclared(DeclaredType t, Void unused) {
+ public Optional<AnnotationMirror> visitDeclared(DeclaredType t, Void unused) {
if (!visiting.add(t)) {
return Optional.empty();
}
@@ -83,7 +85,7 @@ class Nullables {
}
@Override
- public Optional<DeclaredType> visitTypeVariable(TypeVariable t, Void unused) {
+ public Optional<AnnotationMirror> visitTypeVariable(TypeVariable t, Void unused) {
if (!visiting.add(t)) {
return Optional.empty();
}
@@ -93,14 +95,14 @@ class Nullables {
}
@Override
- public Optional<DeclaredType> visitArray(ArrayType t, Void unused) {
+ public Optional<AnnotationMirror> visitArray(ArrayType t, Void unused) {
return nullableIn(t.getAnnotationMirrors())
.map(Optional::of)
.orElseGet(() -> visit(t.getComponentType()));
}
@Override
- public Optional<DeclaredType> visitWildcard(WildcardType t, Void unused) {
+ public Optional<AnnotationMirror> visitWildcard(WildcardType t, Void unused) {
return nullableIn(t.getAnnotationMirrors())
.map(Optional::of)
.orElseGet(
@@ -112,13 +114,13 @@ class Nullables {
}
@Override
- public Optional<DeclaredType> visitIntersection(IntersectionType t, Void unused) {
+ public Optional<AnnotationMirror> visitIntersection(IntersectionType t, Void unused) {
return nullableIn(t.getAnnotationMirrors())
.map(Optional::of)
.orElseGet(() -> visitAll(t.getBounds()));
}
- private Optional<DeclaredType> visitAll(List<? extends TypeMirror> types) {
+ private Optional<AnnotationMirror> visitAll(List<? extends TypeMirror> types) {
return types.stream()
.map(this::visit)
.filter(Optional::isPresent)
diff --git a/value/src/test/java/com/google/auto/value/processor/NullablesTest.java b/value/src/test/java/com/google/auto/value/processor/NullablesTest.java
index 1648bd1f..9e345f53 100644
--- a/value/src/test/java/com/google/auto/value/processor/NullablesTest.java
+++ b/value/src/test/java/com/google/auto/value/processor/NullablesTest.java
@@ -39,6 +39,7 @@ import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion;
+import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
@@ -165,7 +166,9 @@ public class NullablesTest {
expect
.withMessage("method %s should have @Nullable", nullableMethod)
.about(optionals())
- .that(Nullables.nullableMentionedInMethods(notNullablePlusNullable))
+ .that(
+ Nullables.nullableMentionedInMethods(notNullablePlusNullable)
+ .map(AnnotationMirror::getAnnotationType))
.hasValue(nullableType);
}
ran = true;