Enforcing Rules using Java Annotation Processor

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

In this example, we are going to show how we can use the processor API to enforce design rules.

There are scenarios when some particular classes should implements equals/hashCode methods. May be they are going to be stored in a java.util.Map/java.util.Collection or some logic is going to compare them via equals method. Usually this kind of rules are tested during runtime and sometimes the related problems (if the developer forgot to implement the method) are discovered very late, may be in production.

Assume we are expecting all classes implementing a particular interface say Data are required to implement equals method. In this example we will also see how we can use the processor api without registering any annotation. We just have to specify @SupportedAnnotationTypes("*") on the processor class. So generally speaking the processor API is not only useful for annotations processing but we can enforce any kind of design rules.

In this example too, we are going to create two projects. The first one will create the processor and second one will be the client code implementing the interface Data

For the both projects, steps are exactly same as last example.

The Processor Project


Dependencies and Technologies Used :

  • JDK 1.8
  • Maven 3.0.4

Equal Method Check Processor Select All Download
  • equal-method-processor
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • META-INF
            • services


The Client Project

After installing the Processor project 'equal-method-processor' to local maven repository, create another maven project 'equal-method-client' as shown in the following project browser.


Make sure to add dependency of the processor project:

    <dependencies>
        <dependency>
            <groupId>com.logicbig.example</groupId>
            <artifactId>equal-method-processor</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

Dependencies and Technologies Used :

  • com.logicbig.example:equal-method-processor 1.0-SNAPSHOT
  • JDK 1.8
  • Maven 3.0.4

Equal Method Check Client Select All Download
  • equal-method-client
    • src
      • main
        • java
          • com
            • logicbig
              • example

In the client code we created two implementations of the interface Data, DataA and DataB, and the one without implementing the interface, named as Other. We purposely didn't implement the equals method in DataB.

Here's the output on compiling the client project

D:\example-projects\equal-method-client>mvn compile
Listening for transport dt_socket at address: 8000
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building equal-method-client 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ equal-method-client ---
[debug] execute contextualize
[INFO] skip non existing resourceDirectory D:\example-projects\equal-method-client\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ equal-method-client ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to D:\example-projects\equal-method-client\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] 'equals' method must be overridden for the class implementing com.logicbig.example.Data.
   Error class: com.logicbig.example.DataB
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.150s
[INFO] Finished at: Tue Dec 29 21:33:45 CST 2015
[INFO] Final Memory: 12M/245M
[INFO] ------------------------------------------------------------------------
 

See Also