Skip to content

Commit 254537e

Browse files
committed
HV-1363 Encapsulate ConstraintLocationKind inside ConstraintLocation
1 parent 2e22353 commit 254537e

10 files changed

+78
-44
lines changed

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

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import org.hibernate.validator.cfg.AnnotationDef;
2020
import org.hibernate.validator.cfg.ConstraintDef;
2121
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
22-
import org.hibernate.validator.internal.metadata.location.ConstraintLocation.ConstraintLocationKind;
23-
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement.ConstrainedElementKind;
2422
import org.hibernate.validator.internal.properties.Callable;
2523
import org.hibernate.validator.internal.properties.Property;
2624
import org.hibernate.validator.internal.util.annotation.AnnotationDescriptor;
@@ -34,6 +32,7 @@
3432
* related to its location (bean type etc.).
3533
*
3634
* @author Gunnar Morling
35+
* @author Guillaume Smet
3736
*/
3837
class ConfiguredConstraint<A extends Annotation> {
3938

@@ -44,49 +43,36 @@ class ConfiguredConstraint<A extends Annotation> {
4443

4544
private final ConstraintDef<?, A> constraint;
4645
private final ConstraintLocation location;
47-
private final ConstraintLocationKind kind;
4846

49-
private ConfiguredConstraint(ConstraintDef<?, A> constraint, ConstraintLocation location, ConstraintLocationKind kind) {
47+
private ConfiguredConstraint(ConstraintDef<?, A> constraint, ConstraintLocation location) {
5048
this.constraint = constraint;
5149
this.location = location;
52-
this.kind = kind;
5350
}
5451

5552
static <A extends Annotation> ConfiguredConstraint<A> forType(ConstraintDef<?, A> constraint, Class<?> beanType) {
56-
return new ConfiguredConstraint<>( constraint, ConstraintLocation.forClass( beanType ), ConstraintLocationKind.TYPE );
53+
return new ConfiguredConstraint<>( constraint, ConstraintLocation.forClass( beanType ) );
5754
}
5855

5956
static <A extends Annotation> ConfiguredConstraint<A> forFieldProperty(ConstraintDef<?, A> constraint, Property property) {
60-
return new ConfiguredConstraint<>(
61-
constraint,
62-
ConstraintLocation.forProperty( property ),
63-
ConstraintLocationKind.PROPERTY
64-
);
57+
return new ConfiguredConstraint<>( constraint, ConstraintLocation.forProperty( property ) );
6558
}
6659

6760
public static <A extends Annotation> ConfiguredConstraint<A> forParameter(ConstraintDef<?, A> constraint, Callable callable, int parameterIndex) {
68-
return new ConfiguredConstraint<>(
69-
constraint, ConstraintLocation.forParameter( callable, parameterIndex ), getConstraintLocationKindFromCallable( callable )
70-
);
61+
return new ConfiguredConstraint<>( constraint, ConstraintLocation.forParameter( callable, parameterIndex ) );
7162
}
7263

7364
public static <A extends Annotation> ConfiguredConstraint<A> forExecutable(ConstraintDef<?, A> constraint, Callable callable) {
74-
return new ConfiguredConstraint<>(
75-
constraint, ConstraintLocation.forReturnValue( callable ), getConstraintLocationKindFromCallable( callable )
76-
);
65+
return new ConfiguredConstraint<>( constraint, ConstraintLocation.forReturnValue( callable ) );
7766
}
7867

7968
public static <A extends Annotation> ConfiguredConstraint<A> forCrossParameter(ConstraintDef<?, A> constraint, Callable callable) {
80-
return new ConfiguredConstraint<>(
81-
constraint, ConstraintLocation.forCrossParameter( callable ), getConstraintLocationKindFromCallable( callable )
82-
);
69+
return new ConfiguredConstraint<>( constraint, ConstraintLocation.forCrossParameter( callable ) );
8370
}
8471

8572
public static <A extends Annotation> ConfiguredConstraint<A> forTypeArgument(ConstraintDef<?, A> constraint, ConstraintLocation delegate, TypeVariable<?> typeArgument, Type typeOfAnnotatedElement) {
8673
return new ConfiguredConstraint<>(
8774
constraint,
88-
ConstraintLocation.forTypeArgument( delegate, typeArgument, typeOfAnnotatedElement ),
89-
ConstraintLocationKind.TYPE_USE
75+
ConstraintLocation.forTypeArgument( delegate, typeArgument, typeOfAnnotatedElement )
9076
);
9177
}
9278

@@ -116,16 +102,6 @@ public String toString() {
116102
return constraint.toString();
117103
}
118104

119-
public ConstraintLocationKind getConstraintLocationKind() {
120-
return kind;
121-
}
122-
123-
private static ConstraintLocationKind getConstraintLocationKindFromCallable(Callable callable) {
124-
return callable.getConstrainedElementKind() == ConstrainedElementKind.CONSTRUCTOR
125-
? ConstraintLocationKind.CONSTRUCTOR
126-
: ConstraintLocationKind.METHOD;
127-
}
128-
129105
/**
130106
* Runs the given privileged action, using a privileged block if required.
131107
* <b>NOTE:</b> This must never be changed into a publicly available method to avoid execution of arbitrary

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private <A extends Annotation> MetaConstraint<A> asMetaConstraint(ConfiguredCons
7575
constraintHelper,
7676
config.getLocation().getMember(),
7777
config.createAnnotationDescriptor(),
78-
config.getConstraintLocationKind(),
78+
config.getLocation().getKind(),
7979
getConstraintType()
8080
);
8181

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ private <A extends Annotation> MetaConstraint<A> asMetaConstraint(ConfiguredCons
239239
constraintHelper,
240240
config.getLocation().getMember(),
241241
config.createAnnotationDescriptor(),
242-
config.getConstraintLocationKind(),
242+
config.getLocation().getKind(),
243243
getConstraintType()
244244
);
245245

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*
1919
* @author Hardy Ferentschik
2020
* @author Gunnar Morling
21+
* @author Guillaume Smet
2122
*/
2223
class BeanConstraintLocation implements ConstraintLocation {
2324

@@ -66,6 +67,11 @@ public Object getValue(Object parent) {
6667
return parent;
6768
}
6869

70+
@Override
71+
public ConstraintLocationKind getKind() {
72+
return ConstraintLocationKind.TYPE;
73+
}
74+
6975
@Override
7076
public String toString() {
7177
return "BeanConstraintLocation [declaringClass=" + declaringClass + ", typeForValidatorResolution=" + typeForValidatorResolution + "]";

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ static ConstraintLocation forParameter(Callable callable, int index) {
9292
*/
9393
Object getValue(Object parent);
9494

95+
/**
96+
* Returns the nature of the constraint location.
97+
*/
98+
ConstraintLocationKind getKind();
99+
95100
enum ConstraintLocationKind {
96101
TYPE( ElementType.TYPE ),
97102
CONSTRUCTOR( ElementType.CONSTRUCTOR ),

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
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;
13+
import org.hibernate.validator.internal.properties.Callable;
1214
import org.hibernate.validator.internal.properties.Constrainable;
1315
import org.hibernate.validator.internal.util.ExecutableParameterNameProvider;
1416

@@ -17,23 +19,29 @@
1719
*
1820
* @author Hardy Ferentschik
1921
* @author Gunnar Morling
22+
* @author Guillaume Smet
2023
*/
2124
class CrossParameterConstraintLocation implements ConstraintLocation {
2225

23-
private final Constrainable executable;
26+
private final Callable callable;
2427

25-
CrossParameterConstraintLocation(Constrainable executable) {
26-
this.executable = executable;
28+
private final ConstraintLocationKind kind;
29+
30+
CrossParameterConstraintLocation(Callable callable) {
31+
this.callable = callable;
32+
this.kind = callable.getConstrainedElementKind() == ConstrainedElementKind.CONSTRUCTOR
33+
? ConstraintLocationKind.CONSTRUCTOR
34+
: ConstraintLocationKind.METHOD;
2735
}
2836

2937
@Override
3038
public Class<?> getDeclaringClass() {
31-
return executable.getDeclaringClass();
39+
return callable.getDeclaringClass();
3240
}
3341

3442
@Override
3543
public Constrainable getMember() {
36-
return executable;
44+
return callable;
3745
}
3846

3947
@Override
@@ -51,14 +59,19 @@ public Object getValue(Object parent) {
5159
return parent;
5260
}
5361

62+
@Override
63+
public ConstraintLocationKind getKind() {
64+
return kind;
65+
}
66+
5467
@Override
5568
public String toString() {
56-
return "CrossParameterConstraintLocation [executable=" + executable + "]";
69+
return "CrossParameterConstraintLocation [callable=" + callable + "]";
5770
}
5871

5972
@Override
6073
public int hashCode() {
61-
return executable.hashCode();
74+
return callable.hashCode();
6275
}
6376

6477
@Override
@@ -73,7 +86,7 @@ public boolean equals(Object obj) {
7386
return false;
7487
}
7588
CrossParameterConstraintLocation other = (CrossParameterConstraintLocation) obj;
76-
if ( !executable.equals( other.executable ) ) {
89+
if ( !callable.equals( other.callable ) ) {
7790
return false;
7891
}
7992

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
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;
1213
import org.hibernate.validator.internal.properties.Callable;
1314
import org.hibernate.validator.internal.properties.Constrainable;
1415
import org.hibernate.validator.internal.util.ExecutableParameterNameProvider;
@@ -19,17 +20,22 @@
1920
*
2021
* @author Hardy Ferentschik
2122
* @author Gunnar Morling
23+
* @author Guillaume Smet
2224
*/
2325
public class ParameterConstraintLocation implements ConstraintLocation {
2426

2527
private final Callable callable;
2628
private final int index;
2729
private final Type typeForValidatorResolution;
30+
private final ConstraintLocationKind kind;
2831

2932
public ParameterConstraintLocation(Callable callable, int index) {
3033
this.callable = callable;
3134
this.index = index;
3235
this.typeForValidatorResolution = ReflectionHelper.boxedType( callable.getParameterGenericType( index ) );
36+
this.kind = callable.getConstrainedElementKind() == ConstrainedElementKind.CONSTRUCTOR
37+
? ConstraintLocationKind.CONSTRUCTOR
38+
: ConstraintLocationKind.METHOD;
3339
}
3440

3541
@Override
@@ -61,9 +67,14 @@ public Object getValue(Object parent) {
6167
return ( (Object[]) parent )[index];
6268
}
6369

70+
@Override
71+
public ConstraintLocationKind getKind() {
72+
return kind;
73+
}
74+
6475
@Override
6576
public String toString() {
66-
return "ParameterConstraintLocation [executable=" + callable + ", index=" + index + "]";
77+
return "ParameterConstraintLocation [callable=" + callable + ", index=" + index + "]";
6778
}
6879

6980
@Override

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* Property constraint location.
1818
*
1919
* @author Marko Bekhta
20+
* @author Guillaume Smet
2021
*/
2122
public class PropertyConstraintLocation implements ConstraintLocation {
2223

@@ -58,6 +59,11 @@ public Object getValue(Object parent) {
5859
return property.getValueFrom( parent );
5960
}
6061

62+
@Override
63+
public ConstraintLocationKind getKind() {
64+
return ConstraintLocationKind.PROPERTY;
65+
}
66+
6167
@Override
6268
public String toString() {
6369
return "PropertyConstraintLocation [property=" + property + "]";

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
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;
1213
import org.hibernate.validator.internal.properties.Callable;
1314
import org.hibernate.validator.internal.properties.Constrainable;
1415
import org.hibernate.validator.internal.util.ExecutableParameterNameProvider;
@@ -19,13 +20,19 @@
1920
* @author Hardy Ferentschik
2021
* @author Gunnar Morling
2122
* @author Marko Bekhta
23+
* @author Guillaume Smet
2224
*/
2325
class ReturnValueConstraintLocation implements ConstraintLocation {
2426

2527
private final Callable callable;
2628

29+
private final ConstraintLocationKind kind;
30+
2731
ReturnValueConstraintLocation(Callable callable) {
2832
this.callable = callable;
33+
this.kind = callable.getConstrainedElementKind() == ConstrainedElementKind.CONSTRUCTOR
34+
? ConstraintLocationKind.CONSTRUCTOR
35+
: ConstraintLocationKind.METHOD;
2936
}
3037

3138
@Override
@@ -53,9 +60,14 @@ public Object getValue(Object parent) {
5360
return parent;
5461
}
5562

63+
@Override
64+
public ConstraintLocationKind getKind() {
65+
return kind;
66+
}
67+
5668
@Override
5769
public String toString() {
58-
return "ReturnValueConstraintLocation [executable=" + callable + "]";
70+
return "ReturnValueConstraintLocation [callable=" + callable + "]";
5971
}
6072

6173
@Override

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ public ConstraintLocation getOuterDelegate() {
8787
return outerDelegate;
8888
}
8989

90+
@Override
91+
public ConstraintLocationKind getKind() {
92+
return ConstraintLocationKind.TYPE_USE;
93+
}
94+
9095
@Override
9196
public String toString() {
9297
return "TypeArgumentValueConstraintLocation [typeForValidatorResolution=" + StringHelper.toShortString( typeForValidatorResolution )

0 commit comments

Comments
 (0)