Skip to content

Commit 98e066a

Browse files
authored
Update AnnotationBuilder to not rely on toString() (#2725)
* Update AnnotationBuilder to not rely on toString() The toString() implementation of enum constants in annotations changed in JDK-8164819, this makes the previous behaviour more explicit. * Fix test.
1 parent b2112eb commit 98e066a

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

encoders/firebase-encoders-processor/src/main/java/com/google/firebase/encoders/processor/annotations/AnnotationBuilder.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
import javax.lang.model.element.ExecutableElement;
3333
import javax.lang.model.element.Modifier;
3434
import javax.lang.model.element.TypeElement;
35+
import javax.lang.model.element.VariableElement;
3536
import javax.lang.model.util.ElementFilter;
37+
import javax.lang.model.util.SimpleAnnotationValueVisitor8;
3638

3739
/**
3840
* Generates an annotation implementation.
@@ -123,7 +125,21 @@ private static TypeSpec createBuilder(
123125
FieldSpec.builder(field.type, field.name).addModifiers(Modifier.PRIVATE);
124126
AnnotationValue value = annotationImpl.defaults.get(field);
125127
if (value != null) {
126-
fieldSpec.initializer("$L", value);
128+
new SimpleAnnotationValueVisitor8<Void, Void>() {
129+
@Override
130+
public Void visitEnumConstant(VariableElement c, Void unused) {
131+
// Using $L would print the simple name of the enum constant in JDK 17,
132+
// see: https://github.com/square/javapoet/issues/845
133+
fieldSpec.initializer("$T.$L", c.asType(), c.getSimpleName());
134+
return null;
135+
}
136+
137+
@Override
138+
protected Void defaultAction(Object o, Void unused) {
139+
fieldSpec.initializer("$L", value);
140+
return null;
141+
}
142+
}.visit(value, null);
127143
}
128144
annotationBuilder.addMethod(
129145
MethodSpec.methodBuilder(field.name)

encoders/firebase-encoders-processor/src/test/resources/ExpectedAtMyAnnotation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public final class AtMyAnnotation {
3838

3939
private String strVal = "default";
4040

41-
private MyAnnotation.MyEnum enumVal = com.example.MyAnnotation.MyEnum.VALUE1;
41+
private MyAnnotation.MyEnum enumVal = MyAnnotation.MyEnum.VALUE1;
4242

4343
public AtMyAnnotation intVal(int intVal) {
4444
this.intVal = intVal;
@@ -224,4 +224,4 @@ public String toString() {
224224
return sb.toString();
225225
}
226226
}
227-
}
227+
}

0 commit comments

Comments
 (0)