Code Generation using Annotation Processors in the Java language – part 3: Generating Source Code

This post is the third and final part in my series about Code Generation using Annotation Processors in the Java language. In part 1 (read it here) we introduced what Annotations are in the Java language and some of their common uses. In part 2 (read it here) we introduced Annotation Processors, how to build them and how to run them.

Now, in part 3, we are going to show how an Annotation Processor can be used to generate source code.

Continue reading “Code Generation using Annotation Processors in the Java language – part 3: Generating Source Code”

Measure Code Coverage of HtmlUnit Based Tests with Sonar and JaCoCo

This blog post is the third one in a series about Integration Tests with HtmlUnit. The first post, titled “Automating Assembly and Integration Tests with HtmlUnit”, showed how to write integration tests of web UI applications using HtmlUnit. That post can be read here. The second post, titled “Integrate HtmlUnit Based Tests with Apache Maven and Cargo”, showed how to automate the execution of HtmlUnit tests using Maven and Cargo plug-in. That post can be read here.

Finally, in this post we are going to show how to measure code coverage of HtmlUnit tests using Sonar, the popular Continuous Quality Assurance tool, and JaCoCo, a very interesting code coverage tool based on JVM agents instead of instrumenting bytecodes.

Continue reading “Measure Code Coverage of HtmlUnit Based Tests with Sonar and JaCoCo”

Code Generation using Annotation Processors in the Java language – part 2: Annotation Processors

This post is the second part in my series about Code Generation using Annotation Processors in the Java language. In part 1 (read it here) we introduced what Annotations are in the Java language and some of their common uses.

Now in part 2 we are going to introduce Annotation Processors, how to build them and how to run them.

Code Generation using Annotation Processors in the Java language – part 2: Annotation Processors

Annotations are great, sure. You can set any kind of metadata or configuration with them, with a well defined syntax and different types to use.

From what we have seen until now, annotations have advantages compared with Javadocs but not enough to justify their inclusion into the language. Therefore, is it possible to interact with annotations and get the most from them? Sure it is:

  • At runtime, annotations with runtime retention policy are accessible through reflection. The methods getAnnotation() and getAnnotations() in Class class will do the magic (1).
  • At compile time, Annotation Processors, a specialized type of classes, will handle the different annotations found in code being compiled.

Continue reading “Code Generation using Annotation Processors in the Java language – part 2: Annotation Processors”