注解底层实现

package pannotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value={ElementType.FIELD,ElementType.METHOD})
@Retention(value=RetentionPolicy.RUNTIME)
public @interface MyYZ {
	//基本注解:重写,过时,抑制警告
	//元注解:目标,存活期,文档,继承
	//目标有哪些(PACKAGE\TYPE\ANNOTATION_TYPE\PARAMTER\METHOD\FIELD\LOCAL_VARIABLE)
	//存活期(SOURCE\CLASS\RUNTIME)
	String value();
	String Y();
	String Z() default "234";
	int[] w() default{};

}





package pannotation;

import java.lang.annotation.Annotation;

/**
 * 
 * @author 汪方晨
 *
 */
 
public class Test {
	@MyYZ(value="234",Y="80",Z="1353d",w={2,4,7,9})
	@SuppressWarnings({"all"})
	public void show(){
		
	}
	public static void main(String[] args){
		//反射来测试
		Class<?> clazz=Test.class;
		try {
			Annotation[] as=clazz.getMethod("show").getAnnotations();
			for(Annotation a:as){
				if(a instanceof  MyYZ){
					MyYZ yz=(MyYZ)a;
					System.out.println(yz.Y());
					
				}
			}
			
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


}


你可能感兴趣的:(注解,注解底层实现)