|
34 | 34 | import org.hibernate.validator.internal.metadata.raw.ConstrainedElement;
|
35 | 35 | import org.hibernate.validator.internal.metadata.raw.ConstrainedType;
|
36 | 36 | import org.hibernate.validator.internal.properties.Constrainable;
|
37 |
| -import org.hibernate.validator.internal.properties.Property; |
38 | 37 | import org.hibernate.validator.internal.properties.javabean.JavaBeanConstructor;
|
39 | 38 | import org.hibernate.validator.internal.properties.javabean.JavaBeanExecutable;
|
40 | 39 | import org.hibernate.validator.internal.properties.javabean.JavaBeanField;
|
@@ -119,24 +118,58 @@ public TypeConstraintMappingContext<C> defaultGroupSequenceProviderClass(Class<?
|
119 | 118 |
|
120 | 119 | @Override
|
121 | 120 | public PropertyConstraintMappingContext property(String property, ElementType elementType) {
|
122 |
| - Contracts.assertNotNull( property, "The property name must not be null." ); |
123 | 121 | Contracts.assertNotNull( elementType, "The element type must not be null." );
|
| 122 | + |
| 123 | + if ( !( ElementType.FIELD.equals( elementType ) || ElementType.METHOD.equals( elementType ) ) ) { |
| 124 | + throw LOG.getElementTypeHasToBeFieldOrMethodException(); |
| 125 | + } |
| 126 | + |
| 127 | + if ( ElementType.FIELD == elementType ) { |
| 128 | + return field( property ); |
| 129 | + } |
| 130 | + else { |
| 131 | + return getter( property ); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public PropertyConstraintMappingContext field(String property) { |
| 137 | + Contracts.assertNotNull( property, "The property name must not be null." ); |
124 | 138 | Contracts.assertNotEmpty( property, MESSAGES.propertyNameMustNotBeEmpty() );
|
125 | 139 |
|
126 |
| - Property foundProperty = getProperty( |
127 |
| - beanClass, property, elementType |
128 |
| - ); |
| 140 | + JavaBeanField javaBeanField = getFieldProperty( beanClass, property ); |
| 141 | + |
| 142 | + if ( javaBeanField == null || javaBeanField.getDeclaringClass() != beanClass ) { |
| 143 | + throw LOG.getUnableToFindPropertyWithAccessException( beanClass, property, ElementType.FIELD ); |
| 144 | + } |
| 145 | + |
| 146 | + if ( configuredMembers.contains( javaBeanField ) ) { |
| 147 | + throw LOG.getPropertyHasAlreadyBeConfiguredViaProgrammaticApiException( beanClass, property ); |
| 148 | + } |
129 | 149 |
|
130 |
| - if ( foundProperty == null || foundProperty.getDeclaringClass() != beanClass ) { |
131 |
| - throw LOG.getUnableToFindPropertyWithAccessException( beanClass, property, elementType ); |
| 150 | + PropertyConstraintMappingContextImpl context = new FieldPropertyConstraintMappingContextImpl( this, javaBeanField ); |
| 151 | + configuredMembers.add( javaBeanField ); |
| 152 | + propertyContexts.add( context ); |
| 153 | + return context; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public PropertyConstraintMappingContext getter(String property) { |
| 158 | + Contracts.assertNotNull( property, "The property name must not be null." ); |
| 159 | + Contracts.assertNotEmpty( property, MESSAGES.propertyNameMustNotBeEmpty() ); |
| 160 | + |
| 161 | + JavaBeanGetter javaBeanGetter = getGetterProperty( beanClass, property ); |
| 162 | + |
| 163 | + if ( javaBeanGetter == null || javaBeanGetter.getDeclaringClass() != beanClass ) { |
| 164 | + throw LOG.getUnableToFindPropertyWithAccessException( beanClass, property, ElementType.METHOD ); |
132 | 165 | }
|
133 | 166 |
|
134 |
| - if ( configuredMembers.contains( foundProperty ) ) { |
| 167 | + if ( configuredMembers.contains( javaBeanGetter ) ) { |
135 | 168 | throw LOG.getPropertyHasAlreadyBeConfiguredViaProgrammaticApiException( beanClass, property );
|
136 | 169 | }
|
137 | 170 |
|
138 |
| - PropertyConstraintMappingContextImpl context = PropertyConstraintMappingContextImpl.context( elementType, this, foundProperty ); |
139 |
| - configuredMembers.add( foundProperty ); |
| 171 | + PropertyConstraintMappingContextImpl context = new GetterPropertyConstraintMappingContextImpl( this, javaBeanGetter ); |
| 172 | + configuredMembers.add( javaBeanGetter ); |
140 | 173 | propertyContexts.add( context );
|
141 | 174 | return context;
|
142 | 175 | }
|
@@ -252,41 +285,25 @@ protected ConstraintType getConstraintType() {
|
252 | 285 | return ConstraintType.GENERIC;
|
253 | 286 | }
|
254 | 287 |
|
255 |
| - /** |
256 |
| - * Returns the property with the given name and type. |
257 |
| - * |
258 |
| - * @param clazz The class from which to retrieve the property. Cannot be {@code null}. |
259 |
| - * @param property The property name without "is", "get" or "has". Cannot be {@code null} or empty. |
260 |
| - * @param elementType The element type. Either {@code ElementType.FIELD} or {@code ElementType METHOD}. |
261 |
| - * |
262 |
| - * @return the property which matches the name and type or {@code null} if no such property exists. |
263 |
| - */ |
264 |
| - private Property getProperty(Class<?> clazz, String property, ElementType elementType) { |
| 288 | + private JavaBeanField getFieldProperty(Class<?> clazz, String property) { |
265 | 289 | Contracts.assertNotNull( clazz, MESSAGES.classCannotBeNull() );
|
266 | 290 |
|
267 |
| - if ( property == null || property.length() == 0 ) { |
268 |
| - throw LOG.getPropertyNameCannotBeNullOrEmptyException(); |
269 |
| - } |
| 291 | + Field field = run( GetDeclaredField.action( clazz, property ) ); |
| 292 | + return field == null ? null : new JavaBeanField( field ); |
| 293 | + } |
270 | 294 |
|
271 |
| - if ( !( ElementType.FIELD.equals( elementType ) || ElementType.METHOD.equals( elementType ) ) ) { |
272 |
| - throw LOG.getElementTypeHasToBeFieldOrMethodException(); |
273 |
| - } |
| 295 | + private JavaBeanGetter getGetterProperty(Class<?> clazz, String property) { |
| 296 | + Contracts.assertNotNull( clazz, MESSAGES.classCannotBeNull() ); |
274 | 297 |
|
275 |
| - if ( ElementType.FIELD.equals( elementType ) ) { |
276 |
| - Field field = run( GetDeclaredField.action( clazz, property ) ); |
277 |
| - return field == null ? null : new JavaBeanField( field ); |
278 |
| - } |
279 |
| - else { |
280 |
| - Method method = null; |
281 |
| - String methodName = property.substring( 0, 1 ).toUpperCase( Locale.ROOT ) + property.substring( 1 ); |
282 |
| - for ( String prefix : ReflectionHelper.PROPERTY_ACCESSOR_PREFIXES ) { |
283 |
| - method = run( GetMethod.action( clazz, prefix + methodName ) ); |
284 |
| - if ( method != null ) { |
285 |
| - break; |
286 |
| - } |
| 298 | + Method method = null; |
| 299 | + String methodName = property.substring( 0, 1 ).toUpperCase( Locale.ROOT ) + property.substring( 1 ); |
| 300 | + for ( String prefix : ReflectionHelper.PROPERTY_ACCESSOR_PREFIXES ) { |
| 301 | + method = run( GetMethod.action( clazz, prefix + methodName ) ); |
| 302 | + if ( method != null ) { |
| 303 | + break; |
287 | 304 | }
|
288 |
| - return method == null ? null : new JavaBeanGetter( method ); |
289 | 305 | }
|
| 306 | + return method == null ? null : new JavaBeanGetter( method ); |
290 | 307 | }
|
291 | 308 |
|
292 | 309 | /**
|
|
0 commit comments