Skip to content

Commit af67fd7

Browse files
marko-bekhtagsmet
authored andcommitted
HV-1623 Reintroduce field and getter constraint locations
1 parent 53ea896 commit af67fd7

File tree

11 files changed

+92
-41
lines changed

11 files changed

+92
-41
lines changed

engine/src/main/java/org/hibernate/validator/internal/cfg/context/ConfiguredConstraint.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.hibernate.validator.internal.properties.Callable;
2424
import org.hibernate.validator.internal.properties.Property;
2525
import org.hibernate.validator.internal.properties.javabean.JavaBeanField;
26+
import org.hibernate.validator.internal.properties.javabean.JavaBeanGetter;
2627
import org.hibernate.validator.internal.util.annotation.AnnotationDescriptor;
2728
import org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor;
2829
import org.hibernate.validator.internal.util.logging.Log;
@@ -59,7 +60,9 @@ static <A extends Annotation> ConfiguredConstraint<A> forType(ConstraintDef<?, A
5960
static <A extends Annotation> ConfiguredConstraint<A> forProperty(ConstraintDef<?, A> constraint, Property property) {
6061
return new ConfiguredConstraint<>(
6162
constraint,
62-
ConstraintLocation.forProperty( property ),
63+
property instanceof JavaBeanField
64+
? ConstraintLocation.forField( property.as( JavaBeanField.class ) )
65+
: ConstraintLocation.forGetter( property.as( JavaBeanGetter.class ) ),
6366
property instanceof JavaBeanField ? ElementType.FIELD : ElementType.METHOD
6467
);
6568
}

engine/src/main/java/org/hibernate/validator/internal/cfg/context/PropertyConstraintMappingContextImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.hibernate.validator.internal.properties.Callable;
2525
import org.hibernate.validator.internal.properties.Property;
2626
import org.hibernate.validator.internal.properties.javabean.JavaBeanField;
27+
import org.hibernate.validator.internal.properties.javabean.JavaBeanGetter;
2728
import org.hibernate.validator.internal.util.TypeResolutionHelper;
2829

2930
/**
@@ -47,7 +48,9 @@ final class PropertyConstraintMappingContextImpl
4748
super( typeContext.getConstraintMapping(), property.getType() );
4849
this.typeContext = typeContext;
4950
this.property = property;
50-
this.location = ConstraintLocation.forProperty( property );
51+
this.location = property instanceof JavaBeanField
52+
? ConstraintLocation.forField( property.as( JavaBeanField.class ) )
53+
: ConstraintLocation.forGetter( property.as( JavaBeanGetter.class ) );
5154
}
5255

5356
@Override

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.hibernate.validator.internal.metadata.descriptor.PropertyDescriptorImpl;
3232
import org.hibernate.validator.internal.metadata.facets.Cascadable;
3333
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
34-
import org.hibernate.validator.internal.metadata.location.PropertyConstraintLocation;
34+
import org.hibernate.validator.internal.metadata.location.GetterPropertyConstraintLocation;
3535
import org.hibernate.validator.internal.metadata.location.TypeArgumentConstraintLocation;
3636
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement;
3737
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement.ConstrainedElementKind;
@@ -41,6 +41,7 @@
4141
import org.hibernate.validator.internal.properties.Callable;
4242
import org.hibernate.validator.internal.properties.Constrainable;
4343
import org.hibernate.validator.internal.properties.Property;
44+
import org.hibernate.validator.internal.properties.javabean.JavaBeanGetter;
4445
import org.hibernate.validator.internal.util.CollectionHelper;
4546
import org.hibernate.validator.internal.util.TypeResolutionHelper;
4647
import org.hibernate.validator.internal.util.privilegedactions.GetDeclaredMethod;
@@ -243,7 +244,7 @@ protected Set<MetaConstraint<?>> adaptConstraints(ConstrainedElement constrained
243244
return constraints;
244245
}
245246

246-
ConstraintLocation getterConstraintLocation = ConstraintLocation.forProperty( ( (ConstrainedExecutable) constrainedElement ).getCallable().as( Property.class ) );
247+
ConstraintLocation getterConstraintLocation = ConstraintLocation.forGetter( ( (ConstrainedExecutable) constrainedElement ).getCallable().as( JavaBeanGetter.class ) );
247248

248249
// convert return value locations into getter locations for usage within this meta-data
249250
return constraints.stream()
@@ -257,7 +258,7 @@ private MetaConstraint<?> withGetterLocation(ConstraintLocation getterConstraint
257258
// fast track if it's a regular constraint
258259
if ( !(constraint.getLocation() instanceof TypeArgumentConstraintLocation) ) {
259260
// Change the constraint location to a GetterConstraintLocation if it is not already one
260-
if ( constraint.getLocation() instanceof PropertyConstraintLocation ) {
261+
if ( constraint.getLocation() instanceof GetterPropertyConstraintLocation ) {
261262
converted = constraint.getLocation();
262263
}
263264
else {
@@ -284,7 +285,7 @@ private MetaConstraint<?> withGetterLocation(ConstraintLocation getterConstraint
284285
for ( ConstraintLocation location : locationStack ) {
285286
if ( !(location instanceof TypeArgumentConstraintLocation) ) {
286287
// Change the constraint location to a GetterConstraintLocation if it is not already one
287-
if ( location instanceof PropertyConstraintLocation ) {
288+
if ( location instanceof GetterPropertyConstraintLocation ) {
288289
converted = location;
289290
}
290291
else {

engine/src/main/java/org/hibernate/validator/internal/metadata/location/ConstraintLocation.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
import org.hibernate.validator.internal.engine.path.PathImpl;
1313
import org.hibernate.validator.internal.properties.Callable;
1414
import org.hibernate.validator.internal.properties.Constrainable;
15-
import org.hibernate.validator.internal.properties.Property;
15+
import org.hibernate.validator.internal.properties.javabean.JavaBeanField;
16+
import org.hibernate.validator.internal.properties.javabean.JavaBeanGetter;
1617
import org.hibernate.validator.internal.util.ExecutableParameterNameProvider;
1718

1819
/**
@@ -38,8 +39,12 @@ static ConstraintLocation forClass(Class<?> declaringClass) {
3839
return new BeanConstraintLocation( declaringClass );
3940
}
4041

41-
static ConstraintLocation forProperty(Property property) {
42-
return new PropertyConstraintLocation( property );
42+
static ConstraintLocation forField(JavaBeanField field) {
43+
return new FieldPropertyConstraintLocation( field );
44+
}
45+
46+
static ConstraintLocation forGetter(JavaBeanGetter getter) {
47+
return new GetterPropertyConstraintLocation( getter );
4348
}
4449

4550
static ConstraintLocation forTypeArgument(ConstraintLocation delegate, TypeVariable<?> typeParameter, Type typeOfAnnotatedElement) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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.internal.metadata.location;
8+
9+
import org.hibernate.validator.internal.properties.javabean.JavaBeanField;
10+
11+
/**
12+
* Field property constraint location.
13+
*
14+
* @author Marko Bekhta
15+
*/
16+
public class FieldPropertyConstraintLocation extends PropertyConstraintLocation<JavaBeanField> {
17+
18+
FieldPropertyConstraintLocation(JavaBeanField javaBeanGetter) {
19+
super( javaBeanGetter );
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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.internal.metadata.location;
8+
9+
import org.hibernate.validator.internal.properties.javabean.JavaBeanGetter;
10+
11+
/**
12+
* Getter property constraint location.
13+
*
14+
* @author Marko Bekhta
15+
*/
16+
public class GetterPropertyConstraintLocation extends PropertyConstraintLocation<JavaBeanGetter> {
17+
18+
GetterPropertyConstraintLocation(JavaBeanGetter javaBeanGetter) {
19+
super( javaBeanGetter );
20+
}
21+
}

engine/src/main/java/org/hibernate/validator/internal/metadata/location/PropertyConstraintLocation.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@
99
import java.lang.reflect.Type;
1010

1111
import org.hibernate.validator.internal.engine.path.PathImpl;
12-
import org.hibernate.validator.internal.properties.Constrainable;
1312
import org.hibernate.validator.internal.properties.Property;
1413
import org.hibernate.validator.internal.util.ExecutableParameterNameProvider;
1514

1615
/**
17-
* Property constraint location.
16+
* An abstract property constraint location.
1817
*
1918
* @author Marko Bekhta
2019
*/
21-
public class PropertyConstraintLocation implements ConstraintLocation {
20+
public abstract class PropertyConstraintLocation<T extends Property> implements ConstraintLocation {
2221

2322
/**
2423
* The member the constraint was defined on.
2524
*/
26-
private final Property property;
25+
private final T property;
2726

28-
PropertyConstraintLocation(Property property) {
27+
PropertyConstraintLocation(T property) {
2928
this.property = property;
3029
}
3130

@@ -35,7 +34,7 @@ public Class<?> getDeclaringClass() {
3534
}
3635

3736
@Override
38-
public Constrainable getConstrainable() {
37+
public T getConstrainable() {
3938
return property;
4039
}
4140

@@ -60,7 +59,7 @@ public Object getValue(Object parent) {
6059

6160
@Override
6261
public String toString() {
63-
return "PropertyConstraintLocation [property=" + property + "]";
62+
return getClass().getSimpleName() + " [property=" + property + "]";
6463
}
6564

6665
@Override
@@ -72,7 +71,7 @@ public boolean equals(Object o) {
7271
return false;
7372
}
7473

75-
PropertyConstraintLocation that = (PropertyConstraintLocation) o;
74+
PropertyConstraintLocation<?> that = (PropertyConstraintLocation<?>) o;
7675

7776
if ( !property.equals( that.property ) ) {
7877
return false;

engine/src/main/java/org/hibernate/validator/internal/metadata/provider/AnnotationMetaDataProvider.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,46 +218,46 @@ private Set<ConstrainedElement> getFieldMetaData(Class<?> beanClass) {
218218
Set<ConstrainedElement> propertyMetaData = newHashSet();
219219

220220
for ( Field field : run( GetDeclaredFields.action( beanClass ) ) ) {
221-
Property property = new JavaBeanField( field );
221+
JavaBeanField javaBeanField = new JavaBeanField( field );
222222
// HV-172
223223
if ( Modifier.isStatic( field.getModifiers() ) ||
224-
annotationProcessingOptions.areMemberConstraintsIgnoredFor( property ) ||
224+
annotationProcessingOptions.areMemberConstraintsIgnoredFor( javaBeanField ) ||
225225
field.isSynthetic() ) {
226226

227227
continue;
228228
}
229229

230-
propertyMetaData.add( findPropertyMetaData( field, property ) );
230+
propertyMetaData.add( findPropertyMetaData( field, javaBeanField ) );
231231
}
232232
return propertyMetaData;
233233
}
234234

235-
private ConstrainedField findPropertyMetaData(Field field, Property property) {
235+
private ConstrainedField findPropertyMetaData(Field field, JavaBeanField javaBeanField) {
236236
Set<MetaConstraint<?>> constraints = convertToMetaConstraints(
237-
findConstraints( field, ElementType.FIELD, property ),
238-
property
237+
findConstraints( field, ElementType.FIELD, javaBeanField ),
238+
javaBeanField
239239
);
240240

241241
CascadingMetaDataBuilder cascadingMetaDataBuilder = findCascadingMetaData( field );
242-
Set<MetaConstraint<?>> typeArgumentsConstraints = findTypeAnnotationConstraints( field, property );
242+
Set<MetaConstraint<?>> typeArgumentsConstraints = findTypeAnnotationConstraints( field, javaBeanField );
243243

244244
return new ConstrainedField(
245245
ConfigurationSource.ANNOTATION,
246-
property,
246+
javaBeanField,
247247
constraints,
248248
typeArgumentsConstraints,
249249
cascadingMetaDataBuilder
250250
);
251251
}
252252

253-
private Set<MetaConstraint<?>> convertToMetaConstraints(List<ConstraintDescriptorImpl<?>> constraintDescriptors, Property property) {
253+
private Set<MetaConstraint<?>> convertToMetaConstraints(List<ConstraintDescriptorImpl<?>> constraintDescriptors, JavaBeanField javaBeanField) {
254254
if ( constraintDescriptors.isEmpty() ) {
255255
return Collections.emptySet();
256256
}
257257

258258
Set<MetaConstraint<?>> constraints = newHashSet();
259259

260-
ConstraintLocation location = ConstraintLocation.forProperty( property );
260+
ConstraintLocation location = ConstraintLocation.forField( javaBeanField );
261261

262262
for ( ConstraintDescriptorImpl<?> constraintDescription : constraintDescriptors ) {
263263
constraints.add( MetaConstraints.create( typeResolutionHelper, valueExtractorManager, constraintDescription, location ) );
@@ -843,7 +843,7 @@ private TypeArgumentFieldLocation(Field field) {
843843

844844
@Override
845845
public ConstraintLocation toConstraintLocation() {
846-
return ConstraintLocation.forProperty( new JavaBeanField( field ) );
846+
return ConstraintLocation.forField( new JavaBeanField( field ) );
847847
}
848848
}
849849

engine/src/main/java/org/hibernate/validator/internal/xml/mapping/ConstrainedFieldStaxBuilder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,19 @@ ConstrainedField build(Class<?> beanClass, List<String> alreadyProcessedFieldNam
7070
alreadyProcessedFieldNames.add( mainAttributeValue );
7171
}
7272
Field field = findField( beanClass, mainAttributeValue );
73-
JavaBeanField property = new JavaBeanField( field );
74-
ConstraintLocation constraintLocation = ConstraintLocation.forProperty( property );
73+
JavaBeanField javaBeanField = new JavaBeanField( field );
74+
ConstraintLocation constraintLocation = ConstraintLocation.forField( javaBeanField );
7575

7676
Set<MetaConstraint<?>> metaConstraints = constraintTypeStaxBuilders.stream()
7777
.map( builder -> builder.build( constraintLocation, java.lang.annotation.ElementType.FIELD, null ) )
7878
.collect( Collectors.toSet() );
7979

8080
ContainerElementTypeConfiguration containerElementTypeConfiguration = getContainerElementTypeConfiguration(
81-
property.getType(), constraintLocation );
81+
javaBeanField.getType(), constraintLocation );
8282

8383
ConstrainedField constrainedField = new ConstrainedField(
8484
ConfigurationSource.XML,
85-
property,
85+
javaBeanField,
8686
metaConstraints,
8787
containerElementTypeConfiguration.getMetaConstraints(),
8888
getCascadingMetaData( containerElementTypeConfiguration.getTypeParametersCascadingMetaData(), ReflectionHelper.typeOf( field ) )
@@ -91,7 +91,7 @@ ConstrainedField build(Class<?> beanClass, List<String> alreadyProcessedFieldNam
9191
// ignore annotations
9292
if ( ignoreAnnotations.isPresent() ) {
9393
annotationProcessingOptions.ignoreConstraintAnnotationsOnMember(
94-
property,
94+
javaBeanField,
9595
ignoreAnnotations.get()
9696
);
9797
}

engine/src/main/java/org/hibernate/validator/internal/xml/mapping/ConstrainedGetterStaxBuilder.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import org.hibernate.validator.internal.metadata.raw.ConfigurationSource;
2727
import org.hibernate.validator.internal.metadata.raw.ConstrainedExecutable;
2828
import org.hibernate.validator.internal.metadata.raw.ConstrainedParameter;
29-
import org.hibernate.validator.internal.properties.Callable;
30-
import org.hibernate.validator.internal.properties.Property;
3129
import org.hibernate.validator.internal.properties.javabean.JavaBeanGetter;
3230
import org.hibernate.validator.internal.util.ReflectionHelper;
3331
import org.hibernate.validator.internal.util.TypeResolutionHelper;
@@ -74,8 +72,8 @@ ConstrainedExecutable build(Class<?> beanClass, List<String> alreadyProcessedGet
7472
alreadyProcessedGetterNames.add( mainAttributeValue );
7573
}
7674
Method getter = findGetter( beanClass, mainAttributeValue );
77-
Property property = new JavaBeanGetter( beanClass, getter );
78-
ConstraintLocation constraintLocation = ConstraintLocation.forProperty( property );
75+
JavaBeanGetter javaBeanGetter = new JavaBeanGetter( beanClass, getter );
76+
ConstraintLocation constraintLocation = ConstraintLocation.forGetter( javaBeanGetter );
7977

8078
Set<MetaConstraint<?>> metaConstraints = constraintTypeStaxBuilders.stream()
8179
.map( builder -> builder.build( constraintLocation, java.lang.annotation.ElementType.METHOD, null ) )
@@ -86,7 +84,7 @@ ConstrainedExecutable build(Class<?> beanClass, List<String> alreadyProcessedGet
8684

8785
ConstrainedExecutable constrainedGetter = new ConstrainedExecutable(
8886
ConfigurationSource.XML,
89-
property.as( Callable.class ),
87+
javaBeanGetter,
9088
Collections.<ConstrainedParameter>emptyList(),
9189
Collections.<MetaConstraint<?>>emptySet(),
9290
metaConstraints,
@@ -97,7 +95,7 @@ ConstrainedExecutable build(Class<?> beanClass, List<String> alreadyProcessedGet
9795
// ignore annotations
9896
if ( ignoreAnnotations.isPresent() ) {
9997
annotationProcessingOptions.ignoreConstraintAnnotationsOnMember(
100-
property,
98+
javaBeanGetter,
10199
ignoreAnnotations.get()
102100
);
103101
}

0 commit comments

Comments
 (0)