Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the Pluggable Annotation Processor API retrieve source code comments?

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 {
}
like image 292
basszero Avatar asked Oct 28 '11 16:10

basszero


People also ask

How do annotation processors work?

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.

What is gradle annotation processor?

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 which of the following methods of AbstractProcessor will be invoked before all other methods?

When a Processor is created, before any other methods are invoked, the init method of the AbstractProcessor will be invoked.

What is annotation processing tool?

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.


1 Answers

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);
    }
}
like image 65
basszero Avatar answered Sep 17 '22 15:09

basszero