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;
}
通过反射获取属性名称,以及属性值,以及获取@ApiModelProperty参数的value
最新推荐文章于 2023-09-05 22:49:26 发布