“注解”有三種
1:無實際參數,只有聲明
2:只存在單一參數,有些像方法調用
3:有多個參數
標准的“注解”就先不總結了。
想總結一下《如何創建自己的注解》。有很多流行的框架都會用到,所以對以后的學習也會有幫助。
1.無實際參數,只有聲明(表達某種含義)
import java.lang.annotation.Retention; import java.lang.annotation.Target import java.lang.annotation.RetentionPolicy; /* * 獨自開發的注解 * @auther Z,wk * @version 1.0 * */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface SampleRequired{ }
2.有單一參數或多個參數
package sample; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;
//ElementType.TYPE 類的定義
@Target({
ElementType.TYPE,
ElementType.FIELD,
ElementType.CONSTRUCTOR,
ElementType.METHOD
})
@Retention(RetentionPolicy.RUNTIME)
public @interface Info {
String value();
//此處還可以繼續添加
//String value2();
}
3.使用方法
package sample; import java.util.ArrayList; import java.util.List;
// ElementType.TYPE :添加到類或接口的定義上 @Info("SampleClass1 Info") public class SampleClass1 { private List list; public SampleClass1(){ } @Override public boolean equals(Object obj){ return list.equals(obj); } // ElmentType.Method:添加到方法聲明上 @Info("hogehoge") public void initList(){ list = new ArrayList(); list.add(10); } }
package sample; @Info("Sample2 class") public class SampleClass2 {
//ElementType.FIELD : 添加到成員變量上 @Info("foo field") private Foo foo;
//ElementType.CONSTRUCTOR : 添加到構造方法上 @Info("default constract") public SampleClass2(){ foo = new Foo(); }
// ElementType.METHOD : 添加到方法聲明上 @Info("bar method") public void bar() { foo.bar(); } }
4. 取得調用“注解”的對象(筆者理解為:很多框架使用注解的意義所在)
package sample; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; public class Client { public static void main(String[] args){ System.out.println("start annotation sample"); SampleClass1 sc1 = new SampleClass1(); SampleClass2 sc2 = new SampleClass2(); //取得指定的“注解” Annotation annotList[] = Info.class.getAnnotations(); System.out.println("annotation size is [" + annotList.length + "]"); for(int i=0; i<annotList.length; i++){ Annotation anno = annotList[i]; System.out.println(" annotation class is [" + anno.annotationType().getName() + "]"); } /****取得帶有注解的CLASS****/ //SampleClass1 Annotation annotList1[] = SampleClass1.class.getAnnotations(); System.out.println("SampleClass1's annotation size is [" + annotList1.length + "]"); for(int i=0; i<annotList1.length; i++){ Annotation anno1 = annotList1[i]; System.out.println(" annotation class is [" + anno1.annotationType().getName() + "]"); } //SampleClass2 Annotation annotList2[] = SampleClass2.class.getAnnotations(); System.out.println("SampleClass2's annotation size is [" + annotList2.length + "]"); for(int i=0; i<annotList2.length; i++){ Annotation anno2 = annotList2[i]; System.out.println(" annotation class is [" + anno2.annotationType().getName() + "]"); } System.out.println(); System.out.println(); /****取得帶有注解的METHOD****/ //SampleClass1 Method methodList1[] = SampleClass1.class.getMethods(); System.out.println("SampleClass1's method count is [" + methodList1.length + "]"); for(Method method : methodList1){ System.out.println(" method name is [" + method.getName() + "]"); for(Annotation annot : method.getAnnotations()){ System.out.println(" method annotation is [" + annot.annotationType().getName() + "]"); } } //SampleClass2 Method methodList2[] = SampleClass1.class.getMethods(); System.out.println("SampleClass2's method count is [" + methodList2.length + "]"); for(Method method : methodList2){ System.out.println(" method name is [" + method.getName() + "]"); for(Annotation annot : method.getAnnotations()){ System.out.println(" method annotation is [" + annot.annotationType().getName() + "]"); } } System.out.println(); System.out.println(); /****取得帶有注解的FIELD(成員變量)****/ //SampleClass1 Field fieldList1[] = SampleClass1.class.getFields(); System.out.println("SampleClass1 has [" + fieldList1.length + "] fields"); for(Field field : fieldList1){ System.out.println(" field name is [" + field.getName() +"]"); for(Annotation annot : field.getAnnotations()){ System.out.println(" field annotation is [" + annot.annotationType().getName() + "]"); } } //SampleClass1 Field fieldList2[] = SampleClass2.class.getDeclaredFields();//getFields(); System.out.println("SampleClass2 has [" + fieldList2.length + "] fields"); for(Field field : fieldList2){ System.out.println(" field name is [" + field.getName() +"]"); for(Annotation annot : field.getAnnotations()){ System.out.println(" field annotation is [" + annot.annotationType().getName() + "]"); } } System.out.println(); System.out.println(); } }
本人屬於初學者,有不對的地方希望過路的大神批評指正。