All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.huanminabc.utils_common.obj.reflect.AnnotationUtil Maven / Gradle / Ivy

There is a newer version: 1.0.5-RELEASE
Show newest version
package com.gitee.huanminabc.utils_common.obj.reflect;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class AnnotationUtil {

    //获取类中全部注解和注解对应的方法
    public static   Map> getAnnotationAll(Class clazz) {
        Map> map=new HashMap<>();
        //类
        Map mapClass=new HashMap<>();
        mapClass.put(clazz,clazz.getAnnotations());
        map.put("class",mapClass);

        //属性
        Map mapFields=new HashMap<>();
        for (Field declaredField : clazz.getDeclaredFields()) {
            declaredField.setAccessible(true);
            mapFields.put(declaredField,declaredField.getAnnotations());

        }
        map.put("fields",mapFields);
        //方法
        Map mapMethods=new HashMap<>();
        for (Method declaredMethod : clazz.getDeclaredMethods()) {
            declaredMethod.setAccessible(true);
            mapMethods.put(declaredMethod,declaredMethod.getAnnotations());
        }
        map.put("methods",mapMethods);
        return   map;
    }

    //获取指定范围(class,fields,methods)内是否包含指定注解,返回范围对象和注解对象
    public static    Map getAnnotations(Class clazz,Class annotationType,String scope) {
        Map result=new HashMap<>();
        Map> annotationAll = getAnnotationAll(clazz);
        Map objectMap = annotationAll.get(scope);
        for (Map.Entry objectEntry : objectMap.entrySet()) {
            for (Annotation annotation : objectEntry.getValue()) {
                if (annotation.annotationType()==annotationType) {
                    result.put(objectEntry.getKey(),annotation);
                    break;
                }
            }
        }
        return  result;
    }

    //获取类指定注解
    public static    Map getAnnotationsClass(Class clazz,Class annotationType) {
        Map map=new HashMap<>();
        Map aClass = getAnnotations(clazz, annotationType, "class");
        for (Map.Entry objectAnnotationEntry : aClass.entrySet()) {
            map.put( (Class)objectAnnotationEntry.getKey(),objectAnnotationEntry.getValue());
        }
        return  map;
    }
    //获取类中方法指定注解
    public static Map getAnnotationsMethods(Class clazz, Class annotationType) {
        Map map = new HashMap<>();
        Map aClass = AnnotationUtil.getAnnotations(clazz, annotationType, "methods");
        for (Map.Entry objectAnnotationEntry : aClass.entrySet()) {
            map.put((Method) objectAnnotationEntry.getKey(), objectAnnotationEntry.getValue());
        }
        return map;
    }
    //获取类中属性指定注解
    public static Map getAnnotationsFields(Class clazz, Class annotationType) {
        Map map=new HashMap<>();
        Map aClass = AnnotationUtil.getAnnotations(clazz, annotationType, "fields");
        for (Map.Entry objectAnnotationEntry : aClass.entrySet()) {
            map.put( (Field)objectAnnotationEntry.getKey(),objectAnnotationEntry.getValue());
        }
        return  map;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy