Search code examples
javaeclipseeclipse-jdt

Check Annotations of Methods using eclipse JDT library


I am building a prototype of a static analysis tool, for which I intend to use eclipse to do the heavy lifting. How can I check what annotations are applied on a method when I visit the declaration using the ASTVisitor. I am interested in only certain methods of the class under analysis, and I am thinking of marking them using annotations


Solution

  • Try ASTView plugin (http://www.eclipse.org/jdt/ui/astview/index.php), this helps to visualize the AST of a source file and also helps to figure out which nodes to visit.

    You would probably want to override the following in ASTVisitor

    • visit(MarkerAnnotation annotation)
    • visit(SingleMemberAnnotation annotation)
    • visit(NormalAnnotation annotation)

    Or you may visit only method declarations and get the annotations via MethodDeclaration.MODIFIERS2_PROPERTY.