J2SE 5.0 adds support for metadata by introducing an annotation concept.
An annotation in the form of a @xxx can be used as a modifier, which can be placed in any place where the modifier can appear. Public, Static, Final is a modifier of a Java language, and comments can be written anywhere in them.
For example, you can look at the following code:
Public class annotationexample {public @Override int 6code () {return super.hashcode ();}}
In this example we use a Note @Override in a Java Language API, which uses this annotation in method hashcode (), explaining how hashcode is a method that covers the parent method.
As for the specific meaning of the comment, we will explain in the next section.
If you want to use a class, you must first find its definition, or you define it yourself. Note is also a need to be defined, and the annotation that is inserted into a @xxx in the code cannot be compiled.
A simplest annotation definition is similar to the definition of the interface, just like the following code:
Public @INTERFACE INFO {}
As you can see, there is no, but even if so, we can also use it in the program:
Public @INFO STRING INFORMATION;
This kind of empty note that is not defined is called marker annotations.
We can add a definition of a member:
Public @INTERFACE INFO {String Author ();
What is worth noting here is that the definition of the method in the comment class cannot be private, if you don't add a public keyword, the compiler will default to it is public.
After adding the definition of Author, the original note that only writes only a @Info must be modified, and the comment must be written this:
Public @Info (author = "myname") Void Afunction () {}
The member of a note can have default:
Public @INTERFACE INFO {string author () default "myname";
What is the benefit of using the default? We can write this:
Public @Info void Afunction () {}
When making a tag, if you confirm that a member's value is the same as its default value, we can ignore it without having to explicitly assign each member, which is reduced.
If we add this member name called Value, it is:
Public @INTERFACE INFO {String Value ();
There is another annotation to use: We can write such annotation @Info ("information") without having to write @Info (value = "information"), and the values in parentheses will be passed to Value. Such a note is called a single value annotation (Single-Value Annotation)
A note can have many types of different members, such a comment is called full annotation.
The member type in a comment can only be a native type, a string, a Class type, an comment type, an enumeration type, or a one-dimensional array. Suppose we have a comment definition now:
Public @INTERFACE Company {string value ();
Now we want to define another comment, this comment type reflects the information of a person:
Public @Interface Person {public enum gender {male, female}; string name (); int Age (); company company (); gender gender () default gender.male; string description () default "";
You can write this when adding this type of comment in the program code:
@Person (agn = 23, name = "myname", gender = person.gender.feMale, Company = @ Company ("foo corporation")))
In the java.lang package, three comments are defined, namely:
n Deprecated: The @DepRecated meaning in javadoc in the past
N Override: The method of the method covers the method in the parent class
n SuppressWarnings: Using this comment allows the compiler to ignore a specific type of warning message
The specific meaning can refer to the API document.
We know that the introduction of comments, adding the expression of metadata to the Java language, and metadata is data about data. In Java, there is also a comment on the annotation, our corresponding call is meta-annotation
In Java, we can use four predefined comments to comment on the annotation definition, which is:
n Target: Indicates which code segments can be used in this code segment to avoid misuse of comments.
n Retention: Describe whether the compiler is compiling and running to ignore the annotation
n Document: Description Note Whether you appear in Javadoc
n inherited: When we use some kind of comment in a class, sometimes it will contain this annotation information in all subclasses in the future, if @inherited is added in the annotation definition, then this comment will Subclass of the caller
How should we get these metadata after we use annotations in a class to define a series of metadata? We will explain the following examples.
The definition of Annotation is still using two listed because we need to get annotation information in the class file, so you must add a Retion annotation in the annotation definition.
First we define two comments, note Todo explains what else needs to do:
Import Java.lang.Annotation.Retendion; import java.lang.annotation.RetentionPolicy; @Retention (rettionpolicy.Runtime) public @Interface todo {string value ();
Note Author illustrates a method or class definition:
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention (RetentionPolicy.RUNTIME) public @interface Author {public enum Gender {MALE, FEMALE}; String name (); String email (); Gender Gender () Default Gender.Male;} Then we add these two types of comments in a simple class:
Public @todo ("delete this class") class foo {public void methoda () {} public @Author (name = "b", email = "b@foo.com") void methodb () {} public @Author Name = "a", email = "a@foo.com") String Fielda;
With the following code we can extract the corresponding metadata:
Import java.lang.reflect.method; public class getannotation {public static void main (string [] args) {try = class.forname (args [0]); if (Klass.IsannotationPresent (Todo. Class)) {TODO T = Klass.GetanNotation (TODO.CLASS); System.out.Println (T);} for (Method M: Klass.getMethods ()) {IF (m.isannotationPresent (author.class)) { Author a = m.GetanNotation (Author.class); System.out.Printf ("Method:% S, Author:% S% N", M.GetName (), A);}}} catch (ClassNOTFoundExcection) { E.PrintStackTrace ();}}}
Use the following command line running:
Java GetanNotations Foo
The results of the operation are as follows:
@Todo (value = delete this class) method: methodb, author: @Author (gender = Male, Name = B, email=b@foo.com)