advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Program Annotation Facility :

Program Annotation Facility

Benoy Jose

Introduction:

Most Java programmers who have used the Java API reference are familiar with the @deprecated within the comments of a Java method. We know that the tag appears in the comments so that it can help the Javadoc generator to mark this method as deprecated and display the information on the new method set to replace it. There are many comments similar to these (@author, @param etc), which are used to indicate informational parameters. Specific programs with the ability to understand these tags use them to do some specific task. The compiler and the Java runtime usually ignore these comments, as they are included into the comment blocks. However some vendors and programmers have cleverly made use of these comments to include information that could be used by specific custom interpreters to do some routine tasks.

An example is EJBGen by Cedric Beust,(See resources below). EJBGen provides a set of javadoc tags, which can be included into the comments for the method and class etc. These tags are interpreted by the EJBGen compiler which in turn generates appropriate value objects, remote methods, and primary key classes, XML descriptors and so on. So instead of creating all these supporting classes separately EJBGen allows the user to create a single EJB bean class with javadoc attributes specified within it. The tool generates all the supporting classes, relationships and descriptor files required for the EJB. Another example is Xdoclet which has taken this javadoc facility to another level by providing javadoc tags for many common Java tasks like generating bean classes, creating portlets and defining ant tasks. It also has support for many application servers like websphere, weblogic, Sun one Application server and can do tasks ranging from compiling EJBs to deploying applications.

There are many similar efforts like the ones above which use annotations to do routine jobs. The JSR 175 proposes to introduce a Program Annotation facility within the Java programming language, which can help in streamlining these annotation efforts, and also provide a standard way of annotating elements within a Java program.

The annotation facility allows the annotator the facility to define annotation types and then use these annotation types as annotations within a Java program. It also provides control over whether the annotation can be used during compile time or during runtime. Most of the time, third party tools use annotations at compile time to generate supporting classes and artifacts. But in some cases annotations can be used by the Java runtime to do some operations at runtime. The facility allows the annotator to reduce the overhead of carrying the compile time annotations with the class file to the JVM by specifying them to be ‘compile time’ annotations.

How to use Annotations:

To create an annotation we need to define an annotation type first. Annotation types are defined like interfaces with an "@" sign before the interface definition. Annotation types and Annotation are described below in detail.

A sample of how to define "Annotation Types" is given below.

	public @interface SampleAnnotation{
      	String someValue;
} 
public @interface NormalAnnotation{
	String value1;
	int value2;
}

How an annotation can be used is shown below.

@SampleAnnotation (someValue ="Actual Value used in the program”)
public void annotationUsingMethod(){
	…
} 

Annotation Type

The annotation type declaration looks similar to an interface declaration and they have similar characteristics like interfaces. They have the same scope and accessibility as Java interfaces and can be used wherever interfaces can be used.

Some of the characteristics of annotation types are listed below.

  • Annotation types can have zero or more members
  • Annotation types can have other annotation types as their members.
  • Annotation types cannot have members of the their own type.
  • Unlike methods in an interface an annotation type member can have a default value.
  • All annotation types automatically extend the java.lang.annotation.Annotation interface.
  • Annotation types cannot extend any entity (class, interface or Annotation etc).
  • Methods in an annotation type cannot have parameters.
  • Return types of methods can only be primitive types, String, another annotation type or arrays of the above types.
  • Annotation types cannot have the throws clause within them.
  • Generic methods are not allowed in Annotation type declarations.
  • Annotation types cannot be parameterized.

    Special kinds of annotation types called marker annotation types and single member annotation types can be defined for special types of annotation types. A Marker annotation type has no members defined with it. A single member annotation type is a special case of the normal annotation type, which has only one member element within it.

    How to Add Java Applets to Your Site

    New on the Java Boutique:

    New Review:

    Time Management Made Easy with the Quartz Enterprise Job Scheduler
    Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

    New Applet:

    Reverse Complement
    Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

    Elsewhere on internet.com:

    WebDeveloper Java
    Lots of Java information on webdeveloper.com

    WDVL Java
    Thorough Java resource at the Web Developer's Virtual Library.

    ScriptSearch Java
    Hundreds of free Java code files to download.

    jGuru: Your View of the Java Universe
    Customizable portal with online training, FAQs, regular news updates, and tutorials.

  •  Microsoft Visual Studio 2010 Showcase
     Avaya Developer Showcase
     MSDN Spotlight
     PHP for Windows Showcase
    XML error: undefined entity at line 39
    advertisement
    Receive Articles via our XML/RSS feed
    Receive Articles via our XML/RSS feed

    JavaBytes
    Internet Cyclone
    This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

    Windows 7: From Beta to Final Code in One Year
    Google Shows Off Chrome OS, Releases Source
    Microsoft Shows Off Silverlight 4, IE9 Plans
    Metasploit Expands Vulnerability Test Framework
    HyperCard Reborn?
    Fedora 12 Takes Aim at Linux Networking
    Top Supercomputer Nearly Doubles in Speed
    Fedora 12 Linux Tackles Virtualization
    Apple Gives iPhone Developers App Status Tracker
    Novell Sets OpenSUSE 11.2 Free

    Creating Custom Export Filters for StarOffice with XSLT
    WPF Wonders: Using DataTemplates
    Crystal Reports Family Offers Options for Developers
    Avaya Aura Session Manager video
    Avaya Aura Overview video
    Exploring HTML 5's Audio/Video Multimedia Support
    Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
    Understanding the Cloud Computing Security Vulnerabilities
    Cisco and IBM Target a Greener World
    Upgrade to Visual Studio 2010 with the Ultimate Offer

    Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

    internet.commediabistro.comJusttechjobs.comGraphics.com

    Search:

    WebMediaBrands Corporate Info

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs