[hibernate-commits] Hibernate SVN: r20121 - in validator/trunk/hibernate-validator-annotation-processor/src: main/java/org/hibernate/validator/ap/checks and 5 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Sun Aug 8 06:04:21 EDT 2010


Author: gunnar.morling
Date: 2010-08-08 06:04:21 -0400 (Sun, 08 Aug 2010)
New Revision: 20121

Added:
   validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/RetentionPolicyCheck.java
   validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/
   validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintWithWrongRetentionPolicy.java
   validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintWithoutRetentionPolicy.java
Removed:
   validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/invalidcomposedconstraint/
Modified:
   validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintAnnotationVisitor.java
   validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheckFactory.java
   validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/AnnotationApiHelper.java
   validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/ConstraintHelper.java
   validator/trunk/hibernate-validator-annotation-processor/src/main/resources/org/hibernate/validator/ap/ValidationProcessorMessages.properties
   validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/AnnotationTypeValidationTest.java
   validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ValidCustomerNumber.java
Log:
HV-299: Check retention policy of constraint annotation types

Modified: validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintAnnotationVisitor.java
===================================================================
--- validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintAnnotationVisitor.java	2010-08-08 09:53:03 UTC (rev 20120)
+++ validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintAnnotationVisitor.java	2010-08-08 10:04:21 UTC (rev 20121)
@@ -68,7 +68,9 @@
 				processingEnvironment.getElementUtils(), processingEnvironment.getTypeUtils(), annotationApiHelper
 		);
 
-		constraintCheckFactory = new ConstraintCheckFactory( constraintHelper );
+		constraintCheckFactory = new ConstraintCheckFactory(
+				processingEnvironment.getTypeUtils(), constraintHelper, annotationApiHelper
+		);
 	}
 
 	/**

Modified: validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheckFactory.java
===================================================================
--- validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheckFactory.java	2010-08-08 09:53:03 UTC (rev 20120)
+++ validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheckFactory.java	2010-08-08 10:04:21 UTC (rev 20121)
@@ -20,7 +20,9 @@
 import java.util.Map;
 import javax.lang.model.element.AnnotationMirror;
 import javax.lang.model.element.Element;
+import javax.lang.model.util.Types;
 
+import org.hibernate.validator.ap.util.AnnotationApiHelper;
 import org.hibernate.validator.ap.util.CollectionHelper;
 import org.hibernate.validator.ap.util.ConstraintHelper;
 import org.hibernate.validator.ap.util.ConstraintHelper.AnnotationType;
@@ -57,7 +59,7 @@
 
 	private final static SingleValuedChecks NULL_CHECKS = new SingleValuedChecks();
 
-	public ConstraintCheckFactory(ConstraintHelper constraintHelper) {
+	public ConstraintCheckFactory(Types typeUtils, ConstraintHelper constraintHelper, AnnotationApiHelper annotationApiHelper) {
 
 		this.constraintHelper = constraintHelper;
 
@@ -101,6 +103,12 @@
 				AnnotationType.MULTI_VALUED_CONSTRAINT_ANNOTATION,
 				new MultiValuedChecks( constraintHelper, new AnnotationTypeCheck( constraintHelper ) )
 		);
+		annotationTypeChecks.put(
+				AnnotationType.CONSTRAINT_META_ANNOTATION,
+				new SingleValuedChecks(
+						new RetentionPolicyCheck( annotationApiHelper )
+				)
+		);
 		annotationTypeChecks.put( AnnotationType.NO_CONSTRAINT_ANNOTATION, NULL_CHECKS );
 
 		nonAnnotationTypeChecks = CollectionHelper.newHashMap();

Added: validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/RetentionPolicyCheck.java
===================================================================
--- validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/RetentionPolicyCheck.java	                        (rev 0)
+++ validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/RetentionPolicyCheck.java	2010-08-08 10:04:21 UTC (rev 20121)
@@ -0,0 +1,69 @@
+// $Id: RetentionPolicyCheck.java 19310 2010-04-27 22:51:17Z gunnar.morling $
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.hibernate.validator.ap.checks;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.util.Collections;
+import java.util.Set;
+import javax.lang.model.element.AnnotationMirror;
+import javax.lang.model.element.TypeElement;
+
+import org.hibernate.validator.ap.util.AnnotationApiHelper;
+import org.hibernate.validator.ap.util.CollectionHelper;
+
+/**
+ * Checks, that {@link RetentionPolicy#RUNTIME} is declared for constraint annotation types.
+ *
+ * @author Gunnar Morling
+ */
+public class RetentionPolicyCheck extends AbstractConstraintCheck {
+
+	private final AnnotationApiHelper annotationApiHelper;
+
+	public RetentionPolicyCheck(AnnotationApiHelper annotationApiHelper) {
+		this.annotationApiHelper = annotationApiHelper;
+	}
+
+	@Override
+	public Set<ConstraintCheckError> checkAnnotationType(TypeElement element,
+														 AnnotationMirror annotation) {
+
+		Retention retention = element.getAnnotation( Retention.class );
+
+		if ( retention == null ) {
+			return CollectionHelper.asSet(
+					new ConstraintCheckError( element, null, "CONSTRAINT_TYPE_WITH_MISSING_OR_WRONG_RETENTION" )
+			);
+		}
+
+		if ( !retention.value().equals( RetentionPolicy.RUNTIME ) ) {
+			return CollectionHelper.asSet(
+					new ConstraintCheckError(
+							element,
+							annotationApiHelper.getMirror( element.getAnnotationMirrors(), Retention.class ),
+							"CONSTRAINT_TYPE_WITH_MISSING_OR_WRONG_RETENTION"
+					)
+			);
+		}
+
+
+		return Collections.emptySet();
+	}
+
+}

Modified: validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/AnnotationApiHelper.java
===================================================================
--- validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/AnnotationApiHelper.java	2010-08-08 09:53:03 UTC (rev 20120)
+++ validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/AnnotationApiHelper.java	2010-08-08 10:04:21 UTC (rev 20121)
@@ -133,12 +133,22 @@
 	/**
 	 * Returns a TypeMirror for the given class.
 	 *
-	 * @param clazz The class of interest. May not be a an array type.
+	 * @param clazz The class of interest.
 	 *
 	 * @return A TypeMirror for the given class.
 	 */
 	public TypeMirror getMirrorForType(Class<?> clazz) {
 
+		if ( clazz.isArray() ) {
+			return typeUtils.getArrayType( getMirrorForNonArrayType( clazz.getComponentType() ) );
+		}
+		else {
+			return getMirrorForNonArrayType( clazz );
+		}
+	}
+
+	private TypeMirror getMirrorForNonArrayType(Class<?> clazz) {
+
 		if ( clazz.isPrimitive() ) {
 			return primitiveMirrors.get( clazz );
 		}

Modified: validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/ConstraintHelper.java
===================================================================
--- validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/ConstraintHelper.java	2010-08-08 09:53:03 UTC (rev 20120)
+++ validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/ConstraintHelper.java	2010-08-08 10:04:21 UTC (rev 20121)
@@ -116,6 +116,11 @@
 		GRAPH_VALIDATION_ANNOTATION,
 
 		/**
+		 * Given annotation is the @Constraint meta-annotation.
+		 */
+		CONSTRAINT_META_ANNOTATION,
+
+		/**
 		 * Given annotation is not related to the BV API (e.g. @Resource).
 		 */
 		NO_CONSTRAINT_ANNOTATION
@@ -224,6 +229,9 @@
 		else if ( isGraphValidationAnnotation( annotationMirror ) ) {
 			return AnnotationType.GRAPH_VALIDATION_ANNOTATION;
 		}
+		else if ( isConstraintMetaAnnotation( annotationMirror ) ) {
+			return AnnotationType.CONSTRAINT_META_ANNOTATION;
+		}
 		else {
 			return AnnotationType.NO_CONSTRAINT_ANNOTATION;
 		}
@@ -322,6 +330,19 @@
 	}
 
 	/**
+	 * Checks, whether the given annotation mirror represents the {@link javax.validation.Constraint}
+	 * meta-annotation or not.
+	 *
+	 * @param annotationMirror The annotation mirror of interest.
+	 *
+	 * @return True, if the given mirror represents the @Constraint meta-annotation
+	 *         type, false otherwise.
+	 */
+	private boolean isConstraintMetaAnnotation(AnnotationMirror annotationMirror) {
+		return annotationMirror.getAnnotationType().asElement().getSimpleName().contentEquals( "Constraint" );
+	}
+
+	/**
 	 * Checks, whether the given annotation mirror represents a multi-valued
 	 * constraint such as {@link javax.validation.constraints.Pattern.List}.
 	 * That is the case if the annotation has an array-typed attribute with name
@@ -596,14 +617,7 @@
 		Set<TypeMirror> mirrorsForAllowedTypes = CollectionHelper.newHashSet();
 
 		for ( Class<?> oneAllowedType : allowedTypes ) {
-
-			if ( oneAllowedType.isArray() ) {
-				mirrorsForAllowedTypes.add( typeUtils.getArrayType( annotationApiHelper.getMirrorForType( oneAllowedType.getComponentType() ) ) );
-			}
-			else {
-				mirrorsForAllowedTypes.add( annotationApiHelper.getMirrorForType( oneAllowedType ) );
-			}
-
+			mirrorsForAllowedTypes.add( annotationApiHelper.getMirrorForType( oneAllowedType ) );
 		}
 
 		builtInConstraints.put( elementUtils.getName( annotation.getSimpleName() ), mirrorsForAllowedTypes );

Modified: validator/trunk/hibernate-validator-annotation-processor/src/main/resources/org/hibernate/validator/ap/ValidationProcessorMessages.properties
===================================================================
--- validator/trunk/hibernate-validator-annotation-processor/src/main/resources/org/hibernate/validator/ap/ValidationProcessorMessages.properties	2010-08-08 09:53:03 UTC (rev 20120)
+++ validator/trunk/hibernate-validator-annotation-processor/src/main/resources/org/hibernate/validator/ap/ValidationProcessorMessages.properties	2010-08-08 10:04:21 UTC (rev 20121)
@@ -11,3 +11,4 @@
 INVALID_DIAGNOSTIC_KIND_GIVEN=The given value {0} is no valid diagnostic kind. Kind.ERROR will be used.
 ATVALID_NOT_ALLOWED_AT_PRIMITIVE_FIELD=Fields of a primitive type must not annotated with @Valid.
 ATVALID_NOT_ALLOWED_AT_METHOD_RETURNING_PRIMITIVE_TYPE=Methods returning a primitive type must not annotated with @Valid.
+CONSTRAINT_TYPE_WITH_MISSING_OR_WRONG_RETENTION=Constraint annotation types must be annotated with @Retention(RUNTIME).
\ No newline at end of file

Modified: validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/AnnotationTypeValidationTest.java
===================================================================
--- validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/AnnotationTypeValidationTest.java	2010-08-08 09:53:03 UTC (rev 20120)
+++ validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/AnnotationTypeValidationTest.java	2010-08-08 10:04:21 UTC (rev 20121)
@@ -22,6 +22,8 @@
 
 import org.testng.annotations.Test;
 
+import org.hibernate.validator.ap.testmodel.constrainttypes.ConstraintWithWrongRetentionPolicy;
+import org.hibernate.validator.ap.testmodel.constrainttypes.ConstraintWithoutRetentionPolicy;
 import org.hibernate.validator.ap.testmodel.constrainttypes.ValidCustomerNumber;
 import org.hibernate.validator.ap.util.DiagnosticExpectation;
 
@@ -50,4 +52,28 @@
 		);
 	}
 
+	@Test
+	public void testThatConstraintAnnotationTypeWithoutRetentionPolicyCausesCompilationError() {
+
+		File sourceFile = compilerHelper.getSourceFile( ConstraintWithoutRetentionPolicy.class );
+
+		boolean compilationResult =
+				compilerHelper.compile( new ConstraintValidationProcessor(), diagnostics, sourceFile );
+
+		assertFalse( compilationResult );
+		assertThatDiagnosticsMatch( diagnostics, new DiagnosticExpectation( Kind.ERROR, 27 ) );
+	}
+
+	@Test
+	public void testThatConstraintAnnotationTypeWithWrongRetentionPolicyCausesCompilationError() {
+
+		File sourceFile = compilerHelper.getSourceFile( ConstraintWithWrongRetentionPolicy.class );
+
+		boolean compilationResult =
+				compilerHelper.compile( new ConstraintValidationProcessor(), diagnostics, sourceFile );
+
+		assertFalse( compilationResult );
+		assertThatDiagnosticsMatch( diagnostics, new DiagnosticExpectation( Kind.ERROR, 30 ) );
+	}
+
 }

Copied: validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes (from rev 20094, validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/invalidcomposedconstraint)

Added: validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintWithWrongRetentionPolicy.java
===================================================================
--- validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintWithWrongRetentionPolicy.java	                        (rev 0)
+++ validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintWithWrongRetentionPolicy.java	2010-08-08 10:04:21 UTC (rev 20121)
@@ -0,0 +1,31 @@
+// $Id: ConstraintWithWrongRetentionPolicy.java 19033 Aug 1, 2010 5:44:13 PM gunnar.morling $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+* http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.hibernate.validator.ap.testmodel.constrainttypes;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import javax.validation.Constraint;
+
+/**
+ * @author Gunnar Morling
+ */
+ at Constraint(validatedBy = { })
+ at Retention(RetentionPolicy.CLASS)
+public @interface ConstraintWithWrongRetentionPolicy {
+
+}

Added: validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintWithoutRetentionPolicy.java
===================================================================
--- validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintWithoutRetentionPolicy.java	                        (rev 0)
+++ validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintWithoutRetentionPolicy.java	2010-08-08 10:04:21 UTC (rev 20121)
@@ -0,0 +1,28 @@
+// $Id: ConstraintWithoutRetentionPolicy.java 19033 Aug 1, 2010 5:44:13 PM gunnar.morling $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+* http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.hibernate.validator.ap.testmodel.constrainttypes;
+
+import javax.validation.Constraint;
+
+/**
+ * @author Gunnar Morling
+ */
+ at Constraint(validatedBy = { })
+public @interface ConstraintWithoutRetentionPolicy {
+
+}

Modified: validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ValidCustomerNumber.java
===================================================================
--- validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/invalidcomposedconstraint/ValidCustomerNumber.java	2010-07-30 21:29:10 UTC (rev 20094)
+++ validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ValidCustomerNumber.java	2010-08-08 10:04:21 UTC (rev 20121)
@@ -15,7 +15,7 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
-package org.hibernate.validator.ap.testmodel.invalidcomposedconstraint;
+package org.hibernate.validator.ap.testmodel.constrainttypes;
 
 import javax.validation.Payload;
 import javax.validation.constraints.NotNull;



More information about the hibernate-commits mailing list