Compile Time Validation using Java Annotation Processor

[Updated: Sep 10, 2016, Created: Dec 29, 2015]

In this example, we are going to show how we can use annotation processor to do validation and type checking during compile time. We are going to create an annotation DateFormat with a string parameter which would expect from developer to put a correct date format which can be parse to java.time.LocalDate. The processor will also check the type of annotated field, to confirm if it's not other than java.time.LocalDate. On validation failure it will send error message to the compiler, which will fail the whole compilation process. The idea is, during runtime the target field will be displayed on some screen or on a web page by some automatic logic which is expecting a correct date format on the annotation. This example will demonstrate how can we fix the problems during compile time rather than discovering the problems much later, during runtime.

We are going to create two projects. The first one will create the annotation processor and second one will be the client code using the annotation DateFormat

Annotation Processor Project

  1. Create the simple maven project.
  2. In pom.xml, add the maven-compiler-plugin and specify compilerArgument as -proc:none. This is javac option to tell the compiler that only compile the source files without discovering and loading the processor. We will be loading the processor in the client project instead.
  3. Create annotation DateFormat
  4. Create DateFormatValidatorProcessor.
  5. Put FQN of the processor in META-INF/services/javax.annotation.processing.Processor.
  6. Install the project to the local repository using mvn install

Dependencies and Technologies Used :

  • JDK 1.8
  • Maven 3.0.4

Annotation Processor Validation Project Select All Download
  • annotation-processing
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • META-INF
            • services


Creating Client Project

  1. create simple maven project
  2. In pom add dependency of the first project annotation-processing
  3. Create TestClient using the annotation DateFormat on a field. Note we purposely put the wrong date format.


Dependencies and Technologies Used :

  • com.logicbig.example:annotation-processing 1.0-SNAPSHOT
  • JDK 1.8
  • Maven 3.0.4

Annotation Validation Client Select All Download
  • annotation-processing-test
    • src
      • main
        • java
          • com
            • logicbig
              • example

Compile from project root using mvn compile

D:\examples\annotation-processing-test>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building annotation-processing-test 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ annotation-processing-test ---
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ annotation-processing-test ---
[INFO] Compiling 1 source file to D:\examples\annotation-processing-test\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] error: Not a valid date format yyyy/MM/xd
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.657s
[INFO] Finished at: Tue Dec 29 03:36:50 CST 2015
[INFO] Final Memory: 12M/245M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project annotation-processing-test: Compilation failure
[ERROR] error: Not a valid date format yyyy/MM/xd
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Now change the date format on the annotation to valid one "yyyy/MM/dd" (you can set any date format which can be parsed to LocalDate). This time there won't be any error. Also try to change the type of field from LocalDate to say String. You will get type validation compiler error.

See Also