- 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());
- }
- }
- }
- }
- }
- }
- }
来源: http://www.cnblogs.com/-new/p/7588250.html