|
7 | 7 | package org.hibernate.validator.internal.cfg.context;
|
8 | 8 |
|
9 | 9 | import java.lang.annotation.ElementType;
|
| 10 | +import java.lang.invoke.MethodHandles; |
10 | 11 |
|
11 |
| -import org.hibernate.validator.cfg.ConstraintDef; |
12 | 12 | import org.hibernate.validator.cfg.context.ConstructorConstraintMappingContext;
|
13 | 13 | import org.hibernate.validator.cfg.context.ContainerElementConstraintMappingContext;
|
14 | 14 | import org.hibernate.validator.cfg.context.MethodConstraintMappingContext;
|
|
17 | 17 | import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
|
18 | 18 | import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl.ConstraintType;
|
19 | 19 | import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
|
20 |
| -import org.hibernate.validator.internal.metadata.raw.ConfigurationSource; |
21 | 20 | 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; |
25 | 21 | import org.hibernate.validator.internal.properties.Property;
|
26 | 22 | import org.hibernate.validator.internal.properties.javabean.JavaBeanField;
|
27 | 23 | import org.hibernate.validator.internal.properties.javabean.JavaBeanGetter;
|
28 | 24 | 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; |
29 | 27 |
|
30 | 28 | /**
|
31 | 29 | * Constraint mapping creational context which allows to configure the constraints for one bean property.
|
32 | 30 | *
|
33 | 31 | * @author Hardy Ferentschik
|
34 | 32 | * @author Gunnar Morling
|
35 | 33 | * @author Kevin Pollet <[email protected]> (C) 2011 SERLI
|
| 34 | + * @author Marko Bekhta |
36 | 35 | */
|
37 |
| -final class PropertyConstraintMappingContextImpl |
| 36 | +abstract class PropertyConstraintMappingContextImpl<T extends Property> |
38 | 37 | extends CascadableConstraintMappingContextImplBase<PropertyConstraintMappingContext>
|
39 | 38 | implements PropertyConstraintMappingContext {
|
40 | 39 |
|
| 40 | + private static final Log LOG = LoggerFactory.make( MethodHandles.lookup() ); |
| 41 | + |
41 | 42 | private final TypeConstraintMappingContextImpl<?> typeContext;
|
42 | 43 |
|
43 | 44 | // either Field or Method
|
44 |
| - private final Property property; |
| 45 | + private final T property; |
45 | 46 | private final ConstraintLocation location;
|
46 | 47 |
|
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) { |
48 | 67 | super( typeContext.getConstraintMapping(), property.getType() );
|
49 | 68 | this.typeContext = typeContext;
|
50 | 69 | 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; |
54 | 71 | }
|
55 | 72 |
|
56 | 73 | @Override
|
57 | 74 | protected PropertyConstraintMappingContextImpl getThis() {
|
58 | 75 | return this;
|
59 | 76 | }
|
60 | 77 |
|
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 |
| - |
80 | 78 | @Override
|
81 | 79 | public PropertyConstraintMappingContext ignoreAnnotations() {
|
82 | 80 | return ignoreAnnotations( true );
|
@@ -113,29 +111,14 @@ public ContainerElementConstraintMappingContext containerElementType(int index,
|
113 | 111 | return super.containerElement( this, typeContext, location, index, nestedIndexes );
|
114 | 112 | }
|
115 | 113 |
|
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); |
136 | 115 |
|
137 | 116 | @Override
|
138 | 117 | protected ConstraintType getConstraintType() {
|
139 | 118 | return ConstraintType.GENERIC;
|
140 | 119 | }
|
| 120 | + |
| 121 | + protected T getProperty() { |
| 122 | + return property; |
| 123 | + } |
141 | 124 | }
|
0 commit comments