Navigation Menu

Skip to content

Commit

Permalink
HV-1505 Adding ConstraintAnnotationDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta authored and gsmet committed Nov 2, 2017
1 parent ad1a004 commit 4d01312
Show file tree
Hide file tree
Showing 41 changed files with 340 additions and 269 deletions.
Expand Up @@ -10,6 +10,8 @@

import javax.validation.Payload;

import org.hibernate.validator.internal.metadata.core.ConstraintHelper;

/**
* Base class for all constraint definition types. Each sub type represents a
* single constraint annotation type and allows to add this constraint to a bean
Expand Down Expand Up @@ -40,18 +42,18 @@ private C getThis() {
}

public C message(String message) {
addParameter( "message", message );
addParameter( ConstraintHelper.MESSAGE, message );
return getThis();
}

public C groups(Class<?>... groups) {
addParameter( "groups", groups );
addParameter( ConstraintHelper.GROUPS, groups );
return getThis();
}

@SuppressWarnings("unchecked")
public C payload(Class<? extends Payload>... payload) {
addParameter( "payload", payload );
addParameter( ConstraintHelper.PAYLOAD, payload );
return getThis();
}
}
Expand Up @@ -18,7 +18,7 @@
import org.hibernate.validator.cfg.ConstraintDef;
import org.hibernate.validator.internal.metadata.location.ConstraintLocation;
import org.hibernate.validator.internal.util.ExecutableHelper;
import org.hibernate.validator.internal.util.annotation.AnnotationDescriptor;
import org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor;

/**
* Represents a programmatically configured constraint and meta-data
Expand Down Expand Up @@ -93,7 +93,7 @@ public ConstraintLocation getLocation() {
return location;
}

public AnnotationDescriptor<A> createAnnotationDescriptor() {
public ConstraintAnnotationDescriptor<A> createAnnotationDescriptor() {
return constraint.createAnnotationDescriptor();
}

Expand All @@ -113,7 +113,7 @@ private ConstraintDefAccessor(ConstraintDef<?, A> original) {
}

@Override
protected AnnotationDescriptor<A> createAnnotationDescriptor() {
protected ConstraintAnnotationDescriptor<A> createAnnotationDescriptor() {
return super.createAnnotationDescriptor();
}
}
Expand Down
Expand Up @@ -30,7 +30,7 @@
import org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl;
import org.hibernate.validator.internal.util.Contracts;
import org.hibernate.validator.internal.util.TypeHelper;
import org.hibernate.validator.internal.util.annotation.AnnotationDescriptor;
import org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;

Expand Down Expand Up @@ -280,12 +280,12 @@ else if ( TypeHelper.isAssignable( assignableTypes.get( i ), type ) ) {
}

private static final class CacheKey {
private final AnnotationDescriptor<?> annotationDescriptor;
private final ConstraintAnnotationDescriptor<?> annotationDescriptor;
private final Type validatedType;
private final ConstraintValidatorFactory constraintValidatorFactory;
private final int hashCode;

private CacheKey(AnnotationDescriptor<?> annotationDescriptor, Type validatorType, ConstraintValidatorFactory constraintValidatorFactory) {
private CacheKey(ConstraintAnnotationDescriptor<?> annotationDescriptor, Type validatorType, ConstraintValidatorFactory constraintValidatorFactory) {
this.annotationDescriptor = annotationDescriptor;
this.validatedType = validatorType;
this.constraintValidatorFactory = constraintValidatorFactory;
Expand Down
Expand Up @@ -51,7 +51,7 @@
import org.hibernate.validator.internal.metadata.core.ConstraintOrigin;
import org.hibernate.validator.internal.util.CollectionHelper;
import org.hibernate.validator.internal.util.StringHelper;
import org.hibernate.validator.internal.util.annotation.AnnotationDescriptor;
import org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
import org.hibernate.validator.internal.util.privilegedactions.GetAnnotationAttributes;
Expand Down Expand Up @@ -90,7 +90,7 @@ public class ConstraintDescriptorImpl<T extends Annotation> implements Constrain
/**
* The annotation descriptor - accessing the annotation information has a cost so we do it only once.
*/
private final AnnotationDescriptor<T> annotationDescriptor;
private final ConstraintAnnotationDescriptor<T> annotationDescriptor;

/**
* The set of classes implementing the validation for this constraint. See also
Expand Down Expand Up @@ -165,7 +165,7 @@ public class ConstraintDescriptorImpl<T extends Annotation> implements Constrain

public ConstraintDescriptorImpl(ConstraintHelper constraintHelper,
Member member,
AnnotationDescriptor<T> annotationDescriptor,
ConstraintAnnotationDescriptor<T> annotationDescriptor,
ElementType type,
Class<?> implicitGroup,
ConstraintOrigin definedOn,
Expand Down Expand Up @@ -228,20 +228,20 @@ public ConstraintDescriptorImpl(ConstraintHelper constraintHelper,

public ConstraintDescriptorImpl(ConstraintHelper constraintHelper,
Member member,
AnnotationDescriptor<T> annotationDescriptor,
ConstraintAnnotationDescriptor<T> annotationDescriptor,
ElementType type) {
this( constraintHelper, member, annotationDescriptor, type, null, ConstraintOrigin.DEFINED_LOCALLY, null );
}

public ConstraintDescriptorImpl(ConstraintHelper constraintHelper,
Member member,
AnnotationDescriptor<T> annotationDescriptor,
ConstraintAnnotationDescriptor<T> annotationDescriptor,
ElementType type,
ConstraintType constraintType) {
this( constraintHelper, member, annotationDescriptor, type, null, ConstraintOrigin.DEFINED_LOCALLY, constraintType );
}

public AnnotationDescriptor<T> getAnnotationDescriptor() {
public ConstraintAnnotationDescriptor<T> getAnnotationDescriptor() {
return annotationDescriptor;
}

Expand All @@ -256,7 +256,7 @@ public Class<T> getAnnotationType() {

@Override
public String getMessageTemplate() {
return (String) getAttributes().get( ConstraintHelper.MESSAGE );
return annotationDescriptor.getMessage();
}

@Override
Expand Down Expand Up @@ -494,8 +494,8 @@ private static ValidateUnwrappedValue determineValueUnwrapping(Set<Class<? exten
return ValidateUnwrappedValue.DEFAULT;
}

private static ConstraintTarget determineValidationAppliesTo(AnnotationDescriptor<?> annotationDescriptor) {
return annotationDescriptor.getAttribute( ConstraintHelper.VALIDATION_APPLIES_TO, ConstraintTarget.class );
private static ConstraintTarget determineValidationAppliesTo(ConstraintAnnotationDescriptor<?> annotationDescriptor) {
return annotationDescriptor.validationAppliesTo();
}

private void validateCrossParameterConstraintType(Member member, boolean hasCrossParameterValidator) {
Expand Down Expand Up @@ -567,20 +567,20 @@ private boolean isExecutable(ElementType elementType) {
}

@SuppressWarnings("unchecked")
private static Set<Class<? extends Payload>> buildPayloadSet(AnnotationDescriptor<?> annotationDescriptor) {
private static Set<Class<? extends Payload>> buildPayloadSet(ConstraintAnnotationDescriptor<?> annotationDescriptor) {
Set<Class<? extends Payload>> payloadSet = newHashSet();

Class<Payload>[] payloadFromAnnotation = annotationDescriptor.getAttribute( ConstraintHelper.PAYLOAD, Class[].class );
Class<? extends Payload>[] payloadFromAnnotation = annotationDescriptor.getPayload();

if ( payloadFromAnnotation != null ) {
payloadSet.addAll( Arrays.asList( payloadFromAnnotation ) );
}
return CollectionHelper.toImmutableSet( payloadSet );
}

private static Set<Class<?>> buildGroupSet(AnnotationDescriptor<?> annotationDescriptor, Class<?> implicitGroup) {
private static Set<Class<?>> buildGroupSet(ConstraintAnnotationDescriptor<?> annotationDescriptor, Class<?> implicitGroup) {
Set<Class<?>> groupSet = newHashSet();
final Class<?>[] groupsFromAnnotation = annotationDescriptor.getMandatoryAttribute( ConstraintHelper.GROUPS, Class[].class );
final Class<?>[] groupsFromAnnotation = annotationDescriptor.getGroups();
if ( groupsFromAnnotation.length == 0 ) {
groupSet.add( Default.class );
}
Expand Down Expand Up @@ -737,7 +737,7 @@ private <U extends Annotation> ConstraintDescriptorImpl<U> createComposingConstr
final Class<U> annotationType = (Class<U>) constraintAnnotation.annotationType();

// use a annotation proxy
AnnotationDescriptor.Builder<U> annotationDescriptorBuilder = new AnnotationDescriptor.Builder<>(
ConstraintAnnotationDescriptor.Builder<U> annotationDescriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(
annotationType, run( GetAnnotationAttributes.action( constraintAnnotation ) )
);

Expand All @@ -754,8 +754,8 @@ annotationType, run( GetAnnotationAttributes.action( constraintAnnotation ) )
}

//propagate inherited attributes to composing constraints
annotationDescriptorBuilder.setAttribute( ConstraintHelper.GROUPS, groups.toArray( new Class<?>[groups.size()] ) );
annotationDescriptorBuilder.setAttribute( ConstraintHelper.PAYLOAD, payloads.toArray( new Class<?>[payloads.size()] ) );
annotationDescriptorBuilder.setGroups( groups.toArray( new Class<?>[groups.size()] ) );
annotationDescriptorBuilder.setPayload( payloads.toArray( new Class<?>[payloads.size()] ) );
if ( annotationDescriptorBuilder.hasAttribute( ConstraintHelper.VALIDATION_APPLIES_TO ) ) {
ConstraintTarget validationAppliesTo = getValidationAppliesTo();

Expand Down
Expand Up @@ -67,7 +67,7 @@
import org.hibernate.validator.internal.util.ExecutableHelper;
import org.hibernate.validator.internal.util.ReflectionHelper;
import org.hibernate.validator.internal.util.TypeResolutionHelper;
import org.hibernate.validator.internal.util.annotation.AnnotationDescriptor;
import org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;
import org.hibernate.validator.internal.util.privilegedactions.GetDeclaredConstructors;
Expand Down Expand Up @@ -366,7 +366,6 @@ private Set<MetaConstraint<?>> convertToMetaConstraints(List<ConstraintDescripto
* executable.
*
* @param executable The executable of interest.
* @param parameters The parameters of the executable.
*
* @return A list with parameter meta data for the given executable.
*/
Expand Down Expand Up @@ -554,7 +553,7 @@ private <A extends Annotation> ConstraintDescriptorImpl<A> buildConstraintDescri
return new ConstraintDescriptorImpl<>(
constraintHelper,
member,
new AnnotationDescriptor<>( annotation ),
new ConstraintAnnotationDescriptor<>( annotation ),
type
);
}
Expand Down
Expand Up @@ -63,6 +63,13 @@ public AnnotationDescriptor(A annotation) {
this.hashCode = buildHashCode();
}

public AnnotationDescriptor(AnnotationDescriptor<A> descriptor) {
this.type = descriptor.type;
this.attributes = descriptor.attributes;
this.hashCode = descriptor.hashCode;
this.annotation = descriptor.annotation;
}

private AnnotationDescriptor(Class<A> annotationType, Map<String, Object> attributes) {
this.type = annotationType;
this.attributes = CollectionHelper.toImmutableMap( attributes );
Expand Down Expand Up @@ -121,7 +128,7 @@ public boolean equals(Object obj) {
if ( this == obj ) {
return true;
}
if ( obj == null || obj.getClass() != AnnotationDescriptor.class ) {
if ( obj == null || !( obj instanceof AnnotationDescriptor ) ) {
return false;
}

Expand Down Expand Up @@ -191,20 +198,16 @@ private int buildHashCode() {

int nameHashCode = member.getKey().hashCode();

int valueHashCode =
!value.getClass().isArray() ? value.hashCode() :
value.getClass() == boolean[].class ? Arrays.hashCode( (boolean[]) value ) :
value.getClass() == byte[].class ? Arrays.hashCode( (byte[]) value ) :
value.getClass() == char[].class ? Arrays.hashCode( (char[]) value ) :
value.getClass() == double[].class ? Arrays.hashCode( (double[]) value ) :
value.getClass() == float[].class ? Arrays.hashCode( (float[]) value ) :
value.getClass() == int[].class ? Arrays.hashCode( (int[]) value ) :
value.getClass() == long[].class ? Arrays.hashCode(
(long[]) value
) :
value.getClass() == short[].class ? Arrays
.hashCode( (short[]) value ) :
Arrays.hashCode( (Object[]) value );
int valueHashCode = !value.getClass().isArray() ? value.hashCode()
: value.getClass() == boolean[].class ? Arrays.hashCode( (boolean[]) value )
: value.getClass() == byte[].class ? Arrays.hashCode( (byte[]) value )
: value.getClass() == char[].class ? Arrays.hashCode( (char[]) value )
: value.getClass() == double[].class ? Arrays.hashCode( (double[]) value )
: value.getClass() == float[].class ? Arrays.hashCode( (float[]) value )
: value.getClass() == int[].class ? Arrays.hashCode( (int[]) value )
: value.getClass() == long[].class ? Arrays.hashCode( (long[]) value )
: value.getClass() == short[].class ? Arrays.hashCode( (short[]) value )
: Arrays.hashCode( (Object[]) value );

hashCode += 127 * nameHashCode ^ valueHashCode;
}
Expand All @@ -213,35 +216,16 @@ private int buildHashCode() {
}

private boolean areEqual(Object o1, Object o2) {
return
!o1.getClass().isArray() ? o1.equals( o2 ) :
o1.getClass() == boolean[].class ? Arrays.equals( (boolean[]) o1, (boolean[]) o2 ) :
o1.getClass() == byte[].class ? Arrays.equals( (byte[]) o1, (byte[]) o2 ) :
o1.getClass() == char[].class ? Arrays.equals( (char[]) o1, (char[]) o2 ) :
o1.getClass() == double[].class ? Arrays.equals(
(double[]) o1,
(double[]) o2
) :
o1.getClass() == float[].class ? Arrays.equals(
(float[]) o1,
(float[]) o2
) :
o1.getClass() == int[].class ? Arrays.equals(
(int[]) o1,
(int[]) o2
) :
o1.getClass() == long[].class ? Arrays.equals(
(long[]) o1,
(long[]) o2
) :
o1.getClass() == short[].class ? Arrays.equals(
(short[]) o1,
(short[]) o2
) :
Arrays.equals(
(Object[]) o1,
(Object[]) o2
);
return !o1.getClass().isArray() ? o1.equals( o2 )
: o1.getClass() == boolean[].class ? Arrays.equals( (boolean[]) o1, (boolean[]) o2 )
: o1.getClass() == byte[].class ? Arrays.equals( (byte[]) o1, (byte[]) o2 )
: o1.getClass() == char[].class ? Arrays.equals( (char[]) o1, (char[]) o2 )
: o1.getClass() == double[].class ? Arrays.equals( (double[]) o1, (double[]) o2 )
: o1.getClass() == float[].class ? Arrays.equals( (float[]) o1, (float[]) o2 )
: o1.getClass() == int[].class ? Arrays.equals( (int[]) o1, (int[]) o2 )
: o1.getClass() == long[].class ? Arrays.equals( (long[]) o1, (long[]) o2 )
: o1.getClass() == short[].class ? Arrays.equals( (short[]) o1, (short[]) o2 )
: Arrays.equals( (Object[]) o1, (Object[]) o2 );
}

public static class Builder<S extends Annotation> {
Expand Down Expand Up @@ -279,7 +263,7 @@ public Class<S> getType() {
}

public AnnotationDescriptor<S> build() {
return new AnnotationDescriptor<S>( type, getAnnotationAttributes() );
return new AnnotationDescriptor<>( type, getAnnotationAttributes() );
}

private Map<String, Object> getAnnotationAttributes() {
Expand Down Expand Up @@ -340,7 +324,6 @@ private SortedSet<String> getRegisteredAttributesInAlphabeticalOrder() {

/**
* Runs the given privileged action, using a privileged block if required.
* <p>
* <b>NOTE:</b> This must never be changed into a publicly available method to avoid execution of arbitrary
* privileged actions within HV's protection domain.
*/
Expand Down

0 comments on commit 4d01312

Please sign in to comment.