Skip to content

Commit 3f829a6

Browse files
committed
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.
1 parent 95b36fc commit 3f829a6

File tree

1 file changed

+17
-1
lines changed
  • encoders/firebase-encoders-processor/src/main/java/com/google/firebase/encoders/processor/annotations

1 file changed

+17
-1
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", o);
140+
return null;
141+
}
142+
}.visit(value, null);
127143
}
128144
annotationBuilder.addMethod(
129145
MethodSpec.methodBuilder(field.name)

0 commit comments

Comments
 (0)