Skip to content

Commit b041139

Browse files
committed
HV-1363 Propagate ConstraintLocationKind as far as possible
1 parent c9d0345 commit b041139

13 files changed

+80
-90
lines changed

engine/src/main/java/org/hibernate/validator/internal/engine/ConstraintViolationImpl.java

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package org.hibernate.validator.internal.engine;
88

99
import java.io.Serializable;
10-
import java.lang.annotation.ElementType;
1110
import java.lang.invoke.MethodHandles;
1211
import java.util.Map;
1312

@@ -45,17 +44,16 @@ public class ConstraintViolationImpl<T> implements HibernateConstraintViolation<
4544
private final int hashCode;
4645

4746
public static <T> ConstraintViolation<T> forBeanValidation(String messageTemplate,
48-
Map<String, Object> messageParameters,
49-
Map<String, Object> expressionVariables,
50-
String interpolatedMessage,
51-
Class<T> rootBeanClass,
52-
T rootBean,
53-
Object leafBeanInstance,
54-
Object value,
55-
Path propertyPath,
56-
ConstraintDescriptor<?> constraintDescriptor,
57-
ElementType elementType,
58-
Object dynamicPayload) {
47+
Map<String, Object> messageParameters,
48+
Map<String, Object> expressionVariables,
49+
String interpolatedMessage,
50+
Class<T> rootBeanClass,
51+
T rootBean,
52+
Object leafBeanInstance,
53+
Object value,
54+
Path propertyPath,
55+
ConstraintDescriptor<?> constraintDescriptor,
56+
Object dynamicPayload) {
5957
return new ConstraintViolationImpl<>(
6058
messageTemplate,
6159
messageParameters,
@@ -67,26 +65,24 @@ public static <T> ConstraintViolation<T> forBeanValidation(String messageTemplat
6765
value,
6866
propertyPath,
6967
constraintDescriptor,
70-
elementType,
7168
null,
7269
null,
7370
dynamicPayload
7471
);
7572
}
7673

7774
public static <T> ConstraintViolation<T> forParameterValidation(String messageTemplate,
78-
Map<String, Object> messageParameters,
79-
Map<String, Object> expressionVariables,
80-
String interpolatedMessage,
81-
Class<T> rootBeanClass,
82-
T rootBean,
83-
Object leafBeanInstance,
84-
Object value,
85-
Path propertyPath,
86-
ConstraintDescriptor<?> constraintDescriptor,
87-
ElementType elementType,
88-
Object[] executableParameters,
89-
Object dynamicPayload) {
75+
Map<String, Object> messageParameters,
76+
Map<String, Object> expressionVariables,
77+
String interpolatedMessage,
78+
Class<T> rootBeanClass,
79+
T rootBean,
80+
Object leafBeanInstance,
81+
Object value,
82+
Path propertyPath,
83+
ConstraintDescriptor<?> constraintDescriptor,
84+
Object[] executableParameters,
85+
Object dynamicPayload) {
9086
return new ConstraintViolationImpl<>(
9187
messageTemplate,
9288
messageParameters,
@@ -98,26 +94,24 @@ public static <T> ConstraintViolation<T> forParameterValidation(String messageTe
9894
value,
9995
propertyPath,
10096
constraintDescriptor,
101-
elementType,
10297
executableParameters,
10398
null,
10499
dynamicPayload
105100
);
106101
}
107102

108103
public static <T> ConstraintViolation<T> forReturnValueValidation(String messageTemplate,
109-
Map<String, Object> messageParameters,
110-
Map<String, Object> expressionVariables,
111-
String interpolatedMessage,
112-
Class<T> rootBeanClass,
113-
T rootBean,
114-
Object leafBeanInstance,
115-
Object value,
116-
Path propertyPath,
117-
ConstraintDescriptor<?> constraintDescriptor,
118-
ElementType elementType,
119-
Object executableReturnValue,
120-
Object dynamicPayload) {
104+
Map<String, Object> messageParameters,
105+
Map<String, Object> expressionVariables,
106+
String interpolatedMessage,
107+
Class<T> rootBeanClass,
108+
T rootBean,
109+
Object leafBeanInstance,
110+
Object value,
111+
Path propertyPath,
112+
ConstraintDescriptor<?> constraintDescriptor,
113+
Object executableReturnValue,
114+
Object dynamicPayload) {
121115
return new ConstraintViolationImpl<>(
122116
messageTemplate,
123117
messageParameters,
@@ -129,7 +123,6 @@ public static <T> ConstraintViolation<T> forReturnValueValidation(String message
129123
value,
130124
propertyPath,
131125
constraintDescriptor,
132-
elementType,
133126
null,
134127
executableReturnValue,
135128
dynamicPayload
@@ -146,7 +139,6 @@ private ConstraintViolationImpl(String messageTemplate,
146139
Object value,
147140
Path propertyPath,
148141
ConstraintDescriptor<?> constraintDescriptor,
149-
ElementType elementType,
150142
Object[] executableParameters,
151143
Object executableReturnValue,
152144
Object dynamicPayload) {

engine/src/main/java/org/hibernate/validator/internal/engine/ValidatorImpl.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;
1010

11-
import java.lang.annotation.ElementType;
1211
import java.lang.invoke.MethodHandles;
1312
import java.lang.reflect.Constructor;
1413
import java.lang.reflect.Executable;
@@ -63,6 +62,7 @@
6362
import org.hibernate.validator.internal.metadata.core.MetaConstraint;
6463
import org.hibernate.validator.internal.metadata.facets.Cascadable;
6564
import org.hibernate.validator.internal.metadata.facets.Validatable;
65+
import org.hibernate.validator.internal.metadata.location.ConstraintLocation.ConstraintLocationKind;
6666
import org.hibernate.validator.internal.util.Contracts;
6767
import org.hibernate.validator.internal.util.ExecutableHelper;
6868
import org.hibernate.validator.internal.util.ReflectionHelper;
@@ -552,8 +552,8 @@ private void validateCascadedConstraints(BaseBeanValidationContext<?> validation
552552
for ( Cascadable cascadable : validatable.getCascadables() ) {
553553
valueContext.appendNode( cascadable );
554554

555-
ElementType elementType = cascadable.getElementType();
556-
if ( isCascadeRequired( validationContext, valueContext.getCurrentBean(), valueContext.getPropertyPath(), elementType ) ) {
555+
if ( isCascadeRequired( validationContext, valueContext.getCurrentBean(), valueContext.getPropertyPath(),
556+
cascadable.getConstraintLocationKind() ) ) {
557557
Object value = getCascadableValue( validationContext, valueContext.getCurrentBean(), cascadable );
558558
CascadingMetaData cascadingMetaData = cascadable.getCascadingMetaData();
559559

@@ -1283,12 +1283,13 @@ private boolean isValidationRequired(BaseBeanValidationContext<?> validationCont
12831283
validationContext,
12841284
valueContext.getCurrentBean(),
12851285
valueContext.getPropertyPath(),
1286-
metaConstraint.getConstraintLocationKind().getElementType()
1286+
metaConstraint.getConstraintLocationKind()
12871287
);
12881288
}
12891289

1290-
private boolean isReachable(BaseBeanValidationContext<?> validationContext, Object traversableObject, PathImpl path, ElementType type) {
1291-
if ( needToCallTraversableResolver( path, type ) ) {
1290+
private boolean isReachable(BaseBeanValidationContext<?> validationContext, Object traversableObject, PathImpl path,
1291+
ConstraintLocationKind constraintLocationKind) {
1292+
if ( needToCallTraversableResolver( path, constraintLocationKind ) ) {
12921293
return true;
12931294
}
12941295

@@ -1299,31 +1300,32 @@ private boolean isReachable(BaseBeanValidationContext<?> validationContext, Obje
12991300
path.getLeafNode(),
13001301
validationContext.getRootBeanClass(),
13011302
pathToObject,
1302-
type
1303+
constraintLocationKind.getElementType()
13031304
);
13041305
}
13051306
catch (RuntimeException e) {
13061307
throw LOG.getErrorDuringCallOfTraversableResolverIsReachableException( e );
13071308
}
13081309
}
13091310

1310-
private boolean needToCallTraversableResolver(PathImpl path, ElementType type) {
1311+
private boolean needToCallTraversableResolver(PathImpl path, ConstraintLocationKind constraintLocationKind) {
13111312
// as the TraversableResolver interface is designed right now it does not make sense to call it when
13121313
// there is no traversable object hosting the property to be accessed. For this reason we don't call the resolver
13131314
// for class level constraints (ElementType.TYPE) or top level method parameters or return values.
13141315
// see also BV expert group discussion - http://lists.jboss.org/pipermail/beanvalidation-dev/2013-January/000722.html
1315-
return isClassLevelConstraint( type )
1316+
return isClassLevelConstraint( constraintLocationKind )
13161317
|| isCrossParameterValidation( path )
13171318
|| isParameterValidation( path )
13181319
|| isReturnValueValidation( path );
13191320
}
13201321

1321-
private boolean isCascadeRequired(BaseBeanValidationContext<?> validationContext, Object traversableObject, PathImpl path, ElementType type) {
1322-
if ( needToCallTraversableResolver( path, type ) ) {
1322+
private boolean isCascadeRequired(BaseBeanValidationContext<?> validationContext, Object traversableObject, PathImpl path,
1323+
ConstraintLocationKind constraintLocationKind) {
1324+
if ( needToCallTraversableResolver( path, constraintLocationKind ) ) {
13231325
return true;
13241326
}
13251327

1326-
boolean isReachable = isReachable( validationContext, traversableObject, path, type );
1328+
boolean isReachable = isReachable( validationContext, traversableObject, path, constraintLocationKind );
13271329
if ( !isReachable ) {
13281330
return false;
13291331
}
@@ -1335,16 +1337,16 @@ private boolean isCascadeRequired(BaseBeanValidationContext<?> validationContext
13351337
path.getLeafNode(),
13361338
validationContext.getRootBeanClass(),
13371339
pathToObject,
1338-
type
1340+
constraintLocationKind.getElementType()
13391341
);
13401342
}
13411343
catch (RuntimeException e) {
13421344
throw LOG.getErrorDuringCallOfTraversableResolverIsCascadableException( e );
13431345
}
13441346
}
13451347

1346-
private boolean isClassLevelConstraint(ElementType type) {
1347-
return ElementType.TYPE.equals( type );
1348+
private boolean isClassLevelConstraint(ConstraintLocationKind constraintLocationKind) {
1349+
return ConstraintLocationKind.TYPE.equals( constraintLocationKind );
13481350
}
13491351

13501352
private boolean isCrossParameterValidation(PathImpl path) {

engine/src/main/java/org/hibernate/validator/internal/engine/ValueContext.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77
package org.hibernate.validator.internal.engine;
88

9-
import java.lang.annotation.ElementType;
109
import java.lang.reflect.TypeVariable;
1110

1211
import javax.validation.groups.Default;
@@ -19,6 +18,7 @@
1918
import org.hibernate.validator.internal.metadata.facets.Cascadable;
2019
import org.hibernate.validator.internal.metadata.facets.Validatable;
2120
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
21+
import org.hibernate.validator.internal.metadata.location.ConstraintLocation.ConstraintLocationKind;
2222
import org.hibernate.validator.internal.util.ExecutableParameterNameProvider;
2323
import org.hibernate.validator.internal.util.TypeVariables;
2424

@@ -67,9 +67,9 @@ public class ValueContext<T, V> {
6767
private final Validatable currentValidatable;
6868

6969
/**
70-
* The {@code ElementType} the constraint was defined on
70+
* The {@code ConstraintLocationKind} the constraint was defined on
7171
*/
72-
private ElementType elementType;
72+
private ConstraintLocationKind constraintLocationKind;
7373

7474
public static <T, V> ValueContext<T, V> getLocalExecutionContext(BeanMetaDataManager beanMetaDataManager,
7575
ExecutableParameterNameProvider parameterNameProvider, T value, Validatable validatable, PathImpl propertyPath) {
@@ -199,12 +199,12 @@ public final boolean validatingDefault() {
199199
return getCurrentGroup() != null && getCurrentGroup().getName().equals( Default.class.getName() );
200200
}
201201

202-
public final ElementType getElementType() {
203-
return elementType;
202+
public final ConstraintLocationKind getConstraintLocationKind() {
203+
return constraintLocationKind;
204204
}
205205

206-
public final void setElementType(ElementType elementType) {
207-
this.elementType = elementType;
206+
public final void setConstraintLocationKind(ConstraintLocationKind constraintLocationKind) {
207+
this.constraintLocationKind = constraintLocationKind;
208208
}
209209

210210
public final ValueState<V> getCurrentValueState() {
@@ -225,7 +225,7 @@ public String toString() {
225225
sb.append( ", propertyPath=" ).append( propertyPath );
226226
sb.append( ", currentGroup=" ).append( currentGroup );
227227
sb.append( ", currentValue=" ).append( currentValue );
228-
sb.append( ", elementType=" ).append( elementType );
228+
sb.append( ", constraintLocationKind=" ).append( constraintLocationKind );
229229
sb.append( '}' );
230230
return sb.toString();
231231
}

engine/src/main/java/org/hibernate/validator/internal/engine/validationcontext/BeanValidationContext.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ protected ConstraintViolation<T> createConstraintViolation(
6161
localContext.getCurrentValidatedValue(),
6262
propertyPath,
6363
constraintDescriptor,
64-
localContext.getElementType(),
6564
constraintViolationCreationContext.getDynamicPayload()
6665
);
6766
}

engine/src/main/java/org/hibernate/validator/internal/engine/validationcontext/ParameterExecutableValidationContext.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ protected ConstraintViolation<T> createConstraintViolation(
129129
valueContext.getCurrentValidatedValue(),
130130
propertyPath,
131131
constraintDescriptor,
132-
valueContext.getElementType(),
133132
executableParameters,
134133
constraintViolationCreationContext.getDynamicPayload()
135134
);

engine/src/main/java/org/hibernate/validator/internal/engine/validationcontext/PropertyValidationContext.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ protected ConstraintViolation<T> createConstraintViolation(
9494
localContext.getCurrentValidatedValue(),
9595
propertyPath,
9696
constraintDescriptor,
97-
localContext.getElementType(),
9897
constraintViolationCreationContext.getDynamicPayload()
9998
);
10099
}

engine/src/main/java/org/hibernate/validator/internal/engine/validationcontext/ReturnValueExecutableValidationContext.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ protected ConstraintViolation<T> createConstraintViolation(String messageTemplat
101101
valueContext.getCurrentValidatedValue(),
102102
propertyPath,
103103
constraintDescriptor,
104-
valueContext.getElementType(),
105104
executableReturnValue,
106105
constraintViolationCreationContext.getDynamicPayload()
107106
);

engine/src/main/java/org/hibernate/validator/internal/metadata/aggregated/ParameterMetaData.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77
package org.hibernate.validator.internal.metadata.aggregated;
88

9-
import java.lang.annotation.ElementType;
109
import java.lang.reflect.Type;
1110
import java.util.List;
1211
import java.util.Set;
@@ -20,6 +19,7 @@
2019
import org.hibernate.validator.internal.metadata.core.MetaConstraint;
2120
import org.hibernate.validator.internal.metadata.descriptor.ParameterDescriptorImpl;
2221
import org.hibernate.validator.internal.metadata.facets.Cascadable;
22+
import org.hibernate.validator.internal.metadata.location.ConstraintLocation.ConstraintLocationKind;
2323
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement;
2424
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement.ConstrainedElementKind;
2525
import org.hibernate.validator.internal.metadata.raw.ConstrainedParameter;
@@ -64,8 +64,8 @@ public int getIndex() {
6464
}
6565

6666
@Override
67-
public ElementType getElementType() {
68-
return ElementType.PARAMETER;
67+
public ConstraintLocationKind getConstraintLocationKind() {
68+
return ConstraintLocationKind.PARAMETER;
6969
}
7070

7171
@Override

engine/src/main/java/org/hibernate/validator/internal/metadata/aggregated/PropertyCascadable.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
*/
77
package org.hibernate.validator.internal.metadata.aggregated;
88

9-
import java.lang.annotation.ElementType;
109
import java.lang.reflect.Type;
1110

1211
import org.hibernate.validator.internal.engine.path.PathImpl;
1312
import org.hibernate.validator.internal.engine.valueextraction.ValueExtractorManager;
1413
import org.hibernate.validator.internal.metadata.facets.Cascadable;
14+
import org.hibernate.validator.internal.metadata.location.ConstraintLocation.ConstraintLocationKind;
1515
import org.hibernate.validator.internal.properties.Property;
1616
import org.hibernate.validator.internal.properties.javabean.JavaBeanField;
1717

@@ -26,18 +26,18 @@ public class PropertyCascadable implements Cascadable {
2626
private final Property property;
2727
private final Type cascadableType;
2828
private final CascadingMetaData cascadingMetaData;
29-
private final ElementType elementType;
29+
private final ConstraintLocationKind constraintLocationKind;
3030

3131
PropertyCascadable(Property property, CascadingMetaData cascadingMetaData) {
3232
this.property = property;
3333
this.cascadableType = property.getType();
3434
this.cascadingMetaData = cascadingMetaData;
35-
this.elementType = property instanceof JavaBeanField ? ElementType.FIELD : ElementType.METHOD;
35+
this.constraintLocationKind = property instanceof JavaBeanField ? ConstraintLocationKind.PROPERTY : ConstraintLocationKind.METHOD;
3636
}
3737

3838
@Override
39-
public ElementType getElementType() {
40-
return elementType;
39+
public ConstraintLocationKind getConstraintLocationKind() {
40+
return constraintLocationKind;
4141
}
4242

4343
@Override

0 commit comments

Comments
 (0)