*
* @param mapping The constraint configuration created via the programmatic API.
*/
private <T> void initProgrammaticConfiguration(ConstraintMapping mapping) {
ConstraintMappingContext context = ConstraintMappingContext.getFromMapping(mapping);
final Map<Class<?>, List<ConfiguredConstraint<?, BeanConstraintLocation>>> constraintsByType = context.getConstraintConfig();
final Map<Class<?>, List<ConfiguredConstraint<?, MethodConstraintLocation>>> methodConstraintsByType = context
.getMethodConstraintConfig();
final Map<Class<?>, List<BeanConstraintLocation>> cascadeConfigByType = context.getCascadeConfig();
final Map<Class<?>, List<MethodConstraintLocation>> methodCascadeConfigByType = context.getMethodCascadeConfig();
for ( Class<?> clazz : context.getConfiguredClasses() ) {
@SuppressWarnings("unchecked")
Class<T> beanClass = (Class<T>) clazz;
// for each configured entity we have to check whether any of the interfaces or super classes is configured
// via the programmatic api as well
List<Class<?>> classes = ReflectionHelper.computeClassHierarchy( beanClass, true );
Map<Class<?>, List<BeanMetaConstraint<?>>> constraints = newHashMap();
Set<AggregatedMethodMetaData.Builder> builders = newHashSet();
Set<Member> cascadedMembers = newHashSet();
for ( Class<?> classInHierarchy : classes ) {
// if the programmatic config contains constraints for the class in the hierarchy create equivalent meta constraints
List<ConfiguredConstraint<?, BeanConstraintLocation>> constraintsOfType = constraintsByType.get(
classInHierarchy
);
if ( constraintsOfType != null ) {
addProgrammaticConfiguredConstraints(
constraintsOfType,
beanClass,
classInHierarchy,
constraints
);
}
// retrieve the method constraints
List<ConfiguredConstraint<?, MethodConstraintLocation>> methodConstraintsOfType = methodConstraintsByType
.get( classInHierarchy );
if ( methodConstraintsOfType != null ) {
addProgrammaticConfiguredMethodConstraint(
methodConstraintsOfType, beanClass, classInHierarchy, builders
);
}
// retrieve the cascading members of the current class if applicable
List<BeanConstraintLocation> cascadesOfType = cascadeConfigByType.get( classInHierarchy );
if ( cascadesOfType != null ) {
addProgrammaticConfiguredCascade( cascadesOfType, cascadedMembers );
}
// retrieve the cascading method return value and method parameter
List<MethodConstraintLocation> methodCascadesOfType = methodCascadeConfigByType.get( classInHierarchy );
if ( methodCascadesOfType != null ) {
addProgrammaticConfiguredMethodCascade( methodCascadesOfType, builders );
}
}
// build the programmatic configured method metaData
Set<AggregatedMethodMetaData> methodMetaDataMap = newHashSet();
for ( AggregatedMethodMetaData.Builder oneBuilder : builders ) {
methodMetaDataMap.add( oneBuilder.build() );
}
// create the bean metadata with the programmatic configured constraints and cascade
BeanMetaDataImpl<T> metaData = new BeanMetaDataImpl<T>(
beanClass,
constraintHelper,
context.getDefaultSequence( beanClass ),
context.getDefaultGroupSequenceProvider( beanClass ),
constraints,
methodMetaDataMap,
cascadedMembers,
new AnnotationIgnores(),
beanMetaDataCache