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/
本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。若是以爲還有幫助的話,能夠點一下右下角的【推薦】,但願可以持續的爲你們帶來好的技術文章!想跟我一塊兒進步麼?那就【關注】我吧。html