Skip to content

Commit

Permalink
HV-457: Making HV core a test-scoped dependency for the AP, referenci…
Browse files Browse the repository at this point in the history
…ng BV/HV types via the mirror API.
  • Loading branch information
gunnarmorling committed Mar 28, 2012
1 parent 8b5e38b commit c85cb92
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 77 deletions.
1 change: 1 addition & 0 deletions annotation-processor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
import javax.lang.model.util.SimpleAnnotationValueVisitor6;
import javax.lang.model.util.TypeKindVisitor6;
import javax.lang.model.util.Types;
import javax.validation.Payload;

import org.hibernate.validator.ap.util.AnnotationApiHelper;
import org.hibernate.validator.ap.util.CollectionHelper;

import static javax.lang.model.util.ElementFilter.methodsIn;
import static org.hibernate.validator.ap.util.TypeNames.Javax;

/**
* Checks, that each constraint annotation type declares the members message(), groups() and payload() as
Expand Down Expand Up @@ -184,7 +184,9 @@ private Set<ConstraintCheckError> checkPayloadAttribute(TypeElement element) {
boolean typeHasNameClass = type.asElement().getSimpleName().contentEquals( "Class" );
boolean typeHasExactlyOneTypeArgument = type.getTypeArguments().size() == 1;
boolean typeArgumentIsWildcardWithPayloadExtendsBound = validateWildcardBounds(
type.getTypeArguments().get( 0 ), annotationApiHelper.getMirrorForType( Payload.class ), null
type.getTypeArguments().get( 0 ),
annotationApiHelper.getDeclaredTypeByName( Javax.Validation.PAYLOAD ),
null
);

if ( !( typeHasNameClass && typeHasExactlyOneTypeArgument && typeArgumentIsWildcardWithPayloadExtendsBound ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
import java.util.Set;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.TypeElement;
import javax.validation.Constraint;

import org.hibernate.validator.ap.util.AnnotationApiHelper;
import org.hibernate.validator.ap.util.CollectionHelper;
import org.hibernate.validator.ap.util.ConstraintHelper;

import static org.hibernate.validator.ap.util.TypeNames.Javax;

/**
* Checks, that for each constraint annotation type, which is not a composed constraint,
* a validator implementation is specified using {@link Constraint} annotation.
Expand All @@ -48,7 +49,7 @@ public ConstraintValidatorCheck(ConstraintHelper constraintHelper, AnnotationApi
public Set<ConstraintCheckError> checkAnnotationType(TypeElement element, AnnotationMirror annotation) {

AnnotationMirror constraintMirror = annotationApiHelper.getMirror(
element.getAnnotationMirrors(), Constraint.class
element.getAnnotationMirrors(), Javax.Validation.CONSTRAINT
);
boolean atLeastOneValidatorGiven = !annotationApiHelper.getAnnotationArrayValue(
constraintMirror, "validatedBy"
Expand All @@ -59,7 +60,7 @@ public Set<ConstraintCheckError> checkAnnotationType(TypeElement element, Annota
return CollectionHelper.asSet(
new ConstraintCheckError(
element,
annotationApiHelper.getMirror( element.getAnnotationMirrors(), Constraint.class ),
constraintMirror,
"CONSTRAINT_TYPE_WITHOUT_VALIDATOR"
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@
import javax.lang.model.util.ElementKindVisitor6;
import javax.lang.model.util.SimpleTypeVisitor6;
import javax.lang.model.util.Types;
import javax.validation.GroupSequence;

import org.hibernate.validator.ap.util.AnnotationApiHelper;
import org.hibernate.validator.ap.util.CollectionHelper;
import org.hibernate.validator.group.DefaultGroupSequenceProvider;

import static org.hibernate.validator.ap.util.TypeNames.Javax;
import static org.hibernate.validator.ap.util.TypeNames.Org;

/**
* Checks that the {@link org.hibernate.validator.group.GroupSequenceProvider} annotation definition is valid.
Expand All @@ -60,7 +61,7 @@ public class GroupSequenceProviderCheck extends AbstractConstraintCheck {
public GroupSequenceProviderCheck(AnnotationApiHelper annotationApiHelper, Types typeUtils) {
this.typeUtils = typeUtils;
this.annotationApiHelper = annotationApiHelper;
this.defaultGroupSequenceProviderType = annotationApiHelper.getMirrorForType( DefaultGroupSequenceProvider.class );
this.defaultGroupSequenceProviderType = annotationApiHelper.getDeclaredTypeByName( Org.Hibernate.Validator.Group.DEFAULT_GROUP_SEQUENCE_PROVIDER );
}

@Override
Expand All @@ -83,7 +84,10 @@ private Set<ConstraintCheckError> checkHostingElement(TypeElement element, Annot
}

//this error should be raised only if the GroupSequenceProvider annotations is on a class
if ( element.getAnnotation( GroupSequence.class ) != null ) {
if ( annotationApiHelper.getMirror(
element.getAnnotationMirrors(),
Javax.Validation.GROUP_SEQUENCE
) != null ) {
return CollectionHelper.asSet(
new ConstraintCheckError(
element,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.ExecutableElement;
Expand Down Expand Up @@ -131,6 +130,38 @@ public AnnotationMirror getMirror(List<? extends AnnotationMirror> annotationMir
return null;
}

/**
* Returns that mirror from the given list of annotation mirrors that
* represents the annotation type specified by the given class.
*
* @param annotationMirrors A list of annotation mirrors.
* @param annotationClazz The class of the annotation of interest.
*
* @return The mirror from the given list that represents the specified
* annotation or null, if the given list doesn't contain such a
* mirror.
*/
public AnnotationMirror getMirror(List<? extends AnnotationMirror> annotationMirrors, String annotationClazz) {

if ( annotationMirrors == null || annotationClazz == null ) {
return null;
}

TypeMirror mirrorForAnnotation = elementUtils.getTypeElement( annotationClazz ).asType();

for ( AnnotationMirror oneAnnotationMirror : annotationMirrors ) {

if ( typeUtils.isSameType(
oneAnnotationMirror.getAnnotationType(),
mirrorForAnnotation
) ) {
return oneAnnotationMirror;
}
}

return null;
}

/**
* Returns a TypeMirror for the given class.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
*/
package org.hibernate.validator.ap.util;

import java.lang.annotation.Annotation;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
Expand All @@ -36,24 +33,9 @@
import javax.lang.model.util.SimpleAnnotationValueVisitor6;
import javax.lang.model.util.TypeKindVisitor6;
import javax.lang.model.util.Types;
import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.Valid;
import javax.validation.constraints.AssertFalse;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Digits;
import javax.validation.constraints.Future;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.hibernate.validator.group.GroupSequenceProvider;

import static org.hibernate.validator.ap.util.TypeNames.Javax;
import static org.hibernate.validator.ap.util.TypeNames.Org;

/**
* Helper class that deals with all constraint-related stuff, such as
Expand Down Expand Up @@ -135,12 +117,6 @@ public enum AnnotationType {
NO_CONSTRAINT_ANNOTATION
}

/**
* The name of the package containing JSR 303 standard annotations
* ("javax.validation.constraints").
*/
private final Name CONSTRAINT_ANNOTATION_PACKAGE_NAME;

/**
* Contains the supported types for the built-in constraints. Keyed by
* constraint annotation type names, each value is a set with the allowed
Expand All @@ -160,49 +136,58 @@ public ConstraintHelper(Elements elementUtils, Types typeUtils, AnnotationApiHel
this.typeUtils = typeUtils;
this.annotationApiHelper = annotationApiHelper;

CONSTRAINT_ANNOTATION_PACKAGE_NAME = elementUtils.getName( Size.class.getPackage().getName() );

builtInConstraints = CollectionHelper.newHashMap();

registerAllowedTypesForBuiltInConstraint(
AssertFalse.class, CollectionHelper.<Class<?>>asSet( Boolean.class )
Javax.Validation.Constraints.ASSERT_FALSE, CollectionHelper.<Class<?>>asSet( Boolean.class )
);
registerAllowedTypesForBuiltInConstraint( AssertTrue.class, CollectionHelper.<Class<?>>asSet( Boolean.class ) );
registerAllowedTypesForBuiltInConstraint(
DecimalMax.class, CollectionHelper.<Class<?>>asSet( Number.class, String.class )
Javax.Validation.Constraints.ASSERT_TRUE, CollectionHelper.<Class<?>>asSet( Boolean.class )
);
registerAllowedTypesForBuiltInConstraint(
DecimalMin.class, CollectionHelper.<Class<?>>asSet( Number.class, String.class )
Javax.Validation.Constraints.DECIMAL_MAX, CollectionHelper.<Class<?>>asSet( Number.class, String.class )
);
registerAllowedTypesForBuiltInConstraint(
Digits.class, CollectionHelper.<Class<?>>asSet( Number.class, String.class )
Javax.Validation.Constraints.DECIMAL_MIN, CollectionHelper.<Class<?>>asSet( Number.class, String.class )
);
registerAllowedTypesForBuiltInConstraint(
Future.class, CollectionHelper.<Class<?>>asSet( Calendar.class, Date.class )
Javax.Validation.Constraints.DIGITS, CollectionHelper.<Class<?>>asSet( Number.class, String.class )
);
registerAllowedTypesForBuiltInConstraint(
Javax.Validation.Constraints.FUTURE, CollectionHelper.<Class<?>>asSet( Calendar.class, Date.class )
);
registerAllowedTypesForBuiltInConstraintByNames(
Future.class,
Javax.Validation.Constraints.FUTURE,
CollectionHelper.<String>asSet( "org.joda.time.ReadablePartial", "org.joda.time.ReadableInstant" )
);
registerAllowedTypesForBuiltInConstraint(
Max.class, CollectionHelper.<Class<?>>asSet( Number.class, String.class )
Javax.Validation.Constraints.MAX, CollectionHelper.<Class<?>>asSet( Number.class, String.class )
);
registerAllowedTypesForBuiltInConstraint(
Javax.Validation.Constraints.MIN, CollectionHelper.<Class<?>>asSet( Number.class, String.class )
);
registerAllowedTypesForBuiltInConstraint(
Javax.Validation.Constraints.NOT_NULL,
CollectionHelper.<Class<?>>asSet( Object.class )
);
registerAllowedTypesForBuiltInConstraint(
Min.class, CollectionHelper.<Class<?>>asSet( Number.class, String.class )
Javax.Validation.Constraints.NULL,
CollectionHelper.<Class<?>>asSet( Object.class )
);
registerAllowedTypesForBuiltInConstraint( NotNull.class, CollectionHelper.<Class<?>>asSet( Object.class ) );
registerAllowedTypesForBuiltInConstraint( Null.class, CollectionHelper.<Class<?>>asSet( Object.class ) );
registerAllowedTypesForBuiltInConstraint(
Past.class, CollectionHelper.<Class<?>>asSet( Calendar.class, Date.class )
Javax.Validation.Constraints.PAST, CollectionHelper.<Class<?>>asSet( Calendar.class, Date.class )
);
registerAllowedTypesForBuiltInConstraintByNames(
Past.class,
Javax.Validation.Constraints.PAST,
CollectionHelper.<String>asSet( "org.joda.time.ReadablePartial", "org.joda.time.ReadableInstant" )
);

registerAllowedTypesForBuiltInConstraint( Pattern.class, CollectionHelper.<Class<?>>asSet( String.class ) );
registerAllowedTypesForBuiltInConstraint(
Size.class, CollectionHelper.<Class<?>>asSet(
Javax.Validation.Constraints.PATTERN,
CollectionHelper.<Class<?>>asSet( String.class )
);
registerAllowedTypesForBuiltInConstraint(
Javax.Validation.Constraints.SIZE, CollectionHelper.<Class<?>>asSet(
Object[].class,
boolean[].class,
byte[].class,
Expand Down Expand Up @@ -231,7 +216,7 @@ public ConstraintHelper(Elements elementUtils, Types typeUtils, AnnotationApiHel
* otherwise.
*/
public boolean isConstraintAnnotation(Element element) {
return element.getAnnotation( Constraint.class ) != null;
return annotationApiHelper.getMirror( element.getAnnotationMirrors(), Javax.Validation.CONSTRAINT ) != null;
}

/**
Expand Down Expand Up @@ -449,7 +434,7 @@ private boolean isGraphValidationAnnotation(

return typeUtils.isSameType(
annotationMirror.getAnnotationType(),
annotationApiHelper.getMirrorForType( Valid.class )
annotationApiHelper.getDeclaredTypeByName( TypeNames.Javax.Validation.VALID )
);
}

Expand All @@ -466,7 +451,7 @@ private boolean isGroupSequenceProviderAnnotation(

return typeUtils.isSameType(
annotationMirror.getAnnotationType(),
annotationApiHelper.getMirrorForType( GroupSequenceProvider.class )
annotationApiHelper.getDeclaredTypeByName( Org.Hibernate.Validator.Group.GROUP_SEQUENCE_PROVIDER )
);
}

Expand All @@ -483,17 +468,6 @@ private ConstraintCheckResult checkBuiltInConstraint(DeclaredType builtInAnnotat
return ConstraintCheckResult.DISALLOWED;
}

private Set<TypeMirror> getAllowedTypesForBuiltInConstraint(DeclaredType builtInAnnotationType) {

Set<TypeMirror> theValue = builtInConstraints.get( builtInAnnotationType.asElement().getSimpleName() );

if ( theValue == null ) {
theValue = Collections.emptySet();
}

return theValue;
}

/**
* Returns a set containing all those types, at which the specified custom
* constraint-annotation is allowed.
Expand Down Expand Up @@ -616,8 +590,8 @@ private TypeMirror getConstraintValidatorSuperType(TypeMirror type) {
while ( !superTypes.isEmpty() ) {

for ( TypeMirror oneSuperType : superTypes ) {
if ( typeUtils.asElement( oneSuperType ).getSimpleName()
.contentEquals( ConstraintValidator.class.getSimpleName() ) ) {
if ( ( (TypeElement) typeUtils.asElement( oneSuperType ) ).getQualifiedName()
.contentEquals( Javax.Validation.CONSTRAINT_VALIDATOR ) ) {

return oneSuperType;
}
Expand Down Expand Up @@ -645,14 +619,14 @@ private TypeMirror getConstraintValidatorSuperType(TypeMirror type) {
* @return The Constraint meta-annotation.
*
* @throws IllegalArgumentException If the given constraint annotation type isn't annotated with
* the {@link Constraint} meta-annotation.
* the {@link Constraint} meta-annotation.
*/
private AnnotationMirror getConstraintMetaAnnotation(DeclaredType annotationType) {

List<? extends AnnotationMirror> annotationMirrors = annotationType.asElement().getAnnotationMirrors();

AnnotationMirror constraintMetaAnnotation = annotationApiHelper.getMirror(
annotationMirrors, Constraint.class
annotationMirrors, Javax.Validation.CONSTRAINT
);

if ( constraintMetaAnnotation == null ) {
Expand All @@ -678,7 +652,9 @@ public List<? extends AnnotationValue> visitArray(List<? extends AnnotationValue
);
}

private void registerAllowedTypesForBuiltInConstraint(Class<? extends Annotation> annotation, Set<Class<?>> allowedTypes) {
private void registerAllowedTypesForBuiltInConstraint(String annotationType, Set<Class<?>> allowedTypes) {

DeclaredType annotation = annotationApiHelper.getDeclaredTypeByName( annotationType );

Set<TypeMirror> allowedTypesForConstraint = getAllowedTypesForBuiltInConstraint( annotation );

Expand All @@ -687,7 +663,9 @@ private void registerAllowedTypesForBuiltInConstraint(Class<? extends Annotation
}
}

private void registerAllowedTypesForBuiltInConstraintByNames(Class<? extends Annotation> annotation, Set<String> allowedTypes) {
private void registerAllowedTypesForBuiltInConstraintByNames(String annotationType, Set<String> allowedTypes) {

DeclaredType annotation = annotationApiHelper.getDeclaredTypeByName( annotationType );

Set<TypeMirror> allowedTypesForConstraint = getAllowedTypesForBuiltInConstraint( annotation );

Expand All @@ -696,9 +674,9 @@ private void registerAllowedTypesForBuiltInConstraintByNames(Class<? extends Ann
}
}

private Set<TypeMirror> getAllowedTypesForBuiltInConstraint(Class<? extends Annotation> annotation) {
private Set<TypeMirror> getAllowedTypesForBuiltInConstraint(DeclaredType annotation) {

Name key = elementUtils.getName( annotation.getSimpleName() );
Name key = annotation.asElement().getSimpleName();
Set<TypeMirror> allowedTypes = builtInConstraints.get( key );

// create a mapping for the given annotation type if required
Expand All @@ -723,7 +701,7 @@ private Set<TypeMirror> getAllowedTypesForBuiltInConstraint(Class<? extends Anno
private boolean isBuiltInConstraint(DeclaredType constraintAnnotationType) {

return
CONSTRAINT_ANNOTATION_PACKAGE_NAME.equals(
Javax.Validation.Constraints.NAME.contentEquals(
elementUtils.getPackageOf( constraintAnnotationType.asElement() ).getQualifiedName()
);
}
Expand Down
Loading

0 comments on commit c85cb92

Please sign in to comment.