package org.hibernate.validator.internal.metadata;
import static org.hibernate.validator.internal.util.CollectionHelper.newArrayList;
import static org.hibernate.validator.internal.util.ConcurrentReferenceHashMap.Option.IDENTITY_COMPARISONS;
import static org.hibernate.validator.internal.util.ConcurrentReferenceHashMap.ReferenceType.SOFT;
import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;
import java.util.EnumSet;
import java.util.List;
import javax.validation.valueextraction.ValueExtractor;
import org.hibernate.validator.internal.engine.MethodValidationConfiguration;
import org.hibernate.validator.internal.engine.groups.ValidationOrderGenerator;
import org.hibernate.validator.internal.engine.valueextraction.ValueExtractorManager;
import org.hibernate.validator.internal.metadata.aggregated.BeanMetaData;
import org.hibernate.validator.internal.metadata.aggregated.BeanMetaDataImpl;
import org.hibernate.validator.internal.metadata.aggregated.BeanMetaDataImpl.BeanMetaDataBuilder;
import org.hibernate.validator.internal.metadata.core.AnnotationProcessingOptions;
import org.hibernate.validator.internal.metadata.core.AnnotationProcessingOptionsImpl;
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider;
import org.hibernate.validator.internal.metadata.provider.MetaDataProvider;
import org.hibernate.validator.internal.metadata.raw.BeanConfiguration;
import org.hibernate.validator.internal.util.ConcurrentReferenceHashMap;
import org.hibernate.validator.internal.util.Contracts;
import org.hibernate.validator.internal.util.ExecutableHelper;
import org.hibernate.validator.internal.util.ExecutableParameterNameProvider;
import org.hibernate.validator.internal.util.TypeResolutionHelper;
import org.hibernate.validator.internal.util.classhierarchy.ClassHierarchyHelper;
public class BeanMetaDataManager {
private static final int DEFAULT_INITIAL_CAPACITY = 16;
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
private final List<MetaDataProvider> metaDataProviders;
private final ConstraintHelper constraintHelper;
private final TypeResolutionHelper typeResolutionHelper;
private final ValueExtractorManager ;
private final ExecutableParameterNameProvider parameterNameProvider;
private final ConcurrentReferenceHashMap<Class<?>, BeanMetaData<?>> beanMetaDataCache;
private final ExecutableHelper executableHelper;
private final ValidationOrderGenerator validationOrderGenerator;
private final MethodValidationConfiguration methodValidationConfiguration;
public (ConstraintHelper constraintHelper,
ExecutableHelper executableHelper,
TypeResolutionHelper typeResolutionHelper,
ExecutableParameterNameProvider parameterNameProvider,
ValueExtractorManager valueExtractorManager,
ValidationOrderGenerator validationOrderGenerator,
List<MetaDataProvider> optionalMetaDataProviders,
MethodValidationConfiguration methodValidationConfiguration) {
this.constraintHelper = constraintHelper;
this.executableHelper = executableHelper;
this.typeResolutionHelper = typeResolutionHelper;
this.valueExtractorManager = valueExtractorManager;
this.parameterNameProvider = parameterNameProvider;
this.validationOrderGenerator = validationOrderGenerator;
this.metaDataProviders = newArrayList();
this.metaDataProviders.addAll( optionalMetaDataProviders );
this.methodValidationConfiguration = methodValidationConfiguration;
this.beanMetaDataCache = new ConcurrentReferenceHashMap<>(
DEFAULT_INITIAL_CAPACITY,
DEFAULT_LOAD_FACTOR,
DEFAULT_CONCURRENCY_LEVEL,
SOFT,
SOFT,
EnumSet.of( IDENTITY_COMPARISONS )
);
AnnotationProcessingOptions annotationProcessingOptions = getAnnotationProcessingOptionsFromNonDefaultProviders();
AnnotationMetaDataProvider defaultProvider = new AnnotationMetaDataProvider(
constraintHelper,
typeResolutionHelper,
valueExtractorManager,
annotationProcessingOptions
);
this.metaDataProviders.add( defaultProvider );
}
@SuppressWarnings("unchecked")
public <T> BeanMetaData<T> getBeanMetaData(Class<T> beanClass) {
Contracts.assertNotNull( beanClass, MESSAGES.beanTypeCannotBeNull() );
BeanMetaData<T> beanMetaData = (BeanMetaData<T>) beanMetaDataCache.get( beanClass );
if ( beanMetaData != null ) {
return beanMetaData;
}
beanMetaData = createBeanMetaData( beanClass );
BeanMetaData<T> previousBeanMetaData = (BeanMetaData<T>) beanMetaDataCache.putIfAbsent( beanClass, beanMetaData );
if ( previousBeanMetaData != null ) {
return previousBeanMetaData;
}
return beanMetaData;
}
public void clear() {
beanMetaDataCache.clear();
}
public int numberOfCachedBeanMetaDataInstances() {
return beanMetaDataCache.size();
}
private <T> BeanMetaDataImpl<T> createBeanMetaData(Class<T> clazz) {
BeanMetaDataBuilder<T> builder = BeanMetaDataBuilder.getInstance(
constraintHelper, executableHelper, typeResolutionHelper, valueExtractorManager, parameterNameProvider, validationOrderGenerator, clazz, methodValidationConfiguration );
for ( MetaDataProvider provider : metaDataProviders ) {
for ( BeanConfiguration<? super T> beanConfiguration : getBeanConfigurationForHierarchy( provider, clazz ) ) {
builder.add( beanConfiguration );
}
}
return builder.build();
}
private AnnotationProcessingOptions getAnnotationProcessingOptionsFromNonDefaultProviders() {
AnnotationProcessingOptions options = new AnnotationProcessingOptionsImpl();
for ( MetaDataProvider metaDataProvider : metaDataProviders ) {
options.merge( metaDataProvider.getAnnotationProcessingOptions() );
}
return options;
}
private <T> List<BeanConfiguration<? super T>> getBeanConfigurationForHierarchy(MetaDataProvider provider, Class<T> beanClass) {
List<BeanConfiguration<? super T>> configurations = newArrayList();
for ( Class<? super T> clazz : ClassHierarchyHelper.getHierarchy( beanClass ) ) {
BeanConfiguration<? super T> configuration = provider.getBeanConfiguration( clazz );
if ( configuration != null ) {
configurations.add( configuration );
}
}
return configurations;
}
}