Skip to content

Commit 1b55886

Browse files
marko-bekhtagsmet
authored andcommitted
HV-1363 Updated ValidationExtension to use new property filtering
- introduced new helper in cdi module to deal with new PropertyFilter spi.
1 parent 4cb8d13 commit 1b55886

File tree

4 files changed

+81
-47
lines changed

4 files changed

+81
-47
lines changed

cdi/src/main/java/org/hibernate/validator/cdi/ValidationExtension.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151
import org.hibernate.validator.cdi.internal.ValidatorFactoryBean;
5252
import org.hibernate.validator.cdi.internal.interceptor.ValidationEnabledAnnotatedType;
5353
import org.hibernate.validator.cdi.internal.interceptor.ValidationInterceptor;
54+
import org.hibernate.validator.cdi.internal.util.GetterPropertyMatcherHelper;
5455
import org.hibernate.validator.internal.util.Contracts;
5556
import org.hibernate.validator.internal.util.ExecutableHelper;
56-
import org.hibernate.validator.internal.util.ReflectionHelper;
5757
import org.hibernate.validator.internal.util.TypeResolutionHelper;
5858
import org.hibernate.validator.internal.util.logging.Log;
5959
import org.hibernate.validator.internal.util.logging.LoggerFactory;
@@ -99,6 +99,7 @@ public class ValidationExtension implements Extension {
9999
*/
100100
private final Validator validator;
101101
private final ValidatorFactory validatorFactory;
102+
private final GetterPropertyMatcherHelper getterPropertyMatcherHelper;
102103
private final Set<ExecutableType> globalExecutableTypes;
103104
private final boolean isExecutableValidationEnabled;
104105

@@ -119,6 +120,7 @@ public ValidationExtension() {
119120
isExecutableValidationEnabled = bootstrap.isExecutableValidationEnabled();
120121
validatorFactory = config.buildValidatorFactory();
121122
validator = validatorFactory.getValidator();
123+
getterPropertyMatcherHelper = GetterPropertyMatcherHelper.forValidationFactory( validatorFactory );
122124

123125
executableHelper = new ExecutableHelper( new TypeResolutionHelper() );
124126
}
@@ -263,8 +265,7 @@ private <T> void determineConstrainedMethods(AnnotatedType<T> type, BeanDescript
263265
for ( AnnotatedMethod<? super T> annotatedMethod : type.getMethods() ) {
264266
Method method = annotatedMethod.getJavaMember();
265267

266-
//TODO: need to update the getter logic here:
267-
boolean isGetter = false/*ReflectionHelper.isGetterMethod( method )*/;
268+
boolean isGetter = getterPropertyMatcherHelper.isProperty( method );
268269

269270
// obtain @ValidateOnExecution from the top-most method in the hierarchy
270271
Method methodForExecutableTypeRetrieval = replaceWithOverriddenOrInterfaceMethod( method, overriddenAndImplementedMethods );
@@ -317,7 +318,7 @@ private boolean isNonGetterConstrained(Method method, BeanDescriptor beanDescrip
317318
}
318319

319320
private boolean isGetterConstrained(Method method, BeanDescriptor beanDescriptor) {
320-
String propertyName = ReflectionHelper.getPropertyName( method );
321+
String propertyName = getterPropertyMatcherHelper.getPropertyName( method );
321322
PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty( propertyName );
322323
return propertyDescriptor != null && propertyDescriptor.findConstraints()
323324
.declaredOn( ElementType.METHOD )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Hibernate Validator, declare and validate application constraints
3+
*
4+
* License: Apache License, Version 2.0
5+
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6+
*/
7+
package org.hibernate.validator.cdi.internal.util;
8+
9+
import java.lang.reflect.Method;
10+
import java.lang.reflect.Type;
11+
12+
import javax.validation.ValidatorFactory;
13+
14+
import org.hibernate.validator.HibernateValidatorFactory;
15+
import org.hibernate.validator.internal.properties.DefaultGetterPropertyMatcher;
16+
import org.hibernate.validator.spi.properties.ConstrainableExecutable;
17+
import org.hibernate.validator.spi.properties.GetterPropertyMatcher;
18+
19+
/**
20+
* A wrapper around {@link GetterPropertyMatcher}.
21+
*
22+
* @author Marko Bekhta
23+
*/
24+
public class GetterPropertyMatcherHelper {
25+
26+
private final GetterPropertyMatcher getterPropertyMatcher;
27+
28+
private GetterPropertyMatcherHelper(GetterPropertyMatcher getterPropertyMatcher) {
29+
this.getterPropertyMatcher = getterPropertyMatcher;
30+
}
31+
32+
public boolean isProperty(Method method) {
33+
return getterPropertyMatcher.isProperty( new ConstrainableMethod( method ) );
34+
}
35+
36+
public String getPropertyName(Method method) {
37+
return getterPropertyMatcher.getPropertyName( new ConstrainableMethod( method ) );
38+
}
39+
40+
public static GetterPropertyMatcherHelper forValidationFactory(ValidatorFactory factory) {
41+
GetterPropertyMatcher getterPropertyMatcher;
42+
if ( factory instanceof HibernateValidatorFactory ) {
43+
getterPropertyMatcher = factory.unwrap( HibernateValidatorFactory.class ).getGetterPropertyMatcher();
44+
}
45+
else {
46+
getterPropertyMatcher = new DefaultGetterPropertyMatcher();
47+
}
48+
return new GetterPropertyMatcherHelper( getterPropertyMatcher );
49+
}
50+
51+
private static class ConstrainableMethod implements ConstrainableExecutable {
52+
53+
private final Method method;
54+
55+
private ConstrainableMethod(Method method) {
56+
this.method = method;
57+
}
58+
59+
@Override public Class<?> getReturnType() {
60+
return method.getReturnType();
61+
}
62+
63+
@Override public String getName() {
64+
return method.getName();
65+
}
66+
67+
@Override public Type[] getParameterTypes() {
68+
return method.getParameterTypes();
69+
}
70+
71+
public Method getMethod() {
72+
return method;
73+
}
74+
}
75+
}

engine/src/main/java/org/hibernate/validator/internal/properties/DefaultGetterPropertyMatcher.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class DefaultGetterPropertyMatcher implements GetterPropertyMatcher {
1818
private static final String PROPERTY_ACCESSOR_PREFIX_GET = "get";
1919
private static final String PROPERTY_ACCESSOR_PREFIX_IS = "is";
2020
private static final String PROPERTY_ACCESSOR_PREFIX_HAS = "has";
21-
public static final String[] PROPERTY_ACCESSOR_PREFIXES = {
21+
private static final String[] PROPERTY_ACCESSOR_PREFIXES = {
2222
PROPERTY_ACCESSOR_PREFIX_GET,
2323
PROPERTY_ACCESSOR_PREFIX_IS,
2424
PROPERTY_ACCESSOR_PREFIX_HAS

engine/src/main/java/org/hibernate/validator/internal/util/ReflectionHelper.java

-42
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77
package org.hibernate.validator.internal.util;
88

9-
import static org.hibernate.validator.internal.properties.DefaultGetterPropertyMatcher.PROPERTY_ACCESSOR_PREFIXES;
109
import static org.hibernate.validator.internal.util.CollectionHelper.newHashMap;
1110

1211
import java.lang.invoke.MethodHandles;
@@ -83,47 +82,6 @@ public final class ReflectionHelper {
8382
private ReflectionHelper() {
8483
}
8584

86-
/**
87-
* Returns the JavaBeans property name of the given member.
88-
* <p>
89-
* For fields, the field name will be returned. For getter methods, the
90-
* decapitalized property name will be returned, with the "get", "is" or "has"
91-
* prefix stripped off. Getter methods are methods
92-
* </p>
93-
* <ul>
94-
* <li>whose name start with "get" and who have a return type but no parameter
95-
* or</li>
96-
* <li>whose name starts with "is" and who have no parameter and return
97-
* {@code boolean} or</li>
98-
* <li>whose name starts with "has" and who have no parameter and return
99-
* {@code boolean} (HV-specific, not mandated by JavaBeans spec).</li>
100-
* </ul>
101-
*
102-
* @param member The member for which to get the property name.
103-
*
104-
* @return The property name for the given member or {@code null} if the
105-
* member is neither a field nor a getter method according to the
106-
* JavaBeans standard.
107-
*/
108-
@Deprecated
109-
public static String getPropertyName(Member member) {
110-
String name = null;
111-
112-
if ( member instanceof Field ) {
113-
name = member.getName();
114-
}
115-
116-
if ( member instanceof Method ) {
117-
String methodName = member.getName();
118-
for ( String prefix : PROPERTY_ACCESSOR_PREFIXES ) {
119-
if ( methodName.startsWith( prefix ) ) {
120-
name = StringHelper.decapitalize( methodName.substring( prefix.length() ) );
121-
}
122-
}
123-
}
124-
return name;
125-
}
126-
12785
/**
12886
* @param member The {@code Member} instance for which to retrieve the type.
12987
*

0 commit comments

Comments
 (0)