Jdk1.5 annotation syntax (annotations) for a fresh trial

Source: Internet
Author: User
In my previous article about jdk1.5's new features, we introduced six new basic features of jdk1.5. This article continues to introduce another secret weapon of jdk1.5, The New Annotation syntax (annotations ).

In fact, the annotation syntax is familiar to any Java developer. Every day, we use @ author, @ Param, and so on to write comments, and then use javadoc to generate documents. Java's convenient document generation method has been widely praised by developers. Since jdk1.5, the annotation Syntax provides more powerful functions.

Let's first talk about the annotation syntax itself, which is sometimes called meta-data: "data describing data ". Generally, they can be used to generate documents, check the dependencies between codes, and help the compiler perform syntax checks. Popular tools include XDoclet. The javadoc tool has already been perfect for document generation, while Java also provides language-level support for code checks.

We know that javadoc generates a document by extracting the tag information in the Java source file. To learn new annotation syntaxes, you must first familiarize yourself with the new labels. The New Annotation syntax supports two types of labels: system standard labels and user-defined labels. The symbol of the tag is also the same. The @ symbol is added with the tag name. Let's start with the standard labels that jdk1.5 comes.

First, we will introduce @ override, which is used to describe how to overload it. Suppose there is a subclass that must overwrite the methods of the parent class.

========================================================== ==========================================================
Public Class Parent {

Public void Foo (){
System. Out. println ("original Implementation of foo ");
}

}

Public class Child extends parent {

@ Override
Public void Foo (){
System. Out. println ("overide Implementation of foo ");
}

}
========================================================== ==========================================================

So far, we cannot see that this @ override has brought us any benefit. So let's first talk about what the compiler has executed when we use javac to compile it? The compiler checks this method and then finds whether the method exists from the parent class. Otherwise, an error occurs during compilation. This feature helps us avoid some low-level errors. In the above example, The subclass wants to override the Foo () method, but you may have neglected to write it as a FOB (). For such a "low-level error ", if you don't find it in the early stage, you may spend several hours or even a day or two trying to find this bug during system integration testing. Now, the compiler will give errors during compilation,

Child. Java: 3: method does not override a method from its superclass
@ Override
^
1 error
How about this function.

After reading the usage of standard labels, let's take a look at the user-defined labels. First, we will introduce @ interface, which is used to define a new annotation type (annotation type ). Creating a new annotation type looks like defining an interface,
Mytag. Java is used to create a custom tag. The Code is as follows,

========================================================== ==========================================================
Package tiger. annotation;
/**
* User-Defined tags ?? Mytag
*/
Public @ interface mytag {}

After defining a tag, we can use this tag in any Java file,
Import tiger. annotation. mytag;
Public class tagtest {

@ Mytag
Public void testtag (){
}
}
========================================================== ========================================================

The annotation type can also contain member variables,

========================================================== ==============================================
Package tiger. annotation;
/**
* User-Defined tags ?? Mytag with member variables
*/
Public @ interface mytag {

String name ();

Int age ();
}
========================================================== ============================================

Then we can use this label like this,
@ Mytag (name = "mytag", age = 1)
Public void testtag (){
}

The tag is used to help developers extract comments and further process the information based on different requirements. Let's take a look at how to obtain the comments.

========================================================== ============================================
Import java. Lang. annotation. annotation;
Import tiger. annotation. mytag;
Public class tagtest {

@ Mytag (name = "mytag", age = 1)
Public void test (){
}

Public static void main (string [] ARGs ){
Tagtest TT = new tagtest ();
Try {
Annotation [] annotation = TT. getclass (). getmethod ("test"). getannotations ();
For (annotation Tag: annotation ){
System. Out. println ("tag is:" + tag );
System. Out. println ("tag. Name ()" + (mytag) Tag). Name ());
System. Out. println ("tag. Age ()" + (mytag) (TAG). Age ());
}
} Catch (nosuchmethodexception e ){
E. printstacktrace ();
}
}
}
========================================================== ========================================================

Note that before executing this code, we need to add a description label to the custom tag mytag, @ retention, it indicates that the annotation information can be obtained through the reflection mechanism at runtime. If this label is not added, the above Code will not be output. The modified mytag is as follows,

========================================================== ==========================================================
/**
* User-Defined tags ?? Mytag with member variables
*/
@ Retention (retentionpolicy. runtime)
Public @ interface mytag {

String name ();

Int age ();
}
========================================================== ==========================================================

Then we run tagtest to get the output as follows,

Tag is: @ tiger. annotation. mytag (name = mytag, age = 1)
Tag. Name () mytag
Tag. Age () 1

Well, the basic usage of Tiger's New Annotation syntax is so simple. Although the basic usage is simple, it is worth pondering how to deal with it after obtaining the annotation information. We can use them for some syntax checks, check the file relevance and perform various statistics. For more information about the new tiger annotation syntax, visit [link = http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html#.

The above code is passed under Win2k + j2se5 ga.
Cao Wei (tec_caowei@hotmail.com ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.