溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java項目中的注解怎么利用反射機(jī)制讀取

發(fā)布時間:2020-12-03 15:26:22 來源:億速云 閱讀:132 作者:Leah 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)Java項目中的注解怎么利用反射機(jī)制讀取,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

一、自定義注解

元注解: 

@interface注解: 定義注解接口

@Target注解: 用于約束被描述的注解的使用范圍,當(dāng)被描述的注解超出使用范圍則編譯失敗。如:ElementType.METHOD,ElementType.TYPE;

@Retention 注解:用于約束被定義注解的作用范圍,作用范圍有三個:

1、RetentionPolicy.SOURCE:作用范圍是源碼,作用于Java文件中,當(dāng)執(zhí)行javac時去除該注解。

2、RetentionPolicy.CLASS:作用范圍是二進(jìn)制碼,就是存在于class文件中,當(dāng)執(zhí)行Java時去除該注解。

3、RetentionPolicy.RUNTIME:作用范圍為運行時,就是我們可以通過動態(tài)獲取該注釋。

@Documented:用于指定javadoc生成API文檔時顯示該注釋。

@Inherited用于指定被描述的注釋可以被其描述的類的子類繼承,默認(rèn)情況是不能被其子類繼承。

自定義注解接口:

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 表示默認(rèn)值
 
 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 默認(rèn)的為張三
//在定義注解時沒有給定默認(rèn)值時,在此處必須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<&#63;> 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;//強(qiáng)制轉(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<&#63;> interfaces[] = stu.getInterfaces();
  for(Class<&#63;> c:interfaces){
   Method[] imethod = c.getMethods();
   for(Method m:imethod){
    System.out.println(m.getName());
   }
  }
 }

}

關(guān)于Java項目中的注解怎么利用反射機(jī)制讀取就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI