6
6
*/
7
7
package org .hibernate .validator .internal .engine .constraintvalidation ;
8
8
9
- import static org .hibernate .validator .internal .util .CollectionHelper .newArrayList ;
10
-
11
9
import java .util .ArrayList ;
12
10
import java .util .Collections ;
13
11
import java .util .HashMap ;
30
28
31
29
import org .hibernate .validator .constraintvalidation .HibernateConstraintValidatorContext ;
32
30
import org .hibernate .validator .internal .engine .path .PathImpl ;
31
+ import org .hibernate .validator .internal .util .CollectionHelper ;
33
32
import org .hibernate .validator .internal .util .Contracts ;
34
33
import org .hibernate .validator .internal .util .logging .Log ;
35
34
import org .hibernate .validator .internal .util .logging .LoggerFactory ;
@@ -47,9 +46,9 @@ public class ConstraintValidatorContextImpl implements HibernateConstraintValida
47
46
private Map <String , Object > expressionVariables ;
48
47
private final List <String > methodParameterNames ;
49
48
private final ClockProvider clockProvider ;
50
- private final List <ConstraintViolationCreationContext > constraintViolationCreationContexts = newArrayList ( 3 );
51
49
private final PathImpl basePath ;
52
50
private final ConstraintDescriptor <?> constraintDescriptor ;
51
+ private List <ConstraintViolationCreationContext > constraintViolationCreationContexts ;
53
52
private boolean defaultDisabled ;
54
53
private Object dynamicPayload ;
55
54
@@ -129,25 +128,34 @@ public final ConstraintDescriptor<?> getConstraintDescriptor() {
129
128
}
130
129
131
130
public final List <ConstraintViolationCreationContext > getConstraintViolationCreationContexts () {
132
- if ( defaultDisabled && constraintViolationCreationContexts .size () == 0 ) {
133
- throw log .getAtLeastOneCustomMessageMustBeCreatedException ();
131
+ if ( defaultDisabled ) {
132
+ if ( constraintViolationCreationContexts == null || constraintViolationCreationContexts .size () == 0 ) {
133
+ throw log .getAtLeastOneCustomMessageMustBeCreatedException ();
134
+ }
135
+
136
+ return CollectionHelper .toImmutableList ( constraintViolationCreationContexts );
134
137
}
135
138
136
- List <ConstraintViolationCreationContext > returnedConstraintViolationCreationContexts = new ArrayList <>(
137
- constraintViolationCreationContexts
138
- );
139
- if ( !defaultDisabled ) {
140
- returnedConstraintViolationCreationContexts .add (
141
- new ConstraintViolationCreationContext (
142
- getDefaultConstraintMessageTemplate (),
143
- basePath ,
144
- messageParameters != null ? new HashMap <>( messageParameters ) : Collections .emptyMap (),
145
- expressionVariables != null ? new HashMap <>( expressionVariables ) : Collections .emptyMap (),
146
- dynamicPayload
147
- )
148
- );
139
+ if ( constraintViolationCreationContexts == null || constraintViolationCreationContexts .size () == 0 ) {
140
+ return Collections .singletonList ( getDefaultConstraintViolationCreationContext () );
149
141
}
150
- return returnedConstraintViolationCreationContexts ;
142
+
143
+ List <ConstraintViolationCreationContext > returnedConstraintViolationCreationContexts =
144
+ new ArrayList <>( constraintViolationCreationContexts .size () + 1 );
145
+ returnedConstraintViolationCreationContexts .addAll ( constraintViolationCreationContexts );
146
+ returnedConstraintViolationCreationContexts .add ( getDefaultConstraintViolationCreationContext () );
147
+
148
+ return CollectionHelper .toImmutableList ( returnedConstraintViolationCreationContexts );
149
+ }
150
+
151
+ private ConstraintViolationCreationContext getDefaultConstraintViolationCreationContext () {
152
+ return new ConstraintViolationCreationContext (
153
+ getDefaultConstraintMessageTemplate (),
154
+ basePath ,
155
+ messageParameters != null ? new HashMap <>( messageParameters ) : Collections .emptyMap (),
156
+ expressionVariables != null ? new HashMap <>( expressionVariables ) : Collections .emptyMap (),
157
+ dynamicPayload
158
+ );
151
159
}
152
160
153
161
public List <String > getMethodParameterNames () {
@@ -164,6 +172,9 @@ protected NodeBuilderBase(String template, PathImpl path) {
164
172
}
165
173
166
174
public ConstraintValidatorContext addConstraintViolation () {
175
+ if ( constraintViolationCreationContexts == null ) {
176
+ constraintViolationCreationContexts = CollectionHelper .newArrayList ( 3 );
177
+ }
167
178
constraintViolationCreationContexts .add (
168
179
new ConstraintViolationCreationContext (
169
180
messageTemplate ,
0 commit comments