Skip to content

Commit 7475853

Browse files
marko-bekhtagsmet
authored andcommitted
HV-1623 Introduce getter as a constraint location kind
1 parent 49c11c6 commit 7475853

12 files changed

+28
-27
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class GetterCascadable extends PropertyCascadable {
2525

2626
@Override
2727
public ConstraintLocationKind getConstraintLocationKind() {
28-
return ConstraintLocationKind.METHOD;
28+
return ConstraintLocationKind.GETTER;
2929
}
3030

3131
public static class Builder extends PropertyCascadable.Builder {

engine/src/main/java/org/hibernate/validator/internal/metadata/descriptor/ConstraintDescriptorImpl.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ private ConstraintType determineConstraintType(Class<? extends Annotation> const
403403
ConstraintType externalConstraintType) {
404404
ConstraintTarget constraintTarget = validationAppliesTo;
405405
ConstraintType constraintType = null;
406-
boolean isExecutable = isExecutable( constraintLocationKind );
406+
boolean isExecutable = constraintLocationKind.isExecutable();
407407

408408
//target explicitly set to RETURN_VALUE
409409
if ( constraintTarget == ConstraintTarget.RETURN_VALUE ) {
@@ -530,10 +530,6 @@ private void validateComposingConstraintTypes() {
530530
}
531531
}
532532

533-
private boolean isExecutable(ConstraintLocationKind locationKind) {
534-
return locationKind == ConstraintLocationKind.METHOD || locationKind == ConstraintLocationKind.CONSTRUCTOR;
535-
}
536-
537533
@SuppressWarnings("unchecked")
538534
private static Set<Class<? extends Payload>> buildPayloadSet(ConstraintAnnotationDescriptor<?> annotationDescriptor) {
539535
Set<Class<? extends Payload>> payloadSet = newHashSet();

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.lang.reflect.TypeVariable;
1212

1313
import org.hibernate.validator.internal.engine.path.PathImpl;
14+
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement.ConstrainedElementKind;
1415
import org.hibernate.validator.internal.properties.Callable;
1516
import org.hibernate.validator.internal.properties.Constrainable;
1617
import org.hibernate.validator.internal.properties.javabean.JavaBeanField;
@@ -108,6 +109,7 @@ enum ConstraintLocationKind {
108109
METHOD( ElementType.METHOD ),
109110
PARAMETER( ElementType.PARAMETER ),
110111
FIELD( ElementType.FIELD ),
112+
GETTER( ElementType.METHOD ),
111113
TYPE_USE( ElementType.TYPE_USE ),
112114
;
113115

@@ -120,5 +122,19 @@ enum ConstraintLocationKind {
120122
public ElementType getElementType() {
121123
return elementType;
122124
}
125+
126+
public boolean isExecutable() {
127+
return this == CONSTRUCTOR || isMethod();
128+
}
129+
130+
public boolean isMethod() {
131+
return this == METHOD || this == GETTER;
132+
}
133+
134+
public static ConstraintLocationKind of(Callable callable) {
135+
return callable.getConstrainedElementKind() == ConstrainedElementKind.CONSTRUCTOR
136+
? ConstraintLocationKind.CONSTRUCTOR
137+
: callable instanceof JavaBeanGetter ? ConstraintLocationKind.GETTER : ConstraintLocationKind.METHOD;
138+
}
123139
}
124140
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.lang.reflect.Type;
1010

1111
import org.hibernate.validator.internal.engine.path.PathImpl;
12-
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement.ConstrainedElementKind;
1312
import org.hibernate.validator.internal.properties.Callable;
1413
import org.hibernate.validator.internal.properties.Constrainable;
1514
import org.hibernate.validator.internal.util.ExecutableParameterNameProvider;
@@ -29,9 +28,7 @@ class CrossParameterConstraintLocation implements ConstraintLocation {
2928

3029
CrossParameterConstraintLocation(Callable callable) {
3130
this.callable = callable;
32-
this.kind = callable.getConstrainedElementKind() == ConstrainedElementKind.CONSTRUCTOR
33-
? ConstraintLocationKind.CONSTRUCTOR
34-
: ConstraintLocationKind.METHOD;
31+
this.kind = ConstraintLocationKind.of( callable );
3532
}
3633

3734
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ public class GetterPropertyConstraintLocation extends PropertyConstraintLocation
2121

2222
@Override
2323
public ConstraintLocationKind getKind() {
24-
return ConstraintLocationKind.METHOD;
24+
return ConstraintLocationKind.GETTER;
2525
}
2626
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.lang.reflect.Type;
1010

1111
import org.hibernate.validator.internal.engine.path.PathImpl;
12-
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement.ConstrainedElementKind;
1312
import org.hibernate.validator.internal.properties.Callable;
1413
import org.hibernate.validator.internal.properties.Constrainable;
1514
import org.hibernate.validator.internal.util.ExecutableParameterNameProvider;
@@ -33,9 +32,7 @@ public ParameterConstraintLocation(Callable callable, int index) {
3332
this.callable = callable;
3433
this.index = index;
3534
this.typeForValidatorResolution = ReflectionHelper.boxedType( callable.getParameterGenericType( index ) );
36-
this.kind = callable.getConstrainedElementKind() == ConstrainedElementKind.CONSTRUCTOR
37-
? ConstraintLocationKind.CONSTRUCTOR
38-
: ConstraintLocationKind.METHOD;
35+
this.kind = ConstraintLocationKind.of( callable );
3936
}
4037

4138
@Override

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.lang.reflect.Type;
1010

1111
import org.hibernate.validator.internal.engine.path.PathImpl;
12-
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement.ConstrainedElementKind;
1312
import org.hibernate.validator.internal.properties.Callable;
1413
import org.hibernate.validator.internal.properties.Constrainable;
1514
import org.hibernate.validator.internal.util.ExecutableParameterNameProvider;
@@ -30,9 +29,7 @@ class ReturnValueConstraintLocation implements ConstraintLocation {
3029

3130
ReturnValueConstraintLocation(Callable callable) {
3231
this.callable = callable;
33-
this.kind = callable.getConstrainedElementKind() == ConstrainedElementKind.CONSTRUCTOR
34-
? ConstraintLocationKind.CONSTRUCTOR
35-
: ConstraintLocationKind.METHOD;
32+
this.kind = ConstraintLocationKind.of( callable );
3633
}
3734

3835
@Override

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,7 @@ private ConstrainedExecutable findExecutableMetaData(Executable executable) {
303303

304304
Map<ConstraintType, List<ConstraintDescriptorImpl<?>>> executableConstraints = findConstraints(
305305
javaBeanExecutable,
306-
javaBeanExecutable.getConstrainedElementKind() == ConstrainedElement.ConstrainedElementKind.CONSTRUCTOR
307-
? ConstraintLocationKind.CONSTRUCTOR
308-
: ConstraintLocationKind.METHOD
306+
ConstraintLocationKind.of( javaBeanExecutable )
309307
).stream().collect( Collectors.groupingBy( ConstraintDescriptorImpl::getConstraintType ) );
310308

311309
Set<MetaConstraint<?>> crossParameterConstraints;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ ConstrainedExecutable build(Class<?> beanClass, List<String> alreadyProcessedGet
7777
ConstraintLocation constraintLocation = ConstraintLocation.forGetter( javaBeanGetter );
7878

7979
Set<MetaConstraint<?>> metaConstraints = constraintTypeStaxBuilders.stream()
80-
.map( builder -> builder.build( constraintLocation, ConstraintLocationKind.METHOD, null ) )
80+
.map( builder -> builder.build( constraintLocation, ConstraintLocationKind.GETTER, null ) )
8181
.collect( Collectors.toSet() );
8282

8383
ContainerElementTypeConfiguration containerElementTypeConfiguration = getContainerElementTypeConfiguration(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Set<MetaConstraint<?>> build(Callable callable) {
8989
ConstraintLocation constraintLocation = ConstraintLocation.forCrossParameter( callable );
9090

9191
Set<MetaConstraint<?>> crossParameterConstraints = constraintTypeStaxBuilders.stream()
92-
.map( builder -> builder.build( constraintLocation, ConstraintLocationKind.METHOD, ConstraintType.CROSS_PARAMETER ) )
92+
.map( builder -> builder.build( constraintLocation, ConstraintLocationKind.of( callable ), ConstraintType.CROSS_PARAMETER ) )
9393
.collect( Collectors.toSet() );
9494

9595
// ignore annotations

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl;
2121
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
2222
import org.hibernate.validator.internal.metadata.location.ConstraintLocation.ConstraintLocationKind;
23-
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement.ConstrainedElementKind;
2423
import org.hibernate.validator.internal.properties.Callable;
2524
import org.hibernate.validator.internal.util.TypeResolutionHelper;
2625
import org.hibernate.validator.internal.xml.mapping.ContainerElementTypeConfigurationBuilder.ContainerElementTypeConfiguration;
@@ -57,7 +56,7 @@ CascadingMetaDataBuilder build(
5756

5857
ConstraintLocation constraintLocation = ConstraintLocation.forReturnValue( callable );
5958
returnValueConstraints.addAll( constraintTypeStaxBuilders.stream()
60-
.map( builder -> builder.build( constraintLocation, callable.getConstrainedElementKind() == ConstrainedElementKind.CONSTRUCTOR ? ConstraintLocationKind.CONSTRUCTOR : ConstraintLocationKind.METHOD, ConstraintDescriptorImpl.ConstraintType.GENERIC ) )
59+
.map( builder -> builder.build( constraintLocation, ConstraintLocationKind.of( callable ), ConstraintDescriptorImpl.ConstraintType.GENERIC ) )
6160
.collect( Collectors.toSet() ) );
6261

6362
ContainerElementTypeConfiguration containerElementTypeConfiguration = getContainerElementTypeConfiguration( callable.getType(), constraintLocation );

engine/src/test/java/org/hibernate/validator/testutils/ConstraintValidatorInitializationHelper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.hibernate.validator.internal.engine.scripting.DefaultScriptEvaluatorFactory;
1919
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
2020
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl;
21+
import org.hibernate.validator.internal.metadata.location.ConstraintLocation.ConstraintLocationKind;
2122
import org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor;
2223
import org.hibernate.validator.spi.scripting.ScriptEvaluator;
2324
import org.hibernate.validator.spi.scripting.ScriptEvaluatorFactory;
@@ -40,7 +41,7 @@ public static <T extends Annotation> ConstraintDescriptor<T> descriptorFrom(Cons
4041
CONSTRAINT_HELPER,
4142
null,
4243
annotationDescriptor,
43-
null
44+
ConstraintLocationKind.GETTER
4445
);
4546
}
4647

0 commit comments

Comments
 (0)