Skip to content

Commit

Permalink
HV-373 Consistent naming of Builder classes and verification that con…
Browse files Browse the repository at this point in the history
…structors/methods are only defined once per bean
  • Loading branch information
hferentschik committed Feb 18, 2013
1 parent f80fb93 commit 1076981
Show file tree
Hide file tree
Showing 23 changed files with 296 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -930,11 +930,6 @@ private <T> int validateParametersForGroup(ValidationContext<T> validationContex
BeanMetaData<T> beanMetaData = beanMetaDataManager.getBeanMetaData( validationContext.getRootBeanClass() );
ExecutableMetaData executableMetaData = beanMetaData.getMetaDataFor( executable );

if ( executableMetaData == null ) {
// nothing to validate
return 0;
}

// TODO GM: define behavior with respect to redefined default sequences. Should only the
// sequence from the validated bean be honored or also default sequence definitions up in
// the inheritance tree?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface AnnotationProcessingOptions {

boolean areMemberConstraintsIgnoredFor(Member member);

boolean areReturnValueConstraintsIgnored(Member member);
boolean areReturnValueConstraintsIgnoredFor(Member member);

boolean areParameterConstraintsIgnoredFor(Member member, int index);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

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

Expand Down Expand Up @@ -84,7 +83,7 @@ public boolean areMemberConstraintsIgnoredFor(Member member) {
}

@Override
public boolean areReturnValueConstraintsIgnored(Member member) {
public boolean areReturnValueConstraintsIgnoredFor(Member member) {
if ( annotationIgnoresForReturnValues.contains( member ) ) {
return true;
}
Expand Down Expand Up @@ -158,7 +157,7 @@ public void ignoreConstraintAnnotationsOnReturnValue(Member member) {

public void ignoreConstraintAnnotationsOnParameter(Member member, int index) {
if ( annotationIgnoresForMethodParameter.get( member ) == null ) {
List<Integer> tmpList = new ArrayList<Integer>();
List<Integer> tmpList = newArrayList();
tmpList.add( index );
annotationIgnoresForMethodParameter.put( member, tmpList );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ private ConstrainedExecutable findExecutableMetaData(ExecutableElement executabl
Set<MetaConstraint<?>> returnValueConstraints;
Map<Class<?>, Class<?>> groupConversions;
boolean isCascading;
if ( annotationProcessingOptions.areReturnValueConstraintsIgnored( executable.getMember() ) ) {
if ( annotationProcessingOptions.areReturnValueConstraintsIgnoredFor( executable.getMember() ) ) {
returnValueConstraints = Collections.emptySet();
groupConversions = Collections.emptyMap();
isCascading = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,10 @@ public interface Log extends BasicLogger {

@Message(id = 136, value = "The specified constraint annotation class %1$s cannot be loaded.")
ValidationException unableToLoadConstraintAnnotationClassException(String constraintAnnotationClass, @Cause Exception e);

@Message(id = 137, value = "The method '%1$s' is defined twice in the mapping xml for bean %2$s.")
ValidationException getMethodIsDefinedTwiceInMappingXmlForBeanException(String name, String beanClassName);

@Message(id = 138, value = "The constructor '%1$s' is defined twice in the mapping xml for bean %2$s.")
ValidationException getConstructorIsDefinedTwiceInMappingXmlForBeanException(String name, String beanClassName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.hibernate.validator.internal.metadata.core.AnnotationProcessingOptionsImpl;
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.hibernate.validator.internal.metadata.core.MetaConstraint;
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl;
import org.hibernate.validator.internal.metadata.location.CrossParameterConstraintLocation;
import org.hibernate.validator.internal.metadata.location.ExecutableConstraintLocation;
import org.hibernate.validator.internal.metadata.raw.ConfigurationSource;
Expand All @@ -47,10 +46,10 @@
*
* @author Hardy Ferentschik
*/
public class ConstraintExecutableBuilder {
public class ConstrainedExecutableBuilder {
private static final Log log = LoggerFactory.make();

private ConstraintExecutableBuilder() {
private ConstrainedExecutableBuilder() {
}

public static Set<ConstrainedExecutable> buildMethodConstrainedExecutable(List<MethodType> methods,
Expand All @@ -60,6 +59,7 @@ public static Set<ConstrainedExecutable> buildMethodConstrainedExecutable(List<M
ConstraintHelper constraintHelper,
AnnotationProcessingOptionsImpl annotationProcessingOptions) {
Set<ConstrainedExecutable> constrainedExecutables = newHashSet();
List<Method> alreadyProcessedMethods = newArrayList();
for ( MethodType methodType : methods ) {
// parse the parameters
List<Class<?>> parameterTypes = createParameterTypes(
Expand All @@ -84,7 +84,14 @@ public static Set<ConstrainedExecutable> buildMethodConstrainedExecutable(List<M
);
}

ExecutableElement constructorExecutableElement = ExecutableElement.forMethod( method );
if ( alreadyProcessedMethods.contains( method ) ) {
throw log.getMethodIsDefinedTwiceInMappingXmlForBeanException( method.toString(), beanClass.getName() );
}
else {
alreadyProcessedMethods.add( method );
}

ExecutableElement methodExecutableElement = ExecutableElement.forMethod( method );

// ignore annotations
boolean ignoreConstructorAnnotations = methodType.getIgnoreAnnotations() == null ? false : methodType
Expand All @@ -98,7 +105,7 @@ public static Set<ConstrainedExecutable> buildMethodConstrainedExecutable(List<M
methodType.getParameter(),
methodType.getCrossParameterConstraint(),
methodType.getReturnValue(),
constructorExecutableElement,
methodExecutableElement,
constraintHelper,
parameterNameProvider,
annotationProcessingOptions
Expand All @@ -116,6 +123,7 @@ public static Set<ConstrainedExecutable> buildConstructorConstrainedExecutable(L
ConstraintHelper constraintHelper,
AnnotationProcessingOptionsImpl annotationProcessingOptions) {
Set<ConstrainedExecutable> constrainedExecutables = newHashSet();
List<Constructor> alreadyProcessedConstructors = newArrayList();
for ( ConstructorType constructorType : constructors ) {
// parse the parameters
List<Class<?>> constructorParameterTypes = createParameterTypes(
Expand All @@ -128,9 +136,19 @@ public static Set<ConstrainedExecutable> buildConstructorConstrainedExecutable(L
beanClass,
constructorParameterTypes.toArray( new Class[constructorParameterTypes.size()] )
);

if ( constructor == null ) {
throw log.getBeanDoesNotContainConstructorException( beanClass.getName(), constructorParameterTypes );
}
if ( alreadyProcessedConstructors.contains( constructor ) ) {
throw log.getConstructorIsDefinedTwiceInMappingXmlForBeanException(
constructor.toString(),
beanClass.getName()
);
}
else {
alreadyProcessedConstructors.add( constructor );
}

ExecutableElement constructorExecutableElement = ExecutableElement.forConstructor( constructor );

Expand Down Expand Up @@ -165,7 +183,7 @@ private static ConstrainedExecutable parseExecutableType(String defaultPackage,
ConstraintHelper constraintHelper,
ParameterNameProvider parameterNameProvider,
AnnotationProcessingOptionsImpl annotationProcessingOptions) {
List<ConstrainedParameter> parameterMetaData = ConstraintParameterBuilder.buildConstrainedParameters(
List<ConstrainedParameter> parameterMetaData = ConstrainedParameterBuilder.buildConstrainedParameters(
parameterTypeList,
executableElement,
defaultPackage,
Expand All @@ -179,14 +197,13 @@ private static ConstrainedExecutable parseExecutableType(String defaultPackage,
executableElement
);
for ( ConstraintType constraintType : crossParameterConstraintList ) {
ConstraintDescriptorImpl<?> constraintDescriptor = ConstraintDescriptorBuilder.buildConstraintDescriptor(
MetaConstraint<?> metaConstraint = MetaConstraintBuilder.buildMetaConstraint(
constraintLocation,
constraintType,
executableElement.getElementType(),
defaultPackage,
constraintHelper
);
@SuppressWarnings("unchecked")
MetaConstraint<?> metaConstraint = new MetaConstraint( constraintDescriptor, constraintLocation );
crossParameterConstraints.add( metaConstraint );
}

Expand Down Expand Up @@ -225,18 +242,15 @@ private static boolean parseReturnValueType(ReturnValueType returnValueType,
return false;
}

ExecutableConstraintLocation constraintLocation = new ExecutableConstraintLocation(
executableElement
);
ExecutableConstraintLocation constraintLocation = new ExecutableConstraintLocation( executableElement );
for ( ConstraintType constraintType : returnValueType.getConstraint() ) {
ConstraintDescriptorImpl<?> constraintDescriptor = ConstraintDescriptorBuilder.buildConstraintDescriptor(
MetaConstraint<?> metaConstraint = MetaConstraintBuilder.buildMetaConstraint(
constraintLocation,
constraintType,
executableElement.getElementType(),
defaultPackage,
constraintHelper
);
@SuppressWarnings("unchecked")
MetaConstraint<?> metaConstraint = new MetaConstraint( constraintDescriptor, constraintLocation );
returnValueConstraints.add( metaConstraint );
}
groupConversions.putAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.hibernate.validator.internal.metadata.core.AnnotationProcessingOptionsImpl;
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.hibernate.validator.internal.metadata.core.MetaConstraint;
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl;
import org.hibernate.validator.internal.metadata.location.BeanConstraintLocation;
import org.hibernate.validator.internal.metadata.raw.ConfigurationSource;
import org.hibernate.validator.internal.metadata.raw.ConstrainedField;
Expand All @@ -40,32 +39,31 @@
*
* @author Hardy Ferentschik
*/
public class ConstraintFieldBuilder {
public class ConstrainedFieldBuilder {
private static final Log log = LoggerFactory.make();

private ConstraintFieldBuilder() {
private ConstrainedFieldBuilder() {
}

public static Set<ConstrainedField> buildConstrainedFields(List<FieldType> fields,
Class<?> beanClass,
String defaultPackage,
ConstraintHelper constraintHelper,
AnnotationProcessingOptionsImpl annotationProcessingOptions) {
Class<?> beanClass,
String defaultPackage,
ConstraintHelper constraintHelper,
AnnotationProcessingOptionsImpl annotationProcessingOptions) {
Set<ConstrainedField> constrainedFields = newHashSet();
List<String> alreadyProcessedFieldNames = newArrayList();
for ( FieldType fieldType : fields ) {
Field field = findField( beanClass, fieldType.getName(), alreadyProcessedFieldNames );
BeanConstraintLocation constraintLocation = new BeanConstraintLocation( field );
Set<MetaConstraint<?>> metaConstraints = newHashSet();
for ( ConstraintType constraint : fieldType.getConstraint() ) {
ConstraintDescriptorImpl<?> constraintDescriptor = ConstraintDescriptorBuilder.buildConstraintDescriptor(
MetaConstraint<?> metaConstraint = MetaConstraintBuilder.buildMetaConstraint(
constraintLocation,
constraint,
java.lang.annotation.ElementType.FIELD,
defaultPackage,
constraintHelper
);
@SuppressWarnings("unchecked")
MetaConstraint<?> metaConstraint = new MetaConstraint( constraintDescriptor, constraintLocation );
metaConstraints.add( metaConstraint );
}
Map<Class<?>, Class<?>> groupConversions = GroupConversionBuilder.buildGroupConversionMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.hibernate.validator.internal.metadata.core.AnnotationProcessingOptionsImpl;
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.hibernate.validator.internal.metadata.core.MetaConstraint;
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl;
import org.hibernate.validator.internal.metadata.location.ExecutableConstraintLocation;
import org.hibernate.validator.internal.metadata.raw.ConfigurationSource;
import org.hibernate.validator.internal.metadata.raw.ConstrainedExecutable;
Expand All @@ -42,10 +41,10 @@
*
* @author Hardy Ferentschik
*/
public class ConstraintGetterBuilder {
public class ConstrainedGetterBuilder {
private static final Log log = LoggerFactory.make();

private ConstraintGetterBuilder() {
private ConstrainedGetterBuilder() {
}

public static Set<ConstrainedExecutable> buildConstrainedGetters(List<GetterType> getterList,
Expand All @@ -62,14 +61,13 @@ public static Set<ConstrainedExecutable> buildConstrainedGetters(List<GetterType

Set<MetaConstraint<?>> metaConstraints = newHashSet();
for ( ConstraintType constraint : getterType.getConstraint() ) {
ConstraintDescriptorImpl<?> constraintDescriptor = ConstraintDescriptorBuilder.buildConstraintDescriptor(
MetaConstraint<?> metaConstraint = MetaConstraintBuilder.buildMetaConstraint(
constraintLocation,
constraint,
java.lang.annotation.ElementType.METHOD,
defaultPackage,
constraintHelper
);
@SuppressWarnings("unchecked")
MetaConstraint<?> metaConstraint = new MetaConstraint( constraintDescriptor, constraintLocation );
metaConstraints.add( metaConstraint );
}
Map<Class<?>, Class<?>> groupConversions = GroupConversionBuilder.buildGroupConversionMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.hibernate.validator.internal.metadata.core.AnnotationProcessingOptionsImpl;
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.hibernate.validator.internal.metadata.core.MetaConstraint;
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl;
import org.hibernate.validator.internal.metadata.location.ExecutableConstraintLocation;
import org.hibernate.validator.internal.metadata.raw.ConfigurationSource;
import org.hibernate.validator.internal.metadata.raw.ConstrainedParameter;
Expand All @@ -38,9 +37,9 @@
*
* @author Hardy Ferentschik
*/
public class ConstraintParameterBuilder {
public class ConstrainedParameterBuilder {

private ConstraintParameterBuilder() {
private ConstrainedParameterBuilder() {
}

public static List<ConstrainedParameter> buildConstrainedParameters(List<ParameterType> parameterList,
Expand All @@ -56,14 +55,13 @@ public static List<ConstrainedParameter> buildConstrainedParameters(List<Paramet
ExecutableConstraintLocation constraintLocation = new ExecutableConstraintLocation( executableElement, i );
Set<MetaConstraint<?>> metaConstraints = newHashSet();
for ( ConstraintType constraint : parameterType.getConstraint() ) {
ConstraintDescriptorImpl<?> constraintDescriptor = ConstraintDescriptorBuilder.buildConstraintDescriptor(
MetaConstraint<?> metaConstraint = MetaConstraintBuilder.buildMetaConstraint(
constraintLocation,
constraint,
executableElement.getElementType(),
defaultPackage,
constraintHelper
);
@SuppressWarnings("unchecked")
MetaConstraint<?> metaConstraint = new MetaConstraint( constraintDescriptor, constraintLocation );
metaConstraints.add( metaConstraint );
}
Map<Class<?>, Class<?>> groupConversions = GroupConversionBuilder.buildGroupConversionMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.hibernate.validator.internal.metadata.core.AnnotationProcessingOptionsImpl;
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.hibernate.validator.internal.metadata.core.MetaConstraint;
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl;
import org.hibernate.validator.internal.metadata.location.BeanConstraintLocation;
import org.hibernate.validator.internal.metadata.raw.ConfigurationSource;
import org.hibernate.validator.internal.metadata.raw.ConstrainedType;
Expand All @@ -37,9 +36,9 @@
*
* @author Hardy Ferentschik
*/
public class ConstraintTypeBuilder {
public class ConstrainedTypeBuilder {

private ConstraintTypeBuilder() {
private ConstrainedTypeBuilder() {
}

public static ConstrainedType buildConstrainedType(ClassType classType,
Expand All @@ -62,14 +61,13 @@ public static ConstrainedType buildConstrainedType(ClassType classType,
BeanConstraintLocation constraintLocation = new BeanConstraintLocation( beanClass );
Set<MetaConstraint<?>> metaConstraints = newHashSet();
for ( ConstraintType constraint : classType.getConstraint() ) {
ConstraintDescriptorImpl<?> constraintDescriptor = ConstraintDescriptorBuilder.buildConstraintDescriptor(
MetaConstraint<?> metaConstraint = MetaConstraintBuilder.buildMetaConstraint(
constraintLocation,
constraint,
java.lang.annotation.ElementType.TYPE,
defaultPackage,
constraintHelper
);
@SuppressWarnings("unchecked")
MetaConstraint<?> metaConstraint = new MetaConstraint( constraintDescriptor, constraintLocation );
metaConstraints.add( metaConstraint );
}

Expand All @@ -81,13 +79,11 @@ public static ConstrainedType buildConstrainedType(ClassType classType,
);
}

ConstrainedType constrainedType = new ConstrainedType(
return new ConstrainedType(
ConfigurationSource.XML,
constraintLocation,
metaConstraints
);

return constrainedType;
}

private static List<Class<?>> createGroupSequence(GroupSequenceType groupSequenceType, String defaultPackage) {
Expand Down

0 comments on commit 1076981

Please sign in to comment.