Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2011, 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.metadata.provider;
 
 import java.util.List;
 import java.util.Set;
 
 
 import static org.hibernate.validator.internal.util.CollectionHelper.newArrayList;
 import static org.hibernate.validator.internal.util.CollectionHelper.newHashSet;
 import static org.hibernate.validator.internal.util.ConcurrentReferenceHashMap.ReferenceType.SOFT;
 import static org.hibernate.validator.internal.util.ReflectionHelper.getMethods;
 import static org.hibernate.validator.internal.util.ReflectionHelper.newInstance;

Author(s):
Gunnar Morling
Hardy Ferentschik
 
 @SuppressWarnings("deprecation")
 public class AnnotationMetaDataProvider implements MetaDataProvider {
 	private static final Log log = LoggerFactory.make();
The default initial capacity for this cache.
 
 	static final int DEFAULT_INITIAL_CAPACITY = 16;
 
 
 	public AnnotationMetaDataProvider(ConstraintHelper constraintHelperAnnotationProcessingOptions annotationProcessingOptions) {
 		this. = constraintHelper;
 		this. = annotationProcessingOptions;
 				,
 		);
 	}
 
 	}
 
 	public <T> List<BeanConfiguration<? super T>> getBeanConfigurationForHierarchy(Class<T> beanClass) {
 		List<BeanConfiguration<? super T>> configurations = newArrayList();
 
 		for ( Class<?> oneHierarchyClass : ReflectionHelper.computeClassHierarchybeanClasstrue ) ) {
 			@SuppressWarnings("unchecked")
 			BeanConfiguration<? super T> configuration = (BeanConfiguration<? super T>) getBeanConfiguration(
 					oneHierarchyClass
 			);
 			if ( configuration != null ) {
				configurations.addconfiguration );
			}
		}
		return configurations;
	}
	private BeanConfiguration<?> getBeanConfiguration(Class<?> beanClass) {
		BeanConfiguration<?> configuration = .getbeanClass );
		if ( configuration != null ) {
			return configuration;
		}
		configuration = retrieveBeanConfigurationbeanClass );
		.putbeanClassconfiguration );
		return configuration;
	}

Parameters:
beanClass The bean class for which to retrieve the meta data
Returns:
Retrieves constraint related meta data from the annotations of the given type.
	private <T> BeanConfiguration<T> retrieveBeanConfiguration(Class<T> beanClass) {
		Set<ConstrainedElementpropertyMetaData = getPropertyMetaDatabeanClass );
		propertyMetaData.addAllgetMethodMetaDatabeanClass ) );
		//TODO GM: currently class level constraints are represented by a PropertyMetaData. This
		//works but seems somewhat unnatural
		Set<MetaConstraint<?>> classLevelConstraints = getClassLevelConstraintsbeanClass );
		if ( !classLevelConstraints.isEmpty() ) {
			ConstrainedType classLevelMetaData =
							new BeanConstraintLocationbeanClass ),
							classLevelConstraints
					);
			propertyMetaData.addclassLevelMetaData );
		}
		return new BeanConfiguration<T>(
				beanClass,
				propertyMetaData,
		);
	}
	private List<Class<?>> getDefaultGroupSequence(Class<?> beanClass) {
		GroupSequence groupSequenceAnnotation = beanClass.getAnnotationGroupSequence.class );
		return groupSequenceAnnotation != null ? Arrays.asListgroupSequenceAnnotation.value() ) : null;
	}
	private <T> DefaultGroupSequenceProvider<? super T> getDefaultGroupSequenceProvider(Class<T> beanClass) {
		GroupSequenceProvider groupSequenceProviderAnnotation = beanClass.getAnnotationGroupSequenceProvider.class );
		if ( groupSequenceProviderAnnotation != null ) {
			return newGroupSequenceProviderClassInstancebeanClassgroupSequenceProviderAnnotation.value() );
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	private <T> DefaultGroupSequenceProvider<? super T> newGroupSequenceProviderClassInstance(Class<T> beanClassClass<?> providerClass) {
		Method[] providerMethods = getMethodsproviderClass );
		for ( Method method : providerMethods ) {
			Class<?>[] paramTypes = method.getParameterTypes();
			if ( "getValidationGroups".equalsmethod.getName() ) && !method.isBridge()
					&& paramTypes.length == 1 && paramTypes[0].isAssignableFrombeanClass ) ) {
						providerClass"the default group sequence provider"
				);
			}
		}
	}
			return Collections.emptySet();
		}
		Set<MetaConstraint<?>> classLevelConstraints = newHashSet();
		// HV-262
		List<ConstraintDescriptorImpl<?>> classMetaData = findClassLevelConstraintsclazz );
		for ( ConstraintDescriptorImpl<?> constraintDescription : classMetaData ) {
			classLevelConstraints.addcreateMetaConstraintclazzconstraintDescription ) );
		}
		return classLevelConstraints;
	}
	private Set<ConstrainedElementgetPropertyMetaData(Class<?> beanClass) {
		Set<ConstrainedElementpropertyMetaData = newHashSet();
		for ( Field field : ReflectionHelper.getDeclaredFieldsbeanClass ) ) {
			// HV-172
			if ( Modifier.isStaticfield.getModifiers() ) ||
					field.isSynthetic() ) {
				continue;
			}
			propertyMetaData.addfindPropertyMetaDatafield ) );
		}
		return propertyMetaData;
	}
				field
		);
		boolean isCascading = field.isAnnotationPresentValid.class );
		return new ConstrainedField(
				new BeanConstraintLocationfield ),
				constraints,
				isCascading
		);
	}
	private Set<MetaConstraint<?>> convertToMetaConstraints(List<ConstraintDescriptorImpl<?>> constraintDescriptorsField field) {
		Set<MetaConstraint<?>> constraints = newHashSet();
		for ( ConstraintDescriptorImpl<?> constraintDescription : constraintDescriptors ) {
			constraints.addcreateMetaConstraintfieldconstraintDescription ) );
		}
		return constraints;
	}
		Set<ConstrainedMethodmethodMetaData = newHashSet();
		final Method[] declaredMethods = ReflectionHelper.getDeclaredMethodsclazz );
		for ( Method method : declaredMethods ) {
			// HV-172; ignoring synthetic methods (inserted by the compiler), as they can't have any constraints
			// anyway and possibly hide the actual method with the same signature in the built meta model
					method
			) || method
					.isSynthetic() ) {
				continue;
			}
			methodMetaData.addfindMethodMetaDatamethod ) );
		}
		return methodMetaData;
	}

Finds all constraint annotations defined for the given method.

Parameters:
method The method to check for constraints annotations.
Returns:
A meta data object describing the constraints specified for the given method.
		List<ConstrainedParameterparameterConstraints = getParameterMetaDatamethod );
		boolean isCascading = method.isAnnotationPresentValid.class );
		Set<MetaConstraint<?>> constraints =
		return new ConstrainedMethod(
				new MethodConstraintLocationmethod ),
				parameterConstraints,
				constraints,
				isCascading
		);
	}
	private Set<MetaConstraint<?>> convertToMetaConstraints(List<ConstraintDescriptorImpl<?>> constraintsDescriptorsMethod method) {
		Set<MetaConstraint<?>> constraints = newHashSet();
		for ( ConstraintDescriptorImpl<?> oneDescriptor : constraintsDescriptors ) {
			constraints.addcreateReturnValueMetaConstraintmethodoneDescriptor ) );
		}
		return constraints;
	}

Retrieves constraint related meta data for the parameters of the given method.

Parameters:
method The method of interest.
Returns:
A list with parameter meta data for the given method.
		int i = 0;
		for ( Annotation[] annotationsOfOneParameter : method.getParameterAnnotations() ) {
			boolean parameterIsCascading = false;
			String parameterName =  + i;
			Set<MetaConstraint<?>> constraintsOfOneParameter = newHashSet();
			for ( Annotation oneAnnotation : annotationsOfOneParameter ) {
				//1. collect constraints if this annotation is a constraint annotation
						oneAnnotation.
				);
				for ( ConstraintDescriptorImpl<?> constraintDescriptorImpl : constraints ) {
					constraintsOfOneParameter.add(
									methodiconstraintDescriptorImpl
							)
					);
				}
				//2. mark parameter as cascading if this annotation is the @Valid annotation
				if ( oneAnnotation.annotationType().equalsValid.class ) ) {
					parameterIsCascading = true;
				}
			}
			metaData.add(
							new MethodConstraintLocationmethodi ),
							parameterName,
							constraintsOfOneParameter,
							parameterIsCascading
					)
			);
			i++;
		}
		return metaData;
	}

Finds all constraint annotations defined for the given field/method and returns them in a list of constraint descriptors.

Parameters:
member The fields or method to check for constraints annotations.
type The element type the constraint/annotation is placed on.
Returns:
A list of constraint descriptors for all constraint specified for the given field or method.
		assert member instanceof Field || member instanceof Method;
		for ( Annotation annotation : ( (AnnotatedElementmember ).getAnnotations() ) {
			metaData.addAllfindConstraintAnnotationsannotationtype ) );
		}
		return metaData;
	}

Finds all constraint annotations defined for the given class and returns them in a list of constraint descriptors.

Parameters:
beanClass The class to check for constraints annotations.
Returns:
A list of constraint descriptors for all constraint specified on the given class.
		for ( Annotation annotation : beanClass.getAnnotations() ) {
			metaData.addAllfindConstraintAnnotationsannotation. ) );
		}
		return metaData;
	}

Examines the given annotation to see whether it is a single- or multi-valued constraint annotation.

Parameters:
annotation The annotation to examine
type the element type on which the annotation/constraint is placed on
Returns:
A list of constraint descriptors or the empty list in case annotation is neither a single nor multi-valued annotation.
	private <A extends AnnotationList<ConstraintDescriptorImpl<?>> findConstraintAnnotations(A annotationElementType type) {
		List<ConstraintDescriptorImpl<?>> constraintDescriptors = new ArrayList<ConstraintDescriptorImpl<?>>();
		List<Annotationconstraints = new ArrayList<Annotation>();
		Class<? extends AnnotationannotationType = annotation.annotationType();
		if ( .isConstraintAnnotationannotationType )
				|| .isBuiltinConstraintannotationType ) ) {
			constraints.addannotation );
		}
		else if ( .isMultiValueConstraintannotationType ) ) {
			constraints.addAll.getMultiValueConstraintsannotation ) );
		}
		for ( Annotation constraint : constraints ) {
			final ConstraintDescriptorImpl<?> constraintDescriptor = buildConstraintDescriptor(
					constrainttype
			);
			constraintDescriptors.addconstraintDescriptor );
		}
		return constraintDescriptors;
	}
	private <A extends AnnotationMetaConstraint<?> createMetaConstraint(Class<?> declaringClassConstraintDescriptorImpl<A> descriptor) {
		return new MetaConstraint<A>( descriptornew BeanConstraintLocationdeclaringClass ) );
	}
	private <A extends AnnotationMetaConstraint<?> createMetaConstraint(Member memberConstraintDescriptorImpl<A> descriptor) {
		return new MetaConstraint<A>( descriptornew BeanConstraintLocationmember ) );
	}
	private <A extends AnnotationMetaConstraint<A> createParameterMetaConstraint(Method methodint parameterIndexConstraintDescriptorImpl<A> descriptor) {
		return new MetaConstraint<A>( descriptornew MethodConstraintLocationmethodparameterIndex ) );
	}
	private <A extends AnnotationMetaConstraint<A> createReturnValueMetaConstraint(Method methodConstraintDescriptorImpl<A> descriptor) {
		return new MetaConstraint<A>( descriptornew MethodConstraintLocationmethod ) );
	}
	private <A extends AnnotationConstraintDescriptorImpl<A> buildConstraintDescriptor(A annotationElementType type) {
	}
New to GrepCode? Check out our FAQ X