[hibernate-commits] Hibernate SVN: r19324 - in validator/trunk/hibernate-validator-annotation-processor/src: main/java/org/hibernate/validator/ap/util and 3 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Apr 29 15:02:10 EDT 2010


Author: gunnar.morling
Date: 2010-04-29 15:02:09 -0400 (Thu, 29 Apr 2010)
New Revision: 19324

Added:
   validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/
   validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/AbstractCustomConstraintValidator.java
   validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/CustomConstraint.java
   validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/CustomConstraintValidator.java
   validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/FieldLevelValidationUsingInheritedValidator.java
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/util/ConstraintHelper.java
   validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/ConstraintValidationProcessorTest.java
Log:
HV-293: If single constraints can't be processed, this is now just logged, then the next constraint will be processed. Will add a "known bug" note to the AP documentation, that some constraints can't properly be processed in Eclipse.
Also added support for validators, which don't implement ConstraintValidator directly but inherit from another class implementing that IF.

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-04-29 18:37:41 UTC (rev 19323)
+++ validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintAnnotationVisitor.java	2010-04-29 19:02:09 UTC (rev 19324)
@@ -210,6 +210,8 @@
 				Set<ConstraintCheckError> errors = constraintChecks.execute( annotatedElement, oneAnnotationMirror );
 				messager.reportErrors( errors );
 			}
+			//HV-293: if single constraints can't be properly checked, report this and
+			//proceed with next constraints
 			catch ( Exception e ) {
 
 				if ( verbose ) {

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-04-29 18:37:41 UTC (rev 19323)
+++ validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/ConstraintHelper.java	2010-04-29 19:02:09 UTC (rev 19324)
@@ -63,7 +63,7 @@
 
 	/**
 	 * Possible results of a constraint check as returned by
-	 * {@link ConstraintHelper#checkConstraint(DeclaredType, TypeMirror)}.	 *
+	 * {@link ConstraintHelper#checkConstraint(DeclaredType, TypeMirror)}.
 	 *
 	 * @author Gunnar Morling
 	 */
@@ -503,31 +503,8 @@
 				}, null
 		);
 
-		TypeMirror supportedType;
-
-		supportedType = getSupportedTypeUsingAnnotationApi( validatorType );
-
-		// TODO GM: Due to HV-293 the type supported by a given validator can't
-		// always be determined when using
-		// the AP within Eclipse. As work around
-		// reflection might be used in such cases (meaning that the validator
-		// and its supported type have to be on
-		// the AP classpath, which normally wasn't required).
-
-		if ( supportedType == null ) {
-			throw new AssertionError(
-					"Couldn't determine the type supported by validator " + validatorType + "."
-			);
-		}
-		return supportedType;
-	}
-
-	private TypeMirror getSupportedTypeUsingAnnotationApi(
-			TypeMirror validatorType) {
-
 		// contains the bindings of the type parameters from the implemented
-		// ConstraintValidator
-		// interface, e.g. "ConstraintValidator<CheckCase, String>"
+		// ConstraintValidator interface, e.g. "ConstraintValidator<CheckCase, String>"
 		TypeMirror constraintValidatorImplementation = getConstraintValidatorSuperType( validatorType );
 
 		return constraintValidatorImplementation.accept(
@@ -545,17 +522,31 @@
 
 	private TypeMirror getConstraintValidatorSuperType(TypeMirror type) {
 
-		List<? extends TypeMirror> directSupertypes = typeUtils.directSupertypes( type );
+		List<? extends TypeMirror> superTypes = typeUtils.directSupertypes( type );
+		List<TypeMirror> nextSuperTypes = CollectionHelper.newArrayList();
 
-		for ( TypeMirror typeMirror : directSupertypes ) {
-			if ( typeUtils.asElement( typeMirror )
-					.getSimpleName()
-					.contentEquals( ConstraintValidator.class.getSimpleName() ) ) {
-				return typeMirror;
+		//follow the type hierarchy upwards, until we have found the ConstraintValidator IF
+		while ( !superTypes.isEmpty() ) {
+
+			for ( TypeMirror oneSuperType : superTypes ) {
+				if ( typeUtils.asElement( oneSuperType ).getSimpleName()
+						.contentEquals( ConstraintValidator.class.getSimpleName() ) ) {
+
+					return oneSuperType;
+				}
+
+				nextSuperTypes.addAll( typeUtils.directSupertypes( oneSuperType ) );
 			}
+
+			superTypes = nextSuperTypes;
+			nextSuperTypes = CollectionHelper.newArrayList();
 		}
 
-		return null;
+		//HV-293: Actually this should never happen, as we can have only ConstraintValidator implementations
+		//here. The Eclipse JSR 269 implementation unfortunately doesn't always create the type hierarchy 
+		//properly though.
+		//TODO GM: create and report an isolated test case
+		throw new IllegalStateException( "Expected type " + type + " to implement javax.validation.ConstraintValidator, but it doesn't." );
 	}
 
 	/**

Modified: validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/ConstraintValidationProcessorTest.java
===================================================================
--- validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/ConstraintValidationProcessorTest.java	2010-04-29 18:37:41 UTC (rev 19323)
+++ validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/ConstraintValidationProcessorTest.java	2010-04-29 19:02:09 UTC (rev 19324)
@@ -53,6 +53,10 @@
 import org.hibernate.validator.ap.testmodel.customconstraints.CheckCase;
 import org.hibernate.validator.ap.testmodel.customconstraints.CheckCaseValidator;
 import org.hibernate.validator.ap.testmodel.customconstraints.FieldLevelValidationUsingCustomConstraints;
+import org.hibernate.validator.ap.testmodel.inheritedvalidator.AbstractCustomConstraintValidator;
+import org.hibernate.validator.ap.testmodel.inheritedvalidator.CustomConstraint;
+import org.hibernate.validator.ap.testmodel.inheritedvalidator.CustomConstraintValidator;
+import org.hibernate.validator.ap.testmodel.inheritedvalidator.FieldLevelValidationUsingInheritedValidator;
 import org.hibernate.validator.ap.testmodel.invalidcomposedconstraint.ValidCustomerNumber;
 import org.hibernate.validator.ap.testmodel.nouniquevalidatorresolution.NoUniqueValidatorResolution;
 import org.hibernate.validator.ap.testmodel.nouniquevalidatorresolution.SerializableCollection;
@@ -157,6 +161,28 @@
 	}
 
 	@Test
+	public void testThatInheritedValidatorClassesAreHandledCorrectly() {
+
+		File sourceFile1 = compilerHelper.getSourceFile( FieldLevelValidationUsingInheritedValidator.class );
+		File sourceFile2 = compilerHelper.getSourceFile( CustomConstraint.class );
+		File sourceFile3 = compilerHelper.getSourceFile( AbstractCustomConstraintValidator.class );
+		File sourceFile4 = compilerHelper.getSourceFile( CustomConstraintValidator.class );
+
+		boolean compilationResult =
+				compilerHelper.compile(
+						new ConstraintValidationProcessor(),
+						diagnostics,
+						sourceFile1,
+						sourceFile2,
+						sourceFile3,
+						sourceFile4
+				);
+
+		assertFalse( compilationResult );
+		assertThatDiagnosticsMatch( diagnostics, new DiagnosticExpection( Kind.ERROR, 30 ) );
+	}
+
+	@Test
 	public void methodLevelValidationUsingBuiltInConstraints() {
 
 		File sourceFile = compilerHelper.getSourceFile( MethodLevelValidationUsingBuiltInConstraints.class );

Added: validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/AbstractCustomConstraintValidator.java
===================================================================
--- validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/AbstractCustomConstraintValidator.java	                        (rev 0)
+++ validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/AbstractCustomConstraintValidator.java	2010-04-29 19:02:09 UTC (rev 19324)
@@ -0,0 +1,24 @@
+// $Id$
+/*
+* 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.inheritedvalidator;
+
+import javax.validation.ConstraintValidator;
+
+public abstract class AbstractCustomConstraintValidator implements ConstraintValidator<CustomConstraint, String> {
+
+}


Property changes on: validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/AbstractCustomConstraintValidator.java
___________________________________________________________________
Name: svn:keywords
   + Id

Added: validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/CustomConstraint.java
===================================================================
--- validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/CustomConstraint.java	                        (rev 0)
+++ validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/CustomConstraint.java	2010-04-29 19:02:09 UTC (rev 19324)
@@ -0,0 +1,43 @@
+// $Id$
+/*
+* 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.inheritedvalidator;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+ at Target({ METHOD, FIELD, ANNOTATION_TYPE })
+ at Retention(RUNTIME)
+ at Constraint(validatedBy = CustomConstraintValidator.class)
+ at Documented
+public @interface CustomConstraint {
+
+	String message() default "";
+
+	Class<?>[] groups() default { };
+
+	Class<? extends Payload>[] payload() default { };
+
+}
\ No newline at end of file


Property changes on: validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/CustomConstraint.java
___________________________________________________________________
Name: svn:keywords
   + Id

Added: validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/CustomConstraintValidator.java
===================================================================
--- validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/CustomConstraintValidator.java	                        (rev 0)
+++ validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/CustomConstraintValidator.java	2010-04-29 19:02:09 UTC (rev 19324)
@@ -0,0 +1,30 @@
+// $Id$
+/*
+* 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.inheritedvalidator;
+
+import javax.validation.ConstraintValidatorContext;
+
+public class CustomConstraintValidator extends AbstractCustomConstraintValidator {
+
+	public void initialize(CustomConstraint constraintAnnotation) {
+	}
+
+	public boolean isValid(String object, ConstraintValidatorContext constraintContext) {
+		return true;
+	}
+}


Property changes on: validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/CustomConstraintValidator.java
___________________________________________________________________
Name: svn:keywords
   + Id

Added: validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/FieldLevelValidationUsingInheritedValidator.java
===================================================================
--- validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/FieldLevelValidationUsingInheritedValidator.java	                        (rev 0)
+++ validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/FieldLevelValidationUsingInheritedValidator.java	2010-04-29 19:02:09 UTC (rev 19324)
@@ -0,0 +1,33 @@
+// $Id$
+/*
+* 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.inheritedvalidator;
+
+import java.util.Date;
+
+public class FieldLevelValidationUsingInheritedValidator {
+
+	@CustomConstraint
+	public String string;
+
+	/**
+	 * Not allowed.
+	 */
+	@CustomConstraint
+	public Date date;
+
+}


Property changes on: validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/inheritedvalidator/FieldLevelValidationUsingInheritedValidator.java
___________________________________________________________________
Name: svn:keywords
   + Id



More information about the hibernate-commits mailing list