Jul 28, 2014

Emergent Design with Java Annotation Processors


The emergent design process in agile software development can be supported and accelerated using lightweight code generators. The wide spread use of annotations in all major Java APIs, standards and frameworks makes the Java Annotation Processing API a prime candidate to implement such generators in no time. This article will show the emergent design process from code refactoring to a working source code generator for a JPA based DAO implementation.

Background

When using agile software development methods the overall functionality of a system is implemented feature by feature spanning all software layers. The individual software components are being developed test-driven in small increments and undergo continuous refactoring. The internal micro-architecture of the system emerges during this TDD cycle; after a few iterations the commonalities of the overall OO design become apparent. This is when lightweight generators should be introduced to aid in the development of future features and to bring order and symmetry to the emerging design.


Most major Java APIs bring extensive annotation support. These annotations provide the required meta-data that can be used by custom Java annotation processors at compile time to dynamically generate further source code artifacts. By combining the capabilities of annotation processors with standard template engines such as FreeMarker or Velocity simple yet powerful source code generators can be implemented quickly and effortless. 


For example, when using the Java Persistence API you have to annotate POJOs with JPA annotations to define the details of the object-relational mapping.  All these annotations provide enough meta-data to be able to generate a suitable data access object for a JPA entity with CRUD functionality.




Refactoring and Abstraction

harvesting ideomatic patterns

Templating

using FreeMarker / Velocity

Code Generator

java annotation processors

Build Tool Integration





<plugin>  
   <groupId>org.apache.maven.plugins</groupId>  
   <artifactId>maven-compiler-plugin</artifactId>  
   <configuration>  
      <verbose>true</verbose>  
      <encoding>${project.build.sourceEncoding}</encoding>  
      <generatedSourcesDirectory>${project.build.directory}/generated-sources/annotations</generatedSourcesDirectory>  
      <annotationProcessors>  
         <annotationProcessor>de.qaware.blog.DaoSupportAnnotationProcessor</annotationProcessor>              
         <annotationProcessor>de.qaware.blog.PersistenceXMLAnnotationProcessor</annotationProcessor>  
      </annotationProcessors>  
      <compilerArguments>  
         <Ahibernate.dialect>${hibernate.dialect}</Ahibernate.dialect>  
         <Ahibernate.show_sql>${hibernate.show_sql}</Ahibernate.show_sql>  
         <Ahibernate.format_sql>${hibernate.format_sql}</Ahibernate.format_sql>  
         <Ahibernate.hbm2ddl.auto>${hibernate.hbm2ddl.auto}</Ahibernate.hbm2ddl.auto>  
      </compilerArguments>  
   </configuration>  
</plugin>  

maven compiler plugin

Links

No comments:

Post a Comment