Skip to content

Commit

Permalink
[value-context-changes] Introduce HibernateConstrainedType, that wrap…
Browse files Browse the repository at this point in the history
…s class

`HibernateConstrainedType` wraps a class and is used as a key for metadata
manager. It can also wrap strings or any other objects/data for metadata
of property holders and other kinds of objects.
  • Loading branch information
marko-bekhta committed Apr 5, 2019
1 parent 686a788 commit abd5bf5
Show file tree
Hide file tree
Showing 41 changed files with 660 additions and 301 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.engine;

import java.util.List;

/**
* An interface that represents a type/class of a bean that will be validated.
* Based on this type set of constraints will be determined, and applied to the
* validated object.
*
* @author Marko Bekhta
* @since 6.1
*/
public interface HibernateConstrainedType<T> {

/**
* @return a class of an object that will be validated.
*/
Class<T> getActuallClass();

List<HibernateConstrainedType<? super T>> getHierarchy();

boolean isInterface();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.hibernate.validator.cfg.context.MethodConstraintMappingContext;
import org.hibernate.validator.cfg.context.PropertyConstraintMappingContext;
import org.hibernate.validator.cfg.context.TypeConstraintMappingContext;
import org.hibernate.validator.internal.engine.constrainedtype.JavaBeanConstrainedType;
import org.hibernate.validator.internal.engine.ConstraintCreationContext;
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl.ConstraintType;
import org.hibernate.validator.internal.metadata.raw.BeanConfiguration;
Expand Down Expand Up @@ -231,7 +232,7 @@ public ConstructorConstraintMappingContext constructor(Class<?>... parameterType
BeanConfiguration<C> build(ConstraintCreationContext constraintCreationContext) {
return new BeanConfiguration<>(
ConfigurationSource.API,
beanClass,
new JavaBeanConstrainedType<>( beanClass ),
buildConstraintElements( constraintCreationContext ),
defaultGroupSequence,
getDefaultGroupSequenceProvider()
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.engine.constrainedtype;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.validator.engine.HibernateConstrainedType;
import org.hibernate.validator.internal.util.classhierarchy.ClassHierarchyHelper;
import org.hibernate.validator.metadata.BeanMetaDataClassNormalizer;

/**
* An implementation of {@link HibernateConstrainedType} for regular JavaBeans.
* Wrapps a {@link Class} object to adapt it to the needs of HV usages.
*
* @author Marko Bekhta
*/
public class JavaBeanConstrainedType<T> implements HibernateConstrainedType<T> {

private final Class<T> clazz;

public JavaBeanConstrainedType(Class<T> clazz) {
this.clazz = clazz;
}

@Override
public Class<T> getActuallClass() {
return clazz;
}

@Override
public List<HibernateConstrainedType<? super T>> getHierarchy() {
List<Class<? super T>> hierarchy = ClassHierarchyHelper.getHierarchy( clazz );
List<HibernateConstrainedType<? super T>> result = new ArrayList<>( hierarchy.size() );
for ( Class<? super T> clazzz : hierarchy ) {
result.add( new JavaBeanConstrainedType<>( clazzz ) );
}
return result;
}

@Override
public boolean isInterface() {
return clazz.isInterface();
}

public HibernateConstrainedType<T> normalize(BeanMetaDataClassNormalizer normalizer) {
return new NormalizedJavaBeanConstrainedType<>( normalizer, clazz );
}

@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || !( o instanceof JavaBeanConstrainedType ) ) {
return false;
}

JavaBeanConstrainedType<?> that = (JavaBeanConstrainedType<?>) o;

if ( !clazz.equals( that.clazz ) ) {
return false;
}

return true;
}

@Override
public int hashCode() {
return clazz.hashCode();
}

@Override
public String toString() {
return "JavaBeanConstrainedType{" +
"clazz=" + clazz +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.engine.constrainedtype;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.validator.engine.HibernateConstrainedType;
import org.hibernate.validator.internal.util.classhierarchy.ClassHierarchyHelper;
import org.hibernate.validator.metadata.BeanMetaDataClassNormalizer;

/**
* Uses {@link org.hibernate.validator.metadata.BeanMetaDataClassNormalizer} before
* creating a {@link HibernateConstrainedType}.
*
* @author Marko Bekhta
*/
public class NormalizedJavaBeanConstrainedType<T> extends JavaBeanConstrainedType<T> {
private final BeanMetaDataClassNormalizer normalizer;

public NormalizedJavaBeanConstrainedType(BeanMetaDataClassNormalizer normalizer, Class<T> clazz) {
super( (Class<T>) normalizer.normalize( clazz ) );
this.normalizer = normalizer;
}

@Override
public List<HibernateConstrainedType<? super T>> getHierarchy() {
List<Class<? super T>> hierarchy = ClassHierarchyHelper.getHierarchy( getActuallClass() );
List<HibernateConstrainedType<? super T>> result = new ArrayList<>( hierarchy.size() );
//TODO : question - do we actually need to normalize anything here. Wouldn't it be enough to normalize only when
// the metadata is retrieved ?
for ( Class<? super T> clazzz : hierarchy ) {
result.add( (NormalizedJavaBeanConstrainedType<? super T>) new NormalizedJavaBeanConstrainedType<>( normalizer, normalizer.normalize( clazzz ) ) );
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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>.
*/

/**
* Classes implementing {@link org.hibernate.validator.engine.HibernateConstrainedType}.
*/
package org.hibernate.validator.internal.engine.constrainedtype;
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import javax.validation.TraversableResolver;

import org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorInitializationContext;
import org.hibernate.validator.engine.HibernateConstrainedType;
import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManager;
import org.hibernate.validator.internal.engine.path.PathImpl;
import org.hibernate.validator.internal.metadata.aggregated.BeanMetaData;
Expand Down Expand Up @@ -43,49 +44,49 @@ public ValidationContextBuilder(
this.validatorScopedContext = validatorScopedContext;
}

public <T> BaseBeanValidationContext<T> forValidate(Class<T> rootBeanClass, BeanMetaData<T> rootBeanMetaData, T rootBean) {
public <T> BaseBeanValidationContext<T> forValidate(HibernateConstrainedType<T> constrainedType, BeanMetaData<T> rootBeanMetaData, T rootBean) {
return new BeanValidationContext<>(
constraintValidatorManager,
constraintValidatorFactory,
validatorScopedContext,
traversableResolver,
constraintValidatorInitializationContext,
rootBean,
rootBeanClass,
constrainedType.getActuallClass(),
rootBeanMetaData
);
}

public <T> BaseBeanValidationContext<T> forValidateProperty(Class<T> rootBeanClass, BeanMetaData<T> rootBeanMetaData, T rootBean, PathImpl propertyPath) {
public <T> BaseBeanValidationContext<T> forValidateProperty(HibernateConstrainedType<T> constrainedType, BeanMetaData<T> rootBeanMetaData, T rootBean, PathImpl propertyPath) {
return new PropertyValidationContext<>(
constraintValidatorManager,
constraintValidatorFactory,
validatorScopedContext,
traversableResolver,
constraintValidatorInitializationContext,
rootBean,
rootBeanClass,
constrainedType.getActuallClass(),
rootBeanMetaData,
propertyPath.getLeafNode().getName()
);
}

public <T> BaseBeanValidationContext<T> forValidateValue(Class<T> rootBeanClass, BeanMetaData<T> rootBeanMetaData, PathImpl propertyPath) {
public <T> BaseBeanValidationContext<T> forValidateValue(HibernateConstrainedType<T> constrainedType, BeanMetaData<T> rootBeanMetaData, PathImpl propertyPath) {
return new PropertyValidationContext<>(
constraintValidatorManager,
constraintValidatorFactory,
validatorScopedContext,
traversableResolver,
constraintValidatorInitializationContext,
null, //root bean
rootBeanClass,
constrainedType.getActuallClass(),
rootBeanMetaData,
propertyPath.getLeafNode().getName()
);
}

public <T> ExecutableValidationContext<T> forValidateParameters(
Class<T> rootBeanClass,
HibernateConstrainedType<T> constrainedType,
BeanMetaData<T> rootBeanMetaData,
T rootBean,
Executable executable,
Expand All @@ -97,7 +98,7 @@ public <T> ExecutableValidationContext<T> forValidateParameters(
traversableResolver,
constraintValidatorInitializationContext,
rootBean,
rootBeanClass,
constrainedType.getActuallClass(),
rootBeanMetaData,
executable,
rootBeanMetaData.getMetaDataFor( executable ),
Expand All @@ -106,7 +107,7 @@ public <T> ExecutableValidationContext<T> forValidateParameters(
}

public <T> ExecutableValidationContext<T> forValidateReturnValue(
Class<T> rootBeanClass,
HibernateConstrainedType<T> constrainedType,
BeanMetaData<T> rootBeanMetaData,
T rootBean,
Executable executable,
Expand All @@ -118,7 +119,7 @@ public <T> ExecutableValidationContext<T> forValidateReturnValue(
traversableResolver,
constraintValidatorInitializationContext,
rootBean,
rootBeanClass,
constrainedType.getActuallClass(),
rootBeanMetaData,
executable,
rootBeanMetaData.getMetaDataFor( executable ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.hibernate.validator.internal.metadata;

import org.hibernate.validator.engine.HibernateConstrainedType;
import org.hibernate.validator.internal.metadata.aggregated.BeanMetaData;

/**
Expand All @@ -16,7 +17,7 @@
*/
public interface BeanMetaDataManager {

<T> BeanMetaData<T> getBeanMetaData(Class<T> beanClass);
<T> BeanMetaData<T> getBeanMetaData(HibernateConstrainedType<T> constrainedType);

void clear();
}
Loading

0 comments on commit abd5bf5

Please sign in to comment.