package com.annotation;
import java.lang.annotation.Annotation;
/**
* 主意当不想value=11定义时,其他参数必须有default,这样value才能这样定义@MyAnnotation(11)
*/
@MyAnnotation(value=11,name="bb",names={"cc","dd"},getMyEnum=MyEnum.One,getMyBo=Class.class)
public class UseAnnotation {
//省略变量名称有且只能是value
@MyAnnotation(13)
private int inttest;
@MyAnnotation(value=11,name="wodenianga")
private String nnd;
@MyAnnotation(value=12,name="aa",names={"ff","ee"},getMyEnum=MyEnum.Three,getMyBo=MyBo.class)
public static void main(String[] args) throws Exception{
System.out.println("class annotation print------------");
if(UseAnnotation.class.isAnnotationPresent(MyAnnotation.class)){
MyAnnotation ann = UseAnnotation.class.getAnnotation(MyAnnotation.class);
System.out.println(ann.name());
System.out.println(ann.names().length);
System.out.println(ann.value());
System.out.println(ann.getMyEnum());
System.out.println(ann.getMyBo().getName());
}
System.out.println("method annotation print-----------------");
if(UseAnnotation.class.getDeclaredMethod("main", String[].class).isAnnotationPresent(MyAnnotation.class)){
MyAnnotation ann = UseAnnotation.class.getDeclaredMethod("main", String[].class).getAnnotation(MyAnnotation.class);
System.out.println(ann.name());
System.out.println(ann.names().length);
System.out.println(ann.value());
System.out.println(ann.getMyEnum());
System.out.println(ann.getMyBo().getName());
}
System.out.println("fild annotation print-----------------");
if(UseAnnotation.class.getDeclaredField("nnd").isAnnotationPresent(MyAnnotation.class)){
MyAnnotation ann = UseAnnotation.class.getDeclaredField("nnd").getAnnotation(MyAnnotation.class);
System.out.println(ann.name());
System.out.println(ann.names().length);
System.out.println(ann.value());
System.out.println(ann.getMyEnum());
System.out.println(ann.getMyBo().getName());
}
getStr("nid",new String[]{"gf","dfd"});
}
public static String getStr(@MyAnnotation(value=23,name="zhldahd") String test,@MyAnnotation(value=44,names={"sssssssssss","wodema"},name="gpisjo") String[] nida) throws Exception{
Annotation[][] ann = UseAnnotation.class.getDeclaredMethod("getStr", new Class[]{String.class,String[].class}).getParameterAnnotations();
System.out.println(((MyAnnotation)ann[0][0]).name());
System.out.println(((MyAnnotation)ann[0][0]).names()[0]);
System.out.println(((MyAnnotation)ann[1][0]).name());
System.out.println(((MyAnnotation)ann[1][0]).names()[0]);
return test+"玩的才";
}
}