Annotation Processing Tool <- validate annotation validation

I have

 @ColumnMetadata(index=1) ... @ColumnMetadata(index=2) ... @ColumnMetadata(index=3) ... 

And I have to check if the indices are unique using APT. I do not know how to do that. I don’t understand the textbooks, as a rule, I have a problem to find materials on the net.

How to do it? Any tutorials / anything about apt?

+4
source share
1 answer

You probably want to use the plugin annotation API, the successor to the apt tool. Here's a quick start guide: Java 6.0 Features Part 2: An Interchangeable Annotation Processing API

This is roughly what you need to do to check your annotations:

  • Create an annotation handler, it should extend AbstractProcessor .
  • Determine which annotations to look for, add:
    @SupportedAnnotationTypes(value= {"full.name.of.ColumnMetadata"})
  • Cancel the process method.
  • Use the RoundEnvironment parameter to access source code elements. What elements you need depends on what you want to do.
    • Top-down approach: getRootElements provides all classes that you can filter for specific elements that you want to test. This method is useful if you want to analyze the code structure around your annotations, for example, the class in which your method is located or property annotations.
    • Bottom approach: getElementsAnnotatedWith Use this method to get only annotated elements. You can infer the position of the elements, but you may need to track your elements if you want to compare them (for example, by matching the list of annotated elements with the type of the class).
  • Loop over the elements you want and get AnnotationMirror . Get and check the values.
  • If you want to report an error, use the element provided by Messager . You can create nice compiler error messages in your IDE with this.
+7
source

Source: https://habr.com/ru/post/1386394/


All Articles