Skip to content

Commit cace416

Browse files
marko-bekhtagsmet
authored andcommitted
HV-1623 Create different programmatic mapping contexts for getter and field
- create different implementations of PropertyConstraintMappingContextImpl for getter and field to separate the logic.
1 parent 8bb498e commit cace416

File tree

5 files changed

+151
-59
lines changed

5 files changed

+151
-59
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.cfg.context;
8+
9+
import org.hibernate.validator.cfg.ConstraintDef;
10+
import org.hibernate.validator.cfg.context.PropertyConstraintMappingContext;
11+
import org.hibernate.validator.internal.engine.valueextraction.ValueExtractorManager;
12+
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
13+
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl.ConstraintType;
14+
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
15+
import org.hibernate.validator.internal.metadata.raw.ConfigurationSource;
16+
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement;
17+
import org.hibernate.validator.internal.metadata.raw.ConstrainedField;
18+
import org.hibernate.validator.internal.properties.javabean.JavaBeanField;
19+
import org.hibernate.validator.internal.util.TypeResolutionHelper;
20+
21+
/**
22+
* An implementation of {@link PropertyConstraintMappingContextImpl} for a field property.
23+
* Represents a constraint mapping creational context which allows to configure the constraints
24+
* for one of the bean's field properties.
25+
*
26+
* @author Marko Bekhta
27+
*/
28+
final class FieldPropertyConstraintMappingContextImpl extends PropertyConstraintMappingContextImpl<JavaBeanField> {
29+
30+
FieldPropertyConstraintMappingContextImpl(TypeConstraintMappingContextImpl<?> typeContext, JavaBeanField javaBeanField) {
31+
super( typeContext, javaBeanField, ConstraintLocation.forField( javaBeanField ) );
32+
}
33+
34+
@Override
35+
public PropertyConstraintMappingContext constraint(ConstraintDef<?, ?> definition) {
36+
super.addConstraint(
37+
ConfiguredConstraint.forFieldProperty(
38+
definition, getProperty()
39+
)
40+
);
41+
return this;
42+
}
43+
44+
ConstrainedElement build(ConstraintHelper constraintHelper, TypeResolutionHelper typeResolutionHelper, ValueExtractorManager valueExtractorManager) {
45+
return new ConstrainedField(
46+
ConfigurationSource.API,
47+
getProperty(),
48+
getConstraints( constraintHelper, typeResolutionHelper, valueExtractorManager ),
49+
getTypeArgumentConstraints( constraintHelper, typeResolutionHelper, valueExtractorManager ),
50+
getCascadingMetaDataBuilder()
51+
);
52+
}
53+
54+
@Override
55+
protected ConstraintType getConstraintType() {
56+
return ConstraintType.GENERIC;
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.cfg.context;
8+
9+
import org.hibernate.validator.cfg.ConstraintDef;
10+
import org.hibernate.validator.cfg.context.PropertyConstraintMappingContext;
11+
import org.hibernate.validator.internal.engine.valueextraction.ValueExtractorManager;
12+
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
13+
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
14+
import org.hibernate.validator.internal.metadata.raw.ConfigurationSource;
15+
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement;
16+
import org.hibernate.validator.internal.metadata.raw.ConstrainedExecutable;
17+
import org.hibernate.validator.internal.properties.javabean.JavaBeanGetter;
18+
import org.hibernate.validator.internal.util.TypeResolutionHelper;
19+
20+
/**
21+
* An implementation of {@link PropertyConstraintMappingContextImpl} for a getter property.
22+
* Represents a constraint mapping creational context which allows to configure the constraints
23+
* for one of the bean's getter properties.
24+
*
25+
* @author Marko Bekhta
26+
*/
27+
final class GetterPropertyConstraintMappingContextImpl extends PropertyConstraintMappingContextImpl<JavaBeanGetter> {
28+
29+
GetterPropertyConstraintMappingContextImpl(TypeConstraintMappingContextImpl<?> typeContext, JavaBeanGetter javaBeanGetter) {
30+
super( typeContext, javaBeanGetter, ConstraintLocation.forGetter( javaBeanGetter ) );
31+
}
32+
33+
@Override
34+
public PropertyConstraintMappingContext constraint(ConstraintDef<?, ?> definition) {
35+
super.addConstraint(
36+
ConfiguredConstraint.forExecutable(
37+
definition, getProperty()
38+
)
39+
);
40+
return this;
41+
}
42+
43+
ConstrainedElement build(ConstraintHelper constraintHelper, TypeResolutionHelper typeResolutionHelper, ValueExtractorManager valueExtractorManager) {
44+
return new ConstrainedExecutable(
45+
ConfigurationSource.API,
46+
getProperty(),
47+
getConstraints( constraintHelper, typeResolutionHelper, valueExtractorManager ),
48+
getTypeArgumentConstraints( constraintHelper, typeResolutionHelper, valueExtractorManager ),
49+
getCascadingMetaDataBuilder()
50+
);
51+
}
52+
}

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

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

99
import java.lang.annotation.ElementType;
10+
import java.lang.invoke.MethodHandles;
1011

11-
import org.hibernate.validator.cfg.ConstraintDef;
1212
import org.hibernate.validator.cfg.context.ConstructorConstraintMappingContext;
1313
import org.hibernate.validator.cfg.context.ContainerElementConstraintMappingContext;
1414
import org.hibernate.validator.cfg.context.MethodConstraintMappingContext;
@@ -17,66 +17,64 @@
1717
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
1818
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl.ConstraintType;
1919
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
20-
import org.hibernate.validator.internal.metadata.raw.ConfigurationSource;
2120
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement;
22-
import org.hibernate.validator.internal.metadata.raw.ConstrainedExecutable;
23-
import org.hibernate.validator.internal.metadata.raw.ConstrainedField;
24-
import org.hibernate.validator.internal.properties.Callable;
2521
import org.hibernate.validator.internal.properties.Property;
2622
import org.hibernate.validator.internal.properties.javabean.JavaBeanField;
2723
import org.hibernate.validator.internal.properties.javabean.JavaBeanGetter;
2824
import org.hibernate.validator.internal.util.TypeResolutionHelper;
25+
import org.hibernate.validator.internal.util.logging.Log;
26+
import org.hibernate.validator.internal.util.logging.LoggerFactory;
2927

3028
/**
3129
* Constraint mapping creational context which allows to configure the constraints for one bean property.
3230
*
3331
* @author Hardy Ferentschik
3432
* @author Gunnar Morling
3533
* @author Kevin Pollet &lt;[email protected]&gt; (C) 2011 SERLI
34+
* @author Marko Bekhta
3635
*/
37-
final class PropertyConstraintMappingContextImpl
36+
abstract class PropertyConstraintMappingContextImpl<T extends Property>
3837
extends CascadableConstraintMappingContextImplBase<PropertyConstraintMappingContext>
3938
implements PropertyConstraintMappingContext {
4039

40+
private static final Log LOG = LoggerFactory.make( MethodHandles.lookup() );
41+
4142
private final TypeConstraintMappingContextImpl<?> typeContext;
4243

4344
// either Field or Method
44-
private final Property property;
45+
private final T property;
4546
private final ConstraintLocation location;
4647

47-
PropertyConstraintMappingContextImpl(TypeConstraintMappingContextImpl<?> typeContext, Property property) {
48+
static PropertyConstraintMappingContextImpl context(ElementType elementType, TypeConstraintMappingContextImpl<?> typeContext, Property property) {
49+
if ( elementType == ElementType.FIELD ) {
50+
return new FieldPropertyConstraintMappingContextImpl(
51+
typeContext,
52+
property.as( JavaBeanField.class )
53+
);
54+
}
55+
else if ( elementType == ElementType.METHOD ) {
56+
return new GetterPropertyConstraintMappingContextImpl(
57+
typeContext,
58+
property.as( JavaBeanGetter.class )
59+
);
60+
}
61+
else {
62+
throw LOG.getUnexpectedElementType( elementType, ElementType.FIELD, ElementType.METHOD );
63+
}
64+
}
65+
66+
protected PropertyConstraintMappingContextImpl(TypeConstraintMappingContextImpl<?> typeContext, T property, ConstraintLocation location) {
4867
super( typeContext.getConstraintMapping(), property.getType() );
4968
this.typeContext = typeContext;
5069
this.property = property;
51-
this.location = property instanceof JavaBeanField
52-
? ConstraintLocation.forField( property.as( JavaBeanField.class ) )
53-
: ConstraintLocation.forGetter( property.as( JavaBeanGetter.class ) );
70+
this.location = location;
5471
}
5572

5673
@Override
5774
protected PropertyConstraintMappingContextImpl getThis() {
5875
return this;
5976
}
6077

61-
@Override
62-
public PropertyConstraintMappingContext constraint(ConstraintDef<?, ?> definition) {
63-
if ( property instanceof JavaBeanField ) {
64-
super.addConstraint(
65-
ConfiguredConstraint.forFieldProperty(
66-
definition, property.as( JavaBeanField.class )
67-
)
68-
);
69-
}
70-
else {
71-
super.addConstraint(
72-
ConfiguredConstraint.forExecutable(
73-
definition, property.as( Callable.class )
74-
)
75-
);
76-
}
77-
return this;
78-
}
79-
8078
@Override
8179
public PropertyConstraintMappingContext ignoreAnnotations() {
8280
return ignoreAnnotations( true );
@@ -113,29 +111,14 @@ public ContainerElementConstraintMappingContext containerElementType(int index,
113111
return super.containerElement( this, typeContext, location, index, nestedIndexes );
114112
}
115113

116-
ConstrainedElement build(ConstraintHelper constraintHelper, TypeResolutionHelper typeResolutionHelper, ValueExtractorManager valueExtractorManager) {
117-
if ( property instanceof JavaBeanField ) {
118-
return new ConstrainedField(
119-
ConfigurationSource.API,
120-
property,
121-
getConstraints( constraintHelper, typeResolutionHelper, valueExtractorManager ),
122-
getTypeArgumentConstraints( constraintHelper, typeResolutionHelper, valueExtractorManager ),
123-
getCascadingMetaDataBuilder()
124-
);
125-
}
126-
else {
127-
return new ConstrainedExecutable(
128-
ConfigurationSource.API,
129-
property.as( Callable.class ),
130-
getConstraints( constraintHelper, typeResolutionHelper, valueExtractorManager ),
131-
getTypeArgumentConstraints( constraintHelper, typeResolutionHelper, valueExtractorManager ),
132-
getCascadingMetaDataBuilder()
133-
);
134-
}
135-
}
114+
abstract ConstrainedElement build(ConstraintHelper constraintHelper, TypeResolutionHelper typeResolutionHelper, ValueExtractorManager valueExtractorManager);
136115

137116
@Override
138117
protected ConstraintType getConstraintType() {
139118
return ConstraintType.GENERIC;
140119
}
120+
121+
protected T getProperty() {
122+
return property;
123+
}
141124
}

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,24 +122,20 @@ public PropertyConstraintMappingContext property(String property, ElementType el
122122
Contracts.assertNotNull( elementType, "The element type must not be null." );
123123
Contracts.assertNotEmpty( property, MESSAGES.propertyNameMustNotBeEmpty() );
124124

125-
Property member = getProperty(
125+
Property foundProperty = getProperty(
126126
beanClass, property, elementType
127127
);
128128

129-
if ( member == null || member.getDeclaringClass() != beanClass ) {
129+
if ( foundProperty == null || foundProperty.getDeclaringClass() != beanClass ) {
130130
throw LOG.getUnableToFindPropertyWithAccessException( beanClass, property, elementType );
131131
}
132132

133-
if ( configuredMembers.contains( member ) ) {
133+
if ( configuredMembers.contains( foundProperty ) ) {
134134
throw LOG.getPropertyHasAlreadyBeConfiguredViaProgrammaticApiException( beanClass, property );
135135
}
136136

137-
PropertyConstraintMappingContextImpl context = new PropertyConstraintMappingContextImpl(
138-
this,
139-
member
140-
);
141-
142-
configuredMembers.add( member );
137+
PropertyConstraintMappingContextImpl context = PropertyConstraintMappingContextImpl.context( elementType, this, foundProperty );
138+
configuredMembers.add( foundProperty );
143139
propertyContexts.add( context );
144140
return context;
145141
}

engine/src/main/java/org/hibernate/validator/internal/util/logging/Log.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,4 +860,7 @@ ConstraintDefinitionException getConstraintValidatorDefinitionConstraintMismatch
860860

861861
@Message(id = 244, value = "ConstrainedElement expected class was %1$s, but instead received %2$s.")
862862
AssertionError getUnexpectedConstraintElementType(@FormatWith(ClassObjectFormatter.class) Class<?> expecting, @FormatWith(ClassObjectFormatter.class) Class<?> got);
863+
864+
@Message(id = 245, value = "Allowed ElementTypes are %2$s, but instead received %1$s.")
865+
AssertionError getUnexpectedElementType(ElementType received, @FormatWith(ObjectArrayFormatter.class) ElementType... got);
863866
}

0 commit comments

Comments
 (0)