1, introduce
l The latest trend of programming, especially Java programming, is to use metadata
L metadata can be used to create documentation, tracking dependencies in the code, and even perform basic compilation
l Many metadata tools (such as XDoclets) Add these features to the core Java language, temporarily become part of the Java programming function
l Javadoc is a metadata tool, but in addition to generating documents, there is no fixed, practical, standardized way to use data for other purposes, and HTML code is often mixed into the Javadoc output, further reduced its for any other purpose. value
l JSR 175, the metadata tool for Java programming language provides formal reasons and descriptions to merge metadata into the core Java language.
l Tiger adds new features of Annotation, incorporate a more common metadata tool into the core Java language
l Annotation is a modifier that can be added to the code, which can be used for packet declarations, type declarations, constructor, methods, domain variables, parameters, and variables.
l Tiger contains built-in Annotation, and also supports custom ANNOTATION
l This section will outline the advantages of metadata, and introduce Tiger's built-in Annotation
2, the value of metadata
In general, the benefits of metadata are three categories: document preparation, compiler check and code analysis
(1) Documentation
l The code-level document is most commonly referenced, but for the addition of metadata to the Java language, documentation may be the least related reason.
l Because Javadoc has provided a very easy to understand and robust approach to documentation code
(2) Check time check
L-metadata is more important that the compiler can use it to perform basic compile time check
l For details, please refer to the Tiger built-in Annotation: @override
(3) Code analysis
The best feature of the L metadata tool is to analyze the code using additional data.
l Simple case is: Many times, the parameter type or return type of the method is actually not the type you want; for example, the parameter type may be Object, but the method may only use Integer, which is very time to cover the superclass. Easy to occur; metadata can indicate code analysis tool: Although the parameter type is Object, but integer is really needed.
l Complex cases are: Even in simple EJB systems also have strong dependence and complexity, there are HOME and REMOTE interfaces, as well as local Home and Remote interface, and an implementation class, keep all of these synchronization is very difficult Good tools (such as xDoclet) can manage all these dependencies and make sure these do not have "code level", but there are "logic level" contact classes to keep synchronization; metadata can do its role
3, Annotation foundation
l Annotation is: @ANNOTATION name
l When you need data in Annotation, it is provided through Name = Value.
L code can be used in many Annotations, some Annotation will have the same Annotation type
l Annotation Type and Annotation concept Similar to classes and objects L Annotation has three basic types:
Ø Tag Annotation: Only Annotation name, does not contain data, such as @markerannotation
Ø Single value annotation: Only single data, you can simplify the form of Name = Value is Value, such as @SingLevalueanNotation ("My Data")
Ø Complete format Annotation: There are multiple data members, such as @fullannotation (var1 = "data value 1", var2 = "data value 2", var3 = "data value 3")
l You can use the curly bracket to provide a value of the value of the Annotation variable, such as
@TodoItems ({// Curly Braces Indicate An Array of Values IS Being Suppl
@Todo
Severity = Todo.critical,
Item = "add functionality to calculate the mean of the student's grades",
Assignedto = "Brett McLaghlin"
),
@Todo
Severity = TODO.IMPOTANT,
item = "Print Usage Message to Screen if no", "
Assignedto = "Brett McLaghlin"
),
@Todo
Severity = TODO.LOW,
Item = "Roll A New Website Page with this class's new features",
Assignedto = "jason hunter"
)
})
4, Tiger Built-in Annotation
(1) @Override
l @Override is only used for the method, specifies the corresponding method in the superclass
l Simple example:
Public class overridEster {
Public overridEster () {
}
@Override public string toString () {
Return Super.Tostring () "[Override Tester IMplementation]"
}
@Override public int hashcode () {
Return Tostring (). hashcode ();
}
}
l @Override can check the input error results in a problem that cannot override the superclass method, such as the entered input for the HashCode () error is Hascode (), will report an error in compiling:
The method Hascode () of Type OverridEster Must Override a superclass method
l This convenient little function will help quickly capture typing errors.
(2) @DepRecated
l Similarly to the method, indicating that the method should not use L simple example:
Public class deprecatedclass {
@Deprecated public void dosomething () {
System.out.println ("Deprecated Method!");
// Some Code
}
Public void dosomethingelse () {
// this method presumably does what dosomething () DOES, But Better
}
}
l Compile Normally, if the compiler will give a warning message if the method covers or calls a deprecated method.
l Note: I have tested in the Eclipse 3.1m4 environment (even if the compilation parameter, why?) is changed, use the -XLint: deprecated parameter in the command line, Javac only gives the warning message, compile or pass
(3) @suppresswarnings
l Tiger's generic feature enables the compiler to check the type of security, especially Java collection, as follows:
Public void nongenericsMethod () {
List wordlist = new arraylist (); // no type information on the list
WordList.Add ("foo"); // causes error on list address
}
l The compiler will give the following warning information:
TYPE SAFETY: The Method Add (Object) Belongs to the Raw Type List. References To Generic
Type List
L This is very helpful for Tiger's code, but for JDK1.4 and previous versions, constantly receiving unrelated warning messages is very annoying
l You can use @suppresswarnings to block a warning message for the specified type, such as:
@SuppressWarnings (value = {"unchecked"})
Public void nongenericsMethod () {
List wordlist = new arraylist (); // no type information on the list
WordList.Add ("foo"); // causes error on list address
}
l The type value passed to @suppressWarnings is an array, so a number of types of warning information can be blocked at the same time.
l Type value is specified by the compiler manufacturer, so I don't work in the Eclipse 3.1m4 environment and command line, which is probably not specified.