-
Notifications
You must be signed in to change notification settings - Fork 578
Wrapping of a class that defines the constraint validation rules #994
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
Open
marko-bekhta
wants to merge
3
commits into
hibernate:main
Choose a base branch
from
marko-bekhta:value-context
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
5eb1fb4
[value-context-changes] Return metadata hierarchy instead of a class one
marko-bekhta 686a788
[value-context-changes] Delegate retrieving bean metadata to Cascadin…
marko-bekhta abd5bf5
[value-context-changes] Introduce HibernateConstrainedType, that wrap…
marko-bekhta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
engine/src/main/java/org/hibernate/validator/engine/HibernateConstrainedType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 102 additions & 72 deletions
174
engine/src/main/java/org/hibernate/validator/internal/engine/ValidatorImpl.java
Large diffs are not rendered by default.
Oops, something went wrong.
83 changes: 83 additions & 0 deletions
83
...java/org/hibernate/validator/internal/engine/constrainedtype/JavaBeanConstrainedType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + | ||
'}'; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...ibernate/validator/internal/engine/constrainedtype/NormalizedJavaBeanConstrainedType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...e/src/main/java/org/hibernate/validator/internal/engine/constrainedtype/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
getActualClass()