Skip to content

Commit 418e010

Browse files
committed
HV-1623 Encapsulate ConstraintLocationKind inside ConstraintLocation
1 parent f44ecb4 commit 418e010

12 files changed

+76
-38
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.javabean.JavaBeanField;
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, JavaBeanField javaBeanField) {
60-
return new ConfiguredConstraint<>(
61-
constraint,
62-
ConstraintLocation.forField( javaBeanField ),
63-
ConstraintLocationKind.FIELD
64-
);
57+
return new ConfiguredConstraint<>( constraint, ConstraintLocation.forField( javaBeanField ) );
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().getConstrainable(),
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().getConstrainable(),
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
@@ -97,6 +97,11 @@ static ConstraintLocation forParameter(Callable callable, int index) {
9797
*/
9898
Object getValue(Object parent);
9999

100+
/**
101+
* Returns the nature of the constraint location.
102+
*/
103+
ConstraintLocationKind getKind();
104+
100105
enum ConstraintLocationKind {
101106
TYPE( ElementType.TYPE ),
102107
CONSTRUCTOR( ElementType.CONSTRUCTOR ),

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

Lines changed: 14 additions & 2 deletions
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;
@@ -18,13 +19,19 @@
1819
*
1920
* @author Hardy Ferentschik
2021
* @author Gunnar Morling
22+
* @author Guillaume Smet
2123
*/
2224
class CrossParameterConstraintLocation implements ConstraintLocation {
2325

2426
private final Callable callable;
2527

26-
CrossParameterConstraintLocation(Callable executable) {
27-
this.callable = 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;
2835
}
2936

3037
@Override
@@ -52,6 +59,11 @@ public Object getValue(Object parent) {
5259
return parent;
5360
}
5461

62+
@Override
63+
public ConstraintLocationKind getKind() {
64+
return kind;
65+
}
66+
5567
@Override
5668
public String toString() {
5769
return "CrossParameterConstraintLocation [callable=" + callable + "]";

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ public class FieldPropertyConstraintLocation extends PropertyConstraintLocation<
1818
FieldPropertyConstraintLocation(JavaBeanField javaBeanGetter) {
1919
super( javaBeanGetter );
2020
}
21+
22+
@Override
23+
public ConstraintLocationKind getKind() {
24+
return ConstraintLocationKind.FIELD;
25+
}
2126
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ public class GetterPropertyConstraintLocation extends PropertyConstraintLocation
1818
GetterPropertyConstraintLocation(JavaBeanGetter javaBeanGetter) {
1919
super( javaBeanGetter );
2020
}
21+
22+
@Override
23+
public ConstraintLocationKind getKind() {
24+
return ConstraintLocationKind.METHOD;
25+
}
2126
}

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* An abstract property constraint location.
1717
*
1818
* @author Marko Bekhta
19+
* @author Guillaume Smet
1920
*/
2021
public abstract class PropertyConstraintLocation<T extends Property> implements ConstraintLocation {
2122

0 commit comments

Comments
 (0)