I am using the pluggable annotation processing api withing Java6+ to automatically create some deployment XML files. Part of these XML files contains a description of the object. The description is ALWAYS the same content as the Javadoc associated with the class itself. I could force the comment to be a field of the @Block annotation, but that duplicates the information. Is there any way during annotation processing to get the contents of the class/type comment?
In this example, I want to get "A nice description of my block" during annotation processing.
/**
* A nice description of my block
**/
@Block
public class CustomBlock {
}
As we briefly mentioned, annotations processors are typically used to inspect the codebase against the presence of particular annotations and, depending on use case, to: generate a set of source or resource files. mutate (modify) the existing source code. analyze the exiting source code and generate diagnostic messages.
Annotation processing is a Java compilation option which has been around since Java 5. It enables the generation of additional files during compilation, such as classes or documentation.
When a Processor is created, before any other methods are invoked, the init method of the AbstractProcessor will be invoked.
Annotation processing is a tool built into javac for scanning and processing annotations at compile time. It can create new source files; however, it can't modify existing ones. It's done in rounds. The first round starts when the compilation reaches the pre-compile phase.
I seem to always find the answer right after I post on SO.
For future reference, here is the solution
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.TypeElement;
public class CustomAnnotationProcessor extends AbstractProcessor
{
@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment re)
{
// use the protected member, processingEnv
String comment = processingEnv.getElementUtils().getDocComment(anyElement);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With