Java反射获取对象中特定属性的值

问题一:如何找到某个对象中特定属性的值?
public static Object getFieldValueByObject (Object object , String targetFieldName) throws Exception {
Class objClass = object.getClass();
Field[] fields = objClass.getDeclaredFields();
for (Field field:fields) {
String currentFieldName = "";
boolean has_JsonProperty = field.isAnnotationPresent(JsonProperty.class);
if(has_JsonProperty){
currentFieldName = field.getAnnotation(JsonProperty.class).value();
}else {
currentFieldName = field.getName();
}
if(currentFieldName.equals(targetFieldName)){
return field.get(object);
}
}
return null;
}
问题二:如何找到某个对象中的List属性值里面的属性值
public static List getListFieldValueByObject (Object object , String targetFieldName) throws Exception {
List<Object> returnList = new ArrayList<>();
Class objClass = object.getClass();
Field[] fields = objClass.getDeclaredFields();
for (Field field:fields) {
if(field.getGenericType() instanceof List){
List<YouObject> fieldValue = (List) field.get(object);
Class youObjectClass = YouObject.getClass();
Field[] youObjectFields = youObjectClass.getDeclaredFields();
for (YouObject youObject :fieldValue) {
for (Field youObjectField : youObjectFields) {
String currentFieldName = "";
boolean has_JsonProperty = field.isAnnotationPresent(JsonProperty.class);
if(has_JsonProperty){
currentFieldName = field.getAnnotation(JsonProperty.class).value();
}else {
currentFieldName = field.getName();
}
if(currentFieldName.equals(targetFieldName)){
returnList.add(field.get(object));
}
}
}
}
}
return returnList;
}