Skip to main content

How can an AnnotationProcessor generate boilerplate in Java existing source

1 reply [Last post]
Joined: 2006-05-05

I’ve written Annotations, and even used them to generate additional source classes from an annotated original class.
What I have not found is a simple explanation of how to “invisibly insert” boilerplate code into an existing class, and have the results be compiled as if the source had been there all along.

For example, there is discussion of annotations that allow you to put a “@getter” annotation on a class’s field, and when the class is compiled, you can call a that getter method, generated from the field’s name. Yet there is no additional class source file.

How do they do that?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2006-05-05

Well, after reading the forums at

It looks like the answer is
[b]They Cheat[/b]

The correct way is to use a generated superclass or subclass.

However, at least one person ( pfnguyen ) has used the com.sun.source.tree.* and com.sun.source.util.* to do what I think what I want. But I have not seen the code.