JDK1.5 Note Coordination Coordination Calligraphics

xiaoxiao2021-03-06  38

In my previous article JDK1.5 new features, we introduced 6 basic features of JDK1.5, this article continues to introduce another secret weapon of JDK1.5, new annotation syntax (Annotations).

In fact, I've been annotated in the sympathy, and I have been familiar with any Java developers. We use @Author, @param, and so on every day, then generate a document with Javadoc. This convenient documentation method of Java has been widely praised by developers. Starting from JDK1.5, comment syntax provides a more powerful feature.

Let us talk about the annotation syntax itself, and it is sometimes referred to as Meta-Data: "Data of the Data". Generally speaking, they can be used to generate documents, check inter-code dependencies, help compilers to be checked in syntax. The more popular tools have XDoclets. For document generation, there is already a perfect performance of Javadoc tools, and for code checking, Java has also provided language-level support.

We know that Javadoc generates a document by extracting tag information in the Java source file. So learning new annotation syntax, you first have to be familiar with new labels. New comments syntax support two labels, system standard tags, and user-defined labels. The label symbol is also the same, the @ symbol plus the label name. Let's start with the standard label comes from JDK1.5.

First introduce @Override, don't have to be more than Luo, as the name suggests, it is used to explain the method. We assume that there is a subclass that must override the parent class.

============================================================================================================================================================================================================= ===============================

Public class pent {

Public void foo () {

System.out.println ("Original Implementation of Foo");

}

}

Public class child extends pent {

@Override

Public void foo () {

System.out.println ("Overide Implementation of foo");

}

}

============================================================================================================================================================================================================= ============================== So far we can't see this @Override gives us any benefits, so we first Talking about adding a tag, what do we perform when compiled with Javac? The compiler will check this method, then find this method from the parent class, otherwise it is wrong. This feature can help us avoid some low-level errors. Above this example, the subclasses want to override the foo () method, but you may negligently write it into fob (), for such a "low-level error", if you don't find it in the previous period, when you discovery, go to the system integration test It may be in a few hours or even two days to find such bugs. Now, the compiler will give an error when compiling.

Child.java: 3: Method Does NOT OVERRIDE A METHOD from ITS Superclass

@Override

^

1 Error

How, this feature is not bad.

I have seen the use of standard labels, let's take a look at the user's custom label. First introduce @Interface, which is used to define a new annotation type (Annotation Type). Newly built a note type looks up and defined that there is no other in both.

MyTag.java is used to create a user-defined label, the code is as follows.

============================================================================================================================================================================================================= ===============================

Package tiger.annotation;

/ **

* User custom label ?? mytag

* /

Public @ITERFACE 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 () {

}

}

============================================================================================================================================================================================================= ============================= 注 注 类 类 有型 有 还 有 还 有,,

============================================================================================================================================================================================================= ============================

Package tiger.annotation;

/ **

* User custom label ?? MyTag with member variables

* /

Public @ITERFACE myTag {

String name ();

Int Age ();

}

============================================================================================================================================================================================================= ===========================

Then we can use this label like this,

@Mytag (name = "mytag", agn = 1)

Public void testtag () {

}

Using the label is ultimately to help developers to extract the comment information, then further process according to different needs, let's take a look at how to get comment information.

============================================================================================================================================================================================================= =======================================Nnotation;

Import tiger.annotation.mytag;

Public class tagtest {

@Mytag (name = "mytag", agn = 1)

Public void test () {

}

Public static void main (String [] args) {

Tagtest TT = New tagtest ();

Try {

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 still have a little job before performing this code, but also need to give our custom label mytag plus a descriptor tag, @RETENTION, indicating that the comment information will be able to pass the reflex mechanism at runtime get. If this label is not added, the above code will have no output. After modification, myTAG is as follows.

============================================================================================================================================================================================================= =============================== / **

* User custom label ?? MyTag with member variables

* /

@ReTENTION (RETENTIONPOLICY.RUNTIME)

Public @ITERFACE myTag {

String name ();

Int Age ();

}

============================================================================================================================================================================================================= ===============================

Then we execute tagtest to get the output as follows.

Tag is: @ Tiger.annotation.mytag (name = mytag, agn = 1)

tag.name () MyTag

TAG.AGE () 1

Ok, Tiger's new annotation syntax is so simple, although the basic usage is simple, but how to handle it after obtaining the comment message, we can use them to do some grammar check, file-related check, and conduct various statistics. and many more. About more information about Tiger new commentary syntax, you can access [LINK =

Http://java.sun.com/j2se/1.5.0/docs/guide/language/annotation.html].

The above code is passed under Win2K J2SE5 GA.

Cao Wei (TEC_CAOWEI@hotmail.com).

转载请注明原文地址:https://www.9cbs.com/read-76063.html

New Post(0)