-
Notifications
You must be signed in to change notification settings - Fork 571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explore FreeForm validation v2 #989
base: main
Are you sure you want to change the base?
Changes from 1 commit
f61a93d
80a4bb8
9418a35
415abf7
4a523a8
37d4545
00a7af7
f75e407
ac7835f
92b0696
4e021be
9f1b1da
1807098
8076e67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.internal.metadata.core; | ||
|
||
import java.lang.annotation.Annotation; | ||
|
||
import org.hibernate.validator.internal.engine.valueextraction.ValueExtractorManager; | ||
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl; | ||
import org.hibernate.validator.internal.metadata.location.ConstraintLocation; | ||
import org.hibernate.validator.internal.properties.Constrainable; | ||
import org.hibernate.validator.internal.util.TypeResolutionHelper; | ||
|
||
/** | ||
* @author Marko Bekhta | ||
*/ | ||
public class MetaConstraintBuilder<A extends Annotation> { | ||
|
||
private final ConstraintDescriptorImpl<A> constraintDescriptor; | ||
private final ConstraintLocation.Builder locationBuilder; | ||
|
||
public MetaConstraintBuilder(ConstraintDescriptorImpl<A> constraintDescriptor, ConstraintLocation.Builder locationBuilder) { | ||
this.constraintDescriptor = constraintDescriptor; | ||
this.locationBuilder = locationBuilder; | ||
} | ||
|
||
public MetaConstraint<A> build(TypeResolutionHelper typeResolutionHelper, ValueExtractorManager valueExtractorManager, Constrainable constrainable) { | ||
return MetaConstraints.create( typeResolutionHelper, valueExtractorManager, constraintDescriptor, locationBuilder.build( constrainable ) ); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.internal.metadata.location; | ||
|
||
import org.hibernate.validator.internal.metadata.location.ConstraintLocation.Builder; | ||
import org.hibernate.validator.internal.properties.Constrainable; | ||
import org.hibernate.validator.internal.properties.Field; | ||
import org.hibernate.validator.internal.util.Contracts; | ||
|
||
/** | ||
* @author Marko Bekhta | ||
*/ | ||
public class PropertyHolderPropertyConstraintLocationBuilder implements Builder { | ||
|
||
@Override | ||
public ConstraintLocation build(Constrainable constrainable) { | ||
Contracts.assertTrue( constrainable instanceof Field, "Only Field instances are accepted." ); | ||
|
||
return ConstraintLocation.forField( constrainable.as( Field.class ) ); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
import org.hibernate.validator.internal.metadata.provider.MetaDataProvider; | ||
import org.hibernate.validator.internal.metadata.provider.PropertyHolderMetaDataProvider; | ||
import org.hibernate.validator.internal.properties.javabean.JavaBeanHelper; | ||
import org.hibernate.validator.internal.properties.propertyholder.PropertyAccessorCreatorProvider; | ||
import org.hibernate.validator.internal.util.ExecutableHelper; | ||
import org.hibernate.validator.internal.util.ExecutableParameterNameProvider; | ||
import org.hibernate.validator.internal.util.TypeResolutionHelper; | ||
|
@@ -51,6 +52,7 @@ public ConstraintMetaDataManager(ConstraintHelper constraintHelper, | |
TypeResolutionHelper typeResolutionHelper, | ||
ExecutableParameterNameProvider parameterNameProvider, | ||
ValueExtractorManager valueExtractorManager, | ||
PropertyAccessorCreatorProvider propertyAccessorCreatorProvider, | ||
JavaBeanHelper javaBeanHelper, | ||
ValidationOrderGenerator validationOrderGenerator, | ||
MethodValidationConfiguration methodValidationConfiguration, | ||
|
@@ -72,6 +74,7 @@ public ConstraintMetaDataManager(ConstraintHelper constraintHelper, | |
constraintHelper, | ||
typeResolutionHelper, | ||
valueExtractorManager, | ||
propertyAccessorCreatorProvider, | ||
validationOrderGenerator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we try to be consistent in the order of the parameters? |
||
); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
import org.hibernate.validator.internal.metadata.core.ConstraintHelper; | ||
import org.hibernate.validator.internal.metadata.provider.PropertyHolderMetaDataProvider; | ||
import org.hibernate.validator.internal.metadata.raw.propertyholder.PropertyHolderConfiguration; | ||
import org.hibernate.validator.internal.properties.propertyholder.PropertyAccessorCreatorProvider; | ||
import org.hibernate.validator.internal.util.TypeResolutionHelper; | ||
import org.hibernate.validator.internal.util.stereotypes.Immutable; | ||
|
||
|
@@ -45,15 +46,18 @@ public class PropertyHolderBeanMetaDataProvider { | |
*/ | ||
private final ValueExtractorManager valueExtractorManager; | ||
|
||
private final PropertyAccessorCreatorProvider propertyAccessorCreatorProvider; | ||
|
||
private final ValidationOrderGenerator validationOrderGenerator; | ||
|
||
private final MetaDataCache<PropertyHolderMetadataKey> metaDataCache; | ||
|
||
public PropertyHolderBeanMetaDataProvider(List<PropertyHolderMetaDataProvider> propertyHolderMetaDataProviderList, ConstraintHelper constraintHelper, TypeResolutionHelper typeResolutionHelper, ValueExtractorManager valueExtractorManager, ValidationOrderGenerator validationOrderGenerator) { | ||
public PropertyHolderBeanMetaDataProvider(List<PropertyHolderMetaDataProvider> propertyHolderMetaDataProviderList, ConstraintHelper constraintHelper, TypeResolutionHelper typeResolutionHelper, ValueExtractorManager valueExtractorManager, PropertyAccessorCreatorProvider propertyAccessorCreatorProvider, ValidationOrderGenerator validationOrderGenerator) { | ||
this.propertyHolderMetaDataProviderList = propertyHolderMetaDataProviderList; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we make the list immutable? Usually we do that where it is declared as immutable. |
||
this.constraintHelper = constraintHelper; | ||
this.typeResolutionHelper = typeResolutionHelper; | ||
this.valueExtractorManager = valueExtractorManager; | ||
this.propertyAccessorCreatorProvider = propertyAccessorCreatorProvider; | ||
this.validationOrderGenerator = validationOrderGenerator; | ||
|
||
this.metaDataCache = new MetaDataCache<>(); | ||
|
@@ -69,7 +73,7 @@ public <T> BeanMetaData<T> getBeanMetaData(Class<T> propertyHolderClass, String | |
|
||
private <T> BeanMetaDataImpl<T> createBeanMetaData(PropertyHolderMetadataKey metadataKey) { | ||
PropertyHolderBeanMetaDataBuilder builder = PropertyHolderBeanMetaDataBuilder.getInstance( | ||
constraintHelper, typeResolutionHelper, valueExtractorManager, validationOrderGenerator, metadataKey.propertyHolderClass | ||
constraintHelper, typeResolutionHelper, valueExtractorManager, propertyAccessorCreatorProvider, validationOrderGenerator, metadataKey.propertyHolderClass | ||
); | ||
|
||
for ( PropertyHolderMetaDataProvider metaDataProvider : propertyHolderMetaDataProviderList ) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Hibernate Validator, declare and validate application constraints | ||
* | ||
* License: Apache License, Version 2.0 | ||
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. | ||
*/ | ||
package org.hibernate.validator.internal.metadata.raw.propertyholder; | ||
|
||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.Set; | ||
|
||
import org.hibernate.validator.internal.metadata.aggregated.CascadingMetaDataBuilder; | ||
import org.hibernate.validator.internal.metadata.core.MetaConstraint; | ||
import org.hibernate.validator.internal.metadata.raw.ConfigurationSource; | ||
import org.hibernate.validator.internal.metadata.raw.ConstrainedElement; | ||
import org.hibernate.validator.internal.util.CollectionHelper; | ||
import org.hibernate.validator.internal.util.stereotypes.Immutable; | ||
|
||
/** | ||
* Base implementation of with functionality common to all {@link ConstrainedElement} implementations. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's at least a word missing here. |
||
* | ||
* @author Gunnar Morling | ||
* @author Hardy Ferentschik | ||
* @author Marko Bekhta | ||
*/ | ||
public abstract class ConstrainedPropertyHolderElement implements ConstrainedElement { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see anything specific to property holders here. Is this expected? |
||
private final ConstrainedElementKind kind; | ||
protected final ConfigurationSource source; | ||
@Immutable | ||
protected final Set<MetaConstraint<?>> constraints; | ||
protected final CascadingMetaDataBuilder cascadingMetaDataBuilder; | ||
@Immutable | ||
protected final Set<MetaConstraint<?>> typeArgumentConstraints; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's separate them with a new line, it will be more readable. |
||
|
||
public ConstrainedPropertyHolderElement(ConfigurationSource source, | ||
ConstrainedElementKind kind, | ||
Set<MetaConstraint<?>> constraints, | ||
Set<MetaConstraint<?>> typeArgumentConstraints, | ||
CascadingMetaDataBuilder cascadingMetaDataBuilder) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not propagate this way of indenting arguments, it's not very readable. |
||
this.kind = kind; | ||
this.source = source; | ||
this.constraints = constraints != null ? CollectionHelper.toImmutableSet( constraints ) : Collections.<MetaConstraint<?>>emptySet(); | ||
this.typeArgumentConstraints = typeArgumentConstraints != null ? CollectionHelper.toImmutableSet( typeArgumentConstraints ) : Collections.<MetaConstraint<?>>emptySet(); | ||
this.cascadingMetaDataBuilder = cascadingMetaDataBuilder; | ||
} | ||
|
||
@Override | ||
public ConstrainedElementKind getKind() { | ||
return kind; | ||
} | ||
|
||
@Override | ||
public Iterator<MetaConstraint<?>> iterator() { | ||
return constraints.iterator(); | ||
} | ||
|
||
@Override | ||
public Set<MetaConstraint<?>> getConstraints() { | ||
return constraints; | ||
} | ||
|
||
@Override | ||
public Set<MetaConstraint<?>> getTypeArgumentConstraints() { | ||
return typeArgumentConstraints; | ||
} | ||
|
||
@Override | ||
public CascadingMetaDataBuilder getCascadingMetaDataBuilder() { | ||
return cascadingMetaDataBuilder; | ||
} | ||
|
||
@Override | ||
public boolean isConstrained() { | ||
return cascadingMetaDataBuilder.isMarkedForCascadingOnAnnotatedObjectOrContainerElements() | ||
|| cascadingMetaDataBuilder.hasGroupConversionsOnAnnotatedObjectOrContainerElements() | ||
|| !constraints.isEmpty() | ||
|| !typeArgumentConstraints.isEmpty(); | ||
} | ||
|
||
@Override | ||
public ConfigurationSource getSource() { | ||
return source; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "AbstractConstrainedElement [kind=" + kind + ", source=" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The class name is incorrect. |
||
+ source + ", constraints=" | ||
+ constraints + ", cascadingMetaDataBuilder=" | ||
+ cascadingMetaDataBuilder + "]"; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + ( ( source == null ) ? 0 : source.hashCode() ); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if ( this == obj ) { | ||
return true; | ||
} | ||
if ( obj == null ) { | ||
return false; | ||
} | ||
if ( getClass() != obj.getClass() ) { | ||
return false; | ||
} | ||
ConstrainedPropertyHolderElement other = (ConstrainedPropertyHolderElement) obj; | ||
if ( source != other.source ) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The part about location builder in this commit can be ignored. It will be removed in later commits. And I think about removing this builder as well. I think that storing the ConstraintDefs will be enough and this logic will be moved elsewhere.