[hibernate-commits] Hibernate SVN: r19289 - in validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap: util and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Sat Apr 24 06:08:28 EDT 2010


Author: gunnar.morling
Date: 2010-04-24 06:08:27 -0400 (Sat, 24 Apr 2010)
New Revision: 19289

Added:
   validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/MessagerAdapter.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/ConstraintValidationProcessor.java
Log:
HV-313: moving ConstraintAnnotationVisitor#reportError() to new class MessagerAdapter, moving ConstraintAnnotationVisitor#initDiagnosticKind() to ConstraintValidationProcessor

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-23 17:49:40 UTC (rev 19288)
+++ validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintAnnotationVisitor.java	2010-04-24 10:08:27 UTC (rev 19289)
@@ -17,10 +17,7 @@
 */
 package org.hibernate.validator.ap;
 
-import java.text.MessageFormat;
 import java.util.List;
-import java.util.ResourceBundle;
-
 import javax.annotation.processing.ProcessingEnvironment;
 import javax.lang.model.element.AnnotationMirror;
 import javax.lang.model.element.Element;
@@ -32,11 +29,11 @@
 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.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
@@ -47,35 +44,13 @@
  */
 final class ConstraintAnnotationVisitor extends ElementKindVisitor6<Void, List<AnnotationMirror>> {
 
-	/**
-	 * The name of the processor option for setting the diagnostic kind to be
-	 * used when reporting errors during annotation processing. Can be set on
-	 * the command line using the -A option, e.g.
-	 * <code>-AdiagnosticKind=ERROR</code>.
-	 */
-	public final static String DIAGNOSTIC_KIND_PROCESSOR_OPTION_NAME = "diagnosticKind";
-
-	/**
-	 * The diagnostic kind to be used if no or an invalid kind is given as processor option.
-	 */
-	public final static Kind DEFAULT_DIAGNOSTIC_KIND = Kind.ERROR;
-
-	/**
-	 * The kind of diagnostic to be used when reporting any problems.
-	 */
-	private Kind diagnosticKind;
-
-	private final ProcessingEnvironment processingEnvironment;
-
-	private final ResourceBundle errorMessages;
-
 	private final ConstraintHelper constraintHelper;
 
-	public ConstraintAnnotationVisitor(ProcessingEnvironment processingEnvironment) {
+	private final MessagerAdapter messager;
 
-		this.processingEnvironment = processingEnvironment;
+	public ConstraintAnnotationVisitor(ProcessingEnvironment processingEnvironment, MessagerAdapter messager) {
 
-		errorMessages = ResourceBundle.getBundle( "org.hibernate.validator.ap.ValidationProcessorMessages" );
+		this.messager = messager;
 
 		AnnotationApiHelper annotationApiHelper = new AnnotationApiHelper(
 				processingEnvironment.getElementUtils(), processingEnvironment.getTypeUtils()
@@ -84,35 +59,8 @@
 		constraintHelper = new ConstraintHelper(
 				processingEnvironment.getElementUtils(), processingEnvironment.getTypeUtils(), annotationApiHelper
 		);
-
-		initializeDiagnosticKind();
 	}
 
-	private void initializeDiagnosticKind() {
-
-		String diagnosticKindFromOptions = processingEnvironment.getOptions()
-				.get( DIAGNOSTIC_KIND_PROCESSOR_OPTION_NAME );
-
-		if ( diagnosticKindFromOptions != null ) {
-			try {
-				diagnosticKind = Kind.valueOf( diagnosticKindFromOptions );
-			}
-			catch ( IllegalArgumentException e ) {
-
-				processingEnvironment.getMessager().printMessage(
-						Kind.ERROR, MessageFormat.format(
-								errorMessages.getString( "INVALID_DIAGNOSTIC_KIND_GIVEN" ), diagnosticKindFromOptions
-						)
-				);
-
-				diagnosticKind = DEFAULT_DIAGNOSTIC_KIND;
-			}
-		}
-		else {
-			diagnosticKind = DEFAULT_DIAGNOSTIC_KIND;
-		}
-	}
-
 	/**
 	 * <p>
 	 * Checks whether the given mirrors representing one or more constraint annotations are correctly
@@ -136,6 +84,7 @@
 
 		for ( AnnotationMirror oneAnnotationMirror : mirrors ) {
 
+
 			switch ( constraintHelper.getAnnotationType( oneAnnotationMirror ) ) {
 
 				case CONSTRAINT_ANNOTATION:
@@ -208,13 +157,6 @@
 	 * declarations.
 	 * </p>
 	 */
-	// TODO GM: do a more complete check of constraint annotation type
-	// declarations:
-	// 
-	// - check existence of groups(), message(), payload()
-	// - check retention policy
-	// - check, that the set of supported types is not empty
-	// - optionally check, that validated types resolve to non-parametrized types
 	@Override
 	public Void visitTypeAsAnnotationType(TypeElement annotationType,
 										  List<AnnotationMirror> mirrors) {
@@ -289,7 +231,7 @@
 				mirror.getAnnotationType(), annotatedType.asType()
 		) != ConstraintCheckResult.ALLOWED ) {
 
-			reportError(
+			messager.reportError(
 					annotatedType, mirror, "NOT_SUPPORTED_TYPE",
 					mirror.getAnnotationType().asElement().getSimpleName()
 			);
@@ -300,7 +242,7 @@
 
 		if ( isStaticElement( annotatedField ) ) {
 
-			reportError( annotatedField, annotationMirror, "STATIC_FIELDS_MAY_NOT_BE_ANNOTATED" );
+			messager.reportError( annotatedField, annotationMirror, "STATIC_FIELDS_MAY_NOT_BE_ANNOTATED" );
 
 			return;
 		}
@@ -309,7 +251,7 @@
 				annotationMirror.getAnnotationType(), annotatedField.asType()
 		) != ConstraintCheckResult.ALLOWED ) {
 
-			reportError(
+			messager.reportError(
 					annotatedField, annotationMirror, "NOT_SUPPORTED_TYPE",
 					annotationMirror.getAnnotationType().asElement().getSimpleName()
 			);
@@ -320,14 +262,14 @@
 
 		if ( !isGetterMethod( method ) ) {
 
-			reportError( method, mirror, "ONLY_GETTERS_MAY_BE_ANNOTATED" );
+			messager.reportError( method, mirror, "ONLY_GETTERS_MAY_BE_ANNOTATED" );
 
 			return;
 		}
 
 		if ( isStaticElement( method ) ) {
 
-			reportError( method, mirror, "STATIC_METHODS_MAY_NOT_BE_ANNOTATED" );
+			messager.reportError( method, mirror, "STATIC_METHODS_MAY_NOT_BE_ANNOTATED" );
 
 			return;
 		}
@@ -336,7 +278,7 @@
 				mirror.getAnnotationType(), method.getReturnType()
 		) != ConstraintCheckResult.ALLOWED ) {
 
-			reportError(
+			messager.reportError(
 					method, mirror, "NOT_SUPPORTED_RETURN_TYPE",
 					mirror.getAnnotationType().asElement().getSimpleName()
 			);
@@ -346,7 +288,7 @@
 	private void checkConstraintAtAnnotationType(TypeElement annotationType, AnnotationMirror annotationMirror) {
 
 		if ( !constraintHelper.isConstraintAnnotation( annotationType ) ) {
-			reportError( annotationType, annotationMirror, "ONLY_CONSTRAINT_ANNOTATIONS_MAY_BE_ANNOTATED" );
+			messager.reportError( annotationType, annotationMirror, "ONLY_CONSTRAINT_ANNOTATIONS_MAY_BE_ANNOTATED" );
 		}
 
 	}
@@ -356,7 +298,7 @@
 
 		if ( isStaticElement( annotatedField ) ) {
 
-			reportError(
+			messager.reportError(
 					annotatedField, annotationMirror,
 					"STATIC_FIELDS_MAY_NOT_BE_ANNOTATED"
 			);
@@ -366,7 +308,7 @@
 
 		if ( isPrimitiveType( annotatedField.asType() ) ) {
 
-			reportError(
+			messager.reportError(
 					annotatedField, annotationMirror,
 					"ATVALID_NOT_ALLOWED_AT_PRIMITIVE_FIELD"
 			);
@@ -378,7 +320,7 @@
 
 		if ( !isGetterMethod( method ) ) {
 
-			reportError(
+			messager.reportError(
 					method, annotationMirror,
 					"ONLY_GETTERS_MAY_BE_ANNOTATED"
 			);
@@ -388,7 +330,7 @@
 
 		if ( isStaticElement( method ) ) {
 
-			reportError(
+			messager.reportError(
 					method, annotationMirror,
 					"STATIC_METHODS_MAY_NOT_BE_ANNOTATED"
 			);
@@ -398,7 +340,7 @@
 
 		if ( isPrimitiveType( method.getReturnType() ) ) {
 
-			reportError(
+			messager.reportError(
 					method, annotationMirror,
 					"ATVALID_NOT_ALLOWED_AT_METHOD_RETURNING_PRIMITIVE_TYPE"
 			);
@@ -430,30 +372,4 @@
 		return typeMirror.getKind().isPrimitive();
 	}
 
-	/**
-	 * Reports an error at the given location using the given message key and
-	 * optionally the given message parameters.
-	 *
-	 * @param element The element at which the error shall be reported.
-	 * @param annotationMirror 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}.
-	 */
-	private void reportError(Element element, AnnotationMirror annotationMirror, String messageKey, Object... messageParameters) {
-
-		String message;
-
-		if ( messageParameters == null ) {
-			message = errorMessages.getString( messageKey );
-		}
-		else {
-			message = MessageFormat.format( errorMessages.getString( messageKey ), messageParameters );
-		}
-
-		processingEnvironment.getMessager().printMessage(
-				diagnosticKind, message, element, annotationMirror
-		);
-	}
-
 }

Modified: validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintValidationProcessor.java
===================================================================
--- validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintValidationProcessor.java	2010-04-23 17:49:40 UTC (rev 19288)
+++ validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintValidationProcessor.java	2010-04-24 10:08:27 UTC (rev 19289)
@@ -17,9 +17,11 @@
 */
 package org.hibernate.validator.ap;
 
+import java.text.MessageFormat;
 import java.util.List;
 import java.util.Set;
 import javax.annotation.processing.AbstractProcessor;
+import javax.annotation.processing.ProcessingEnvironment;
 import javax.annotation.processing.RoundEnvironment;
 import javax.annotation.processing.SupportedAnnotationTypes;
 import javax.annotation.processing.SupportedOptions;
@@ -29,15 +31,17 @@
 import javax.lang.model.element.Element;
 import javax.lang.model.element.ElementVisitor;
 import javax.lang.model.element.TypeElement;
+import javax.tools.Diagnostic.Kind;
 
 import org.hibernate.validator.ap.util.AnnotationApiHelper;
+import org.hibernate.validator.ap.util.MessagerAdapter;
 
 /**
  * An annotation processor for checking <a
  * href="http://jcp.org/en/jsr/detail?id=303">Bean Validation</a> constraints.
  * The processor supports the following options:
  * <ul>
- * <li><code>diagnosticKind</code>: the severity with which any occured problems
+ * <li><code>diagnosticKind</code>: the severity with which any occurred problems
  * shall be reported. Must be given in form of the string representation of a
  * value from {@link javax.tools.Diagnostic.Kind}, e.g.
  * "diagnosticKind=WARNING". Default is Kind.ERROR.</li>
@@ -48,19 +52,42 @@
  * @author Hardy Ferentschik
  * @author Gunnar Morling
  */
-// TODO GM: check @Valid annotation
-// TODO GM: add documentation for AP to HV reference guide
 @SupportedAnnotationTypes("*")
 @SupportedSourceVersion(SourceVersion.RELEASE_6)
- at SupportedOptions(ConstraintAnnotationVisitor.DIAGNOSTIC_KIND_PROCESSOR_OPTION_NAME)
+ at SupportedOptions(ConstraintValidationProcessor.DIAGNOSTIC_KIND_PROCESSOR_OPTION_NAME)
 public class ConstraintValidationProcessor extends AbstractProcessor {
 
 	/**
+	 * The name of the processor option for setting the diagnostic kind to be
+	 * used when reporting errors during annotation processing. Can be set on
+	 * the command line using the -A option, e.g.
+	 * <code>-AdiagnosticKind=ERROR</code>.
+	 */
+	public final static String DIAGNOSTIC_KIND_PROCESSOR_OPTION_NAME = "diagnosticKind";
+
+	/**
+	 * The diagnostic kind to be used if no or an invalid kind is given as processor option.
+	 */
+	public final static Kind DEFAULT_DIAGNOSTIC_KIND = Kind.ERROR;
+	/**
 	 * Whether this processor claims all processed annotations exclusively or not.
 	 */
 	private static final boolean ANNOTATIONS_CLAIMED_EXCLUSIVELY = false;
 
+	/**
+	 * The messager to be used for error reports.
+	 */
+	private MessagerAdapter messager;
+
 	@Override
+	public synchronized void init(ProcessingEnvironment processingEnv) {
+
+		super.init( processingEnv );
+
+		messager = new MessagerAdapter( processingEnv.getMessager(), getDiagnosticKind() );
+	}
+
+	@Override
 	public boolean process(
 			final Set<? extends TypeElement> annotations,
 			final RoundEnvironment roundEnvironment) {
@@ -69,7 +96,9 @@
 				processingEnv.getElementUtils(), processingEnv.getTypeUtils()
 		);
 
-		ElementVisitor<Void, List<AnnotationMirror>> visitor = new ConstraintAnnotationVisitor( processingEnv );
+		ElementVisitor<Void, List<AnnotationMirror>> visitor = new ConstraintAnnotationVisitor(
+				processingEnv, messager
+		);
 
 		for ( TypeElement oneAnnotation : annotations ) {
 
@@ -88,4 +117,33 @@
 		return ANNOTATIONS_CLAIMED_EXCLUSIVELY;
 	}
 
+	/**
+	 * Retrieves the diagnostic kind to be used for error messages. If given in processor options, it
+	 * will be taken from there, otherwise the default value Kind.ERROR will be returned.
+	 *
+	 * @return The diagnostic kind to be used for error messages.
+	 */
+	private Kind getDiagnosticKind() {
+
+		String diagnosticKindFromOptions = processingEnv.getOptions()
+				.get( DIAGNOSTIC_KIND_PROCESSOR_OPTION_NAME );
+
+		if ( diagnosticKindFromOptions != null ) {
+			try {
+				return Kind.valueOf( diagnosticKindFromOptions );
+			}
+			catch ( IllegalArgumentException e ) {
+				super.processingEnv.getMessager().printMessage(
+						Kind.WARNING, MessageFormat.format(
+								"The given value {0} is no valid diagnostic kind. {1} will be used.",
+								diagnosticKindFromOptions,
+								DEFAULT_DIAGNOSTIC_KIND
+						)
+				);
+			}
+		}
+
+		return DEFAULT_DIAGNOSTIC_KIND;
+	}
+
 }

Added: 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	                        (rev 0)
+++ validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/MessagerAdapter.java	2010-04-24 10:08:27 UTC (rev 19289)
@@ -0,0 +1,88 @@
+// $Id: MessagerAdapter.java 19033 2010-03-19 21:27:15Z gunnar.morling $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+* http://www.apache.org/licenses/LICENSE-2.0
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.hibernate.validator.ap.util;
+
+import java.text.MessageFormat;
+import java.util.ResourceBundle;
+import javax.annotation.processing.Messager;
+import javax.lang.model.element.AnnotationMirror;
+import javax.lang.model.element.Element;
+import javax.tools.Diagnostic.Kind;
+
+/**
+ * Wrapper around {@link Messager}, which adds the ability to format error messages using {@link MessageFormat}.
+ *
+ * @author Gunnar Morling
+ */
+public class MessagerAdapter {
+
+	/**
+	 * Contains the texts to be displayed.
+	 */
+	private final ResourceBundle errorMessages;
+
+	private final Messager messager;
+
+	/**
+	 * The kind of diagnostic to be used when reporting any problems.
+	 */
+	private Kind diagnosticKind;
+
+	/**
+	 * Creates a new MessagerAdapter.
+	 *
+	 * @param messager The underlying messager.
+	 * @param diagnosticKind The kind with which messages shall be reported.
+	 */
+	public MessagerAdapter(Messager messager, Kind diagnosticKind) {
+
+		this.messager = messager;
+		this.diagnosticKind = diagnosticKind;
+
+		errorMessages = ResourceBundle.getBundle( "org.hibernate.validator.ap.ValidationProcessorMessages" );
+	}
+
+	/**
+	 * Reports an error at the given location using the given message key and
+	 * optionally the given message parameters.
+	 *
+	 * @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}.
+	 */
+	public void reportError(Element element, AnnotationMirror annotation, String messageKey, Object... messageParameters) {
+
+		String message = errorMessages.getString( messageKey );
+
+		if ( message != null &&
+				messageParameters != null ) {
+
+			message = MessageFormat.format( errorMessages.getString( messageKey ), messageParameters );
+		}
+		else {
+			message = messageKey;
+		}
+
+		messager.printMessage(
+				diagnosticKind, message, element, annotation
+		);
+	}
+
+}



More information about the hibernate-commits mailing list