From d5fe9702fe391af05be349827f4966bb9ca42d16 Mon Sep 17 00:00:00 2001 From: marko-bekhta Date: Sun, 18 Mar 2018 23:32:58 +0100 Subject: [PATCH] HV-1363 Updated ValidationExtension to use new property filtering - introduced new helper in cdi module to deal with new PropertyFilter spi. --- .../validator/cdi/ValidationExtension.java | 9 ++- .../util/GetterPropertyMatcherHelper.java | 75 +++++++++++++++++++ .../DefaultGetterPropertyMatcher.java | 2 +- .../internal/util/ReflectionHelper.java | 42 ----------- 4 files changed, 81 insertions(+), 47 deletions(-) create mode 100644 cdi/src/main/java/org/hibernate/validator/cdi/internal/util/GetterPropertyMatcherHelper.java diff --git a/cdi/src/main/java/org/hibernate/validator/cdi/ValidationExtension.java b/cdi/src/main/java/org/hibernate/validator/cdi/ValidationExtension.java index ea0f4c52f6..173ee93568 100644 --- a/cdi/src/main/java/org/hibernate/validator/cdi/ValidationExtension.java +++ b/cdi/src/main/java/org/hibernate/validator/cdi/ValidationExtension.java @@ -51,9 +51,9 @@ import org.hibernate.validator.cdi.internal.ValidatorFactoryBean; import org.hibernate.validator.cdi.internal.interceptor.ValidationEnabledAnnotatedType; import org.hibernate.validator.cdi.internal.interceptor.ValidationInterceptor; +import org.hibernate.validator.cdi.internal.util.GetterPropertyMatcherHelper; import org.hibernate.validator.internal.util.Contracts; import org.hibernate.validator.internal.util.ExecutableHelper; -import org.hibernate.validator.internal.util.ReflectionHelper; import org.hibernate.validator.internal.util.TypeResolutionHelper; import org.hibernate.validator.internal.util.logging.Log; import org.hibernate.validator.internal.util.logging.LoggerFactory; @@ -99,6 +99,7 @@ public class ValidationExtension implements Extension { */ private final Validator validator; private final ValidatorFactory validatorFactory; + private final GetterPropertyMatcherHelper getterPropertyMatcherHelper; private final Set globalExecutableTypes; private final boolean isExecutableValidationEnabled; @@ -119,6 +120,7 @@ public ValidationExtension() { isExecutableValidationEnabled = bootstrap.isExecutableValidationEnabled(); validatorFactory = config.buildValidatorFactory(); validator = validatorFactory.getValidator(); + getterPropertyMatcherHelper = GetterPropertyMatcherHelper.forValidationFactory( validatorFactory ); executableHelper = new ExecutableHelper( new TypeResolutionHelper() ); } @@ -263,8 +265,7 @@ private void determineConstrainedMethods(AnnotatedType type, BeanDescript for ( AnnotatedMethod annotatedMethod : type.getMethods() ) { Method method = annotatedMethod.getJavaMember(); - //TODO: need to update the getter logic here: - boolean isGetter = false/*ReflectionHelper.isGetterMethod( method )*/; + boolean isGetter = getterPropertyMatcherHelper.isProperty( method ); // obtain @ValidateOnExecution from the top-most method in the hierarchy Method methodForExecutableTypeRetrieval = replaceWithOverriddenOrInterfaceMethod( method, overriddenAndImplementedMethods ); @@ -317,7 +318,7 @@ private boolean isNonGetterConstrained(Method method, BeanDescriptor beanDescrip } private boolean isGetterConstrained(Method method, BeanDescriptor beanDescriptor) { - String propertyName = ReflectionHelper.getPropertyName( method ); + String propertyName = getterPropertyMatcherHelper.getPropertyName( method ); PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty( propertyName ); return propertyDescriptor != null && propertyDescriptor.findConstraints() .declaredOn( ElementType.METHOD ) diff --git a/cdi/src/main/java/org/hibernate/validator/cdi/internal/util/GetterPropertyMatcherHelper.java b/cdi/src/main/java/org/hibernate/validator/cdi/internal/util/GetterPropertyMatcherHelper.java new file mode 100644 index 0000000000..5b39de7d11 --- /dev/null +++ b/cdi/src/main/java/org/hibernate/validator/cdi/internal/util/GetterPropertyMatcherHelper.java @@ -0,0 +1,75 @@ +/* + * Hibernate Validator, declare and validate application constraints + * + * License: Apache License, Version 2.0 + * See the license.txt file in the root directory or . + */ +package org.hibernate.validator.cdi.internal.util; + +import java.lang.reflect.Method; +import java.lang.reflect.Type; + +import javax.validation.ValidatorFactory; + +import org.hibernate.validator.HibernateValidatorFactory; +import org.hibernate.validator.internal.properties.DefaultGetterPropertyMatcher; +import org.hibernate.validator.spi.properties.ConstrainableExecutable; +import org.hibernate.validator.spi.properties.GetterPropertyMatcher; + +/** + * A wrapper around {@link GetterPropertyMatcher}. + * + * @author Marko Bekhta + */ +public class GetterPropertyMatcherHelper { + + private final GetterPropertyMatcher getterPropertyMatcher; + + private GetterPropertyMatcherHelper(GetterPropertyMatcher getterPropertyMatcher) { + this.getterPropertyMatcher = getterPropertyMatcher; + } + + public boolean isProperty(Method method) { + return getterPropertyMatcher.isProperty( new ConstrainableMethod( method ) ); + } + + public String getPropertyName(Method method) { + return getterPropertyMatcher.getPropertyName( new ConstrainableMethod( method ) ); + } + + public static GetterPropertyMatcherHelper forValidationFactory(ValidatorFactory factory) { + GetterPropertyMatcher getterPropertyMatcher; + if ( factory instanceof HibernateValidatorFactory ) { + getterPropertyMatcher = factory.unwrap( HibernateValidatorFactory.class ).getGetterPropertyMatcher(); + } + else { + getterPropertyMatcher = new DefaultGetterPropertyMatcher(); + } + return new GetterPropertyMatcherHelper( getterPropertyMatcher ); + } + + private static class ConstrainableMethod implements ConstrainableExecutable { + + private final Method method; + + private ConstrainableMethod(Method method) { + this.method = method; + } + + @Override public Class getReturnType() { + return method.getReturnType(); + } + + @Override public String getName() { + return method.getName(); + } + + @Override public Type[] getParameterTypes() { + return method.getParameterTypes(); + } + + public Method getMethod() { + return method; + } + } +} diff --git a/engine/src/main/java/org/hibernate/validator/internal/properties/DefaultGetterPropertyMatcher.java b/engine/src/main/java/org/hibernate/validator/internal/properties/DefaultGetterPropertyMatcher.java index cb437d5cd5..94123fe024 100644 --- a/engine/src/main/java/org/hibernate/validator/internal/properties/DefaultGetterPropertyMatcher.java +++ b/engine/src/main/java/org/hibernate/validator/internal/properties/DefaultGetterPropertyMatcher.java @@ -18,7 +18,7 @@ public class DefaultGetterPropertyMatcher implements GetterPropertyMatcher { private static final String PROPERTY_ACCESSOR_PREFIX_GET = "get"; private static final String PROPERTY_ACCESSOR_PREFIX_IS = "is"; private static final String PROPERTY_ACCESSOR_PREFIX_HAS = "has"; - public static final String[] PROPERTY_ACCESSOR_PREFIXES = { + private static final String[] PROPERTY_ACCESSOR_PREFIXES = { PROPERTY_ACCESSOR_PREFIX_GET, PROPERTY_ACCESSOR_PREFIX_IS, PROPERTY_ACCESSOR_PREFIX_HAS diff --git a/engine/src/main/java/org/hibernate/validator/internal/util/ReflectionHelper.java b/engine/src/main/java/org/hibernate/validator/internal/util/ReflectionHelper.java index ccab9c9316..dca9279db1 100644 --- a/engine/src/main/java/org/hibernate/validator/internal/util/ReflectionHelper.java +++ b/engine/src/main/java/org/hibernate/validator/internal/util/ReflectionHelper.java @@ -6,7 +6,6 @@ */ package org.hibernate.validator.internal.util; -import static org.hibernate.validator.internal.properties.DefaultGetterPropertyMatcher.PROPERTY_ACCESSOR_PREFIXES; import static org.hibernate.validator.internal.util.CollectionHelper.newHashMap; import java.lang.invoke.MethodHandles; @@ -83,47 +82,6 @@ public final class ReflectionHelper { private ReflectionHelper() { } - /** - * Returns the JavaBeans property name of the given member. - *

- * For fields, the field name will be returned. For getter methods, the - * decapitalized property name will be returned, with the "get", "is" or "has" - * prefix stripped off. Getter methods are methods - *

- *
    - *
  • whose name start with "get" and who have a return type but no parameter - * or
  • - *
  • whose name starts with "is" and who have no parameter and return - * {@code boolean} or
  • - *
  • whose name starts with "has" and who have no parameter and return - * {@code boolean} (HV-specific, not mandated by JavaBeans spec).
  • - *
- * - * @param member The member for which to get the property name. - * - * @return The property name for the given member or {@code null} if the - * member is neither a field nor a getter method according to the - * JavaBeans standard. - */ - @Deprecated - public static String getPropertyName(Member member) { - String name = null; - - if ( member instanceof Field ) { - name = member.getName(); - } - - if ( member instanceof Method ) { - String methodName = member.getName(); - for ( String prefix : PROPERTY_ACCESSOR_PREFIXES ) { - if ( methodName.startsWith( prefix ) ) { - name = StringHelper.decapitalize( methodName.substring( prefix.length() ) ); - } - } - } - return name; - } - /** * @param member The {@code Member} instance for which to retrieve the type. *