Author: epbernard
Date: 2009-05-04 13:06:34 -0400 (Mon, 04 May 2009)
New Revision: 16507
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/custom/
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/custom/BoundariesConstraintValidator.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/custom/CustomConstraintValidatorTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/custom/Positive.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/custom/PositiveConstraintValidator.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ValidatorTypeHelper.java
Log:
HV-146 ConstraintValidator impls cannot inherit from a super class implementing
ConstraintValidator
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ValidatorTypeHelper.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ValidatorTypeHelper.java 2009-05-04
15:34:08 UTC (rev 16506)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ValidatorTypeHelper.java 2009-05-04
17:06:34 UTC (rev 16507)
@@ -86,16 +86,8 @@
}
else if ( type instanceof Class ) {
Class clazz = ( Class ) type;
- Type returnedType = resolveTypes( resolvedTypes, clazz.getGenericSuperclass() );
- if ( returnedType != null ) {
- return returnedType;
- }
- for ( Type genericInterface : clazz.getGenericInterfaces() ) {
- returnedType = resolveTypes( resolvedTypes, genericInterface );
- if ( returnedType != null ) {
- return returnedType;
- }
- }
+ final Type returnedType = resolveTypeForClassAndHierarchy( resolvedTypes, clazz );
+ if ( returnedType != null) return returnedType;
}
else if ( type instanceof ParameterizedType ) {
ParameterizedType paramType = ( ParameterizedType ) type;
@@ -116,13 +108,25 @@
return type;
}
else {
- resolveTypes( resolvedTypes, rawType.getGenericSuperclass() );
- for ( Type genericInterface : rawType.getGenericInterfaces() ) {
- resolveTypes( resolvedTypes, genericInterface );
- }
+ Type returnedType = resolveTypeForClassAndHierarchy( resolvedTypes, rawType );
+ if ( returnedType != null) return returnedType;
}
}
//else we don't care I think
return null;
}
+
+ private static Type resolveTypeForClassAndHierarchy(Map<Type, Type> resolvedTypes,
Class<?> clazz) {
+ Type returnedType = resolveTypes( resolvedTypes, clazz.getGenericSuperclass() );
+ if ( returnedType != null ) {
+ return returnedType;
+ }
+ for ( Type genericInterface : clazz.getGenericInterfaces() ) {
+ returnedType = resolveTypes( resolvedTypes, genericInterface );
+ if ( returnedType != null ) {
+ return returnedType;
+ }
+ }
+ return null;
+ }
}
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/custom/BoundariesConstraintValidator.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/custom/BoundariesConstraintValidator.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/custom/BoundariesConstraintValidator.java 2009-05-04
17:06:34 UTC (rev 16507)
@@ -0,0 +1,22 @@
+package org.hibernate.validation.constraints.custom;
+
+import java.lang.annotation.Annotation;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public abstract class BoundariesConstraintValidator<T extends Annotation>
implements ConstraintValidator<T, Integer> {
+ private int low;
+ private int high;
+
+ protected void initialize(int low, int high) {
+ this.low = low;
+ this.high = high;
+ }
+
+ public boolean isValid(Integer value, ConstraintValidatorContext
constraintValidatorContext) {
+ return value >= low && value <= high;
+ }
+}
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/custom/CustomConstraintValidatorTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/custom/CustomConstraintValidatorTest.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/custom/CustomConstraintValidatorTest.java 2009-05-04
17:06:34 UTC (rev 16507)
@@ -0,0 +1,33 @@
+package org.hibernate.validation.constraints.custom;
+
+import java.util.Set;
+import javax.validation.Validation;
+import javax.validation.Validator;
+import javax.validation.ConstraintViolation;
+import javax.validation.PropertyDescriptor;
+
+import org.testng.annotations.Test;
+import static org.testng.Assert.*;
+
+/**
+ * @author Emmanuel Bernard
+ */
+
+public class CustomConstraintValidatorTest {
+
+ @Test
+ public void testInheritedConstraintValidationImpl() {
+ Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
+ Phone p = new Phone();
+ p.size = -2;
+ final PropertyDescriptor propertyDescriptor = validator.getConstraintsForClass(
Phone.class )
+ .getConstraintsForProperty( "size" );
+ assertNotNull( propertyDescriptor );
+ final Set<ConstraintViolation<Phone>> constraintViolations =
validator.validate( p );
+ assertEquals( 1, constraintViolations.size() );
+ }
+
+ public static class Phone {
+ @Positive public int size;
+ }
+}
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/custom/Positive.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/custom/Positive.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/custom/Positive.java 2009-05-04
17:06:34 UTC (rev 16507)
@@ -0,0 +1,22 @@
+package org.hibernate.validation.constraints.custom;
+
+import java.lang.annotation.Target;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Documented;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import javax.validation.Constraint;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Constraint( validatedBy = { PositiveConstraintValidator.class })
+@Target({ METHOD, FIELD, ANNOTATION_TYPE })
+@Retention(RUNTIME)
+@Documented
+public @interface Positive {
+ String message() default "{validation.positive}";
+ Class<?>[] groups() default {};
+}
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/custom/PositiveConstraintValidator.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/custom/PositiveConstraintValidator.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/constraints/custom/PositiveConstraintValidator.java 2009-05-04
17:06:34 UTC (rev 16507)
@@ -0,0 +1,10 @@
+package org.hibernate.validation.constraints.custom;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class PositiveConstraintValidator extends
BoundariesConstraintValidator<Positive> {
+ public void initialize(Positive constraintAnnotation) {
+ super.initialize( 0, Integer.MAX_VALUE );
+ }
+}
Show replies by date