4

Can anyone show me how to add annotation to a field using JavaPoet?

All examples I've managed to explore so far are about adding annotations to the classes and methods.

It looks like I'm missing something pretty straighforward here.

shabunc
  • 23,119
  • 19
  • 77
  • 102

1 Answers1

7

Hooray, I've figured it out, so might be it would be a help for someone else:

typeSpecBuilder
          .addField(
              FieldSpec.builder(<SomeClass>, <name>)
                  .addAnnotation(<AnnotationClass>)
                  .addModifiers(Modifier.PUBLIC)
                  .build()
          );
shabunc
  • 23,119
  • 19
  • 77
  • 102