-
Notifications
You must be signed in to change notification settings - Fork 566
/
CascadableConstraintMappingContextImplBase.java
213 lines (180 loc) · 7.67 KB
/
CascadableConstraintMappingContextImplBase.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
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.internal.cfg.context;
import static org.hibernate.validator.internal.util.CollectionHelper.newHashMap;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.hibernate.validator.cfg.context.Cascadable;
import org.hibernate.validator.cfg.context.ContainerElementConstraintMappingContext;
import org.hibernate.validator.cfg.context.ContainerElementTarget;
import org.hibernate.validator.cfg.context.GroupConversionTargetContext;
import org.hibernate.validator.internal.engine.valueextraction.ValueExtractorManager;
import org.hibernate.validator.internal.metadata.cascading.CascadingTypeParameter;
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.hibernate.validator.internal.metadata.core.MetaConstraint;
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
import org.hibernate.validator.internal.util.Contracts;
import org.hibernate.validator.internal.util.TypeHelper;
import org.hibernate.validator.internal.util.TypeResolutionHelper;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
/**
* Base class for all implementations of cascadable context types.
*
* @author Gunnar Morling
*/
abstract class CascadableConstraintMappingContextImplBase<C extends Cascadable<C>>
extends ConstraintMappingContextImplBase implements Cascadable<C> {
private static final Log LOG = LoggerFactory.make();
private final Type configuredType;
protected boolean isCascading;
protected final Map<Class<?>, Class<?>> groupConversions = newHashMap();
/**
* Contexts for configuring nested container elements, if any. Indexed by type parameter.
*/
private final Map<Integer, ContainerElementConstraintMappingContextImpl> containerElementContexts = new HashMap<>();
private final Set<ContainerElementPathKey> configuredPaths = new HashSet<>();
CascadableConstraintMappingContextImplBase(DefaultConstraintMapping mapping, Type configuredType) {
super( mapping );
this.configuredType = configuredType;
}
/**
* Returns this object, narrowed down to the specific sub-type.
*
* @return this object, narrowed down to the specific sub-type
*
* @see <a href="http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ206">"Get this" trick</a>
*/
protected abstract C getThis();
/**
* Adds a group conversion for this element.
*
* @param from the source group of the conversion
* @param to the target group of the conversion
*/
public void addGroupConversion(Class<?> from, Class<?> to) {
groupConversions.put( from, to );
}
@Override
public C valid() {
isCascading = true;
return getThis();
}
@Override
public GroupConversionTargetContext<C> convertGroup(Class<?> from) {
return new GroupConversionTargetContextImpl<>( from, getThis(), this );
}
public ContainerElementConstraintMappingContext containerElement(ContainerElementTarget parent, TypeConstraintMappingContextImpl<?> typeContext,
ConstraintLocation location) {
// HV-1428 Container element support is disabled for arrays
if ( TypeHelper.isArray( configuredType ) ) {
throw LOG.getContainerElementConstraintsAndCascadedValidationNotSupportedOnArraysException( configuredType );
}
if ( configuredType instanceof ParameterizedType ) {
if ( ( (ParameterizedType) configuredType ).getActualTypeArguments().length > 1 ) {
throw LOG.getNoTypeArgumentIndexIsGivenForTypeWithMultipleTypeArgumentsException( configuredType );
}
}
else if ( !TypeHelper.isArray( configuredType ) ) {
throw LOG.getTypeIsNotAParameterizedNorArrayTypeException( configuredType );
}
return containerElement( parent, typeContext, location, 0 );
}
public ContainerElementConstraintMappingContext containerElement(ContainerElementTarget parent, TypeConstraintMappingContextImpl<?> typeContext,
ConstraintLocation location, int index, int... nestedIndexes) {
Contracts.assertTrue( index >= 0, "Type argument index must not be negative" );
// HV-1428 Container element support is disabled for arrays
if ( TypeHelper.isArray( configuredType ) ) {
throw LOG.getContainerElementConstraintsAndCascadedValidationNotSupportedOnArraysException( configuredType );
}
if ( !( configuredType instanceof ParameterizedType ) && !( TypeHelper.isArray( configuredType ) ) ) {
throw LOG.getTypeIsNotAParameterizedNorArrayTypeException( configuredType );
}
ContainerElementPathKey key = new ContainerElementPathKey( index, nestedIndexes );
boolean configuredBefore = !configuredPaths.add( key );
if ( configuredBefore ) {
throw LOG.getContainerElementTypeHasAlreadyBeenConfiguredViaProgrammaticApiException(
location.getTypeForValidatorResolution()
);
}
ContainerElementConstraintMappingContextImpl containerElementContext = new ContainerElementConstraintMappingContextImpl(
typeContext, parent, location, index
);
containerElementContexts.put( index, containerElementContext );
if ( nestedIndexes.length > 0 ) {
return containerElementContext.nestedContainerElement( nestedIndexes );
}
else {
return containerElementContext;
}
}
public boolean isCascading() {
return isCascading;
}
protected Set<MetaConstraint<?>> getTypeArgumentConstraints(ConstraintHelper constraintHelper, TypeResolutionHelper typeResolutionHelper, ValueExtractorManager valueExtractorManager) {
return containerElementContexts.values()
.stream()
.map( t -> t.build( constraintHelper, typeResolutionHelper, valueExtractorManager ) )
.flatMap( Set::stream )
.collect( Collectors.toSet() );
}
protected CascadingTypeParameter getCascadingMetaData() {
Map<TypeVariable<?>, CascadingTypeParameter> typeParametersCascadingMetaData = containerElementContexts.values().stream()
.filter( c -> c.getCascadingTypeParameter() != null )
.collect( Collectors.toMap( c -> c.getCascadingTypeParameter().getTypeParameter(), c -> c.getCascadingTypeParameter() ) );
for ( ContainerElementConstraintMappingContextImpl typeArgumentContext : containerElementContexts.values() ) {
CascadingTypeParameter cascadingTypeParameter = typeArgumentContext.getCascadingTypeParameter();
if ( cascadingTypeParameter != null ) {
typeParametersCascadingMetaData.put( cascadingTypeParameter.getTypeParameter(), cascadingTypeParameter );
}
}
return CascadingTypeParameter.annotatedObject( configuredType, isCascading, typeParametersCascadingMetaData, groupConversions );
}
private static class ContainerElementPathKey {
private final int index;
private final int[] nestedIndexes;
public ContainerElementPathKey(int index, int[] nestedIndexes) {
this.index = index;
this.nestedIndexes = nestedIndexes;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + index;
result = prime * result + Arrays.hashCode( nestedIndexes );
return result;
}
@Override
public boolean equals(Object obj) {
if ( this == obj ) {
return true;
}
if ( obj == null ) {
return false;
}
if ( getClass() != obj.getClass() ) {
return false;
}
ContainerElementPathKey other = (ContainerElementPathKey) obj;
if ( index != other.index ) {
return false;
}
if ( !Arrays.equals( nestedIndexes, other.nestedIndexes ) ) {
return false;
}
return true;
}
}
}