Detect default field values in annotation processor
Stéphane Nicoll
snicoll at pivotal.io
Mon Nov 28 13:50:36 UTC 2016
Hello,
I am working on the Spring Boot project where we've built an annotation
processor that extracts some information about well-defined classes in
order to generate a metadata file. To reduce it to the scope of my
question, consider the following class:
@ConfigurationProperties
public class Foo {
private static final String DEFAULT_NAME = "name";
private String name = DEFAULT_NAME;
private Integer counter = 42;
private List<String> hosts = Collections.singletonList("localhost");
}
And an annotation processor that kicks in based on the presence of
@ConfigurationProperties.
I would like to be able to extract the field initialization values defined
in the class. Concretely:
* "name" requires us to browse to a symbol that is defined in the class.
That constant may be defined in the parent as well
* 42 is the actual symbol so it's easy
* "localhost" sounds a bit funky to extract but it's a heuristic that
worked quite well for us (read: checking if the right part of the statement
is a method invocation that we know, and if so extract the parameter
value(s)).
We have an implementation that works[1] but I am wondering if there isn't
an easier way and, more importantly, something that does not rely on a
compiler specific implementation. One drawback of this implementation is
that it does not work with the eclipse compiler.
Thoughts?
Thanks!
S.
[1]
https://github.com/spring-projects/spring-boot/tree/e15b3e463f312524495349673a16cb67cfaa2eae/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/fieldvalues/javac
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20161128/16c61b70/attachment.html>
More information about the compiler-dev
mailing list