package com.fjs.scenic.utils;

import com.fjs.scenic.annotation.FieldAnnotation;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ReflectUtil
 * @Author: lfj
 * @Description: TODO
 * @Date: 2019/9/30 14:02
 **/
public class ReflectUtil {

    /**
     * 获取类的自定义注解@FieldAnnotation 的name值
     * @param obj 类
     * @throws NoSuchFieldException
     * @throws SecurityException
     */
    public static List<String> getFieldAnnotationValue(Object obj) throws NoSuchFieldException, SecurityException {
        List<String> annotations = new ArrayList<>();
        // 获取所有的字段
        Field[] fields = obj.getClass().getDeclaredFields();

        for (Field f : fields) {
            // 判断字段注解是否存在
            boolean annotationPresent2 = f.isAnnotationPresent(FieldAnnotation.class);

            if (annotationPresent2) {

                FieldAnnotation name = f.getAnnotation(FieldAnnotation.class);
                // 获取注解值
                String nameStr = name.name();
                annotations.add(nameStr);
            }else{
                annotations.add("");
            }

        }
        return annotations;

    }
}
