Skip to content

Commit

Permalink
HV-1148 Removing reference to ConstraintLocation from ConstrainedElement
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarmorling committed Nov 8, 2016
1 parent 1655fb3 commit 0e8a053
Show file tree
Hide file tree
Showing 30 changed files with 149 additions and 134 deletions.
Expand Up @@ -18,7 +18,6 @@
import org.hibernate.validator.internal.engine.valuehandling.UnwrapMode;
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.hibernate.validator.internal.metadata.core.MetaConstraint;
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
import org.hibernate.validator.internal.metadata.raw.ConfigurationSource;
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement;
import org.hibernate.validator.internal.metadata.raw.ConstrainedExecutable;
Expand Down Expand Up @@ -107,7 +106,7 @@ public ConstrainedElement build(ConstraintHelper constraintHelper, ExecutablePar
// TODO HV-919 Support specification of type parameter constraints via XML and API
return new ConstrainedExecutable(
ConfigurationSource.API,
ConstraintLocation.forReturnValue( executable ),
executable,
getParameters( constraintHelper, parameterNameProvider ),
crossParameterContext != null ? crossParameterContext.getConstraints( constraintHelper ) : Collections.<MetaConstraint<?>>emptySet(),
returnValueContext != null ? returnValueContext.getConstraints( constraintHelper ) : Collections.<MetaConstraint<?>>emptySet(),
Expand All @@ -130,7 +129,7 @@ private List<ConstrainedParameter> getParameters(ConstraintHelper constraintHelp
constrainedParameters.add(
new ConstrainedParameter(
ConfigurationSource.API,
ConstraintLocation.forParameter( executable, i ),
executable,
ReflectionHelper.typeOf( executable, i ),
i,
parameterNameProvider.getParameterNames( executable ).get( i )
Expand Down
Expand Up @@ -17,7 +17,6 @@
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.hibernate.validator.internal.metadata.core.MetaConstraint;
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl.ConstraintType;
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
import org.hibernate.validator.internal.metadata.raw.ConfigurationSource;
import org.hibernate.validator.internal.metadata.raw.ConstrainedParameter;
import org.hibernate.validator.internal.util.ExecutableParameterNameProvider;
Expand Down Expand Up @@ -100,7 +99,7 @@ public ConstrainedParameter build(ConstraintHelper constraintHelper, ExecutableP
// TODO HV-919 Support specification of type parameter constraints via XML and API
return new ConstrainedParameter(
ConfigurationSource.API,
ConstraintLocation.forParameter( executableContext.getExecutable(), parameterIndex ),
executableContext.getExecutable(),
ReflectionHelper.typeOf( executableContext.getExecutable(), parameterIndex ),
parameterIndex,
parameterNameProvider.getParameterNames( executableContext.getExecutable() ).get( parameterIndex ),
Expand Down
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.validator.internal.cfg.context;

import java.lang.annotation.ElementType;
import java.lang.reflect.Executable;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
Expand All @@ -19,7 +20,6 @@
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.hibernate.validator.internal.metadata.core.MetaConstraint;
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl.ConstraintType;
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
import org.hibernate.validator.internal.metadata.raw.ConfigurationSource;
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement;
import org.hibernate.validator.internal.metadata.raw.ConstrainedExecutable;
Expand Down Expand Up @@ -95,7 +95,7 @@ ConstrainedElement build(ConstraintHelper constraintHelper) {
if ( member instanceof Field ) {
return new ConstrainedField(
ConfigurationSource.API,
ConstraintLocation.forProperty( member ),
(Field) member,
getConstraints( constraintHelper ),
Collections.<MetaConstraint<?>>emptySet(),
groupConversions,
Expand All @@ -106,7 +106,7 @@ ConstrainedElement build(ConstraintHelper constraintHelper) {
else {
return new ConstrainedExecutable(
ConfigurationSource.API,
ConstraintLocation.forProperty( member ),
(Executable) member,
getConstraints( constraintHelper ),
groupConversions,
isCascading,
Expand Down
Expand Up @@ -26,7 +26,6 @@
import org.hibernate.validator.cfg.context.TypeConstraintMappingContext;
import org.hibernate.validator.internal.metadata.core.ConstraintHelper;
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl.ConstraintType;
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
import org.hibernate.validator.internal.metadata.raw.BeanConfiguration;
import org.hibernate.validator.internal.metadata.raw.ConfigurationSource;
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement;
Expand Down Expand Up @@ -200,7 +199,7 @@ private Set<ConstrainedElement> buildConstraintElements(ConstraintHelper constra
elements.add(
new ConstrainedType(
ConfigurationSource.API,
ConstraintLocation.forClass( beanClass ),
beanClass,
getConstraints( constraintHelper )
)
);
Expand Down
Expand Up @@ -359,7 +359,7 @@ public final void add(ConstrainedElement constrainedElement) {
* @param executable The executable to merge.
*/
private void addToExecutablesByDeclaringType(ConstrainedExecutable executable) {
Class<?> beanClass = executable.getLocation().getDeclaringClass();
Class<?> beanClass = executable.getExecutable().getDeclaringClass();
ConstrainedExecutable mergedExecutable = executablesByDeclaringType.get( beanClass );

if ( mergedExecutable != null ) {
Expand Down
Expand Up @@ -159,8 +159,8 @@ public void add(ConstrainedElement constrainedElement) {
if ( constrainedParameter == null ) {
constrainedParameter = newConstrainedParameter;
}
else if ( newConstrainedParameter.getLocation().getDeclaringClass().isAssignableFrom(
constrainedParameter.getLocation().getDeclaringClass()
else if ( newConstrainedParameter.getExecutable().getDeclaringClass().isAssignableFrom(
constrainedParameter.getExecutable().getDeclaringClass()
) ) {
// If the current parameter is from a method hosted on a parent class,
// use this parent class parameter name instead of the more specific one.
Expand Down
Expand Up @@ -20,6 +20,7 @@
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import javax.validation.ElementKind;
Expand Down Expand Up @@ -241,8 +242,8 @@ public static class Builder extends MetaDataBuilder {
public Builder(Class<?> beanClass, ConstrainedField constrainedField, ConstraintHelper constraintHelper) {
super( beanClass, constraintHelper );

this.propertyName = constrainedField.getLocation().getPropertyName();
this.propertyType = ReflectionHelper.typeOf( constrainedField.getLocation().getMember() );
this.propertyName = constrainedField.getField().getName();
this.propertyType = ReflectionHelper.typeOf( constrainedField.getField() );
add( constrainedField );
}

Expand All @@ -257,8 +258,8 @@ public Builder(Class<?> beanClass, ConstrainedType constrainedType, ConstraintHe
public Builder(Class<?> beanClass, ConstrainedExecutable constrainedMethod, ConstraintHelper constraintHelper) {
super( beanClass, constraintHelper );

this.propertyName = constrainedMethod.getLocation().getPropertyName();
this.propertyType = ReflectionHelper.typeOf( constrainedMethod.getLocation().getMember() );
this.propertyName = ReflectionHelper.getPropertyName( constrainedMethod.getExecutable() );
this.propertyType = ReflectionHelper.typeOf( constrainedMethod.getExecutable() );
add( constrainedMethod );
}

Expand All @@ -273,10 +274,7 @@ public boolean accepts(ConstrainedElement constrainedElement) {
return false;
}

return equals(
constrainedElement.getLocation().getPropertyName(),
propertyName
);
return Objects.equals( getPropertyName( constrainedElement ), propertyName );
}

@Override
Expand Down Expand Up @@ -305,14 +303,29 @@ public final void add(ConstrainedElement constrainedElement) {

if ( constrainedElement.getKind() == ConstrainedElementKind.FIELD ) {
typeArgumentsConstraints.addAll( ( (ConstrainedField) constrainedElement ).getTypeArgumentsConstraints() );

if ( constrainedElement.isCascading() && cascadingMember == null ) {
cascadingMember = ( (ConstrainedField) constrainedElement ).getField();
}
}
else if ( constrainedElement.getKind() == ConstrainedElementKind.METHOD ) {
typeArgumentsConstraints.addAll( ( (ConstrainedExecutable) constrainedElement ).getTypeArgumentsConstraints() );

if ( constrainedElement.isCascading() && cascadingMember == null ) {
cascadingMember = ( (ConstrainedExecutable) constrainedElement ).getExecutable();
}
}
}

if ( constrainedElement.isCascading() && cascadingMember == null ) {
cascadingMember = constrainedElement.getLocation().getMember();
private String getPropertyName(ConstrainedElement constrainedElement) {
if ( constrainedElement.getKind() == ConstrainedElementKind.FIELD ) {
return ReflectionHelper.getPropertyName( ( (ConstrainedField) constrainedElement ).getField() );
}
else if ( constrainedElement.getKind() == ConstrainedElementKind.METHOD ) {
return ReflectionHelper.getPropertyName( ( (ConstrainedExecutable) constrainedElement ).getExecutable() );
}

return null;
}

@Override
Expand All @@ -332,9 +345,5 @@ public PropertyMetaData build() {
unwrapMode()
);
}

private boolean equals(String s1, String s2) {
return ( s1 != null && s1.equals( s2 ) ) || ( s1 == null && s2 == null );
}
}
}
Expand Up @@ -52,8 +52,8 @@ protected boolean isStrictSubType(Class<?> clazz, Class<?> otherClazz) {
* {@code otherExecutable}, {@code false} otherwise
*/
protected boolean isDefinedOnSubType(ConstrainedExecutable executable, ConstrainedExecutable otherExecutable) {
Class<?> clazz = executable.getLocation().getDeclaringClass();
Class<?> otherClazz = otherExecutable.getLocation().getDeclaringClass();
Class<?> clazz = executable.getExecutable().getDeclaringClass();
Class<?> otherClazz = otherExecutable.getExecutable().getDeclaringClass();

return isStrictSubType( clazz, otherClazz );
}
Expand All @@ -69,8 +69,8 @@ protected boolean isDefinedOnSubType(ConstrainedExecutable executable, Constrain
* {@code otherExecutable}, {@code false} otherwise
*/
protected boolean isDefinedOnParallelType(ConstrainedExecutable executable, ConstrainedExecutable otherExecutable) {
Class<?> clazz = executable.getLocation().getDeclaringClass();
Class<?> otherClazz = otherExecutable.getLocation().getDeclaringClass();
Class<?> clazz = executable.getExecutable().getDeclaringClass();
Class<?> otherClazz = otherExecutable.getExecutable().getDeclaringClass();

return !( clazz.isAssignableFrom( otherClazz ) || otherClazz.isAssignableFrom( clazz ) );
}
Expand Down
Expand Up @@ -22,8 +22,8 @@ public void apply(ConstrainedExecutable method, ConstrainedExecutable otherMetho
otherMethod.hasParameterConstraints() &&
!method.isEquallyParameterConstrained( otherMethod ) ) {
throw log.getParameterConfigurationAlteredInSubTypeException(
method.getLocation().getMember(),
otherMethod.getLocation().getMember()
method.getExecutable(),
otherMethod.getExecutable()
);
}
}
Expand Down
Expand Up @@ -24,8 +24,8 @@ public void apply(ConstrainedExecutable method, ConstrainedExecutable otherMetho

if ( isDefinedOnParallelType( method, otherMethod ) && isCascaded && hasGroupConversions ) {
throw log.getMethodsFromParallelTypesMustNotDefineGroupConversionsForCascadedReturnValueException(
method.getLocation().getMember(),
otherMethod.getLocation().getMember()
method.getExecutable(),
otherMethod.getExecutable()
);
}
}
Expand Down
Expand Up @@ -21,8 +21,8 @@ public void apply(ConstrainedExecutable method, ConstrainedExecutable otherMetho
if ( isDefinedOnParallelType( method, otherMethod ) &&
( method.hasParameterConstraints() || otherMethod.hasParameterConstraints() ) ) {
throw log.getParameterConstraintsDefinedInMethodsFromParallelTypesException(
method.getLocation().getMember(),
otherMethod.getLocation().getMember()
method.getExecutable(),
otherMethod.getExecutable()
);
}
}
Expand Down
Expand Up @@ -21,8 +21,8 @@ public void apply(ConstrainedExecutable method, ConstrainedExecutable otherMetho
if ( method.isCascading() && otherMethod.isCascading() &&
( isDefinedOnSubType( method, otherMethod ) || isDefinedOnSubType( otherMethod, method ) ) ) {
throw log.getMethodReturnValueMustNotBeMarkedMoreThanOnceForCascadedValidationException(
method.getLocation().getMember(),
otherMethod.getLocation().getMember()
method.getExecutable(),
otherMethod.getExecutable()
);
}
}
Expand Down
Expand Up @@ -23,7 +23,7 @@ public void apply(ConstrainedExecutable executable, ConstrainedExecutable otherE
if ( ( executable.getExecutable() instanceof Method ) &&
( (Method) executable.getExecutable() ).getReturnType() == void.class &&
( !executable.getConstraints().isEmpty() || executable.isCascading() ) ) {
throw log.getVoidMethodsMustNotBeConstrainedException( executable.getLocation().getMember() );
throw log.getVoidMethodsMustNotBeConstrainedException( executable.getExecutable() );
}
}
}
Expand Up @@ -153,7 +153,7 @@ private <T> BeanConfiguration<T> retrieveBeanConfiguration(Class<T> beanClass) {
ConstrainedType classLevelMetaData =
new ConstrainedType(
ConfigurationSource.ANNOTATION,
ConstraintLocation.forClass( beanClass ),
beanClass,
classLevelConstraints
);
constrainedElements.add( classLevelMetaData );
Expand Down Expand Up @@ -256,7 +256,7 @@ private ConstrainedField findPropertyMetaData(Field field) {

return new ConstrainedField(
ConfigurationSource.ANNOTATION,
ConstraintLocation.forProperty( field ),
field,
constraints,
typeArgumentsConstraints,
groupConversions,
Expand Down Expand Up @@ -393,7 +393,7 @@ private ConstrainedExecutable findExecutableMetaData(Executable executable) {

return new ConstrainedExecutable(
ConfigurationSource.ANNOTATION,
ConstraintLocation.forReturnValue( executable ),
executable,
parameterConstraints,
crossParameterConstraints,
returnValueConstraints,
Expand Down Expand Up @@ -457,7 +457,7 @@ private List<ConstrainedParameter> getParameterMetaData(Executable executable) {
metaData.add(
new ConstrainedParameter(
ConfigurationSource.ANNOTATION,
ConstraintLocation.forParameter( executable, i ),
executable,
ReflectionHelper.typeOf( executable, i ),
i,
parameterName,
Expand Down Expand Up @@ -511,7 +511,7 @@ else if ( parameterAnnotation.annotationType().equals( UnwrapValidatedValue.clas
metaData.add(
new ConstrainedParameter(
ConfigurationSource.ANNOTATION,
ConstraintLocation.forParameter( executable, i ),
executable,
ReflectionHelper.typeOf( executable, i ),
i,
parameterName,
Expand Down
Expand Up @@ -13,7 +13,6 @@

import org.hibernate.validator.internal.engine.valuehandling.UnwrapMode;
import org.hibernate.validator.internal.metadata.core.MetaConstraint;
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;

/**
* Base implementation of with functionality common to all {@link ConstrainedElement} implementations.
Expand All @@ -24,22 +23,19 @@
public abstract class AbstractConstrainedElement implements ConstrainedElement {
private final ConstrainedElementKind kind;
protected final ConfigurationSource source;
protected final ConstraintLocation location;
protected final Set<MetaConstraint<?>> constraints;
protected final Map<Class<?>, Class<?>> groupConversions;
protected final boolean isCascading;
protected final UnwrapMode unwrapMode;

public AbstractConstrainedElement(ConfigurationSource source,
ConstrainedElementKind kind,
ConstraintLocation location,
Set<MetaConstraint<?>> constraints,
Map<Class<?>, Class<?>> groupConversions,
boolean isCascading,
UnwrapMode unwrapMode) {
this.kind = kind;
this.source = source;
this.location = location;
this.constraints = constraints != null ? Collections.unmodifiableSet( constraints ) : Collections.<MetaConstraint<?>>emptySet();
this.groupConversions = Collections.unmodifiableMap( groupConversions );
this.isCascading = isCascading;
Expand All @@ -51,11 +47,6 @@ public ConstrainedElementKind getKind() {
return kind;
}

@Override
public ConstraintLocation getLocation() {
return location;
}

@Override
public Iterator<MetaConstraint<?>> iterator() {
return constraints.iterator();
Expand Down Expand Up @@ -89,7 +80,7 @@ public UnwrapMode unwrapMode() {
@Override
public String toString() {
return "AbstractConstrainedElement [kind=" + kind + ", source="
+ source + ", location=" + location + ", constraints="
+ source + ", constraints="
+ constraints + ", groupConversions=" + groupConversions
+ ", isCascading=" + isCascading + ", unwrapMode="
+ unwrapMode + "]";
Expand Down
Expand Up @@ -11,7 +11,6 @@

import org.hibernate.validator.internal.engine.valuehandling.UnwrapMode;
import org.hibernate.validator.internal.metadata.core.MetaConstraint;
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;

/**
* Represents a (potentially) constrained Java element such as a type, field or
Expand Down Expand Up @@ -56,13 +55,6 @@ enum ConstrainedElementKind {
*/
ConstrainedElementKind getKind();

/**
* Returns the location of this constrained element.
*
* @return The location of this constrained element.
*/
ConstraintLocation getLocation();

/**
* Returns a set containing the constraints specified for this constrained
* element.
Expand Down

0 comments on commit 0e8a053

Please sign in to comment.