Java custom notes and annotations

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The Java annotation is added in the code of some meta information, for some tools in the compilation, analysis and use the runtime, it explains, allocation function.

The practical logic of its actual will not affect the code, only plays the auxiliary labeled as

Use. But for Java JDK. Java.lang.annotation is described in detail.

Meta annotation refers to the annotated annotation, JAVA JDK provides four yuan notes, respectively:

@Document indicates that the annotations to the javaDoc document contains. JDK1.5 +

@Inherited indicates that the annotations can be sub class inheritance. JDK1.5 +

@Retention policy Retention marked annotation. JDK1.5 +

JDK provides three retention strategies:

@Retention(RetentionPolicy.SOURCE) Note: only exists in the source code, byte code Class file does not exist the annotation.

@Retention(RetentionPolicy.CLASS) -- Indicate annotations will only be retained after the compiler in Class bytecode file, the runtime cannot obtain.

@Retention(RetentionPolicy.RUNTIME)  Note: Mark will be retained in the class bytecode file, and the run time can be obtained by reflection mechanism.

JDK source code:

public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

@Effect of target Target marked annotation.

The role of the object are:

JDK source code

public enum ElementType {
    
  /** Class, interface (including annotation type), or enum declaration */ -- Mark classes, interfaces, annotation, enumeration
TYPE, /** Field declaration (includes enum constants) */ - label field, enumeration constants FIELD, /** Method declaration */ Method: Label METHOD, /** Parameter declaration */ -- marking parameters PARAMETER, /** Constructor declaration */-- Mark constructor CONSTRUCTOR, /** Local variable declaration */ Local variables -- Annotation LOCAL_VARIABLE, /** Annotation type declaration */ -- Notes ANNOTATION_TYPE, /** Package declaration */ -- Annotation package PACKAGE }

Understand that yuan note Java notes, if you want to customize the Java annotation, how to define?

1: Custom annotations:

Use the keyword: @interface

A custom class notes 1.1

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
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface AkClass {
	String value() default "hello";
}

 The use of 1.2 kinds of annotations, such as:

import demo.annontation.AkClass;
@AkClass()
public interface AkClassTest {
	
    public abstract void getAK();

}

 1.3 through the java reflection mechanism to obtain and analytic class notes, such as:

 Note: only when the class notes using the RetentionPolicy.RUNTIME retention strategies, at runtime, obtained by the java reflection mechanism

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import demo.annontation.AkClass;
import demo.annontation.AkMethod;

/**
 * Scan the current package inside, what class is AkClass class glosses over
 *
 */

public class Main {
	
	public static void main(String[] args) {
		//To determine whether a class is annotated
		System.out.println("Whether the object annotation type: "+AkClassTest.class.isAnnotation());
		System.out.println("Call of the specified class attribute value: "+AkClassTest.class.getAnnotation(AkClass.class).value());
		//Get all the notes a concrete class
		Annotation[] annotations = AkClassTest.class.getAnnotations();
		for(Annotation annotation : annotations){
			//Determine the current annotation object is a custom annotation
			if(annotation.annotationType() == AkClass.class){
				System.out.println("Type annotation name:"+AkClass.class.getSimpleName());
				Method[] methods = AkClass.class.getDeclaredMethods();
				for(Method method : methods){
					System.out.println("Type annotation method: "+method.getName());
				}
				System.out.println();
			}
		}
		System.out.println("Note all method to obtain all the methods on a class of");
		Method[] methods = AkClassTest.class.getMethods();
		for(Method method : methods){
			System.out.println("Method name: "+method.getName());
			System.out.println("Method call notes of value: "+method.getAnnotation(AkMethod.class).value());
			Annotation[] mAnnotations = method.getAnnotations();
			for(Annotation mAnnotation : mAnnotations){
				if(mAnnotation.annotationType() == AkMethod.class){
					System.out.println("Note name: "+AkMethod.class.getSimpleName());
				}
			}
		}
	}
}


 The operation results


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Lewis at November 16, 2013 - 10:12 AM