Author: gunnar.morling
Date: 2010-04-27 18:51:17 -0400 (Tue, 27 Apr 2010)
New Revision: 19310
Added:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/AbstractConstraintCheck.java
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/AnnotationTypeCheck.java
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheck.java
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheckError.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/checks/ConstraintChecks.java
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/GetterCheck.java
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/MultiValuedChecks.java
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/PrimitiveCheck.java
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/SingleValuedChecks.java
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/StaticCheck.java
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/TypeCheck.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/MessagerAdapter.java
Log:
HV-316: extracted check methods from ConstraintAnnotationVisitor
* Created new IF ConstraintCheck
* Converted all check methods from the visitor into classes implementing ConstraintCheck
* Created class ConstraintCheckFactory, that knows which checks are required for which
element/annotation combinations
* Visitor retrieves all checks to be executed from factory, executes them and reports any
errors
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-27
08:40:55 UTC (rev 19309)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintAnnotationVisitor.java 2010-04-27
22:51:17 UTC (rev 19310)
@@ -18,27 +18,31 @@
package org.hibernate.validator.ap;
import java.util.List;
+import java.util.Set;
+import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementVisitor;
import javax.lang.model.element.ExecutableElement;
-import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
-import javax.lang.model.type.TypeKind;
-import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.ElementKindVisitor6;
+import javax.tools.Diagnostic.Kind;
+import org.hibernate.validator.ap.checks.ConstraintCheck;
+import org.hibernate.validator.ap.checks.ConstraintCheckError;
+import org.hibernate.validator.ap.checks.ConstraintCheckFactory;
+import org.hibernate.validator.ap.checks.ConstraintChecks;
import org.hibernate.validator.ap.util.AnnotationApiHelper;
import org.hibernate.validator.ap.util.ConstraintHelper;
-import org.hibernate.validator.ap.util.ConstraintHelper.ConstraintCheckResult;
import org.hibernate.validator.ap.util.MessagerAdapter;
/**
- * An {@link ElementVisitor} that visits elements (type declarations, methods
- * and fields) annotated with constraint annotations from the Bean Validation
- * API.
+ * An {@link ElementVisitor} that visits annotated elements (type declarations,
+ * methods and fields) and applies different {@link ConstraintCheck}s to them.
+ * Each {@link ConstraintCheckError} occurred will be reported using the
+ * {@link Messager} API.
*
* @author Gunnar Morling.
*/
@@ -48,6 +52,11 @@
private final MessagerAdapter messager;
+ private ConstraintCheckFactory constraintCheckFactory;
+
+ //TODO GM: establish processor option for this
+ private boolean verbose = true;
+
public ConstraintAnnotationVisitor(ProcessingEnvironment processingEnvironment,
MessagerAdapter messager) {
this.messager = messager;
@@ -59,317 +68,156 @@
constraintHelper = new ConstraintHelper(
processingEnvironment.getElementUtils(), processingEnvironment.getTypeUtils(),
annotationApiHelper
);
+
+ constraintCheckFactory = new ConstraintCheckFactory( constraintHelper );
}
/**
* <p>
- * Checks whether the given mirrors representing one or more constraint annotations are
correctly
- * specified at the given method. The following checks are performed:</p>
+ * Checks whether the given annotations are correctly specified at the given
+ * method. The following checks are performed:
+ * </p>
* <ul>
* <li>
- * The method must be a JavaBeans getter method (name starts with "is",
"get" or "has",
- * method has return type, but no parameters).
- * </li>
+ * Constraint annotations may only be given at non-static, JavaBeans getter
+ * methods which's return type is supported by the constraints.</li>
* <li>
- * The return type of the method must be supported by the constraints.
- * </li>
- * <li>
- * The method must not be static.
- * </li>
+ * The <code>@Valid</code> annotation may only be given at non-static,
+ * non-primitive JavaBeans getter methods.</li>
* </ul>
*/
@Override
public Void visitExecutableAsMethod(ExecutableElement method,
List<AnnotationMirror> mirrors) {
- for ( AnnotationMirror oneAnnotationMirror : mirrors ) {
+ checkConstraints( method, mirrors );
-
- switch ( constraintHelper.getAnnotationType( oneAnnotationMirror ) ) {
-
- case CONSTRAINT_ANNOTATION:
- checkConstraintAtMethod( method, oneAnnotationMirror );
- break;
-
- case MULTI_VALUED_CONSTRAINT_ANNOTATION:
- for ( AnnotationMirror onePartOfMultiValuedConstraint :
- constraintHelper.getPartsOfMultiValuedConstraint( oneAnnotationMirror ) ) {
-
- checkConstraintAtMethod( method, onePartOfMultiValuedConstraint );
- }
- break;
-
- case GRAPH_VALIDATION_ANNOTATION:
- checkGraphValidationAnnotationAtMethod( method, oneAnnotationMirror );
- }
-
- }
-
return null;
}
/**
* <p>
- * Checks whether the given mirrors representing one or more constraint annotations are
correctly
- * specified at the given field. The following checks are performed:</p>
+ * Checks whether the given annotations are correctly specified at the given
+ * field. The following checks are performed:
+ * </p>
* <ul>
* <li>
- * The type of the field must be supported by the constraints.
- * </li>
+ * Constraint annotations may only be given at non-static fields which's
+ * type is supported by the constraints.</li>
* <li>
- * The field must not be static.
- * </li>
+ * The <code>@Valid</code> annotation may only be given at non-static,
+ * non-primitive fields.</li>
* </ul>
*/
@Override
public Void visitVariableAsField(VariableElement annotatedField,
List<AnnotationMirror> mirrors) {
- for ( AnnotationMirror oneAnnotationMirror : mirrors ) {
+ checkConstraints( annotatedField, mirrors );
- switch ( constraintHelper.getAnnotationType( oneAnnotationMirror ) ) {
-
- case CONSTRAINT_ANNOTATION:
- checkConstraintAtField( annotatedField, oneAnnotationMirror );
- break;
-
- case MULTI_VALUED_CONSTRAINT_ANNOTATION:
- for ( AnnotationMirror onePartOfMultiValuedConstraint :
- constraintHelper.getPartsOfMultiValuedConstraint( oneAnnotationMirror ) ) {
-
- checkConstraintAtField( annotatedField, onePartOfMultiValuedConstraint );
- }
- break;
-
- case GRAPH_VALIDATION_ANNOTATION:
- checkGraphValidationAnnotationAtField( annotatedField, oneAnnotationMirror );
- }
- }
-
return null;
}
/**
* <p>
- * Checks whether the given mirrors representing one or more constraint
- * annotations are correctly specified at the given annotation type
- * declaration. The only annotation types allowed to be annotated with a
- * constraint annotation are other (composed) constraint annotation type
- * declarations.
+ * Checks whether the given annotations are correctly specified at the given
+ * annotation type declaration. The following checks are performed:
* </p>
+ * <ul>
+ * <li>
+ * The only annotation types allowed to be annotated with other constraint
+ * annotations are composed constraint annotation type declarations.</li>
+ * </ul>
*/
@Override
public Void visitTypeAsAnnotationType(TypeElement annotationType,
List<AnnotationMirror> mirrors) {
- for ( AnnotationMirror oneAnnotationMirror : mirrors ) {
+ checkConstraints( annotationType, mirrors );
- switch ( constraintHelper.getAnnotationType( oneAnnotationMirror ) ) {
-
- case CONSTRAINT_ANNOTATION:
- checkConstraintAtAnnotationType( annotationType, oneAnnotationMirror );
- break;
-
- case MULTI_VALUED_CONSTRAINT_ANNOTATION:
- for ( AnnotationMirror onePartOfMultiValuedConstraint :
- constraintHelper.getPartsOfMultiValuedConstraint( oneAnnotationMirror ) ) {
-
- checkConstraintAtAnnotationType( annotationType, onePartOfMultiValuedConstraint );
- }
- break;
- }
- }
-
return null;
}
+ /**
+ * <p>
+ * Checks whether the given annotations are correctly specified at the given
+ * class type declaration. The following checks are performed:
+ * </p>
+ * <ul>
+ * <li>
+ * Constraint annotations may at types supported by the constraints.</li>
+ * <li>
+ * </ul>
+ */
@Override
public Void visitTypeAsClass(TypeElement e, List<AnnotationMirror> p) {
- return visitClassOrInterfaceOrEnumType( e, p );
+
+ checkConstraints( e, p );
+ return null;
}
+ /**
+ * <p>
+ * Checks whether the given annotations are correctly specified at the given
+ * enum type declaration. The following checks are performed:
+ * </p>
+ * <ul>
+ * <li>
+ * Constraint annotations may at types supported by the constraints.</li>
+ * <li>
+ * </ul>
+ */
@Override
public Void visitTypeAsEnum(TypeElement e, List<AnnotationMirror> p) {
- return visitClassOrInterfaceOrEnumType( e, p );
+
+ checkConstraints( e, p );
+ return null;
}
+ /**
+ * <p>
+ * Checks whether the given annotations are correctly specified at the given
+ * interface type declaration. The following checks are performed:
+ * </p>
+ * <ul>
+ * <li>
+ * Constraint annotations may at types supported by the constraints.</li>
+ * <li>
+ * </ul>
+ */
@Override
public Void visitTypeAsInterface(TypeElement e, List<AnnotationMirror> p) {
- return visitClassOrInterfaceOrEnumType( e, p );
+
+ checkConstraints( e, p );
+ return null;
}
- // ==================================
- // private API below
- // ==================================
+ /**
+ * Retrieves the checks required for the given element and annotations,
+ * executes them and reports all occurred errors.
+ *
+ * @param annotatedElement The element to check.
+ * @param mirrors The annotations to check.
+ */
+ private void checkConstraints(Element annotatedElement, List<AnnotationMirror>
mirrors) {
- private Void visitClassOrInterfaceOrEnumType(TypeElement annotatedType,
- List<AnnotationMirror> mirrors) {
-
for ( AnnotationMirror oneAnnotationMirror : mirrors ) {
- switch ( constraintHelper.getAnnotationType( oneAnnotationMirror ) ) {
+ try {
- case CONSTRAINT_ANNOTATION:
- checkConstraintAtType( annotatedType, oneAnnotationMirror );
- break;
-
- case MULTI_VALUED_CONSTRAINT_ANNOTATION:
- for ( AnnotationMirror onePartOfMultiValuedConstraint :
- constraintHelper.getPartsOfMultiValuedConstraint( oneAnnotationMirror ) ) {
- checkConstraintAtType( annotatedType, onePartOfMultiValuedConstraint );
- }
- break;
+ ConstraintChecks constraintChecks = constraintCheckFactory.getConstraintChecks(
+ annotatedElement, oneAnnotationMirror
+ );
+ Set<ConstraintCheckError> errors = constraintChecks.execute( annotatedElement,
oneAnnotationMirror );
+ messager.reportErrors( errors );
}
+ catch ( Exception e ) {
+ if ( verbose ) {
+ messager.getDelegate()
+ .printMessage( Kind.NOTE, e.getMessage(), annotatedElement, oneAnnotationMirror
);
+ }
+ }
}
-
- return null;
}
- private void checkConstraintAtType(TypeElement annotatedType, AnnotationMirror mirror)
{
-
- if ( constraintHelper.checkConstraint(
- mirror.getAnnotationType(), annotatedType.asType()
- ) != ConstraintCheckResult.ALLOWED ) {
-
- messager.reportError(
- annotatedType, mirror, "NOT_SUPPORTED_TYPE",
- mirror.getAnnotationType().asElement().getSimpleName()
- );
- }
- }
-
- private void checkConstraintAtField(VariableElement annotatedField, AnnotationMirror
annotationMirror) {
-
- if ( isStaticElement( annotatedField ) ) {
-
- messager.reportError( annotatedField, annotationMirror,
"STATIC_FIELDS_MAY_NOT_BE_ANNOTATED" );
-
- return;
- }
-
- if ( constraintHelper.checkConstraint(
- annotationMirror.getAnnotationType(), annotatedField.asType()
- ) != ConstraintCheckResult.ALLOWED ) {
-
- messager.reportError(
- annotatedField, annotationMirror, "NOT_SUPPORTED_TYPE",
- annotationMirror.getAnnotationType().asElement().getSimpleName()
- );
- }
- }
-
- private void checkConstraintAtMethod(ExecutableElement method, AnnotationMirror mirror)
{
-
- if ( !isGetterMethod( method ) ) {
-
- messager.reportError( method, mirror, "ONLY_GETTERS_MAY_BE_ANNOTATED" );
-
- return;
- }
-
- if ( isStaticElement( method ) ) {
-
- messager.reportError( method, mirror, "STATIC_METHODS_MAY_NOT_BE_ANNOTATED"
);
-
- return;
- }
-
- if ( constraintHelper.checkConstraint(
- mirror.getAnnotationType(), method.getReturnType()
- ) != ConstraintCheckResult.ALLOWED ) {
-
- messager.reportError(
- method, mirror, "NOT_SUPPORTED_RETURN_TYPE",
- mirror.getAnnotationType().asElement().getSimpleName()
- );
- }
- }
-
- private void checkConstraintAtAnnotationType(TypeElement annotationType,
AnnotationMirror annotationMirror) {
-
- if ( !constraintHelper.isConstraintAnnotation( annotationType ) ) {
- messager.reportError( annotationType, annotationMirror,
"ONLY_CONSTRAINT_ANNOTATIONS_MAY_BE_ANNOTATED" );
- }
-
- }
-
- private void checkGraphValidationAnnotationAtField(
- VariableElement annotatedField, AnnotationMirror annotationMirror) {
-
- if ( isStaticElement( annotatedField ) ) {
-
- messager.reportError(
- annotatedField, annotationMirror,
- "STATIC_FIELDS_MAY_NOT_BE_ANNOTATED"
- );
-
- return;
- }
-
- if ( isPrimitiveType( annotatedField.asType() ) ) {
-
- messager.reportError(
- annotatedField, annotationMirror,
- "ATVALID_NOT_ALLOWED_AT_PRIMITIVE_FIELD"
- );
- }
- }
-
- private void checkGraphValidationAnnotationAtMethod(
- ExecutableElement method, AnnotationMirror annotationMirror) {
-
- if ( !isGetterMethod( method ) ) {
-
- messager.reportError(
- method, annotationMirror,
- "ONLY_GETTERS_MAY_BE_ANNOTATED"
- );
-
- return;
- }
-
- if ( isStaticElement( method ) ) {
-
- messager.reportError(
- method, annotationMirror,
- "STATIC_METHODS_MAY_NOT_BE_ANNOTATED"
- );
-
- return;
- }
-
- if ( isPrimitiveType( method.getReturnType() ) ) {
-
- messager.reportError(
- method, annotationMirror,
- "ATVALID_NOT_ALLOWED_AT_METHOD_RETURNING_PRIMITIVE_TYPE"
- );
- }
- }
-
- private boolean isGetterMethod(ExecutableElement method) {
- return isJavaBeanGetterName( method.getSimpleName().toString() )
- && !hasParameters( method ) && hasReturnValue( method );
- }
-
- private boolean hasReturnValue(ExecutableElement method) {
- return method.getReturnType().getKind() != TypeKind.VOID;
- }
-
- private boolean hasParameters(ExecutableElement method) {
- return !method.getParameters().isEmpty();
- }
-
- private boolean isJavaBeanGetterName(String methodName) {
- return methodName.startsWith( "is" ) || methodName.startsWith(
"has" ) || methodName.startsWith( "get" );
- }
-
- private boolean isStaticElement(Element element) {
- return element.getModifiers().contains( Modifier.STATIC );
- }
-
- private boolean isPrimitiveType(TypeMirror typeMirror) {
- return typeMirror.getKind().isPrimitive();
- }
-
}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/AbstractConstraintCheck.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/AbstractConstraintCheck.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/AbstractConstraintCheck.java 2010-04-27
22:51:17 UTC (rev 19310)
@@ -0,0 +1,67 @@
+// $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.element.VariableElement;
+
+/**
+ * <p>
+ * Abstract base class for {@link ConstraintCheck} implementations. Concrete
+ * checks should only override those check methods applicable for their
+ * supported element types.
+ * </p>
+ * <p>
+ * For instance would a check ensuring that constraint annotations are only
+ * given at non-static fields or methods only override
<code>checkField()</code>
+ * and <code>checkMethod()</code>.
+ * </p>
+ * <p>
+ * All check methods not overridden will return an empty list.
+ * </p>
+ *
+ * @author Gunnar Morling
+ */
+public class AbstractConstraintCheck implements ConstraintCheck {
+
+ public Set<ConstraintCheckError> checkField(VariableElement element,
AnnotationMirror annotation) {
+
+ return Collections.emptySet();
+ }
+
+ public Set<ConstraintCheckError> checkMethod(ExecutableElement element,
AnnotationMirror annotation) {
+
+ return Collections.emptySet();
+ }
+
+ public Set<ConstraintCheckError> checkAnnotationType(TypeElement element,
+ AnnotationMirror annotation) {
+
+ return Collections.emptySet();
+ }
+
+ public Set<ConstraintCheckError> checkNonAnnotationType(
+ TypeElement element, AnnotationMirror annotation) {
+
+ return Collections.emptySet();
+ }
+}
Property changes on:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/AbstractConstraintCheck.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/AnnotationTypeCheck.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/AnnotationTypeCheck.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/AnnotationTypeCheck.java 2010-04-27
22:51:17 UTC (rev 19310)
@@ -0,0 +1,60 @@
+// $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.TypeElement;
+
+import org.hibernate.validator.ap.util.CollectionHelper;
+import org.hibernate.validator.ap.util.ConstraintHelper;
+
+/**
+ * Checks, that only constraint annotation types are annotated with other
+ * constraint annotations ("constraint composition"), but not non-constraint
+ * annotations.
+ *
+ * @author Gunnar Morling
+ */
+public class AnnotationTypeCheck extends AbstractConstraintCheck {
+
+ private final ConstraintHelper constraintHelper;
+
+ public AnnotationTypeCheck(ConstraintHelper constraintHelper) {
+ this.constraintHelper = constraintHelper;
+ }
+
+ @Override
+ public Set<ConstraintCheckError> checkAnnotationType(TypeElement element,
+ AnnotationMirror annotation) {
+
+ if ( !constraintHelper.isConstraintAnnotation( element ) ) {
+
+ return CollectionHelper.asSet(
+ new ConstraintCheckError(
+ element, annotation, "ONLY_CONSTRAINT_ANNOTATIONS_MAY_BE_ANNOTATED"
+ )
+ );
+ }
+
+ return Collections.emptySet();
+ }
+
+
+}
Property changes on:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/AnnotationTypeCheck.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheck.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheck.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheck.java 2010-04-27
22:51:17 UTC (rev 19310)
@@ -0,0 +1,99 @@
+// $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.Set;
+import javax.lang.model.element.AnnotationMirror;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.element.VariableElement;
+
+/**
+ * <p>
+ * Implementations represent checks, which determine whether a given constraint
+ * annotation is allowed at a given element.
+ * </p>
+ * <p>
+ * Implementations should be derived from {@link AbstractConstraintCheck} in
+ * order to implement only those check methods applicable for the element kinds
+ * supported by the check.
+ * </p>
+ *
+ * @author Gunnar Morling
+ */
+public interface ConstraintCheck {
+
+ /**
+ * Checks, whether the given annotation is allowed at the given field.
+ *
+ * @param element An annotated field.
+ * @param annotation An annotation at that field.
+ *
+ * @return A set with errors, that describe, why the given annotation is
+ * not allowed at the given element. In case no errors occur (the
+ * given annotation is allowed at the given element), an empty set
+ * must be returned.
+ */
+ Set<ConstraintCheckError> checkField(VariableElement element,
+ AnnotationMirror annotation);
+
+ /**
+ * Checks, whether the given annotation is allowed at the given method.
+ *
+ * @param element An annotated method.
+ * @param annotation An annotation at that method.
+ *
+ * @return A set with errors, that describe, why the given annotation is
+ * not allowed at the given element. In case no errors occur (the
+ * given annotation is allowed at the given element), an empty set
+ * must be returned.
+ */
+ Set<ConstraintCheckError> checkMethod(ExecutableElement element,
+ AnnotationMirror annotation);
+
+ /**
+ * Checks, whether the given annotation is allowed at the given annotation
+ * type declaration.
+ *
+ * @param element An annotated annotation type declaration.
+ * @param annotation An annotation at that annotation type.
+ *
+ * @return A set with errors, that describe, why the given annotation is
+ * not allowed at the given element. In case no errors occur (the
+ * given annotation is allowed at the given element), an empty set
+ * must be returned.
+ */
+ Set<ConstraintCheckError> checkAnnotationType(TypeElement element,
+ AnnotationMirror annotation);
+
+ /**
+ * Checks, whether the given annotation is allowed at the given type
+ * declaration (class, interface, enum).
+ *
+ * @param element An annotated type declaration.
+ * @param annotation An annotation at that type.
+ *
+ * @return A set with errors, that describe, why the given annotation is
+ * not allowed at the given element. In case no errors occur (the
+ * given annotation is allowed at the given element), an empty set
+ * must be returned.
+ */
+ Set<ConstraintCheckError> checkNonAnnotationType(TypeElement element,
+ AnnotationMirror annotation);
+
+}
Property changes on:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheck.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheckError.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheckError.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheckError.java 2010-04-27
22:51:17 UTC (rev 19310)
@@ -0,0 +1,143 @@
+// $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.text.MessageFormat;
+import java.util.Arrays;
+import javax.lang.model.element.AnnotationMirror;
+import javax.lang.model.element.Element;
+
+/**
+ * The result of the execution of a {@link ConstraintCheck}. Comprises
+ * information about the location at which the error occurred and a message
+ * describing the occured error.
+ *
+ * @author Gunnar Morling
+ */
+public class ConstraintCheckError {
+
+ private final Element element;
+
+ private final AnnotationMirror annotationMirror;
+
+ private final String messageKey;
+
+ private final Object[] messageParameters;
+
+ /**
+ * Creates a new ConstraintCheckError.
+ *
+ * @param element The element at which the error occurred.
+ * @param annotationMirror The annotation that causes the error.
+ * @param messageKey A key for retrieving an error message template from the bundle
+ * <p/>
+ * <code>org.hibernate.validator.ap.ValidationProcessorMessages.</code>
+ * @param messageParameters An array with values to put into the error message template
+ * using {@link MessageFormat}. The number of elements must match
+ * the number of place holders in the message template.
+ */
+ public ConstraintCheckError(Element element,
+ AnnotationMirror annotationMirror, String messageKey, Object...
messageParameters) {
+
+ this.element = element;
+ this.annotationMirror = annotationMirror;
+ this.messageKey = messageKey;
+ this.messageParameters = messageParameters;
+ }
+
+ public Element getElement() {
+ return element;
+ }
+
+ public AnnotationMirror getAnnotationMirror() {
+ return annotationMirror;
+ }
+
+ public String getMessageKey() {
+ return messageKey;
+ }
+
+ public Object[] getMessageParameters() {
+ return messageParameters;
+ }
+
+ @Override
+ public String toString() {
+ return "ConstraintCheckError [annotationMirror=" + annotationMirror
+ + ", element=" + element + ", messageKey=" + messageKey
+ + ", messageParameters=" + Arrays.toString( messageParameters )
+ + "]";
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime
+ * result
+ + ( ( annotationMirror == null ) ? 0 : annotationMirror.hashCode() );
+ result = prime * result + ( ( element == null ) ? 0 : element.hashCode() );
+ result = prime * result
+ + ( ( messageKey == null ) ? 0 : messageKey.hashCode() );
+ result = prime * result + Arrays.hashCode( messageParameters );
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if ( this == obj ) {
+ return true;
+ }
+ if ( obj == null ) {
+ return false;
+ }
+ if ( getClass() != obj.getClass() ) {
+ return false;
+ }
+ ConstraintCheckError other = ( ConstraintCheckError ) obj;
+ if ( annotationMirror == null ) {
+ if ( other.annotationMirror != null ) {
+ return false;
+ }
+ }
+ else if ( !annotationMirror.equals( other.annotationMirror ) ) {
+ return false;
+ }
+ if ( element == null ) {
+ if ( other.element != null ) {
+ return false;
+ }
+ }
+ else if ( !element.equals( other.element ) ) {
+ return false;
+ }
+ if ( messageKey == null ) {
+ if ( other.messageKey != null ) {
+ return false;
+ }
+ }
+ else if ( !messageKey.equals( other.messageKey ) ) {
+ return false;
+ }
+ if ( !Arrays.equals( messageParameters, other.messageParameters ) ) {
+ return false;
+ }
+ return true;
+ }
+
+}
Property changes on:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheckError.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added:
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
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheckFactory.java 2010-04-27
22:51:17 UTC (rev 19310)
@@ -0,0 +1,150 @@
+// $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.Map;
+import javax.lang.model.element.AnnotationMirror;
+import javax.lang.model.element.Element;
+
+import org.hibernate.validator.ap.util.CollectionHelper;
+import org.hibernate.validator.ap.util.ConstraintHelper;
+import org.hibernate.validator.ap.util.ConstraintHelper.AnnotationType;
+
+/**
+ * A factory in charge of determining the {@link ConstraintCheck}s required for
+ * the validation of annotations at given elements.
+ *
+ * @author Gunnar Morling
+ */
+public class ConstraintCheckFactory {
+
+ /**
+ * Holds the checks to be executed for field elements.
+ */
+ private final Map<AnnotationType, ConstraintChecks> fieldChecks;
+
+ /**
+ * Holds the checks to be executed for method elements keyed.
+ */
+ private final Map<AnnotationType, ConstraintChecks> methodChecks;
+
+ /**
+ * Holds the checks to be executed for annotation type declarations.
+ */
+ private final Map<AnnotationType, ConstraintChecks> annotationTypeChecks;
+
+ /**
+ * Holds the checks to be executed for class/interface/enum declarations.
+ */
+ private final Map<AnnotationType, ConstraintChecks> nonAnnotationTypeChecks;
+
+ private ConstraintHelper constraintHelper;
+
+ private final static SingleValuedChecks NULL_CHECKS = new SingleValuedChecks();
+
+ public ConstraintCheckFactory(ConstraintHelper constraintHelper) {
+
+ this.constraintHelper = constraintHelper;
+
+ fieldChecks = CollectionHelper.newHashMap();
+ fieldChecks.put(
+ AnnotationType.CONSTRAINT_ANNOTATION,
+ new SingleValuedChecks( new StaticCheck(), new TypeCheck( constraintHelper ) )
+ );
+ fieldChecks.put(
+ AnnotationType.MULTI_VALUED_CONSTRAINT_ANNOTATION,
+ new MultiValuedChecks( constraintHelper, new StaticCheck(), new TypeCheck(
constraintHelper ) )
+ );
+ fieldChecks.put(
+ AnnotationType.GRAPH_VALIDATION_ANNOTATION,
+ new SingleValuedChecks( new StaticCheck(), new PrimitiveCheck() )
+ );
+ fieldChecks.put( AnnotationType.NO_CONSTRAINT_ANNOTATION, NULL_CHECKS );
+
+ methodChecks = CollectionHelper.newHashMap();
+ methodChecks.put(
+ AnnotationType.CONSTRAINT_ANNOTATION,
+ new SingleValuedChecks( new GetterCheck(), new StaticCheck(), new TypeCheck(
constraintHelper ) )
+ );
+ methodChecks.put(
+ AnnotationType.MULTI_VALUED_CONSTRAINT_ANNOTATION, new MultiValuedChecks(
+ constraintHelper, new GetterCheck(), new StaticCheck(), new TypeCheck(
constraintHelper )
+ )
+ );
+ methodChecks.put(
+ AnnotationType.GRAPH_VALIDATION_ANNOTATION,
+ new SingleValuedChecks( new GetterCheck(), new StaticCheck(), new PrimitiveCheck() )
+ );
+ methodChecks.put( AnnotationType.NO_CONSTRAINT_ANNOTATION, NULL_CHECKS );
+
+ annotationTypeChecks = CollectionHelper.newHashMap();
+ annotationTypeChecks.put(
+ AnnotationType.CONSTRAINT_ANNOTATION,
+ new SingleValuedChecks( new AnnotationTypeCheck( constraintHelper ) )
+ );
+ annotationTypeChecks.put(
+ AnnotationType.MULTI_VALUED_CONSTRAINT_ANNOTATION,
+ new MultiValuedChecks( constraintHelper, new AnnotationTypeCheck( constraintHelper )
)
+ );
+ annotationTypeChecks.put( AnnotationType.NO_CONSTRAINT_ANNOTATION, NULL_CHECKS );
+
+ nonAnnotationTypeChecks = CollectionHelper.newHashMap();
+ nonAnnotationTypeChecks.put(
+ AnnotationType.CONSTRAINT_ANNOTATION, new SingleValuedChecks( new TypeCheck(
constraintHelper ) )
+ );
+ nonAnnotationTypeChecks.put(
+ AnnotationType.MULTI_VALUED_CONSTRAINT_ANNOTATION,
+ new MultiValuedChecks( constraintHelper, new TypeCheck( constraintHelper ) )
+ );
+ nonAnnotationTypeChecks.put( AnnotationType.NO_CONSTRAINT_ANNOTATION, NULL_CHECKS );
+ }
+
+ /**
+ * Returns those checks that have to be performed to validate the given
+ * annotation at the given element. In case no checks have to be performed
+ * (e.g. because the given annotation is no constraint annotation) an empty
+ * {@link ConstraintChecks} instance will be returned. It's therefore always
+ * safe to operate on the returned object.
+ *
+ * @param annotatedElement An annotated element, e.g. a type declaration or a method.
+ * @param annotation An annotation.
+ *
+ * @return The checks to be performed to validate the given annotation at
+ * the given element.
+ */
+ public ConstraintChecks getConstraintChecks(Element annotatedElement, AnnotationMirror
annotation) {
+
+ AnnotationType annotationType = constraintHelper.getAnnotationType( annotation );
+
+ switch ( annotatedElement.getKind() ) {
+ case FIELD:
+ return fieldChecks.get( annotationType );
+ case METHOD:
+ return methodChecks.get( annotationType );
+ case ANNOTATION_TYPE:
+ return annotationTypeChecks.get( annotationType );
+ case CLASS:
+ case INTERFACE:
+ case ENUM:
+ return nonAnnotationTypeChecks.get( annotationType );
+ default:
+ return NULL_CHECKS;
+ }
+ }
+
+}
Property changes on:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheckFactory.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintChecks.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintChecks.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintChecks.java 2010-04-27
22:51:17 UTC (rev 19310)
@@ -0,0 +1,45 @@
+// $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.Set;
+import javax.lang.model.element.AnnotationMirror;
+import javax.lang.model.element.Element;
+
+/**
+ * Represents an ordered set of {@link ConstraintCheck}s with the ability
+ * to execute these checks against given elements and their annotations.
+ *
+ * @author Gunnar Morling
+ */
+public interface ConstraintChecks {
+
+ /**
+ * Executes the checks contained within this set against the given element
+ * and annotation.
+ *
+ * @param element An annotated element.
+ * @param annotation The annotation to check.
+ *
+ * @return A set with errors. Will be empty in case all checks passed
+ * successfully.
+ */
+ Set<ConstraintCheckError> execute(Element element,
+ AnnotationMirror annotation);
+
+}
\ No newline at end of file
Property changes on:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintChecks.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/GetterCheck.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/GetterCheck.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/GetterCheck.java 2010-04-27
22:51:17 UTC (rev 19310)
@@ -0,0 +1,69 @@
+// $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.type.TypeKind;
+
+import org.hibernate.validator.ap.util.CollectionHelper;
+
+/**
+ * Checks whether a given element is a valid getter method.
+ *
+ * @author Gunnar Morling
+ */
+public class GetterCheck extends AbstractConstraintCheck {
+
+ @Override
+ public Set<ConstraintCheckError> checkMethod(ExecutableElement element,
+ AnnotationMirror annotation) {
+
+ if ( !isGetterMethod( element ) ) {
+ return CollectionHelper.asSet(
+ new ConstraintCheckError(
+ element, annotation, "ONLY_GETTERS_MAY_BE_ANNOTATED"
+ )
+ );
+ }
+
+ return Collections.emptySet();
+ }
+
+ private boolean isGetterMethod(ExecutableElement method) {
+
+ return isJavaBeanGetterName( method.getSimpleName().toString() )
+ && !hasParameters( method ) && hasReturnValue( method );
+ }
+
+ private boolean hasReturnValue(ExecutableElement method) {
+ return method.getReturnType().getKind() != TypeKind.VOID;
+ }
+
+ private boolean hasParameters(ExecutableElement method) {
+ return !method.getParameters().isEmpty();
+ }
+
+ private boolean isJavaBeanGetterName(String methodName) {
+ return methodName.startsWith( "is" ) || methodName.startsWith(
"has" ) || methodName.startsWith( "get" );
+ }
+
+}
+
Property changes on:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/GetterCheck.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/MultiValuedChecks.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/MultiValuedChecks.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/MultiValuedChecks.java 2010-04-27
22:51:17 UTC (rev 19310)
@@ -0,0 +1,67 @@
+// $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.Set;
+import javax.lang.model.element.AnnotationMirror;
+import javax.lang.model.element.Element;
+
+import org.hibernate.validator.ap.util.CollectionHelper;
+import org.hibernate.validator.ap.util.ConstraintHelper;
+
+/**
+ * A {@link ConstraintChecks} implementation, that executed the contained checks
+ * against all parts of given multi-valued annotations.
+ *
+ * @author Gunnar Morling
+ */
+public class MultiValuedChecks implements ConstraintChecks {
+
+ private final ConstraintHelper constraintHelper;
+
+ private final SingleValuedChecks delegate;
+
+ /**
+ * Creates a new MultiValuedChecks.
+ *
+ * @param constraintHelper Helper for handling multi-valued constraints.
+ * @param checks The checks to execute.
+ */
+ public MultiValuedChecks(ConstraintHelper constraintHelper,
+ ConstraintCheck... checks) {
+
+ this.constraintHelper = constraintHelper;
+ this.delegate = new SingleValuedChecks( checks );
+ }
+
+ public Set<ConstraintCheckError> execute(Element element,
+ AnnotationMirror annotation) {
+
+ Set<ConstraintCheckError> theValue = CollectionHelper.newHashSet();
+
+ //execute the checks on each element of the multi-valued constraint
+ for ( AnnotationMirror onePartOfMultiValuedConstraint :
+ constraintHelper.getPartsOfMultiValuedConstraint( annotation ) ) {
+
+ theValue.addAll( delegate.execute( element, onePartOfMultiValuedConstraint ) );
+ }
+
+ return theValue;
+ }
+
+}
Property changes on:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/MultiValuedChecks.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/PrimitiveCheck.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/PrimitiveCheck.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/PrimitiveCheck.java 2010-04-27
22:51:17 UTC (rev 19310)
@@ -0,0 +1,69 @@
+// $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.Element;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.VariableElement;
+import javax.lang.model.type.TypeMirror;
+
+import org.hibernate.validator.ap.util.CollectionHelper;
+
+/**
+ * Validates that the given element is not of a primitive type. Applies to
+ * fields and methods (the return type is evaluated).
+ *
+ * @author Gunnar Morling
+ */
+public class PrimitiveCheck extends AbstractConstraintCheck {
+
+ @Override
+ public Set<ConstraintCheckError> checkField(VariableElement element,
+ AnnotationMirror annotation) {
+
+ return checkInternal( element, annotation, element.asType(),
"ATVALID_NOT_ALLOWED_AT_PRIMITIVE_FIELD" );
+ }
+
+ @Override
+ public Set<ConstraintCheckError> checkMethod(ExecutableElement element,
+ AnnotationMirror annotation) {
+
+ return checkInternal(
+ element, annotation, element.getReturnType(),
"ATVALID_NOT_ALLOWED_AT_METHOD_RETURNING_PRIMITIVE_TYPE"
+ );
+ }
+
+ private Set<ConstraintCheckError> checkInternal(Element element,
+ AnnotationMirror annotation, TypeMirror type, String messageKey) {
+
+ if ( type.getKind().isPrimitive() ) {
+
+ return CollectionHelper.asSet(
+ new ConstraintCheckError(
+ element, annotation, messageKey
+ )
+ );
+ }
+
+ return Collections.emptySet();
+ }
+
+}
Property changes on:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/PrimitiveCheck.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/SingleValuedChecks.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/SingleValuedChecks.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/SingleValuedChecks.java 2010-04-27
22:51:17 UTC (rev 19310)
@@ -0,0 +1,92 @@
+// $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.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+import javax.lang.model.element.AnnotationMirror;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.element.VariableElement;
+
+import org.hibernate.validator.ap.util.CollectionHelper;
+
+/**
+ * A {@link ConstraintChecks} implementation that simply executes all
+ * contained checks against given elements and annotations.
+ *
+ * @author Gunnar Morling
+ */
+public class SingleValuedChecks implements ConstraintChecks {
+
+ //TODO GM: the "ordered set" character is currently ensured by adding
+ //each check only once in ConstraintCheckFactory. Should this be a real set?
+ private final List<ConstraintCheck> checks;
+
+ /**
+ * Creates a new SingleValuedChecks.
+ *
+ * @param checks The checks to execute.
+ */
+ public SingleValuedChecks(ConstraintCheck... checks) {
+
+ if ( checks == null ) {
+ this.checks = Collections.emptyList();
+ }
+ else {
+ this.checks = Arrays.asList( checks );
+ }
+ }
+
+ public Set<ConstraintCheckError> execute(Element element, AnnotationMirror
annotation) {
+
+ Set<ConstraintCheckError> theValue = CollectionHelper.newHashSet();
+
+ //for each check execute the check method appropriate for the kind of
+ //the given element
+ for ( ConstraintCheck oneCheck : checks ) {
+
+ if ( element.getKind() == ElementKind.FIELD ) {
+ theValue.addAll( oneCheck.checkField( ( VariableElement ) element, annotation ) );
+ }
+ else if ( element.getKind() == ElementKind.METHOD ) {
+ theValue.addAll( oneCheck.checkMethod( ( ExecutableElement ) element, annotation )
);
+ }
+ else if ( element.getKind() == ElementKind.ANNOTATION_TYPE ) {
+ theValue.addAll( oneCheck.checkAnnotationType( ( TypeElement ) element, annotation )
);
+ }
+ else if (
+ element.getKind() == ElementKind.CLASS ||
+ element.getKind() == ElementKind.INTERFACE ||
+ element.getKind() == ElementKind.ENUM ) {
+
+ theValue.addAll( oneCheck.checkNonAnnotationType( ( TypeElement ) element, annotation
) );
+ }
+
+ if ( !theValue.isEmpty() ) {
+ return theValue;
+ }
+ }
+
+ return theValue;
+ }
+}
Property changes on:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/SingleValuedChecks.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/StaticCheck.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/StaticCheck.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/StaticCheck.java 2010-04-27
22:51:17 UTC (rev 19310)
@@ -0,0 +1,64 @@
+// $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.Element;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.Modifier;
+import javax.lang.model.element.VariableElement;
+
+import org.hibernate.validator.ap.util.CollectionHelper;
+
+/**
+ * Checks, that the given element is not a static element. Applies to fields
+ * and methods.
+ *
+ * @author Gunnar Morling
+ */
+public class StaticCheck extends AbstractConstraintCheck {
+
+ @Override
+ public Set<ConstraintCheckError> checkField(VariableElement element,
AnnotationMirror annotation) {
+
+ return checkInternal( element, annotation,
"STATIC_FIELDS_MAY_NOT_BE_ANNOTATED" );
+ }
+
+ @Override
+ public Set<ConstraintCheckError> checkMethod(ExecutableElement element,
AnnotationMirror annotation) {
+
+ return checkInternal( element, annotation,
"STATIC_METHODS_MAY_NOT_BE_ANNOTATED" );
+ }
+
+ private Set<ConstraintCheckError> checkInternal(Element element,
+ AnnotationMirror annotation, String messageKey) {
+ if ( isStaticElement( element ) ) {
+
+ return CollectionHelper.asSet( new ConstraintCheckError( element, annotation,
messageKey ) );
+ }
+
+ return Collections.emptySet();
+ }
+
+ private boolean isStaticElement(Element element) {
+ return element.getModifiers().contains( Modifier.STATIC );
+ }
+
+}
Property changes on:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/StaticCheck.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/TypeCheck.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/TypeCheck.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/TypeCheck.java 2010-04-27
22:51:17 UTC (rev 19310)
@@ -0,0 +1,87 @@
+// $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.Element;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.element.VariableElement;
+import javax.lang.model.type.TypeMirror;
+
+import org.hibernate.validator.ap.util.CollectionHelper;
+import org.hibernate.validator.ap.util.ConstraintHelper;
+import org.hibernate.validator.ap.util.ConstraintHelper.ConstraintCheckResult;
+
+/**
+ * Checks, that constraint annotations are only specified at elements
+ * of a type supported by the constraints. Applies to fields, methods and
+ * non-annotation type declarations.
+ *
+ * @author Gunnar Morling
+ */
+public class TypeCheck extends AbstractConstraintCheck {
+
+ private ConstraintHelper constraintHelper;
+
+ public TypeCheck(ConstraintHelper constraintHelper) {
+ this.constraintHelper = constraintHelper;
+ }
+
+ @Override
+ public Set<ConstraintCheckError> checkField(VariableElement element,
+ AnnotationMirror annotation) {
+
+ return checkInternal( element, annotation, element.asType(),
"NOT_SUPPORTED_TYPE" );
+ }
+
+ @Override
+ public Set<ConstraintCheckError> checkMethod(ExecutableElement element,
+ AnnotationMirror annotation) {
+
+ return checkInternal( element, annotation, element.getReturnType(),
"NOT_SUPPORTED_RETURN_TYPE" );
+ }
+
+ @Override
+ public Set<ConstraintCheckError> checkNonAnnotationType(
+ TypeElement element, AnnotationMirror annotation) {
+
+ return checkInternal( element, annotation, element.asType(),
"NOT_SUPPORTED_TYPE" );
+ }
+
+ private Set<ConstraintCheckError> checkInternal(Element element,
+ AnnotationMirror annotation, TypeMirror type, String messageKey) {
+
+ if ( constraintHelper.checkConstraint(
+ annotation.getAnnotationType(), type
+ ) != ConstraintCheckResult.ALLOWED ) {
+
+ return CollectionHelper.asSet(
+ new ConstraintCheckError(
+ element, annotation, messageKey,
+ annotation.getAnnotationType().asElement().getSimpleName()
+ )
+ );
+ }
+
+ return Collections.emptySet();
+ }
+
+}
Property changes on:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/TypeCheck.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/MessagerAdapter.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/MessagerAdapter.java 2010-04-27
08:40:55 UTC (rev 19309)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/MessagerAdapter.java 2010-04-27
22:51:17 UTC (rev 19310)
@@ -1,4 +1,4 @@
-// $Id: MessagerAdapter.java 19033 2010-03-19 21:27:15Z gunnar.morling $
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
@@ -19,11 +19,12 @@
import java.text.MessageFormat;
import java.util.ResourceBundle;
+import java.util.Set;
import javax.annotation.processing.Messager;
-import javax.lang.model.element.AnnotationMirror;
-import javax.lang.model.element.Element;
import javax.tools.Diagnostic.Kind;
+import org.hibernate.validator.ap.checks.ConstraintCheckError;
+
/**
* Wrapper around {@link Messager}, which adds the ability to format error messages using
{@link MessageFormat}.
*
@@ -58,30 +59,43 @@
}
/**
- * Reports an error at the given location using the given message key and
- * optionally the given message parameters.
+ * Returns the messager used by this adapter.
*
- * @param element The element at which the error shall be reported.
- * @param annotation The annotation mirror at which the error shall be reported.
- * @param messageKey The message key to be used to retrieve the text.
- * @param messageParameters An optional array of message parameters to be put into the
- * message using a {@link MessageFormat}.
+ * @return The underlying messager.
*/
- public void reportError(Element element, AnnotationMirror annotation, String messageKey,
Object... messageParameters) {
+ public Messager getDelegate() {
+ return messager;
+ }
- String message = errorMessages.getString( messageKey );
+ /**
+ * Reports the given errors against the underlying {@link Messager} using
+ * the specified {@link Kind}.
+ *
+ * @param errors A set with errors to report. May be empty but must not be
+ * null.
+ */
+ public void reportErrors(Set<ConstraintCheckError> errors) {
+ for ( ConstraintCheckError oneError : errors ) {
+ reportError( oneError );
+ }
+ }
- if ( message != null &&
- messageParameters != null ) {
+ /**
+ * Reports the given error. Message parameters will be put into the template
+ * retrieved from the resource bundle if applicable.
+ *
+ * @param error The error to report.
+ */
+ private void reportError(ConstraintCheckError error) {
- message = MessageFormat.format( errorMessages.getString( messageKey ),
messageParameters );
+ String message = errorMessages.getString( error.getMessageKey() );
+
+ if ( error.getMessageParameters() != null ) {
+ message = MessageFormat.format( message, error.getMessageParameters() );
}
- else {
- message = messageKey;
- }
messager.printMessage(
- diagnosticKind, message, element, annotation
+ diagnosticKind, message, error.getElement(), error.getAnnotationMirror()
);
}
Property changes on:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/MessagerAdapter.java
___________________________________________________________________
Name: svn:keywords
+ Id