Permalink
Browse files

APPNG-2184 use ReflectionUtils to find field/method on super class(es)

  • Loading branch information...
madness-inc committed Sep 20, 2018
1 parent 41781ab commit d7913e4d1031426f228f5a695556cb47b63ad932
@@ -70,6 +70,7 @@
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.context.MessageSource;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* Default {@link ValidationProvider} implementation.
@@ -194,22 +195,19 @@ private Validation getValidationNode(FieldDef fieldDef) {
Set<ConstraintDescriptor<?>> constraints = null;
try {
Class<?> propertyType = validationClass;
Class<?> concreteType = validationClass;
Field ancestor = null;
boolean validAnnotationPresent = true;
if (!rootPath.equals(leafName)) {
String[] segments = rootPath.split("\\.");
for (String segment : rootPath.split("\\.")) {
Field field = propertyType.getDeclaredField(segment);
Class<?> propertyType = validationClass;
Class<?> concreteType = validationClass;
Field ancestor = null;
if (!rootPath.equals(leafName)) {
for (String segment : rootPath.split("\\.")) {
Field field = ReflectionUtils.findField(propertyType, segment);
if (null != field) {
if (null != ancestor) {
String methodName = "get" + StringUtils.capitalize(segment);
Method getter = propertyType.getMethod(methodName);
Valid fieldAnnotation = field.getAnnotation(Valid.class);
Valid methodAnnotarion = getter.getAnnotation(Valid.class);
validAnnotationPresent = !(null == fieldAnnotation && null == methodAnnotarion);
if (!validAnnotationPresent) {
Method getter = ReflectionUtils.findMethod(propertyType,
"get" + StringUtils.capitalize(segment));
Valid methodAnnotarion = null == getter ? null : getter.getAnnotation(Valid.class);
if (null == fieldAnnotation && null == methodAnnotarion) {
log.debug("Annotation @{} not found on property {}.{} of {}, returning",
Valid.class.getName(), ancestor.getName(), field.getName(), validationClass);
return null;
@@ -229,20 +227,17 @@ private Validation getValidationNode(FieldDef fieldDef) {
}
}
}
}
BeanDescriptor beanDescriptor = validator.getConstraintsForClass(concreteType);
if (null != beanDescriptor) {
PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty(leafName);
if (null != propertyDescriptor) {
constraints = propertyDescriptor.getConstraintDescriptors();
log.debug("Found constraint(s) for path {} on type {}: {}", propertyPath, validationClass,
constraints);
}
BeanDescriptor beanDescriptor = validator.getConstraintsForClass(concreteType);
if (null != beanDescriptor) {
PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty(leafName);
if (null != propertyDescriptor) {
constraints = propertyDescriptor.getConstraintDescriptors();
log.debug("Found constraint(s) for path {} on type {}: {}", propertyPath, validationClass, constraints);
}
} catch (ReflectiveOperationException | SecurityException e) {
log.warn(String.format("Field '%s' not found on class %s!", propertyPath, validationClass), e);
}
return constraints;
}
@@ -125,7 +125,16 @@
</rule>
</validation>
</field>
<field name="offspringNames['Han'].name" type="text" binding="offspringNames['Han'].name" />
<field name="offspringNames['Han'].name" type="text" binding="offspringNames['Han'].name">
<validation>
<notNull>
<message ref="offspringNames['Han'].name" class="ERROR" code="{javax.validation.constraints.NotNull.message}">must not be null</message>
</notNull>
<rule type="javax.validation.constraints.NotBlank" name="notBlank">
<message ref="offspringNames['Han'].name" class="ERROR" code="{javax.validation.constraints.NotBlank.message}">Thou must provide a value!</message>
</rule>
</validation>
</field>
<field name="father.father.name" type="text" binding="father.father.name">
<validation>
<notNull>

0 comments on commit d7913e4

Please sign in to comment.