通过反射获取属性名称,以及属性值,以及获取@ApiModelProperty参数的value

import dto.File;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class getAllPro {


    public static void main(String[] args) {
        Detail score = new Detail();
        score.setName("法外狂徒张三");
        score.setAmt(new BigDecimal("10000.4"));
        score.setClasses("三年二班");
        score.setScore("100");
        Score score1 = new Score(score,2);
        String[] names = getFiledName(score1);
        System.out.println("已经获取属性名称");
        Map<String,String> map = getApiModelProperty(score1);
        Iterator iterator = map.keySet().iterator();
        Map<String,Object> valueMap = new HashMap<>();
        while(iterator.hasNext()){
            String property = iterator.next().toString();
            getFieldValueByName(property,score,valueMap);
            System.out.println("value:"+valueMap.get(property));
        }
        System.out.println("获取属性名称和属性名称");
    }


    /**
     * 根据属性名获取属性值
     * */
    private static Map<String,Object> getFieldValueByName(String fieldName, Object o,Map<String,Object> map) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[] {});
            Object value = method.invoke(o, new Object[] {});
            map.put(fieldName,value);
            return map;
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    /**
     * 获取属性名数组
     * */
    private static String[] getFiledName(Object o){
        Field[] fields=o.getClass().getDeclaredFields();
        String[] fieldNames=new String[fields.length];
        for(int i=0;i<fields.length;i++){
            System.out.println(fields[i].getType());
            fieldNames[i]=fields[i].getName();
        }
        return fieldNames;
    }

    /**
     * 通过反射获取@ApiModelProperty的value
     */
    public static Map<String,String>  getApiModelProperty (Object o) {
        Map<String,String> map = new HashMap<>();

            // 1.根据类路径获取类
//            Class<?> c = Class.forName(classPath);
            // 2.获取类的属性
            Field[] declaredFields = o.getClass().getDeclaredFields();
            // 3.遍历属性,获取属性上ApiModelProperty的值,属性的名,存入Properties
            if (declaredFields.length != 0) {
                for (Field field : declaredFields) {
                    if (field.getAnnotation(ApiModelProperty.class) != null) {
                        // key和value可根据需求存
                        // 这存的key为注解的值,value为类属性名
                        map.put(field.getName(),field.getAnnotation(ApiModelProperty.class).value());
                    }
                }
                return map;
            }
        return null;
    }
}




@Getter
@Setter
 class Score{

    @ApiModelProperty("成绩")
    private String score;

    @ApiModelProperty("姓名")
    private String name;

    @ApiModelProperty("班级")
    private String classes;

    @ApiModelProperty("金额")
    private BigDecimal amt;

    public void Score(){

    }

    public Score (Detail detail,int num){
//        File file = SpringFactory.getBean(File.class);
        if(num == 1){
            this.score = detail.getScore();
            this.name = detail.getName();
            this.classes = detail.getClasses();
            this.amt = detail.getAmt();
        }else{
//            file.setLoanNo("aaaa");
//            file.setFileUid("bbbbb");
//            this.score = file.getLoanNo();
//            this.name = file.getFileUid();
            this.classes = detail.getClasses();
            this.amt = detail.getAmt();
        }
    }

}

@Getter
@Setter
class Detail{

    @ApiModelProperty("成绩")
    private String score;

    @ApiModelProperty("姓名")
    private String name;

    @ApiModelProperty("班级")
    private String classes;

    @ApiModelProperty("金额")
    private BigDecimal amt;

}

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值