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 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;
/**
* 用户自定义标签??MyTag
*/
public @interface MyTag { }
定义了一个tag之后,我们就可以在任何java文件中使用这个tag了,
import tiger.annotation.MyTag;
public class TagTest{
@MyTag
public void testTag(){
}
}
Annotation types can also have member variables,
package tiger.annotation;
/**
* 用户自定义标签??带有成员变量的MyTag
*/
public @interface MyTag {
String name();
int age();
}
And then we can use this tag,
@MyTag(name="MyTag",age=1)
public void testTag(){
}