Skip to content

Commit 824568f

Browse files
committed
Update nullable logic for new encoding
1 parent 226924a commit 824568f

3 files changed

Lines changed: 24 additions & 17 deletions

File tree

value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,9 @@ private void defineVarsForType(
371371
@Override
372372
Optional<String> nullableAnnotationForMethod(
373373
ExecutableElement propertyMethod, ImmutableList<AnnotationMirror> methodAnnotations) {
374-
OptionalInt nullableAnnotationIndex = nullableAnnotationIndex(methodAnnotations);
375-
if (nullableAnnotationIndex.isPresent()) {
376-
ImmutableList<String> annotations = annotationStrings(methodAnnotations);
377-
return Optional.of(annotations.get(nullableAnnotationIndex.getAsInt()));
374+
Optional<String> nullableAnnotationList = nullableAnnotationIfInList(methodAnnotations);
375+
if (nullableAnnotationList.isPresent()) {
376+
return nullableAnnotationList;
378377
} else {
379378
List<? extends AnnotationMirror> typeAnnotations =
380379
propertyMethod.getReturnType().getAnnotationMirrors();
@@ -385,6 +384,15 @@ Optional<String> nullableAnnotationForMethod(
385384
}
386385
}
387386

387+
public static Optional<String> nullableAnnotationIfInList(ImmutableList<AnnotationMirror> methodAnnotations) {
388+
OptionalInt nullableAnnotationIndex = nullableAnnotationIndex(methodAnnotations);
389+
if (nullableAnnotationIndex.isPresent()) {
390+
ImmutableList<String> annotations = annotationStrings(methodAnnotations);
391+
return Optional.of(annotations.get(nullableAnnotationIndex.getAsInt()));
392+
}
393+
return Optional.empty();
394+
}
395+
388396
private static OptionalInt nullableAnnotationIndex(List<? extends AnnotationMirror> annotations) {
389397
for (int i = 0; i < annotations.size(); i++) {
390398
if (isNullable(annotations.get(i))) {

value/src/main/java/com/google/auto/value/processor/BuilderSpec.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.auto.common.MoreTypes;
2525
import com.google.auto.value.processor.AutoValueOrOneOfProcessor.Property;
2626
import com.google.common.collect.ImmutableBiMap;
27+
import com.google.common.collect.ImmutableList;
2728
import com.google.common.collect.ImmutableMap;
2829
import com.google.common.collect.ImmutableMultimap;
2930
import com.google.common.collect.ImmutableSet;
@@ -41,6 +42,7 @@
4142
import javax.lang.model.element.Modifier;
4243
import javax.lang.model.element.TypeElement;
4344
import javax.lang.model.element.TypeParameterElement;
45+
import javax.lang.model.element.VariableElement;
4446
import javax.lang.model.type.DeclaredType;
4547
import javax.lang.model.type.TypeKind;
4648
import javax.lang.model.type.TypeMirror;
@@ -306,20 +308,16 @@ public PropertySetter(ExecutableElement setter, TypeMirror propertyType) {
306308
this.nullableAnnotation = "";
307309
} else {
308310
String rawTarget = TypeEncoder.encodeRaw(erasedPropertyType);
309-
Optionalish optional = Optionalish.createIfOptional(propertyType, rawTarget);
311+
Optionalish optional = Optionalish.createIfOptional(propertyType);
310312
String nullableAnnotation = "";
311-
String of = null;
313+
String of;
312314
if (optional != null) {
313-
for (AnnotationMirror annotationMirror : parameterElement.getAnnotationMirrors()) {
314-
AnnotationOutput annotationOutput = new AnnotationOutput(typeSimplifier);
315-
String annotationName = annotationOutput.sourceFormForAnnotation(annotationMirror);
316-
if (annotationName.equals("@Nullable") || annotationName.endsWith(".Nullable")) {
317-
of = optional.getNullable();
318-
nullableAnnotation = annotationName + " ";
319-
break;
320-
}
321-
}
322-
if (of == null) {
315+
ImmutableList<AnnotationMirror> annotationMirrors = ImmutableList.copyOf(parameterElement.getAnnotationMirrors());
316+
Optional<String> nullableAnnotationFromParam = AutoValueProcessor.nullableAnnotationIfInList(annotationMirrors);
317+
if (nullableAnnotationFromParam.isPresent()) {
318+
of = optional.getNullable();
319+
nullableAnnotation = nullableAnnotationFromParam.get() + " ";
320+
} else {
323321
of = "of";
324322
}
325323
} else {

value/src/main/java/com/google/auto/value/processor/Optionalish.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ public String getEmpty() {
9898

9999
/**
100100
* Returns a string representing the method call to obtain the nullable version of this Optional.
101-
* This will be something like {@code "fromNullable()"} or possibly {@code "ofNullable()"}. It does not have a final semicolon.
101+
* This will be something like {@code "fromNullable()"} or possibly {@code "ofNullable()"}.
102+
* It does not have a final semicolon.
102103
*
103104
* <p>This method is public so that it can be referenced as {@code p.optional.nullable} from
104105
* templates.

0 commit comments

Comments
 (0)