Annotations are always used in the development process, but no annotations have been defined by themselves. Recently unemployed at home, research organized a few, and strive to know the reason why.
This article tries to describe what annotations are, and how to customize annotations in your program with a demo. Demo has no real meaning, just use annotations for annotations.
1, what is annotation.
Annotations are features that are available at the beginning of JDK 1.5 and are now widely used. Here's a description of the annotations in the 14th chapter of the Java Madness Handout:
"Annotation is actually a special tag in the code that can be read at compile, class load, run time, and perform the processing of the response." By using annotation, a program developer can embed some additional information in the source file without changing the original logic. Code analysis tools, development tools, and deployment tools can be validated or deployed through these supplemental information. ”
The JUnit unit test is based on annotations. I think that using dynamic proxies and annotations, you can write a myjunit entirely.
2, demo implementation:
Note file:
This document contains three main sections:
A. The annotation file is declared using @interface.
B. There is a meta annotation (annotation) in the outer layer of a, the main function is to our custom annotation of the scope of implementation, time declaration. The following is a note meaning description of the annotation:
@documented: Indicates that a type of annotation will be documented through Javadoc and a similar default tool. You should use this type to annotate these types of declarations: their annotations affect the use of the elements that are commented by their clients. If a type declaration is annotated with documented, its annotation becomes part of the public API of the annotation element.
@Target: Indicates the kind of program element that the annotation type applies to. If the Target element annotation does not exist in the annotation type declaration, the declared type can be used on any program element. If such a meta annotation exists, the compiler enforces the specified usage limit. For example, this meta comment indicates that the declaration type is itself, or is a meta annotation type. It can only be used on annotation type declarations.
@inherited: Indicates that the annotation type is automatically inherited. If a inherited element annotation exists in the annotation type declaration and the user queries the annotation type in a class of declarations, and there is no comment of that type in the declaration, the annotation type is automatically queried in the superclass of the class. This process repeats until the annotation of this type is found or the top level (Object) of the class hierarchy is reached. If no superclass has a comment of that type, the query indicates that the current class does not have such a comment. Note that if you use anything other than the annotation type annotation class, this meta annotation type is invalid. Also note that this meta annotation only contributes to inheriting annotations from superclass, and that annotations to implemented interfaces are not valid.
@Retention: How long to keep annotations for annotation types. If the Retention annotation does not exist in the annotation type declaration, the retention policy defaults to Retentionpolicy.class.
C. The method name is the parameter name, and the return value type is the type of the parameter, and defaults are used to set the default value of the method.
Here's the code:
Package com.array7.annotationTest;
Import java.lang.annotation.Documented;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Inherited;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;
@Documented
@Target (elementtype.method)
@Inherited
@Retention (retentionpolicy.runtime)
Public @interface MethodInfo {
String author () default "Array7"; Author
String date (); Date
double version () default 1.0; Version number
String comments (); Comments
}
Then define a class that calls the annotation:
Package com.array7.annotationTest;
public class Annotationexample {
/**
* Non-static method
* @param msg
/@MethodInfo (author = "Seven Lang", comments = "side Method Test Note: print10 ", date =" 2013-11-29 ", Version = 1.0) public
void Print10 (Object msg) {
System.out.println (msg); c8/>}
/**
* static method
/@MethodInfo (author = "Seven Lang", comments = "method Test Comment: print20", date = "2013-11-20", Version = 2.0) public
static void print20 () {
System.out.println ("test");
}
The last is the annotation parsing class (I wrote the code in the main method for simplicity):
Package com.array7.annotationTest;
Import java.lang.annotation.Annotation;
Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method; public class Annotationparsing {public static void main (string[] args) {try {class<?> clazz = Class.forName ("Com.array7.annotationTest.AnnotationExample");
This can be encapsulated as a dynamic proxy class. Class clazz = AnnotationParsing.class.getClassLoader (). LoadClass ("Com.array7.annotationTest.AnnotationExample") ;
This way is OK. For (Method Method:clazz.getMethods ()) {if (Method.isannotationpresent (Methodinfo.class)) {for (Annotation a
No:method.getAnnotations ()) {System.out.println ("Annotation in method [" + Method + "]:" + anno);
MethodInfo MethodInfo = method.getannotation (Methodinfo.class);
if (methodinfo.version () = = 1.0) {Method.invoke (Clazz.newinstance (), methodinfo.comments ());
else if (methodinfo.version () = = 2.0) {Method.invoke (null); }}}} CATCH (SecurityException e) {e.printstacktrace ();
catch (ClassNotFoundException e) {e.printstacktrace ();
catch (Illegalaccessexception e) {e.printstacktrace ();
catch (IllegalArgumentException e) {e.printstacktrace ();
catch (InvocationTargetException e) {e.printstacktrace ();
catch (Instantiationexception e) {//TODO auto-generated catch block E.printstacktrace ();
}
}
}
Program Final output:
Annotation in method [public void Com.array7.annotationTest.AnnotationExample.print10 (Java.lang.Object)]:@ Com.array7.annotationTest.MethodInfo (author= Seven Lang, version=1.0, comments= method test Annotation, date=2013-11-20)
method Test Comment
Annotation in method [public static void com.array7.annotationTest.AnnotationExample.print20 ()]:@ Com.array7.annotationTest.MethodInfo (author= Seven Lang, version=2.0, comments= method test Annotation, date=2013-11-20)
Through the above output we can also find that the annotations are limited to perform the instantiation of the method, and then the static method.
The above mentioned knowledge theory and practice, I through books, Java documents, Internet data summarized and personally verified.
If any of the friends found that there is no accurate expression, please correct, lest misleading mistake himself, thank you.