Author: gunnar.morling
Date: 2010-08-08 13:58:31 -0400 (Sun, 08 Aug 2010)
New Revision: 20128
Added:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/AnnotationTypeMemberCheck.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintsWithWrongMessageAttribute.java
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/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/ConstraintsWithIllegalRetentionPolicies.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintsWithIllegalTargets.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintsWithoutValidator.java
Log:
HV-299: Implemented check, that each constraint annotation type defines a member
'String message()'
Added:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/AnnotationTypeMemberCheck.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/AnnotationTypeMemberCheck.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/AnnotationTypeMemberCheck.java 2010-08-08
17:58:31 UTC (rev 20128)
@@ -0,0 +1,102 @@
+// $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.checks;
+
+import java.util.Collections;
+import java.util.Set;
+import javax.lang.model.element.AnnotationMirror;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.util.Types;
+
+import org.hibernate.validator.ap.util.AnnotationApiHelper;
+import org.hibernate.validator.ap.util.CollectionHelper;
+
+import static javax.lang.model.util.ElementFilter.methodsIn;
+
+/**
+ * Checks, that each constraint annotation type declares the members message(), groups()
and payload().
+ *
+ * @author Gunnar Morling
+ */
+public class AnnotationTypeMemberCheck extends AbstractConstraintCheck {
+
+ private final AnnotationApiHelper annotationApiHelper;
+
+ private final Types typeUtils;
+
+ public AnnotationTypeMemberCheck(AnnotationApiHelper annotationApiHelper, Types
typeUtils) {
+
+ this.annotationApiHelper = annotationApiHelper;
+ this.typeUtils = typeUtils;
+ }
+
+ @Override
+ public Set<ConstraintCheckError> checkAnnotationType(TypeElement element,
AnnotationMirror annotation) {
+
+ Set<ConstraintCheckError> theValue = CollectionHelper.newHashSet();
+
+ theValue.addAll( checkMessageAttribute( element ) );
+ theValue.addAll( checkGroupsAttribute( element ) );
+ theValue.addAll( checkPayloadAttribute( element ) );
+
+ return theValue;
+ }
+
+ private Set<ConstraintCheckError> checkMessageAttribute(TypeElement element) {
+
+ ExecutableElement messageMethod = getMethod( element, "message" );
+
+ if ( messageMethod == null ) {
+ return CollectionHelper.asSet(
+ new ConstraintCheckError( element, null,
"CONSTRAINT_TYPE_MUST_DECLARE_MESSAGE_MEMBER" )
+ );
+ }
+
+ if ( !typeUtils.isSameType(
+ annotationApiHelper.getMirrorForType( String.class ), messageMethod.getReturnType()
+ ) ) {
+ return CollectionHelper.asSet(
+ new ConstraintCheckError( messageMethod, null,
"RETURN_TYPE_MUST_BE_STRING" )
+ );
+ }
+
+ return Collections.emptySet();
+ }
+
+ private Set<ConstraintCheckError> checkGroupsAttribute(TypeElement element) {
+ return Collections.emptySet();
+ }
+
+ private Set<ConstraintCheckError> checkPayloadAttribute(TypeElement element) {
+ return Collections.emptySet();
+ }
+
+ private ExecutableElement getMethod(TypeElement element, String name) {
+
+ for ( ExecutableElement oneMethod : methodsIn( element.getEnclosedElements() ) ) {
+
+ if ( oneMethod.getSimpleName().contentEquals( name ) ) {
+ return oneMethod;
+ }
+ }
+
+ return null;
+ }
+
+}
Property changes on:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/AnnotationTypeMemberCheck.java
___________________________________________________________________
Name: svn:keywords
+ Id
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
17:45:24 UTC (rev 20127)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheckFactory.java 2010-08-08
17:58:31 UTC (rev 20128)
@@ -108,7 +108,8 @@
new SingleValuedChecks(
new RetentionPolicyCheck( annotationApiHelper ),
new TargetCheck( annotationApiHelper ),
- new ConstraintValidatorCheck( constraintHelper, annotationApiHelper )
+ new ConstraintValidatorCheck( constraintHelper, annotationApiHelper ),
+ new AnnotationTypeMemberCheck( annotationApiHelper, typeUtils )
)
);
annotationTypeChecks.put( AnnotationType.NO_CONSTRAINT_ANNOTATION, NULL_CHECKS );
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
17:45:24 UTC (rev 20127)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/resources/org/hibernate/validator/ap/ValidationProcessorMessages.properties 2010-08-08
17:58:31 UTC (rev 20128)
@@ -13,4 +13,6 @@
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).
CONSTRAINT_TYPE_WITH_WRONG_TARGET=Constraint annotation types must have at least one of
the element types FIELD, METHOD, TYPE or ANNOTATION_TYPE as target.
-CONSTRAINT_TYPE_WITHOUT_VALIDATOR=For non-composed constraints a validator implementation
must be specified using @Constraint#validatedBy().
\ No newline at end of file
+CONSTRAINT_TYPE_WITHOUT_VALIDATOR=For non-composed constraints a validator implementation
must be specified using @Constraint#validatedBy().
+CONSTRAINT_TYPE_MUST_DECLARE_MESSAGE_MEMBER=Constraint annotation types must declare a
member 'String message()'.
+RETURN_TYPE_MUST_BE_STRING=Return type of constraint annotation type member
'message()' must be 'String'.
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
17:45:24 UTC (rev 20127)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/AnnotationTypeValidationTest.java 2010-08-08
17:58:31 UTC (rev 20128)
@@ -24,6 +24,7 @@
import
org.hibernate.validator.ap.testmodel.constrainttypes.ConstraintsWithIllegalRetentionPolicies;
import
org.hibernate.validator.ap.testmodel.constrainttypes.ConstraintsWithIllegalTargets;
+import
org.hibernate.validator.ap.testmodel.constrainttypes.ConstraintsWithWrongMessageAttribute;
import org.hibernate.validator.ap.testmodel.constrainttypes.ConstraintsWithoutValidator;
import org.hibernate.validator.ap.testmodel.constrainttypes.DummyValidator;
import org.hibernate.validator.ap.testmodel.constrainttypes.ValidCustomerNumber;
@@ -66,7 +67,7 @@
assertFalse( compilationResult );
assertThatDiagnosticsMatch(
diagnostics,
- new DiagnosticExpectation( Kind.ERROR, 33 ), new DiagnosticExpectation( Kind.ERROR,
42 )
+ new DiagnosticExpectation( Kind.ERROR, 34 ), new DiagnosticExpectation( Kind.ERROR,
49 )
);
}
@@ -82,7 +83,7 @@
assertFalse( compilationResult );
assertThatDiagnosticsMatch(
diagnostics,
- new DiagnosticExpectation( Kind.ERROR, 42 ), new DiagnosticExpectation( Kind.ERROR,
52 )
+ new DiagnosticExpectation( Kind.ERROR, 43 ), new DiagnosticExpectation( Kind.ERROR,
59 )
);
}
@@ -98,7 +99,23 @@
assertFalse( compilationResult );
assertThatDiagnosticsMatch(
diagnostics,
- new DiagnosticExpectation( Kind.ERROR, 34 )
+ new DiagnosticExpectation( Kind.ERROR, 35 )
);
}
+
+ @Test
+ public void
testThatConstraintAnnotationTypeWithMissingOrWrongMessageAttributeCausesCompilationError()
{
+
+ File sourceFile1 = compilerHelper.getSourceFile(
ConstraintsWithWrongMessageAttribute.class );
+ File sourceFile2 = compilerHelper.getSourceFile( DummyValidator.class );
+
+ boolean compilationResult =
+ compilerHelper.compile( new ConstraintValidationProcessor(), diagnostics,
sourceFile1, sourceFile2 );
+
+ assertFalse( compilationResult );
+ assertThatDiagnosticsMatch(
+ diagnostics,
+ new DiagnosticExpectation( Kind.ERROR, 35 ), new DiagnosticExpectation( Kind.ERROR,
50 )
+ );
+ }
}
Modified:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintsWithIllegalRetentionPolicies.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintsWithIllegalRetentionPolicies.java 2010-08-08
17:45:24 UTC (rev 20127)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintsWithIllegalRetentionPolicies.java 2010-08-08
17:58:31 UTC (rev 20128)
@@ -20,6 +20,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.validation.Constraint;
+import javax.validation.Payload;
/**
* @author Gunnar Morling
@@ -33,6 +34,12 @@
@Retention(RetentionPolicy.CLASS)
public @interface ConstraintWithWrongRetentionPolicy {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+
}
/**
@@ -41,6 +48,12 @@
@Constraint(validatedBy = { DummyValidator.class })
public @interface ConstraintWithoutRetentionPolicy {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+
}
/**
@@ -50,6 +63,12 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface ConstraintWithCorrectRetentionPolicy {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+
}
}
\ No newline at end of file
Modified:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintsWithIllegalTargets.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintsWithIllegalTargets.java 2010-08-08
17:45:24 UTC (rev 20127)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintsWithIllegalTargets.java 2010-08-08
17:58:31 UTC (rev 20128)
@@ -21,6 +21,7 @@
import java.lang.annotation.RetentionPolicy;
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;
@@ -42,6 +43,12 @@
@Target({ LOCAL_VARIABLE })
public @interface ConstraintWithWrongTarget {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+
}
/**
@@ -52,6 +59,12 @@
@Target({ })
public @interface ConstraintWithEmptyTarget {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+
}
/**
@@ -61,6 +74,12 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface ConstraintWithDefaultTarget {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+
}
/**
@@ -71,6 +90,12 @@
@Target({ FIELD })
public @interface ConstraintWithAllowedTargetField {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+
}
/**
@@ -81,6 +106,12 @@
@Target({ METHOD })
public @interface ConstraintWithAllowedTargetMethod {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+
}
/**
@@ -91,6 +122,12 @@
@Target({ TYPE })
public @interface ConstraintWithAllowedTargetType {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+
}
/**
@@ -101,6 +138,12 @@
@Target({ ANNOTATION_TYPE })
public @interface ConstraintWithAllowedTargetAnnotationType {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+
}
}
\ No newline at end of file
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintsWithWrongMessageAttribute.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintsWithWrongMessageAttribute.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintsWithWrongMessageAttribute.java 2010-08-08
17:58:31 UTC (rev 20128)
@@ -0,0 +1,73 @@
+// $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.constrainttypes;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+/**
+ * @author Gunnar Morling
+ */
+public interface ConstraintsWithWrongMessageAttribute {
+
+ /**
+ * Compilation error expected as no message() attribute is specified.
+ */
+ @Retention(RetentionPolicy.RUNTIME)
+ @Constraint(validatedBy = { DummyValidator.class })
+ public @interface ConstraintWithoutMessageAttribute {
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+
+ }
+
+ /**
+ * Compilation error expected as message() attribute doesn't have String as return
type.
+ */
+ @Retention(RetentionPolicy.RUNTIME)
+ @Constraint(validatedBy = { DummyValidator.class })
+ public @interface ConstraintWithMessageAttributeWithWrongReturnType {
+
+ public int message();
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+
+ }
+
+ /**
+ * No compilation error expected.
+ */
+ @Retention(RetentionPolicy.RUNTIME)
+ @Constraint(validatedBy = { DummyValidator.class })
+ public @interface ConstraintWithMessageAttribute {
+
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+
+ }
+
+}
Property changes on:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintsWithWrongMessageAttribute.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintsWithoutValidator.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintsWithoutValidator.java 2010-08-08
17:45:24 UTC (rev 20127)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/constrainttypes/ConstraintsWithoutValidator.java 2010-08-08
17:58:31 UTC (rev 20128)
@@ -20,6 +20,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.validation.Constraint;
+import javax.validation.Payload;
import javax.validation.constraints.Size;
/**
@@ -34,6 +35,12 @@
@Constraint(validatedBy = { })
public @interface ConstraintWithoutValidator {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+
}
/**
@@ -43,6 +50,12 @@
@Constraint(validatedBy = { DummyValidator.class })
public @interface ConstraintWithValidator {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+
}
/**
@@ -53,6 +66,12 @@
@Constraint(validatedBy = { })
public @interface ComposedConstraintWithoutValidator {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+
}
}
\ No newline at end of file