Skip to content

Commit

Permalink
HV-373 Implementing annotation ignores
Browse files Browse the repository at this point in the history
  • Loading branch information
hferentschik committed Feb 18, 2013
1 parent 9c0d6ae commit 2650b72
Show file tree
Hide file tree
Showing 19 changed files with 470 additions and 369 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.Map;
import java.util.Set;

import org.hibernate.validator.internal.metadata.core.AnnotationProcessingOptions;
import org.hibernate.validator.internal.metadata.core.AnnotationProcessingOptionsImpl;
import org.hibernate.validator.internal.metadata.location.BeanConstraintLocation;
import org.hibernate.validator.internal.metadata.location.ExecutableConstraintLocation;
import org.hibernate.validator.spi.group.DefaultGroupSequenceProvider;
Expand All @@ -44,7 +44,7 @@ public class ConstraintMappingContext {
private final Set<Class<?>> configuredClasses;
private final Map<Class<?>, List<Class<?>>> defaultGroupSequences;
private final Map<Class<?>, Class<? extends DefaultGroupSequenceProvider<?>>> defaultGroupSequenceProviders;
private final AnnotationProcessingOptions annotationProcessingOptions;
private final AnnotationProcessingOptionsImpl annotationProcessingOptions;

public ConstraintMappingContext() {
this.constraintConfig = newHashMap();
Expand All @@ -54,7 +54,7 @@ public ConstraintMappingContext() {
this.configuredClasses = newHashSet();
this.defaultGroupSequences = newHashMap();
this.defaultGroupSequenceProviders = newHashMap();
this.annotationProcessingOptions = new AnnotationProcessingOptions();
this.annotationProcessingOptions = new AnnotationProcessingOptionsImpl();
}

/**
Expand Down Expand Up @@ -88,7 +88,7 @@ public final List<Class<?>> getDefaultSequence(Class<?> beanType) {
return defaultGroupSequences.get( beanType );
}

public final AnnotationProcessingOptions getAnnotationProcessingOptions() {
public final AnnotationProcessingOptionsImpl getAnnotationProcessingOptions() {
return annotationProcessingOptions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public PropertyConstraintMappingContext constraint(ConstraintDef<?, ?> definitio
}

public PropertyConstraintMappingContext ignoreAnnotations() {
mapping.getAnnotationProcessingOptions().ignorePropertyLevelConstraintAnnotationsOnMember( member );
mapping.getAnnotationProcessingOptions().ignoreConstraintAnnotationsOnMember( member );
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -185,7 +186,7 @@ private <T> BeanMetaDataImpl<T> createBeanMetaData(Class<T> clazz) {
* @return returns the annotation ignores from the non annotation based meta data providers
*/
private AnnotationProcessingOptions getAnnotationProcessingOptionsFromNonDefaultProviders() {
AnnotationProcessingOptions options = new AnnotationProcessingOptions();
AnnotationProcessingOptions options = new AnnotationProcessingOptionsImpl();
for ( MetaDataProvider metaDataProvider : metaDataProviders ) {
options.merge( metaDataProvider.getAnnotationProcessingOptions() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,8 @@ public void add(BeanConfiguration<? super T> configuration) {
}

private void addMetaDataToBuilder(ConstrainedElement constrainableElement, Set<BuilderDelegate> builders) {
for ( BuilderDelegate oneBuilder : builders ) {
boolean foundBuilder = oneBuilder.add( constrainableElement );
for ( BuilderDelegate builder : builders ) {
boolean foundBuilder = builder.add( constrainableElement );

if ( foundBuilder ) {
return;
Expand All @@ -509,13 +509,8 @@ private void addMetaDataToBuilder(ConstrainedElement constrainableElement, Set<B
public BeanMetaDataImpl<T> build() {
Set<ConstraintMetaData> aggregatedElements = newHashSet();

for ( BuilderDelegate oneBuilder : builders ) {
aggregatedElements.addAll(
oneBuilder.build(
( defaultGroupSequence != null && defaultGroupSequence.size() > 1 ) || defaultGroupSequenceProvider != null,
defaultGroupSequence
)
);
for ( BuilderDelegate builder : builders ) {
aggregatedElements.addAll( builder.build() );
}

return new BeanMetaDataImpl<T>(
Expand Down Expand Up @@ -585,7 +580,7 @@ public boolean add(ConstrainedElement constrainedElement) {
if ( propertyBuilder != null && propertyBuilder.accepts( constrainedElement ) ) {
propertyBuilder.add( constrainedElement );

if ( added == false && constrainedElement.getKind() == ConstrainedElementKind.METHOD && methodBuilder == null ) {
if ( !added && constrainedElement.getKind() == ConstrainedElementKind.METHOD && methodBuilder == null ) {
ConstrainedExecutable constrainedMethod = (ConstrainedExecutable) constrainedElement;
methodBuilder = new ExecutableMetaData.Builder(
beanClass,
Expand All @@ -600,18 +595,18 @@ public boolean add(ConstrainedElement constrainedElement) {
return added;
}

public Set<ConstraintMetaData> build(boolean defaultGroupSequenceRedefined, List<Class<?>> defaultGroupSequence) {
Set<ConstraintMetaData> theValue = newHashSet();
public Set<ConstraintMetaData> build() {
Set<ConstraintMetaData> metaDataSet = newHashSet();

if ( propertyBuilder != null ) {
theValue.add( propertyBuilder.build() );
metaDataSet.add( propertyBuilder.build() );
}

if ( methodBuilder != null ) {
theValue.add( methodBuilder.build() );
metaDataSet.add( methodBuilder.build() );
}

return theValue;
return metaDataSet;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual contributors
* Copyright 2013, 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.
*
Expand All @@ -16,19 +16,7 @@
*/
package org.hibernate.validator.internal.metadata.core;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;

import static org.hibernate.validator.internal.util.CollectionHelper.newArrayList;
import static org.hibernate.validator.internal.util.CollectionHelper.newHashMap;

/**
* An {@code AnnotationProcessingOptions} instance keeps track of annotations which should be ignored as configuration source.
Expand All @@ -37,145 +25,14 @@
*
* @author Hardy Ferentschik
*/
public class AnnotationProcessingOptions {

private static final Log log = LoggerFactory.make();

/**
* Keeps track whether the 'ignore-annotations' flag is set on bean level in the xml configuration. If 'ignore-annotations'
* is not specified {@code true} is the default.
*/
private final Map<Class<?>, Boolean> ignoreAnnotationDefaults = newHashMap();

/**
* Keeps track of explicitly excluded members (fields and properties) for a given class. If a member appears in
* the list mapped to a given class 'ignore-annotations' was explicitly set to {@code true} in the configuration
* for this class.
*/
private final Map<Class<?>, List<Member>> propertyConstraintAnnotationIgnores = newHashMap();

/**
* Keeps track of explicitly excluded class level constraints.
*/
private final Map<Class<?>, Boolean> classConstraintAnnotationIgnores = newHashMap();

/**
* Keeps track of explicitly excluded constructor constraints
*/
private final Map<Class<?>, List<Constructor>> constructorAnnotationIgnores = newHashMap();

/**
* Keeps track of explicitly excluded method constraints
*/
private final Map<Class<?>, List<Method>> methodAnnotationIgnores = newHashMap();

public void ignoreAnnotationConstraintForClass(Class<?> clazz, Boolean b) {
if ( b == null ) {
ignoreAnnotationDefaults.put( clazz, Boolean.TRUE );
}
else {
ignoreAnnotationDefaults.put( clazz, b );
}
}

public boolean areConstraintAnnotationsIgnored(Class<?> clazz) {
return ignoreAnnotationDefaults.containsKey( clazz ) && ignoreAnnotationDefaults.get( clazz );
}

public void ignorePropertyLevelConstraintAnnotationsOnMember(Member member) {
Class<?> beanClass = member.getDeclaringClass();
if ( propertyConstraintAnnotationIgnores.get( beanClass ) == null ) {
List<Member> tmpList = new ArrayList<Member>();
tmpList.add( member );
propertyConstraintAnnotationIgnores.put( beanClass, tmpList );
}
else {
propertyConstraintAnnotationIgnores.get( beanClass ).add( member );
}
}

public void ignoreConstraintAnnotationsOnConstructor(Constructor constructor) {
Class<?> beanClass = constructor.getDeclaringClass();
if ( constructorAnnotationIgnores.get( beanClass ) == null ) {
List<Constructor> tmpList = newArrayList();
tmpList.add( constructor );
constructorAnnotationIgnores.put( beanClass, tmpList );
}
else {
constructorAnnotationIgnores.get( beanClass ).add( constructor );
}
}

public void ignoreConstraintAnnotationsOnMethod(Method method) {
Class<?> beanClass = method.getDeclaringClass();
if ( methodAnnotationIgnores.get( beanClass ) == null ) {
List<Method> tmpList = newArrayList();
tmpList.add( method );
methodAnnotationIgnores.put( beanClass, tmpList );
}
else {
methodAnnotationIgnores.get( beanClass ).add( method );
}
}

public boolean arePropertyLevelConstraintAnnotationsIgnored(Member member) {
boolean ignoreAnnotation;
Class<?> clazz = member.getDeclaringClass();
List<Member> ignoreAnnotationForMembers = propertyConstraintAnnotationIgnores.get( clazz );
if ( ignoreAnnotationForMembers == null || !ignoreAnnotationForMembers.contains( member ) ) {
ignoreAnnotation = areConstraintAnnotationsIgnored( clazz );
}
else {
ignoreAnnotation = ignoreAnnotationForMembers.contains( member );
}
if ( ignoreAnnotation ) {
logMessage( member, clazz );
}
return ignoreAnnotation;
}

public void ignoreClassLevelConstraintAnnotations(Class<?> clazz, boolean b) {
classConstraintAnnotationIgnores.put( clazz, b );
}
public interface AnnotationProcessingOptions {
boolean areClassLevelConstraintsIgnoredFor(Class<?> clazz);

public boolean areClassLevelConstraintAnnotationsIgnored(Class<?> clazz) {
boolean ignoreAnnotation;
if ( classConstraintAnnotationIgnores.containsKey( clazz ) ) {
ignoreAnnotation = classConstraintAnnotationIgnores.get( clazz );
}
else {
ignoreAnnotation = areConstraintAnnotationsIgnored( clazz );
}
if ( log.isDebugEnabled() && ignoreAnnotation ) {
log.debugf( "Class level annotation are getting ignored for %s.", clazz.getName() );
}
return ignoreAnnotation;
}
boolean areMemberConstraintsIgnoredFor(Member member);

public void merge(AnnotationProcessingOptions annotationProcessingOptions) {
this.ignoreAnnotationDefaults.putAll( annotationProcessingOptions.ignoreAnnotationDefaults );
this.propertyConstraintAnnotationIgnores
.putAll( annotationProcessingOptions.propertyConstraintAnnotationIgnores );
this.classConstraintAnnotationIgnores
.putAll( annotationProcessingOptions.classConstraintAnnotationIgnores );
}
boolean areReturnValueConstraintsIgnored(Member member);

private void logMessage(Member member, Class<?> clazz) {
if ( log.isTraceEnabled() ) {
String type;
if ( member instanceof Field ) {
type = "Field";
}
else {
type = "Property";
}
boolean areParameterConstraintsIgnoredFor(Member member, int index);

log.debugf(
"%s level annotations are getting ignored for %s.%s.",
type,
clazz.getName(),
member.getName()
);
}
}
void merge(AnnotationProcessingOptions annotationProcessingOptions);
}

0 comments on commit 2650b72

Please sign in to comment.