Skip to content

Commit 0c9d582

Browse files
marko-bekhtagsmet
authored andcommitted
HV-1363 Wrap ElementType with ConstraintLocationKind
1 parent 5a33544 commit 0c9d582

27 files changed

+130
-84
lines changed

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package org.hibernate.validator.internal.cfg.context;
88

99
import java.lang.annotation.Annotation;
10-
import java.lang.annotation.ElementType;
1110
import java.lang.invoke.MethodHandle;
1211
import java.lang.invoke.MethodHandles;
1312
import java.lang.reflect.Type;
@@ -20,9 +19,10 @@
2019
import org.hibernate.validator.cfg.AnnotationDef;
2120
import org.hibernate.validator.cfg.ConstraintDef;
2221
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;
2324
import org.hibernate.validator.internal.properties.Callable;
2425
import org.hibernate.validator.internal.properties.Property;
25-
import org.hibernate.validator.internal.properties.javabean.JavaBeanField;
2626
import org.hibernate.validator.internal.util.annotation.AnnotationDescriptor;
2727
import org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor;
2828
import org.hibernate.validator.internal.util.logging.Log;
@@ -44,49 +44,49 @@ class ConfiguredConstraint<A extends Annotation> {
4444

4545
private final ConstraintDef<?, A> constraint;
4646
private final ConstraintLocation location;
47-
private final ElementType elementType;
47+
private final ConstraintLocationKind kind;
4848

49-
private ConfiguredConstraint(ConstraintDef<?, A> constraint, ConstraintLocation location, ElementType elementType) {
49+
private ConfiguredConstraint(ConstraintDef<?, A> constraint, ConstraintLocation location, ConstraintLocationKind kind) {
5050
this.constraint = constraint;
5151
this.location = location;
52-
this.elementType = elementType;
52+
this.kind = kind;
5353
}
5454

5555
static <A extends Annotation> ConfiguredConstraint<A> forType(ConstraintDef<?, A> constraint, Class<?> beanType) {
56-
return new ConfiguredConstraint<>( constraint, ConstraintLocation.forClass( beanType ), ElementType.TYPE );
56+
return new ConfiguredConstraint<>( constraint, ConstraintLocation.forClass( beanType ), ConstraintLocationKind.TYPE );
5757
}
5858

5959
static <A extends Annotation> ConfiguredConstraint<A> forFieldProperty(ConstraintDef<?, A> constraint, Property property) {
6060
return new ConfiguredConstraint<>(
6161
constraint,
6262
ConstraintLocation.forProperty( property ),
63-
ElementType.FIELD
63+
ConstraintLocationKind.PROPERTY
6464
);
6565
}
6666

6767
public static <A extends Annotation> ConfiguredConstraint<A> forParameter(ConstraintDef<?, A> constraint, Callable callable, int parameterIndex) {
6868
return new ConfiguredConstraint<>(
69-
constraint, ConstraintLocation.forParameter( callable, parameterIndex ), callable.isConstructor() ? ElementType.CONSTRUCTOR : ElementType.METHOD
69+
constraint, ConstraintLocation.forParameter( callable, parameterIndex ), getConstraintLocationKindFromCallable( callable )
7070
);
7171
}
7272

7373
public static <A extends Annotation> ConfiguredConstraint<A> forExecutable(ConstraintDef<?, A> constraint, Callable callable) {
7474
return new ConfiguredConstraint<>(
75-
constraint, ConstraintLocation.forReturnValue( callable ), callable.isConstructor() ? ElementType.CONSTRUCTOR : ElementType.METHOD
75+
constraint, ConstraintLocation.forReturnValue( callable ), getConstraintLocationKindFromCallable( callable )
7676
);
7777
}
7878

7979
public static <A extends Annotation> ConfiguredConstraint<A> forCrossParameter(ConstraintDef<?, A> constraint, Callable callable) {
8080
return new ConfiguredConstraint<>(
81-
constraint, ConstraintLocation.forCrossParameter( callable ), callable.isConstructor() ? ElementType.CONSTRUCTOR : ElementType.METHOD
81+
constraint, ConstraintLocation.forCrossParameter( callable ), getConstraintLocationKindFromCallable( callable )
8282
);
8383
}
8484

8585
public static <A extends Annotation> ConfiguredConstraint<A> forTypeArgument(ConstraintDef<?, A> constraint, ConstraintLocation delegate, TypeVariable<?> typeArgument, Type typeOfAnnotatedElement) {
8686
return new ConfiguredConstraint<>(
8787
constraint,
8888
ConstraintLocation.forTypeArgument( delegate, typeArgument, typeOfAnnotatedElement ),
89-
ElementType.TYPE_USE
89+
ConstraintLocationKind.TYPE_USE
9090
);
9191
}
9292

@@ -116,8 +116,14 @@ public String toString() {
116116
return constraint.toString();
117117
}
118118

119-
public ElementType getElementType() {
120-
return elementType;
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;
121127
}
122128

123129
/**

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.getElementType(),
78+
config.getConstraintLocationKind(),
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.getElementType(),
242+
config.getConstraintLocationKind(),
243243
getConstraintType()
244244
);
245245

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ private boolean isValidationRequired(BaseBeanValidationContext<?> validationCont
12831283
validationContext,
12841284
valueContext.getCurrentBean(),
12851285
valueContext.getPropertyPath(),
1286-
metaConstraint.getElementType()
1286+
metaConstraint.getConstraintLocationKind().getElementType()
12871287
);
12881288
}
12891289

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import static org.hibernate.validator.internal.util.CollectionHelper.newHashMap;
1010
import static org.hibernate.validator.internal.util.CollectionHelper.newHashSet;
1111

12-
import java.lang.annotation.ElementType;
1312
import java.lang.invoke.MethodHandles;
1413
import java.lang.reflect.Executable;
1514
import java.util.ArrayList;
@@ -39,6 +38,7 @@
3938
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl;
4039
import org.hibernate.validator.internal.metadata.descriptor.ExecutableDescriptorImpl;
4140
import org.hibernate.validator.internal.metadata.facets.Cascadable;
41+
import org.hibernate.validator.internal.metadata.location.ConstraintLocation.ConstraintLocationKind;
4242
import org.hibernate.validator.internal.metadata.raw.BeanConfiguration;
4343
import org.hibernate.validator.internal.metadata.raw.ConfigurationSource;
4444
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement;
@@ -368,7 +368,7 @@ public List<Class<? super T>> getClassHierarchy() {
368368

369369
private static Set<ConstraintDescriptorImpl<?>> getClassLevelConstraintsAsDescriptors(Set<MetaConstraint<?>> constraints) {
370370
return constraints.stream()
371-
.filter( c -> c.getElementType() == ElementType.TYPE )
371+
.filter( c -> c.getConstraintLocationKind() == ConstraintLocationKind.TYPE )
372372
.map( MetaConstraint::getDescriptor )
373373
.collect( Collectors.toSet() );
374374
}

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.hibernate.validator.internal.metadata.core.MetaConstraint;
2929
import org.hibernate.validator.internal.metadata.descriptor.ExecutableDescriptorImpl;
3030
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement;
31+
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement.ConstrainedElementKind;
3132
import org.hibernate.validator.internal.metadata.raw.ConstrainedExecutable;
3233
import org.hibernate.validator.internal.metadata.raw.ConstrainedParameter;
3334
import org.hibernate.validator.internal.properties.Callable;
@@ -255,7 +256,7 @@ public static class Builder extends MetaDataBuilder {
255256
/**
256257
* Either CONSTRUCTOR or METHOD.
257258
*/
258-
private final ConstrainedElement.ConstrainedElementKind kind;
259+
private final ConstrainedElementKind kind;
259260
private final Set<ConstrainedExecutable> constrainedExecutables = newHashSet();
260261
private Callable callable;
261262
private final boolean isGetterMethod;
@@ -311,21 +312,25 @@ public boolean accepts(ConstrainedElement constrainedElement) {
311312
}
312313

313314
private boolean isResolvedToSameMethodInHierarchy(Callable first, Callable other) {
314-
if ( first.isConstructor() || other.isConstructor() ) {
315+
if ( isConstructor( first ) || isConstructor( other ) ) {
315316
return first.equals( other );
316317
}
317318

318319
return first.isResolvedToSameMethodInHierarchy( executableHelper, getBeanClass(), other );
319320
}
320321

321322
private boolean overrides(Callable first, Callable other) {
322-
if ( first.isConstructor() || other.isConstructor() ) {
323+
if ( isConstructor( first ) || isConstructor( other ) ) {
323324
return false;
324325
}
325326

326327
return executableHelper.overrides( first, other );
327328
}
328329

330+
private boolean isConstructor(Callable callable) {
331+
return callable.getConstrainedElementKind() == ConstrainedElementKind.CONSTRUCTOR;
332+
}
333+
329334
@Override
330335
public final void add(ConstrainedElement constrainedElement) {
331336
super.add( constrainedElement );
@@ -380,11 +385,11 @@ public ExecutableMetaData build() {
380385
assertCorrectnessOfConfiguration();
381386

382387
return new ExecutableMetaData(
383-
kind == ConstrainedElement.ConstrainedElementKind.CONSTRUCTOR ? callable.getDeclaringClass().getSimpleName() : callable.getName(),
388+
kind == ConstrainedElementKind.CONSTRUCTOR ? callable.getDeclaringClass().getSimpleName() : callable.getName(),
384389
callable.getType(),
385390
callable.getParameterTypes(),
386-
kind == ConstrainedElement.ConstrainedElementKind.CONSTRUCTOR ? ElementKind.CONSTRUCTOR : ElementKind.METHOD,
387-
kind == ConstrainedElement.ConstrainedElementKind.CONSTRUCTOR ? Collections.singleton( callable.getSignature() ) :
391+
kind == ConstrainedElementKind.CONSTRUCTOR ? ElementKind.CONSTRUCTOR : ElementKind.METHOD,
392+
kind == ConstrainedElementKind.CONSTRUCTOR ? Collections.singleton( callable.getSignature() ) :
388393
CollectionHelper.toImmutableSet( signatures ),
389394
adaptOriginsAndImplicitGroups( getDirectConstraints() ),
390395
adaptOriginsAndImplicitGroups( getContainerElementConstraints() ),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private <A extends Annotation> MetaConstraint<A> adaptOriginAndImplicitGroup(Met
131131
constraintHelper,
132132
constraint.getLocation().getMember(),
133133
constraint.getDescriptor().getAnnotationDescriptor(),
134-
constraint.getElementType(),
134+
constraint.getConstraintLocationKind(),
135135
constraintClass.isInterface() ? constraintClass : null,
136136
definedIn,
137137
constraint.getDescriptor().getConstraintType()

engine/src/main/java/org/hibernate/validator/internal/metadata/core/MetaConstraint.java

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

99
import java.lang.annotation.Annotation;
10-
import java.lang.annotation.ElementType;
1110
import java.lang.reflect.Type;
1211
import java.lang.reflect.TypeVariable;
1312
import java.util.List;
@@ -23,6 +22,7 @@
2322
import org.hibernate.validator.internal.engine.valueextraction.ValueExtractorHelper;
2423
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl;
2524
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
25+
import org.hibernate.validator.internal.metadata.location.ConstraintLocation.ConstraintLocationKind;
2626
import org.hibernate.validator.internal.util.StringHelper;
2727
import org.hibernate.validator.internal.util.stereotypes.Immutable;
2828

@@ -100,8 +100,8 @@ public final ConstraintDescriptorImpl<A> getDescriptor() {
100100
return constraintTree.getDescriptor();
101101
}
102102

103-
public final ElementType getElementType() {
104-
return constraintTree.getDescriptor().getElementType();
103+
public final ConstraintLocationKind getConstraintLocationKind() {
104+
return constraintTree.getDescriptor().getConstraintLocationKind();
105105
}
106106

107107
public boolean validateConstraint(ValidationContext<?> validationContext, ValueContext<?, Object> valueContext) {
@@ -123,7 +123,7 @@ public boolean validateConstraint(ValidationContext<?> validationContext, ValueC
123123
}
124124

125125
private boolean doValidateConstraint(ValidationContext<?> executionContext, ValueContext<?, ?> valueContext) {
126-
valueContext.setElementType( getElementType() );
126+
valueContext.setElementType( getConstraintLocationKind().getElementType() );
127127
boolean validationResult = constraintTree.validateConstraints( executionContext, valueContext );
128128

129129
return validationResult;

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.io.Serializable;
1313
import java.lang.annotation.Annotation;
1414
import java.lang.annotation.Documented;
15-
import java.lang.annotation.ElementType;
1615
import java.lang.annotation.Repeatable;
1716
import java.lang.annotation.Retention;
1817
import java.lang.annotation.Target;
@@ -45,6 +44,7 @@
4544
import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorDescriptor;
4645
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
4746
import org.hibernate.validator.internal.metadata.core.ConstraintOrigin;
47+
import org.hibernate.validator.internal.metadata.location.ConstraintLocation.ConstraintLocationKind;
4848
import org.hibernate.validator.internal.properties.Callable;
4949
import org.hibernate.validator.internal.properties.Constrainable;
5050
import org.hibernate.validator.internal.properties.Property;
@@ -132,7 +132,7 @@ public class ConstraintDescriptorImpl<T extends Annotation> implements Constrain
132132
* Describes on which level ({@code TYPE}, {@code METHOD}, {@code FIELD}) the constraint was
133133
* defined on.
134134
*/
135-
private final ElementType elementType;
135+
private final ConstraintLocationKind constraintLocationKind;
136136

137137
/**
138138
* The origin of the constraint. Defined on the actual root class or somewhere in the class hierarchy
@@ -165,12 +165,12 @@ public class ConstraintDescriptorImpl<T extends Annotation> implements Constrain
165165
public ConstraintDescriptorImpl(ConstraintHelper constraintHelper,
166166
Constrainable constrainable,
167167
ConstraintAnnotationDescriptor<T> annotationDescriptor,
168-
ElementType type,
168+
ConstraintLocationKind kind,
169169
Class<?> implicitGroup,
170170
ConstraintOrigin definedOn,
171171
ConstraintType externalConstraintType) {
172172
this.annotationDescriptor = annotationDescriptor;
173-
this.elementType = type;
173+
this.constraintLocationKind = kind;
174174
this.definedOn = definedOn;
175175
this.isReportAsSingleInvalidConstraint = annotationDescriptor.getType().isAnnotationPresent(
176176
ReportAsSingleViolation.class
@@ -206,7 +206,6 @@ public ConstraintDescriptorImpl(ConstraintHelper constraintHelper,
206206
this.constraintType = determineConstraintType(
207207
annotationDescriptor.getType(),
208208
constrainable,
209-
type,
210209
!genericValidatorDescriptors.isEmpty(),
211210
!crossParameterValidatorDescriptors.isEmpty(),
212211
externalConstraintType
@@ -228,16 +227,16 @@ public ConstraintDescriptorImpl(ConstraintHelper constraintHelper,
228227
public ConstraintDescriptorImpl(ConstraintHelper constraintHelper,
229228
Constrainable constrainable,
230229
ConstraintAnnotationDescriptor<T> annotationDescriptor,
231-
ElementType type) {
232-
this( constraintHelper, constrainable, annotationDescriptor, type, null, ConstraintOrigin.DEFINED_LOCALLY, null );
230+
ConstraintLocationKind kind) {
231+
this( constraintHelper, constrainable, annotationDescriptor, kind, null, ConstraintOrigin.DEFINED_LOCALLY, null );
233232
}
234233

235234
public ConstraintDescriptorImpl(ConstraintHelper constraintHelper,
236235
Constrainable constrainable,
237236
ConstraintAnnotationDescriptor<T> annotationDescriptor,
238-
ElementType type,
237+
ConstraintLocationKind kind,
239238
ConstraintType constraintType) {
240-
this( constraintHelper, constrainable, annotationDescriptor, type, null, ConstraintOrigin.DEFINED_LOCALLY, constraintType );
239+
this( constraintHelper, constrainable, annotationDescriptor, kind, null, ConstraintOrigin.DEFINED_LOCALLY, constraintType );
241240
}
242241

243242
public ConstraintAnnotationDescriptor<T> getAnnotationDescriptor() {
@@ -313,8 +312,8 @@ public boolean isReportAsSingleViolation() {
313312
return isReportAsSingleInvalidConstraint;
314313
}
315314

316-
public ElementType getElementType() {
317-
return elementType;
315+
public ConstraintLocationKind getConstraintLocationKind() {
316+
return constraintLocationKind;
318317
}
319318

320319
public ConstraintOrigin getDefinedOn() {
@@ -361,7 +360,7 @@ public String toString() {
361360
sb.append( ", payloads=" ).append( payloads );
362361
sb.append( ", hasComposingConstraints=" ).append( composingConstraints.isEmpty() );
363362
sb.append( ", isReportAsSingleInvalidConstraint=" ).append( isReportAsSingleInvalidConstraint );
364-
sb.append( ", elementType=" ).append( elementType );
363+
sb.append( ", constraintLocationKind=" ).append( constraintLocationKind );
365364
sb.append( ", definedOn=" ).append( definedOn );
366365
sb.append( ", groups=" ).append( groups );
367366
sb.append( ", attributes=" ).append( annotationDescriptor.getAttributes() );
@@ -389,7 +388,6 @@ public String toString() {
389388
* </ul>
390389
*
391390
* @param constrainable The annotated member
392-
* @param elementType The type of the annotated element
393391
* @param hasGenericValidators Whether the constraint has at least one generic validator or
394392
* not
395393
* @param hasCrossParameterValidator Whether the constraint has a cross-parameter validator
@@ -400,13 +398,12 @@ public String toString() {
400398
*/
401399
private ConstraintType determineConstraintType(Class<? extends Annotation> constraintAnnotationType,
402400
Constrainable constrainable,
403-
ElementType elementType,
404401
boolean hasGenericValidators,
405402
boolean hasCrossParameterValidator,
406403
ConstraintType externalConstraintType) {
407404
ConstraintTarget constraintTarget = validationAppliesTo;
408405
ConstraintType constraintType = null;
409-
boolean isExecutable = isExecutable( elementType );
406+
boolean isExecutable = isExecutable( constraintLocationKind );
410407

411408
//target explicitly set to RETURN_VALUE
412409
if ( constraintTarget == ConstraintTarget.RETURN_VALUE ) {
@@ -533,8 +530,8 @@ private void validateComposingConstraintTypes() {
533530
}
534531
}
535532

536-
private boolean isExecutable(ElementType elementType) {
537-
return elementType == ElementType.METHOD || elementType == ElementType.CONSTRUCTOR;
533+
private boolean isExecutable(ConstraintLocationKind locationKind) {
534+
return locationKind == ConstraintLocationKind.METHOD || locationKind == ConstraintLocationKind.CONSTRUCTOR;
538535
}
539536

540537
@SuppressWarnings("unchecked")
@@ -744,7 +741,7 @@ annotationType, run( GetAnnotationAttributes.action( constraintAnnotation ) )
744741
}
745742

746743
return new ConstraintDescriptorImpl<>(
747-
constraintHelper, member, annotationDescriptorBuilder.build(), elementType, null, definedOn, constraintType
744+
constraintHelper, member, annotationDescriptorBuilder.build(), constraintLocationKind, null, definedOn, constraintType
748745
);
749746
}
750747

0 commit comments

Comments
 (0)