Creating a custom annotation
import java.lang.annotation.*; Import Java.lang.reflect.Method, @Documented @target (Elementtype.method) @Retention (retentionpolicy.runtime) public @Interface MethodInfo { default "Hupeng"; default "1.0"; String date (); String comment ();}
- Annotation methods can ' t have parameters.
- Annotation methods return types is limited to primitives, String, Enums, Annotation or array of these.
- Annotation methods can has default values.
@Target:
@Target illustrates the range of objects that annotation modifies: annotation can be used for packages, types (classes, interfaces, enumerations, annotation types), type members (methods, constructor methods, member variables, enumeration values), Method parameters and local variables (such as loop variables, catch parameters). Target can be more clearly decorated by using target in the declaration of the annotation type.
Function: Used to describe the scope of use of annotations (i.e., where the annotations described can be used)
The values (ElementType) are:
1.CONSTRUCTOR: Used to describe the constructor
2.FIELD: Used to describe the domain
3.local_variable: Used to describe local variables
4.METHOD: Used to describe the method
5.PACKAGE: Used to describe the package
6.PARAMETER: Used to describe parameters
7.TYPE: Used to describe classes, interfaces (including annotation types) or enum declarations
@Retention:
@Retention defines how long the annotation is retained: Some annotation appear only in the source code and are discarded by the compiler, while others are compiled in the class file The annotation that are compiled in the class file may be ignored by the virtual machine, while others will be read when class is loaded (note that it does not affect class execution because annotation is separated from the class in use). Using this meta-annotation can limit the "life cycle" of annotation.
Function: Indicates the level at which the annotation information needs to be saved to describe the life cycle of the annotation (i.e., the scope of the annotation being described is valid)
The values (Retentionpoicy) are:
1.SOURCE: Valid in source file (i.e., source file retention)
2.CLASS: Valid in class file (that is, class reserved)
3.RUNTIME: Valid at run time (that is, runtime retention)
@Documented:
@Documented used to describe other types of annotation that should be used as public APIs for annotated program members, so they can be documented by tools such as Javadoc. Documented is a markup annotation with no members.
@Inherited:
@Inherited meta-annotation is a markup annotation, @Inherited illustrates that a type that is labeled is inherited. , the annotation will be used for subclasses of the class.
Note: @Inherited the annotation type is inherited by subclasses of the class being labeled. The annotation class does not inherit from the interface it implements, and the method does not inherit annotation from the method it overloads.
Java built-in annotation
Starting with the Java5 version, it comes with three standard annontation types,
- Override,java.lang.override is a marker annotation type, which is used as a labeling method. It illustrates the way that the annotated method overloads the parent class and plays the role of the assertion. If we use this annotation in a method that does not overwrite the parent class method, the Java compiler will alert you with a compilation error. This annotaton often adds a supportability verification process when we try to override the parent method and then write the wrong method name.
- Deprecated,deprecated is also a kind of marker annotation. , the compiler will not encourage the use of this annotated program element. So using this modification has a certain "continuity": if we use this obsolete type or member in code by inheriting or overwriting it, although the inherited or overwritten type or member is not declared as @Deprecated, the compiler still has to call the police. Note: This tag is different @Deprecated this annotation type and the @deprecated in Javadoc: The former is recognized by the Java compiler, The latter is a description that is used by the Javadoc tool to generate documents that contain why a program member is obsolete, how it should be banned or substituted.
- Suppresswarnings, this annotation can tell the Java compiler to turn off warnings for classes, methods, and member variables. Sometimes there are some warnings to compile, some hidden bugs for these warnings, some unavoidable, and some warning messages that you don't want to see can be masked by this annotation. Suppresswarning is not a marker annotation. It has a member of type string[], and the value of this member is the forbidden warning name. For the Javac compiler, the compiler ignores unrecognized warning names.
Let's use the Java built-in annotation and custom annotation
Public classannotationexample {@Override @MethodInfo (author= "XXX", Version = "1.0", date = "2015/03/26", comment = "override toString ()") PublicString toString () {return"Annotationexample{}"; } @Deprecated @MethodInfo (comment= "Deprecated method", date = "2015/03/26") Public Static voidOldmethod () {System.out.println ("Old method, don ' t use it."); } @SuppressWarnings ({"Unchecked", "deprecation"}) @MethodInfo (author= "Pankaj", comment = "Main method", date = "Nov", Version = "1.0") Public Static voidgenericstest () {Oldmethod (); }}Using reflection to parse annotation
Note that our annotation retention Policy must be runtime, or we will not be able to get any data from him at runtime.
Importjava.lang.annotation.Annotation;ImportJava.lang.reflect.Method;/*** Created by Administrator on 2015/3/26.*/ Public classannotationparsing { Public Static voidMain (string[] args) { for(Method method:annotationexample.class. GetMethods ()) { if(Method.isannotationpresent (MethodInfo.class)) { for(Annotation annotation:method.getAnnotations ()) {System.out.println (Annotation+ "in method:" +method); } MethodInfo MethodInfo= Method.getannotation (MethodInfo.class); if("1.0". Equals (Methodinfo.version ())) {System.out.println ("Method with revision no 1.0 =" +method); } } } }}
Java Custom Annotation