java反射之獲取類、方法、方法參數、類參數注解


Target注解介紹:

@Target:注解的作用目標
@Target(ElementType.TYPE)——接口、類、枚舉、注解
@Target(ElementType.FIELD)——字段、枚舉的常量
@Target(ElementType.METHOD)——方法
@Target(ElementType.PARAMETER)——方法參數
@Target(ElementType.CONSTRUCTOR) ——構造函數
@Target(ElementType.LOCAL_VARIABLE)——局部變量
@Target(ElementType.ANNOTATION_TYPE)——注解
@Target(ElementType.PACKAGE)——包

聲明注解:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface TypeAu{
    String name();
}
@Target({ElementType.METHOD,})
@Retention(RetentionPolicy.RUNTIME)
@interface MethodAu{
    String name();
}
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@interface ParamAu{
    String name();
}
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@interface ParamTypeAu{
    String name();
}

 

添加注解:

@TypeAu(name="測試類注解")
public class Test002 {
    @ParamTypeAu(name="測試類變量注解")
    private String str = "";
    @MethodAu(name="測試類方法注解")
    public String method2(@ParamAu(name="測試類方法參數注解")  String p1,String p2){
        return null;
    }

 

獲取注解:

public static void main(String[] args) {
        // 獲取類注解:
        if(Test002.class.isAnnotationPresent(TypeAu.class)){
            System.out.println(Test002.class.getAnnotation(TypeAu.class).name());
        }

        // 獲取類變量注解:
        Field[] fields = Test002.class.getDeclaredFields();
        for (Field f :
                fields) {
            if(f.isAnnotationPresent(ParamTypeAu.class)){
                System.out.println(f.getAnnotation(ParamTypeAu.class).name());
            }
        }

        // 獲取類方法注解:
        Method[] methods = Test002.class.getDeclaredMethods();
        for (Method m :
                methods) {
            if(m.isAnnotationPresent(MethodAu.class)){
                System.out.println(m.getAnnotation(MethodAu.class).name());
            }
        }


        // 獲取類方法參數注解:
        Method[] methods2 = Test002.class.getDeclaredMethods();
        for (Method m :
                methods2) {
            // 獲取方法的所有參數
            Parameter[] parameters = m.getParameters();
            for (Parameter p :
                    parameters) {
                // 判斷是否存在注解
                if(p.isAnnotationPresent(ParamAu.class)){
                    System.out.println(p.getAnnotation(ParamAu.class).name());
                }
            }
        }
    }

主要使用的API是Class類中的實現接口AnnotatedElement的方法

isAnnotationPresent --- 檢測該元素是否被對應注解修飾
getAnnotation --- 獲取注解對象

 

完整測試代碼:

package com.wh.pj1.test001;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

@TypeAu(name="測試類注解")
public class Test002 {
    @ParamTypeAu(name="測試類變量注解")
    private String str = "";
    @MethodAu(name="測試類方法注解")
    public String method2(@ParamAu(name="測試類方法參數注解")  String p1,String p2){
        return null;
    }


    public static void main(String[] args) {
        // 獲取類注解:
        if(Test002.class.isAnnotationPresent(TypeAu.class)){
            System.out.println(Test002.class.getAnnotation(TypeAu.class).name());
        }

        // 獲取類變量注解:
        Field[] fields = Test002.class.getDeclaredFields();
        for (Field f :
                fields) {
            if(f.isAnnotationPresent(ParamTypeAu.class)){
                System.out.println(f.getAnnotation(ParamTypeAu.class).name());
            }
        }

        // 獲取類方法注解:
        Method[] methods = Test002.class.getDeclaredMethods();
        for (Method m :
                methods) {
            if(m.isAnnotationPresent(MethodAu.class)){
                System.out.println(m.getAnnotation(MethodAu.class).name());
            }
        }


        // 獲取類方法參數注解:
        Method[] methods2 = Test002.class.getDeclaredMethods();
        for (Method m :
                methods2) {
            // 獲取方法的所有參數
            Parameter[] parameters = m.getParameters();
            for (Parameter p :
                    parameters) {
                // 判斷是否存在注解
                if(p.isAnnotationPresent(ParamAu.class)){
                    System.out.println(p.getAnnotation(ParamAu.class).name());
                }
            }
        }
    }
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface TypeAu{
    String name();
}
@Target({ElementType.METHOD,})
@Retention(RetentionPolicy.RUNTIME)
@interface MethodAu{
    String name();
}
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@interface ParamAu{
    String name();
}
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@interface ParamTypeAu{
    String name();
}
View Code

 

 

 

參考文章:

https://www.jianshu.com/p/13991ec28dd3


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM