class Father { public void f() {} } public class Son extends Father{ @Override public void f() {} }
@Override
,这就是一个注解@Controller public class StudentController {
@Controller
也是一个注解public interface Annotation { boolean equals(Object obj); int hashCode(); String toString(); Class<? extends Annotation> annotationType(); }
ElementType | 含义 |
---|---|
ElementType | 含义 |
ANNOTATION_TYPE | 注解类型声明 |
CONSTRUCTOR | 构造方法声明 |
FIELD | 字段声明(包括枚举常量) |
LOCAL_VARIABLE | 局部变量声明 |
METHOD | 方法声明 |
PACKAGE | 包声明 |
PARAMETER | 参数声明 |
TYPE | 类、接口(包括注解类型)或枚举声明 |
import java.lang.annotation.Target; import java.lang.annotation.ElementType; @Target(ElementType.FIELD) public @interface MyAnnotation { }
RetentionPoicy | 意义 |
---|---|
SOURCE | 源文件中保留,比如@Override,用于与编译器交互 |
CLASS | source,Class文件保留,用于编译时生成额外的文件 |
RUNTIME | sorce,class文件,运行时保留 |
import java.lang.annotation.Documented; @Documented public @interface Demo { }
@Inherited @Retention( RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface MyAnnotation { String msg() default "jiajun"; } @MyAnnotation class Father { } class son extends Father{ } public class Demo6 { public static void main(String[] args) { Father f=new son(); System.out.println(Arrays.toString(f.getClass().getAnnotations())); } } //输出:[@MyAnnotation(msg=jiajun)]
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention { /** * Returns the retention policy. * @return the retention policy */ RetentionPolicy value(); }
@MyAnnotation()
,获取到的参数值是默认值@Retention( RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface MyAnnotation { String msg() default "jiajun"; } @MyAnnotation(msg="666") class Test1 {} public class Test2 { public static void main(String[] args) { Test1 t=new Test1(); Class c=t.getClass(); MyAnnotation ma = (MyAnnotation) c.getAnnotation(MyAnnotation.class); System.out.println(ma.msg()); } }
@Retention( RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface MyAnnotation { String name() default "jiajun"; } @MyAnnotation(name="jiajun") public class Test { } public static void main(String[] args) { Class clazz = Test.class; Annotation[] annotations = clazz.getAnnotations(); for(Annotation annotation : annotations){ if(annotation instanceof MyAnnotation){ MyAnnotation myAnnotation = (MyAnnotation) annotation; System.out.println("name: " + myAnnotation.name()); } } }
@Retention( RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MyAnnotation { String name() default "jiajun"; } public class Test { @MyAnnotation(name="jiajun") public void doSomething(){} } public class Demo { public static void main(String[] args) { Class clazz=Test.class; Method[] methods=clazz.getMethods(); for(Method method :methods) { if(method.getName()=="doSomething") { Annotation annotation = method.getAnnotation(MyAnnotation.class); if(annotation instanceof MyAnnotation){ MyAnnotation myAnnotation = (MyAnnotation) annotation; System.out.println("name: " + myAnnotation.name()); } } } } }
@Retention( RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface MyAnnotation { String name() default "jiajun"; } public class Test { public static void doSomething( @MyAnnotation(name="jiajun") String parameter){ } } public class Demo { public static void main(String[] args) { Class clazz=Test.class; Method[] methods=clazz.getMethods(); for(Method method :methods) { if(method.getName()=="doSomething") { Annotation[][] parameterAnnotations = method.getParameterAnnotations(); Class[] parameterTypes = method.getParameterTypes(); int i=0; for(Annotation[] annotations : parameterAnnotations){ Class parameterType = parameterTypes[i++]; for(Annotation annotation : annotations){ if(annotation instanceof MyAnnotation){ MyAnnotation myAnnotation = (MyAnnotation) annotation; System.out.println("param: " + parameterType.getName()); System.out.println("name : " + myAnnotation.name()); } } } } } } }
作者:jiajun 出处: http://www.cnblogs.com/-new/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如果觉得还有帮助的话,可以点一下右下角的【推荐】,希望能够持续的为大家带来好的技术文章!想跟我一起进步么?那就【关注】我吧。