Transferred from: http://blog.csdn.net/yixiaogang109/article/details/7328466
Java annotations are some of the meta-information that is attached to the code, used by some tools to parse and use at compile and run time, to illustrate and configure Functionality.
Annotations do not and do not affect the actual logic of the code, only the ancillary role. Included in the Java.lang.annotation Package.
1, Yuan Annotation
Meta annotations are annotations of Annotations. including @Retention @Target @Document @Inherited four KINDS.
1.1. @Retention: define retention policies for annotations
@Retention (retentionpolicy.source)//annotations only exist in the source code and do not contain @Retention (retentionpolicy.class)//default retention policy in the CLASS bytecode File. Annotations exist in a class bytecode file, but are not available at runtime, @Retention (retentionpolicy.runtime)//annotations exist in a class bytecode file and can be obtained at runtime by reflection at 1.2, @ Target: define the purpose of the annotation the source code for its definition is:
[java]View PlainCopy
- @Documented
- @Retention (retentionpolicy.runtime)
- @Target (elementtype.annotation_type)
- Public @interface Target {
- elementtype[] value ();
- }
@Target (elementtype.type)//interfaces, classes, enumerations, annotations @Target (elementtype.field)//fields, constants of enumerations @Target (elementtype.method)// Method @target (elementtype.parameter)//method parameter @target (elementtype.constructor)//constructor @target (elementtype.local_variable ///local variable @target (elementtype.annotation_type)//note @target (elementtype.package)///package by the above source can be known, his elementtype can have multiple, An annotation can be a class, a method, a field, and so on 1.3, @Document: Note that the note will be included in Javadoc 1.4, @Inherited: The description subclass can inherit from the parent class of the note 2, the Java annotations are customized below is an example of a custom annotation
[java]View PlainCopy
- @Documented
- @Target ({elementtype.type,elementtype.method})
- @Retention (retentionpolicy.runtime)
- Public @interface Yts {
- public enum ytstype{util,entity,service,model}
- public ytstype ClassType () default ytstype.util;
- }
[java]View PlainCopy
- @Documented
- @Retention (retentionpolicy.runtime)
- @Target (ELEMENTTYPE.METHOD)
- @Inherited
- Public @interface HelloWorld {
- public String name ()default "";
- }
@Retention (retentionpolicy.runtime)
The annotation defined is that the annotations exist in the class bytecode file and can be obtained through reflection at run Time.
@Target ({elementtype.type,elementtype.method})
So this annotation can be a class annotation, or it can be an annotation of a method.
Such an annotation is customized, of course, the members of the annotation can be the basic data type, but also for the data, object, and so on
3 The annotations are defined, so how do we get them and parse the annotations?
The reflection mechanism of Java can help, get annotations, the code is as Follows:
[java]View PlainCopy
- Public class Parseannotation {
- public void Parsemethod (Class clazz) throws illegalargumentexception, illegalaccessexception, invocationtargetexception, securityexception, nosuchmethodexception, instantiationexception{
- Object obj = clazz.getconstructor (new class[]{}). newinstance (new object[]{});
- for (Method method:clazz.getDeclaredMethods ()) {
- HelloWorld say = Method.getannotation (HelloWorld. class);
- String name = "";
- if (say! = Null) {
- Name = Say.name ();
- Method.invoke (obj, name);
- }
- Yts Yts = (yts) method.getannotation (yts. class);
- if (yts! = Null) {
- if (YtsType.util.equals (yts.classtype ())) {
- System.out.println ("this is a util method");
- }else{
- System.out.println ("this was a other method");
- }
- }
- }
- }
- @SuppressWarnings ("unchecked")
- public void Parsetype (Class clazz) throws illegalargumentexception, illegalaccessexception, invocationtargetexception{
- Yts Yts = (yts) clazz.getannotation (yts. class);
- if (yts! = Null) {
- if (YtsType.util.equals (yts.classtype ())) {
- System.out.println ("this is a util class");
- }else{
- System.out.println ("this was a other class");
- }
- }
- }
- }
The previous method is to parse the method annotation, the latter method is to get the class annotation
The following is the test method class
[java]View PlainCopy
- @Yts (classtype =ytstype.util)
- Public class Sayhell {
- @HelloWorld (name = "xiaoming")
- @Yts
- public void SayHello (String Name) {
- if (name = = Null | | name.equals (")") {
- System.out.println ("hello world!");
- }else{
- System.out.println (name + "say Hello world!");
- }
- }
- }
[html]View PlainCopy
- public static void main (string[] Args) throws illegalargumentexception, illegalaccessexception, invocationtargetexception, securityexception, nosuchmethodexception, instantiationexception {
- Parseannotation parse = new Parseannotation ();
- Parse.parsemethod (sayhell.class);
- Parse.parsetype (sayhell.class);
- }
Turn! Java Custom Annotations