j2se1.5 annotation Syntax _jsp programming

Source: Internet
Author: User
Speaking of annotation syntax, it is familiar to any Java developer, we use @author, @param, and so on every day to write comments and then generate documents with Javadoc. This handy documentation generation method for Java is widely praised by developers. Starting with JDK1.5, annotation syntax provides a more powerful feature.

Let's talk about the annotation syntax itself, which is sometimes called Meta-data: "data that describes data." In general, they can be used to generate documents, check dependencies between codes, and help the compiler make grammar checks. Nowadays the more popular tools have Xdoclet and so on. There is already a perfect performance of the Javadoc tool for document generation, and Java now provides language-level support for code checking.

We know that Javadoc generates documents by extracting tag information from Java source files. So to learn the new annotation syntax, the first thing to be familiar with is the new tag. The new annotation syntax supports two types of tags, system standard tags, and user-defined labels. The symbol for the label is the same, the @ symbol plus the label name. Let's start with the standard label that JDK1.5.

First introduced @override, also need not wordy, as the name suggests, is used to explain the method of the cover. We assume that there is a subclass that must override the method 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 can't see that this @override has brought us any benefits, so let's start with adding this tag, what does the compiler do when we compile with Javac? The compiler checks this method and then looks for this method from the parent class, or it compiles an error. This feature can help us avoid some low-level errors. In the example above, subclasses want to overwrite the Foo () method, however, you may inadvertently put it on the FOB (), for such a "low-level error", if you do not find in the early days, to the system integration test, you may be a few hours or even one or two days to find such a bug. Well now, the compiler will give you an error when compiling.

Child.java:3: Method does not override a method of from its superclass
@Override
^
1 Error


How about, this function is also good.
Looking at the use of standard tags, let's look at user-defined tags. Introduce @interface first, which defines the new annotation type (annotation type). Creating a new annotation type appears to be no different than defining a interface, Mytag.java is used to create a new user-defined label with the following code,

===============================================================================
Package tiger.annotation;
/**
* User-defined label?? MyTag
*/
Public @interface MyTag {}

Once we have defined a tag, we can use this tag in any Java file,
Import Tiger.annotation.MyTag;
public class tagtest{

@MyTag
public void Testtag () {
}
}
===============================================================================

Annotation types can also have member variables,

==============================================================================
Package tiger.annotation;
/**
* User-defined label?? MyTag with member variables
*/
Public @interface MyTag {

String name ();

int age ();
}
=============================================================================

And then we can use this tag,

@MyTag (name= "MyTag", age=1)
public void Testtag () {
}

The use of tags is ultimately to help developers extract annotation information, and then follow the different requirements for further processing, let's look at how to get the annotation information.

=============================================================================
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 ();
}
}
}
===============================================================================

One thing to note is that we have a little bit of work to do before we execute this code, and we need to add a description tag to our custom tag MyTag, @ Retention, to show that the annotation information will be available at runtime through the reflection mechanism. If you do not add this tag, the code above will not have any output. The revised MyTag are as follows:

================================================================================
/**
* User-defined label?? MyTag with member variables
*/
@Retention (Retentionpolicy.runtime)
Public @interface MyTag {

String name ();

int age ();
}
================================================================================

And then we execute tagtest can get output as follows,

Tag is: @tiger. Annotation.mytag (Name=mytag, age=1)
Tag.name () MyTag
Tag.age () 1

All right, tiger. The basic usage of the new annotation syntax is so simple, but the basic usage is simple, but what to do after getting the annotation information is worth examining, we can use them to do some grammar checking, file correlation check, carry on various statistics and so on. For more information on Tiger's new annotation syntax, you can access [link=http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html].

The above code is passed under Win2K + j2se5 Ga.

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.