/
ConstraintTree.java
519 lines (457 loc) · 19.7 KB
/
ConstraintTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hibernate.validator.internal.engine;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorFactory;
import javax.validation.ConstraintViolation;
import javax.validation.metadata.ConstraintDescriptor;
import org.hibernate.validator.constraints.CompositionType;
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl;
import org.hibernate.validator.internal.util.CollectionHelper;
import org.hibernate.validator.internal.util.LRUMap;
import org.hibernate.validator.internal.util.TypeHelper;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
import static org.hibernate.validator.constraints.CompositionType.ALL_FALSE;
import static org.hibernate.validator.constraints.CompositionType.AND;
import static org.hibernate.validator.constraints.CompositionType.OR;
/**
* Due to constraint composition a single constraint annotation can lead to a whole constraint tree being validated.
* This class encapsulates such a tree.
*
* @author Hardy Ferentschik
* @author Federico Mancini
* @author Dag Hovland
* @author Kevin Pollet <kevin.pollet@serli.com> (C) 2012 SERLI
*/
public class ConstraintTree<A extends Annotation> {
private static final Log log = LoggerFactory.make();
private static final int MAX_TYPE_CACHE_SIZE = 20;
private final ConstraintTree<?> parent;
private final List<ConstraintTree<?>> children;
/**
* The constraint descriptor for the constraint represented by this constraint tree.
*/
private final ConstraintDescriptorImpl<A> descriptor;
/**
* A maps of all available constraint validator classes for this constraint mapped to their validator types.
*/
private final Map<Type, Class<? extends ConstraintValidator<?, ?>>> availableValidatorTypes;
private final Map<ValidatorCacheKey, ConstraintValidator<A, ?>> constraintValidatorCache;
private final Map<Type, Type> suitableTypeMap;
public ConstraintTree(ConstraintDescriptorImpl<A> descriptor) {
this( descriptor, null );
}
private ConstraintTree(ConstraintDescriptorImpl<A> descriptor, ConstraintTree<?> parent) {
this.parent = parent;
this.descriptor = descriptor;
this.constraintValidatorCache = new ConcurrentHashMap<ValidatorCacheKey, ConstraintValidator<A, ?>>();
final Set<ConstraintDescriptorImpl<?>> composingConstraints = new HashSet<ConstraintDescriptorImpl<?>>();
for ( ConstraintDescriptor<?> composingConstraint : descriptor.getComposingConstraints() ) {
composingConstraints.add( (ConstraintDescriptorImpl<?>) composingConstraint );
}
children = new ArrayList<ConstraintTree<?>>( composingConstraints.size() );
for ( ConstraintDescriptorImpl<?> composingDescriptor : composingConstraints ) {
ConstraintTree<?> treeNode = createConstraintTree( composingDescriptor );
children.add( treeNode );
}
availableValidatorTypes = TypeHelper.getValidatorsTypes( descriptor.getConstraintValidatorClasses() );
suitableTypeMap = Collections.synchronizedMap( new LRUMap<Type, Type>( MAX_TYPE_CACHE_SIZE ) );
}
private <U extends Annotation> ConstraintTree<U> createConstraintTree(ConstraintDescriptorImpl<U> composingDescriptor) {
return new ConstraintTree<U>( composingDescriptor, this );
}
public final List<ConstraintTree<?>> getChildren() {
return children;
}
public final ConstraintDescriptorImpl<A> getDescriptor() {
return descriptor;
}
public final <T, U, V, E extends ConstraintViolation<T>> boolean validateConstraints(ValidationContext<T, E> executionContext, ValueContext<U, V> valueContext) {
Set<E> constraintViolations = new HashSet<E>();
validateConstraints( executionContext, valueContext, constraintViolations );
if ( !constraintViolations.isEmpty() ) {
executionContext.addConstraintFailures( constraintViolations );
return false;
}
return true;
}
private <T, U, V, E extends ConstraintViolation<T>> void validateConstraints(ValidationContext<T, E> executionContext,
ValueContext<U, V> valueContext,
Set<E> constraintViolations) {
CompositionResult compositionResult = validateComposingConstraints(
executionContext, valueContext, constraintViolations
);
Set<E> localViolationList = CollectionHelper.newHashSet();
// After all children are validated the actual ConstraintValidator of the constraint itself is executed (provided
// there is one)
// If fail fast mode is enabled and there are already failing constraints we don't need to validate the constraint (HV-550)
if ( !descriptor.getConstraintValidatorClasses().isEmpty()
&& ( !executionContext.isFailFastModeEnabled() || constraintViolations.isEmpty() ) ) {
if ( log.isTraceEnabled() ) {
log.tracef(
"Validating value %s against constraint defined by %s.",
valueContext.getCurrentValidatedValue(),
descriptor
);
}
ConstraintValidator<A, V> validator = getInitializedValidator(
valueContext.getTypeOfAnnotatedElement(),
executionContext.getConstraintValidatorFactory()
);
ConstraintValidatorContextImpl constraintValidatorContext = new ConstraintValidatorContextImpl(
valueContext.getPropertyPath(), descriptor
);
localViolationList.addAll(
validateSingleConstraint(
executionContext,
valueContext,
constraintValidatorContext,
validator
)
);
// We re-evaluate the boolean composition by taking into consideration also the violations
// from the local constraintValidator
if ( localViolationList.isEmpty() ) {
compositionResult.setAtLeastOneTrue( true );
}
else {
compositionResult.setAllTrue( false );
}
}
if ( !passesCompositionTypeRequirement( constraintViolations, compositionResult ) ) {
prepareFinalConstraintViolations(
executionContext, valueContext, constraintViolations, localViolationList
);
}
}
/**
* Before the final constraint violations can be reported back we need to check whether we have a composing
* constraint whose result should be reported as single violation.
*
* @param executionContext Meta data about top level validation
* @param valueContext Meta data for currently validated value
* @param constraintViolations Used to accumulate constraint violations
* @param localViolationList List of constraint violations of top level constraint
*/
private <T, U, V, E extends ConstraintViolation<T>> void prepareFinalConstraintViolations(ValidationContext<T, E> executionContext, ValueContext<U, V> valueContext, Set<E> constraintViolations, Set<E> localViolationList) {
if ( reportAsSingleViolation() ) {
// We clear the current violations list anyway
constraintViolations.clear();
// But then we need to distinguish whether the local ConstraintValidator has reported
// violations or not (or if there is no local ConstraintValidator at all).
// If not we create a violation
// using the error message in the annotation declaration at top level.
if ( localViolationList.isEmpty() ) {
final String message = (String) getDescriptor().getAttributes().get( "message" );
MessageAndPath messageAndPath = new MessageAndPath( message, valueContext.getPropertyPath() );
E violation = executionContext.createConstraintViolation(
valueContext, messageAndPath, descriptor
);
constraintViolations.add( violation );
}
}
// Now, if there were some violations reported by
// the local ConstraintValidator, they need to be added to constraintViolations.
// Whether we need to report them as a single constraint or just add them to the other violations
// from the composing constraints, has been taken care of in the previous conditional block.
// This takes also care of possible custom error messages created by the constraintValidator,
// as checked in test CustomErrorMessage.java
// If no violations have been reported from the local ConstraintValidator, or no such validator exists,
// then we just add an empty list.
constraintViolations.addAll( localViolationList );
}
/**
* Validates all composing constraints recursively.
*
* @param executionContext Meta data about top level validation
* @param valueContext Meta data for currently validated value
* @param constraintViolations Used to accumulate constraint violations
*
* @return Returns an instance of {@code CompositionResult} relevant for boolean composition of constraints
*/
private <T, U, V, E extends ConstraintViolation<T>> CompositionResult validateComposingConstraints(ValidationContext<T, E> executionContext,
ValueContext<U, V> valueContext,
Set<E> constraintViolations) {
CompositionResult compositionResult = new CompositionResult( true, false );
for ( ConstraintTree<?> tree : getChildren() ) {
Set<E> tmpViolationList = new HashSet<E>();
tree.validateConstraints( executionContext, valueContext, tmpViolationList );
constraintViolations.addAll( tmpViolationList );
if ( tmpViolationList.isEmpty() ) {
compositionResult.setAtLeastOneTrue( true );
// no need to further validate constraints, because at least one validation passed
if ( descriptor.getCompositionType() == OR ) {
break;
}
}
else {
compositionResult.setAllTrue( false );
if ( executionContext.isFailFastModeEnabled() && descriptor.getCompositionType() == AND ) {
break;
}
}
}
return compositionResult;
}
private boolean passesCompositionTypeRequirement(Set<?> constraintViolations, CompositionResult compositionResult) {
CompositionType compositionType = getDescriptor().getCompositionType();
boolean passedValidation = false;
switch ( compositionType ) {
case OR:
passedValidation = compositionResult.isAtLeastOneTrue();
break;
case AND:
passedValidation = compositionResult.isAllTrue();
break;
case ALL_FALSE:
passedValidation = !compositionResult.isAtLeastOneTrue();
break;
}
assert ( !passedValidation || !( compositionType == AND ) || constraintViolations.isEmpty() );
if ( passedValidation ) {
constraintViolations.clear();
}
return passedValidation;
}
private <T, U, V, E extends ConstraintViolation<T>> Set<E> validateSingleConstraint(ValidationContext<T, E> executionContext,
ValueContext<U, V> valueContext,
ConstraintValidatorContextImpl constraintValidatorContext,
ConstraintValidator<A, V> validator) {
boolean isValid;
Set<E> cv = new HashSet<E>();
try {
isValid = validator.isValid( valueContext.getCurrentValidatedValue(), constraintValidatorContext );
}
catch ( RuntimeException e ) {
throw log.getExceptionDuringIsValidCall( e );
}
if ( !isValid ) {
//We do not add them these violations yet, since we don't know how they are
//going to influence the final boolean evaluation
cv.addAll(
executionContext.createConstraintViolations(
valueContext, constraintValidatorContext
)
);
}
return cv;
}
/**
* @return {@code} true if the current constraint should be reportes as single violation, {@code false otherwise}.
* When using negation, we only report the single top-level violation, as
* it is hard, especially for ALL_FALSE to give meaningful reports
*/
private boolean reportAsSingleViolation() {
return getDescriptor().isReportAsSingleViolation()
|| getDescriptor().getCompositionType() == ALL_FALSE;
}
/**
* @param validatedValueType The type of the value to be validated (the type of the member/class the constraint was placed on).
* @param constraintFactory constraint factory used to instantiate the constraint validator.
*
* @return A initialized constraint validator matching the type of the value to be validated.
*/
@SuppressWarnings("unchecked")
private <V> ConstraintValidator<A, V> getInitializedValidator(Type validatedValueType, ConstraintValidatorFactory constraintFactory) {
Class<? extends ConstraintValidator<?, ?>> validatorClass = findMatchingValidatorClass( validatedValueType );
// check if we have the default validator factory. If not we don't use caching (see HV-242)
if ( !( constraintFactory instanceof ConstraintValidatorFactoryImpl ) ) {
return createAndInitializeValidator( constraintFactory, validatorClass );
}
ConstraintValidator<A, V> constraintValidator;
ValidatorCacheKey key = new ValidatorCacheKey( constraintFactory, validatorClass );
if ( !constraintValidatorCache.containsKey( key ) ) {
constraintValidator = createAndInitializeValidator( constraintFactory, validatorClass );
constraintValidatorCache.put( key, constraintValidator );
}
else {
constraintValidator = (ConstraintValidator<A, V>) constraintValidatorCache.get( key );
log.tracef( "Constraint validator %s found in cache.", constraintValidator );
}
return constraintValidator;
}
@SuppressWarnings("unchecked")
private <V> ConstraintValidator<A, V> createAndInitializeValidator(ConstraintValidatorFactory constraintFactory, Class<? extends ConstraintValidator<?, ?>> validatorClass) {
ConstraintValidator<A, V> constraintValidator;
constraintValidator = (ConstraintValidator<A, V>) constraintFactory.getInstance(
validatorClass
);
if ( constraintValidator == null ) {
throw log.getConstraintFactoryMustNotReturnNullException( validatorClass.getName() );
}
initializeConstraint( descriptor, constraintValidator );
return constraintValidator;
}
/**
* Runs the validator resolution algorithm.
*
* @param validatedValueType The type of the value to be validated (the type of the member/class the constraint was placed on).
*
* @return The class of a matching validator.
*/
private Class<? extends ConstraintValidator<?, ?>> findMatchingValidatorClass(Type validatedValueType) {
if ( suitableTypeMap.containsKey( validatedValueType ) ) {
return availableValidatorTypes.get( suitableTypeMap.get( validatedValueType ) );
}
List<Type> discoveredSuitableTypes = findSuitableValidatorTypes( validatedValueType );
resolveAssignableTypes( discoveredSuitableTypes );
verifyResolveWasUnique( validatedValueType, discoveredSuitableTypes );
Type suitableType = discoveredSuitableTypes.get( 0 );
suitableTypeMap.put( validatedValueType, suitableType );
return availableValidatorTypes.get( suitableType );
}
private void verifyResolveWasUnique(Type valueClass, List<Type> assignableClasses) {
if ( assignableClasses.size() == 0 ) {
String className = valueClass.toString();
if ( valueClass instanceof Class ) {
Class<?> clazz = (Class<?>) valueClass;
if ( clazz.isArray() ) {
className = clazz.getComponentType().toString() + "[]";
}
else {
className = clazz.getName();
}
}
throw log.getNoValidatorFoundForTypeException( className );
}
else if ( assignableClasses.size() > 1 ) {
StringBuilder builder = new StringBuilder();
for ( Type clazz : assignableClasses ) {
builder.append( clazz );
builder.append( ", " );
}
builder.delete( builder.length() - 2, builder.length() );
throw log.getMoreThanOneValidatorFoundForTypeException( valueClass, builder.toString() );
}
}
private List<Type> findSuitableValidatorTypes(Type type) {
List<Type> determinedSuitableTypes = new ArrayList<Type>();
for ( Type validatorType : availableValidatorTypes.keySet() ) {
if ( TypeHelper.isAssignable( validatorType, type )
&& !determinedSuitableTypes.contains( validatorType ) ) {
determinedSuitableTypes.add( validatorType );
}
}
return determinedSuitableTypes;
}
/**
* Tries to reduce all assignable classes down to a single class.
*
* @param assignableTypes The set of all classes which are assignable to the class of the value to be validated and
* which are handled by at least one of the validators for the specified constraint.
*/
private void resolveAssignableTypes(List<Type> assignableTypes) {
if ( assignableTypes.size() == 0 || assignableTypes.size() == 1 ) {
return;
}
List<Type> typesToRemove = new ArrayList<Type>();
do {
typesToRemove.clear();
Type type = assignableTypes.get( 0 );
for ( int i = 1; i < assignableTypes.size(); i++ ) {
if ( TypeHelper.isAssignable( type, assignableTypes.get( i ) ) ) {
typesToRemove.add( type );
}
else if ( TypeHelper.isAssignable( assignableTypes.get( i ), type ) ) {
typesToRemove.add( assignableTypes.get( i ) );
}
}
assignableTypes.removeAll( typesToRemove );
} while ( typesToRemove.size() > 0 );
}
private <V> void initializeConstraint
(ConstraintDescriptor<A>
descriptor, ConstraintValidator<A, V>
constraintValidator) {
try {
constraintValidator.initialize( descriptor.getAnnotation() );
}
catch ( RuntimeException e ) {
throw log.getUnableToInitializeConstraintValidatorException( constraintValidator.getClass().getName(), e );
}
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append( "ConstraintTree" );
sb.append( "{ descriptor=" ).append( descriptor );
sb.append( ", isRoot=" ).append( parent == null );
sb.append( '}' );
return sb.toString();
}
private static final class ValidatorCacheKey {
private ConstraintValidatorFactory constraintValidatorFactory;
private Class<? extends ConstraintValidator<?, ?>> validatorType;
private ValidatorCacheKey(ConstraintValidatorFactory constraintValidatorFactory, Class<? extends ConstraintValidator<?, ?>> validatorType) {
this.constraintValidatorFactory = constraintValidatorFactory;
this.validatorType = validatorType;
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
ValidatorCacheKey that = (ValidatorCacheKey) o;
if ( constraintValidatorFactory != null ? !constraintValidatorFactory.equals( that.constraintValidatorFactory ) : that.constraintValidatorFactory != null ) {
return false;
}
if ( validatorType != null ? !validatorType.equals( that.validatorType ) : that.validatorType != null ) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = constraintValidatorFactory != null ? constraintValidatorFactory.hashCode() : 0;
result = 31 * result + ( validatorType != null ? validatorType.hashCode() : 0 );
return result;
}
}
private static final class CompositionResult {
private boolean allTrue;
private boolean atLeastOneTrue;
CompositionResult(boolean allTrue, boolean atLeastOneTrue) {
this.allTrue = allTrue;
this.atLeastOneTrue = atLeastOneTrue;
}
public boolean isAllTrue() {
return allTrue;
}
public boolean isAtLeastOneTrue() {
return atLeastOneTrue;
}
public void setAllTrue(boolean allTrue) {
this.allTrue = allTrue;
}
public void setAtLeastOneTrue(boolean atLeastOneTrue) {
this.atLeastOneTrue = atLeastOneTrue;
}
}
}