/
MetaConstraints.java
187 lines (160 loc) · 9.03 KB
/
MetaConstraints.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
/*
* 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.metadata.core;
import java.lang.annotation.Annotation;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import javax.validation.metadata.ValidateUnwrappedValue;
import org.hibernate.validator.internal.engine.valueextraction.ValueExtractorDescriptor;
import org.hibernate.validator.internal.engine.valueextraction.ValueExtractorHelper;
import org.hibernate.validator.internal.engine.valueextraction.ValueExtractorManager;
import org.hibernate.validator.internal.metadata.core.MetaConstraint.ContainerClassTypeParameterAndExtractor;
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl;
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
import org.hibernate.validator.internal.metadata.location.TypeArgumentConstraintLocation;
import org.hibernate.validator.internal.util.TypeHelper;
import org.hibernate.validator.internal.util.TypeResolutionHelper;
import org.hibernate.validator.internal.util.TypeVariableBindings;
import org.hibernate.validator.internal.util.TypeVariables;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
import com.fasterxml.classmate.ResolvedType;
/**
* Helper used to create {@link MetaConstraint}s.
*
* @author Guillaume Smet
*/
public class MetaConstraints {
private static final Log LOG = LoggerFactory.make( MethodHandles.lookup() );
private MetaConstraints() {
}
public static <A extends Annotation> MetaConstraint<A> create(TypeResolutionHelper typeResolutionHelper, ValueExtractorManager valueExtractorManager,
ConstraintDescriptorImpl<A> constraintDescriptor, ConstraintLocation location) {
List<ContainerClassTypeParameterAndExtractor> valueExtractionPath = new ArrayList<>();
Type typeOfValidatedElement = addValueExtractorDescriptorForWrappedValue( typeResolutionHelper, valueExtractorManager, constraintDescriptor,
valueExtractionPath, location );
ConstraintLocation current = location;
do {
if ( current instanceof TypeArgumentConstraintLocation ) {
addValueExtractorDescriptorForTypeArgumentLocation( valueExtractorManager, valueExtractionPath, (TypeArgumentConstraintLocation) current );
current = ( (TypeArgumentConstraintLocation) current ).getDelegate();
}
else {
current = null;
}
}
while ( current != null );
Collections.reverse( valueExtractionPath );
return new MetaConstraint<>( constraintDescriptor, location, valueExtractionPath, typeOfValidatedElement );
}
private static <A extends Annotation> Type addValueExtractorDescriptorForWrappedValue(TypeResolutionHelper typeResolutionHelper,
ValueExtractorManager valueExtractorManager, ConstraintDescriptorImpl<A> constraintDescriptor,
List<ContainerClassTypeParameterAndExtractor> valueExtractionPath, ConstraintLocation location) {
if ( ValidateUnwrappedValue.SKIP.equals( constraintDescriptor.getValueUnwrapping() ) ) {
return location.getTypeForValidatorResolution();
}
Class<?> declaredType = TypeHelper.getErasedReferenceType( location.getTypeForValidatorResolution() );
Set<ValueExtractorDescriptor> valueExtractorDescriptorCandidates = valueExtractorManager.getMaximallySpecificValueExtractors( declaredType );
ValueExtractorDescriptor selectedValueExtractorDescriptor;
// we want to force the unwrapping so we require one and only one maximally specific value extractors
if ( ValidateUnwrappedValue.UNWRAP.equals( constraintDescriptor.getValueUnwrapping() ) ) {
switch ( valueExtractorDescriptorCandidates.size() ) {
case 0:
throw LOG.getNoValueExtractorFoundForUnwrapException( declaredType );
case 1:
selectedValueExtractorDescriptor = valueExtractorDescriptorCandidates.iterator().next();
break;
default:
throw LOG.getUnableToGetMostSpecificValueExtractorDueToSeveralMaximallySpecificValueExtractorsDeclaredException(
declaredType,
ValueExtractorHelper.toValueExtractorClasses( valueExtractorDescriptorCandidates ) );
}
}
// we are in the implicit (DEFAULT) case so:
// - if we don't have a maximally specific value extractor marked with @UnwrapByDefault, we don't unwrap
// - if we have one maximally specific value extractors that is marked with @UnwrapByDefault, we unwrap
// - otherwise, we throw an exception as we can't choose between the value extractors
else {
Set<ValueExtractorDescriptor> unwrapByDefaultValueExtractorDescriptorCandidates = valueExtractorDescriptorCandidates.stream()
.filter( ved -> ved.isUnwrapByDefault() )
.collect( Collectors.toSet() );
switch ( unwrapByDefaultValueExtractorDescriptorCandidates.size() ) {
case 0:
return location.getTypeForValidatorResolution();
case 1:
selectedValueExtractorDescriptor = unwrapByDefaultValueExtractorDescriptorCandidates.iterator().next();
break;
default:
throw LOG.getImplicitUnwrappingNotAllowedWhenSeveralMaximallySpecificValueExtractorsMarkedWithUnwrapByDefaultDeclaredException(
declaredType,
ValueExtractorHelper.toValueExtractorClasses( unwrapByDefaultValueExtractorDescriptorCandidates ) );
}
}
if ( selectedValueExtractorDescriptor.getExtractedType().isPresent() ) {
valueExtractionPath.add( new ContainerClassTypeParameterAndExtractor( declaredType, null, null, selectedValueExtractorDescriptor ) );
return selectedValueExtractorDescriptor.getExtractedType().get();
}
else {
Class<?> wrappedValueType = getWrappedValueType( typeResolutionHelper, location.getTypeForValidatorResolution(), selectedValueExtractorDescriptor );
TypeVariable<?> typeParameter = getContainerClassTypeParameter( declaredType, selectedValueExtractorDescriptor );
valueExtractionPath.add( new ContainerClassTypeParameterAndExtractor( declaredType, typeParameter, TypeVariables.getTypeParameterIndex( typeParameter ), selectedValueExtractorDescriptor ) );
return wrappedValueType;
}
}
private static void addValueExtractorDescriptorForTypeArgumentLocation( ValueExtractorManager valueExtractorManager,
List<ContainerClassTypeParameterAndExtractor> valueExtractionPath, TypeArgumentConstraintLocation typeArgumentConstraintLocation ) {
Class<?> declaredType = typeArgumentConstraintLocation.getContainerClass();
TypeVariable<?> typeParameter = typeArgumentConstraintLocation.getTypeParameter();
ValueExtractorDescriptor valueExtractorDescriptor = valueExtractorManager
.getMaximallySpecificAndContainerElementCompliantValueExtractor( declaredType, typeParameter );
if ( valueExtractorDescriptor == null ) {
throw LOG.getNoValueExtractorFoundForTypeException( declaredType, typeParameter );
}
TypeVariable<?> actualTypeParameter = TypeVariables.getActualTypeParameter( typeParameter );
valueExtractionPath.add( new ContainerClassTypeParameterAndExtractor(
TypeVariables.getContainerClass( typeParameter ),
actualTypeParameter,
TypeVariables.getTypeParameterIndex( actualTypeParameter ),
valueExtractorDescriptor ) );
}
/**
* Returns the sub-types binding for the single type parameter of the super-type. E.g. for {@code IntegerProperty}
* and {@code Property<T>}, {@code Integer} would be returned.
*/
private static Class<?> getWrappedValueType(TypeResolutionHelper typeResolutionHelper, Type declaredType, ValueExtractorDescriptor valueExtractorDescriptor) {
ResolvedType resolvedType = typeResolutionHelper.getTypeResolver().resolve( declaredType );
List<ResolvedType> resolvedTypeParameters = resolvedType.typeParametersFor( valueExtractorDescriptor.getContainerType() );
if ( resolvedTypeParameters.isEmpty() ) {
throw LOG.getNoValueExtractorFoundForUnwrapException( declaredType );
}
else {
return resolvedTypeParameters.get( TypeVariables.getTypeParameterIndex( valueExtractorDescriptor.getExtractedTypeParameter() ) ).getErasedType();
}
}
private static TypeVariable<?> getContainerClassTypeParameter(Class<?> declaredType, ValueExtractorDescriptor selectedValueExtractorDescriptor) {
if ( selectedValueExtractorDescriptor.getExtractedTypeParameter() == null ) {
return null;
}
Map<Class<?>, Map<TypeVariable<?>, TypeVariable<?>>> allBindings = TypeVariableBindings.getTypeVariableBindings( declaredType );
Map<TypeVariable<?>, TypeVariable<?>> extractorTypeBindings = allBindings.get( selectedValueExtractorDescriptor.getContainerType() );
if ( extractorTypeBindings == null ) {
return null;
}
return extractorTypeBindings.entrySet().stream()
.filter( e -> Objects.equals( e.getKey().getGenericDeclaration(), declaredType ) )
.collect( Collectors.toMap( Map.Entry::getValue, Map.Entry::getKey ) )
.get( selectedValueExtractorDescriptor.getExtractedTypeParameter() );
}
}