Java 自定義注解及利用反射讀取注解的實例
一、自定義注解
元注解:
@interface注解: 定義注解接口
@Target注解: 用于約束被描述的注解的使用范圍,當(dāng)被描述的注解超出使用范圍則編譯失敗。如:ElementType.METHOD,ElementType.TYPE;
@Retention 注解:用于約束被定義注解的作用范圍,作用范圍有三個:
1、RetentionPolicy.SOURCE:作用范圍是源碼,作用于Java文件中,當(dāng)執(zhí)行javac時去除該注解。
2、RetentionPolicy.CLASS:作用范圍是二進制碼,就是存在于class文件中,當(dāng)執(zhí)行Java時去除該注解。
3、RetentionPolicy.RUNTIME:作用范圍為運行時,就是我們可以通過動態(tài)獲取該注釋。
@Documented:用于指定javadoc生成API文檔時顯示該注釋。
@Inherited:用于指定被描述的注釋可以被其描述的類的子類繼承,默認情況是不能被其子類繼承。
自定義注解接口:
package com.java.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD,ElementType.TYPE})
@Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation_my {
String name() default "張三";//defalt 表示默認值
String say() default "hello world";
int age() default 21;
}
接下來我們定義一個接口:
package com.java.annotation;
@Annotation_my //使用我們剛才定義的注解
public interface Person {
@Annotation_my
public void name();
@Annotation_my
public void say();
@Annotation_my
public void age();
}
接口定義好了,我們就可以寫接口的實現(xiàn)類了(接口不能實例化)
package com.java.annotation;
@Annotation_my
@SuppressWarnings("unused")
public class Student implements Person {
private String name;
@Override
@Annotation_my(name="流氓公子") //賦值給name 默認的為張三
//在定義注解時沒有給定默認值時,在此處必須name賦初值
public void name() {
}
@Override
@Annotation_my(say=" hello world !")
public void say() {
}
@Override
@Annotation_my(age=20)
public void age() {
}
}
然后我們就編寫一個測試類測試我們的注解
package com.java.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Text {
Annotation[] annotation = null;
public static void main(String[] args) throws ClassNotFoundException {
new Text().getAnnotation();
}
public void getAnnotation() throws ClassNotFoundException{
Class<?> stu = Class.forName("com.java.annotation.Student");//靜態(tài)加載類
boolean isEmpty = stu.isAnnotationPresent(com.java.annotation.Annotation_my.class);//判斷stu是不是使用了我們剛才定義的注解接口if(isEmpty){
annotation = stu.getAnnotations();//獲取注解接口中的
for(Annotation a:annotation){
Annotation_my my = (Annotation_my)a;//強制轉(zhuǎn)換成Annotation_my類型
System.out.println(stu+":\n"+my.name()+" say: "+my.say()+" my age: "+my.age());
}
}
Method[] method = stu.getMethods();//
System.out.println("Method");
for(Method m:method){
boolean ismEmpty = m.isAnnotationPresent(com.java.annotation.Annotation_my.class);
if(ismEmpty){
Annotation[] aa = m.getAnnotations();
for(Annotation a:aa){
Annotation_my an = (Annotation_my)a;
System.out.println(m+":\n"+an.name()+" say: "+an.say()+" my age: "+an.age());
}
}
}
//get Fields by force
System.out.println("get Fileds by force !");
Field[] field = stu.getDeclaredFields();
for(Field f:field){
f.setAccessible(true);
System.out.println(f.getName());
}
System.out.println("get methods in interfaces !");
Class<?> interfaces[] = stu.getInterfaces();
for(Class<?> c:interfaces){
Method[] imethod = c.getMethods();
for(Method m:imethod){
System.out.println(m.getName());
}
}
}
}
以上這篇Java 自定義注解及利用反射讀取注解的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java8學(xué)習(xí)教程之函數(shù)引用的使用方法
這篇文章主要給大家介紹了關(guān)于java8學(xué)習(xí)教程之函數(shù)引用的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)下吧。2017-09-09
springboot集成ES實現(xiàn)磁盤文件全文檢索的示例代碼
這篇文章主要介紹了springboot集成ES實現(xiàn)磁盤文件全文檢索的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
處理Log4j2不能打印行號的問題(AsyncLogger)
這篇文章主要介紹了處理Log4j2不能打印行號的問題(AsyncLogger),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12

