Welcome!

Java Authors: Maureen O'Gara, Liz McMillan, Walter H. Pinson, III, Yakov Werde, Tony Bishop

Related Topics: Java

Java: Article

Java Annotation Facility - A Primer

JDK 5 Has Changed Source Code Generation in a Seminal Way

In this method of invocation we used the -nocompile to ensure that the source file specified isn't complied. The -classpath option specifies the classpath required by the processor. The -A option is ignored by the APT tool and is used by the annotation processor to get any specific processing-related information. In our case, we specified the location of the struts-config.xml in the file system.

The last step is to process the files with the annotation processor. Besides the command line, one could write an ANT Custom Task to generate the code. We strongly recommend that this be done. Since more and more projects are using ANT for automating the build process, it makes more sense to do an ANT build for automating code generation as well.

Writing an ANT task is easy and done by writing a custom task and extending the org.apache.tools.ant.Task class. Once that's done, the next step is to define the attributes and the corresponding setter methods for the attributes. The core processing logic is provided in the execute method called by the ANT framework. In our case, we delegate the bulk of the code generation work to the com.sun.tools.apt.main.Main class from the tools.jar. This is the actual byte code that's also used by the apt command line.

The following XML snippet from an ANT build shows how use the custom ANT task:


<taskdef name="apt"
classname="com.article.jdj.
annotation.task.APTTask" >
<classpath>
<pathelement
location="F:\Article\Dev\Annotation\
dist\Annotation.jar"/>
</classpath>
</taskdef>
<target name="main">
<apt factory="com.jdj.article.gen.StrutsConfigGenerator"
src="F:\Article\Dev\Annotation\src\com\jdj\article\
action\*
.java"
processorOption="struts-config=
F: \Article\Dev\Annotation\generated\
struts-config.xml" >
<classpath>
<pathelement location="C:\Sun\AppServer\
lib\j2ee.jar"/>
<pathelement location="...">
</classpath>
</apt>
</target>

Summary
In this article we introduced the idea of defining annotation types, how to use defined types as annotation and how to go about generating code or other supporting files based on annotation. This new and powerful technology is transforming the way we code. As more and more tools and products start using this facility, developers will realize improved quality in their code and a concomitant increase in productivity.

Resources

  • JavaCC: https://javacc.dev.java.net/
  • JDoM: www.jdom.org/
  • APT: http://java.sun.com/j2se/1.5.0/docs/guide/apt/index.html
  • Annotations: http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html
  • JSR 175: www.jcp.org/en/jsr/detail?id=175
  • Comments (6) View Comments

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


    Most Recent Comments
    udaykirand 09/08/08 10:15:23 AM EDT

    Really Excellent Information. But i have some doubts. initially i have some aversion towards annotations but after reading this article i develop some interest on it. later my R & D i want to create an annotation which is like @Singleton when ever i applied this annotation for a class then i want to make a class as singleton class. could you please help me out from this scenario.

    in the same way @ThrowException(exceptionType="ArithmeticException.class")

    many more ideas but i couldn't able to move in forward direction because there is no much information about annotations in Google also.
    even no where i found the source code of this article. if you have any please send me complete source code with compilation and execution instructions. please please its really great help for me.my id is udaykiranmca@gmail.com

    Thanks,
    udaykiranmca@gmail.com
    please send to this mail id .

    Terry Corbet 08/06/06 06:00:49 PM EDT

    This is a critique for the editor, not the author.

    A primer that uses for an example something that requires experience with Struts is not what you should have provided. Ok, so the guy you got to do all the hard work happened to be interested in solving a Struts problem, but your job, as editor, should have been to carefully think through the issues of documenting this new Java facility and providing examples with as little dependence upon some specific toolkit-framework-environment as possible. With Mustang about to add to the value of annotation, I am sure there is much need for carefully-thought-out articles demonstrating valuable, incremental information. When K&R contrived 'Hello World', it was after thoughtful consideration about learning processes. Combined with the fact that the source code was not correctly provided, the overall impression is that you, as an editor, just thought you could get something for nothing and pass it along where enough Google hits would provide the mass of advertising that is blinking all around me as I enter this suggestion.

    Vish 07/21/05 10:00:00 PM EDT

    Bob,

    Sorry about that. Can you send me an email at unicode@yahoo.com, and I will send you the src

    Thanks,

    Bob Shewan 07/21/05 05:40:07 PM EDT

    As Neil noted there isn't any source code in the zip file. Would you be kind enough to send what you have.

    Thanks

    Bob Shewan

    vish krishnan 03/13/05 10:23:16 AM EST

    Neil, sorry about that. Send me an email and I will send you all the source that I have got. My email is unicode at yahoo dot com (OR) krviswanath at Deloitte dot com

    thanks

    Neil Hornbeck 03/11/05 07:33:41 AM EST

    The source zip file Viswanth1003.zip only contains compiled code.