1 回答

TA貢獻1825條經驗 獲得超4個贊
這是我使用 Apache commons 的解決方案:
public static Collection<String> getPropertyNamesListWithAnnotation(Class<?> targetClass, Class<? extends Annotation> annotationClass) {
Set<String> fieldNamesWithAnnotation = FieldUtils.getFieldsListWithAnnotation(targetClass, annotationClass).stream().map(Field::getName).collect(Collectors.toSet());
fieldNamesWithAnnotation.addAll(MethodUtils.getMethodsListWithAnnotation(targetClass, annotationClass, true, false).stream()
.map(Method::getName)
.filter(LangHelper::isValidGetterOrSetter)
.map(name -> StringUtils.uncapitalize(RegExUtils.replaceFirst(name, "^(get|set|is)", "")))
.collect(Collectors.toSet()));
return fieldNamesWithAnnotation;
}
private static boolean isValidGetterOrSetter(String methodName) {
if (!StringUtils.startsWithAny(methodName, "get", "set", "is")) {
LOG.warn("Annotated method is no valid getter or setter: '{}' -> Ignoring", methodName);
return false;
}
return true;
}
添加回答
舉報