diff --git a/.gitignore b/.gitignore index 87fc3ac549..f6189ee196 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ atlassian-ide-plugin.xml .classpath .project .settings +.factorypath # NetBeans specific files/directories .nbattrs diff --git a/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintAnnotationVisitor.java b/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintAnnotationVisitor.java index 43a3810808..aa73363708 100644 --- a/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintAnnotationVisitor.java +++ b/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintAnnotationVisitor.java @@ -31,6 +31,7 @@ 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.Configuration; import org.hibernate.validator.ap.util.ConstraintHelper; import org.hibernate.validator.ap.util.MessagerAdapter; @@ -51,10 +52,10 @@ final class ConstraintAnnotationVisitor extends ElementKindVisitor6Bean Validation constraints. * The processor supports the following options: * + *
  • verbose: whether a verbose output shall be created or not. + * Must be given as String parsable by {@link Boolean#parseBoolean}. Default is + * false.
  • + *
  • methodConstraintsSupported: Whether constraints at other + * methods than JavaBeans getter methods may be annotated with constraints or + * not. Must be given as String parsable by {@link Boolean#parseBoolean}. Can be + * set to false in order to allow only getter based property + * constraints but not method level constraints as supported by Hibernate + * Validator. Default is true.
  • * * @author Hardy Ferentschik * @author Gunnar Morling */ @SupportedAnnotationTypes("*") @SupportedSourceVersion(SourceVersion.RELEASE_6) -@SupportedOptions({ - ConstraintValidationProcessor.DIAGNOSTIC_KIND_PROCESSOR_OPTION_NAME, - ConstraintValidationProcessor.VERBOSE_PROCESSOR_OPTION_NAME +@SupportedOptions( { + Configuration.DIAGNOSTIC_KIND_PROCESSOR_OPTION, + Configuration.VERBOSE_PROCESSOR_OPTION, + Configuration.METHOD_CONSTRAINTS_SUPPORTED_PROCESSOR_OPTION }) 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. - */ - public final static String DIAGNOSTIC_KIND_PROCESSOR_OPTION_NAME = "diagnosticKind"; - - /** - * The name of the processor option for activating verbose message reporting. - */ - public final static String VERBOSE_PROCESSOR_OPTION_NAME = "verbose"; - - /** - * 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. */ @@ -85,17 +76,17 @@ public class ConstraintValidationProcessor extends AbstractProcessor { private MessagerAdapter messager; /** - * Whether logging information shall be put out in a verbose way or not. + * Provides access to this processor's configuration options. */ - private boolean verbose; + private Configuration configuration; @Override public synchronized void init(ProcessingEnvironment processingEnv) { super.init( processingEnv ); - this.verbose = isVerbose(); - messager = new MessagerAdapter( processingEnv.getMessager(), getDiagnosticKind() ); + configuration = new Configuration( processingEnv.getOptions(), processingEnv.getMessager() ); + messager = new MessagerAdapter( processingEnv.getMessager(), configuration.getDiagnosticKind() ); } @Override @@ -108,7 +99,7 @@ public boolean process( ); ElementVisitor> visitor = new ConstraintAnnotationVisitor( - processingEnv, messager, verbose + processingEnv, messager, configuration ); for ( TypeElement oneAnnotation : annotations ) { @@ -128,54 +119,4 @@ public boolean process( 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; - } - - /** - * Retrieves the value for the "verbose" property from the options. - * - * @return The value for the "verbose" property. - */ - private boolean isVerbose() { - - boolean theValue = Boolean.parseBoolean( processingEnv.getOptions().get( VERBOSE_PROCESSOR_OPTION_NAME ) ); - - if ( theValue ) { - super.processingEnv.getMessager().printMessage( - Kind.NOTE, MessageFormat.format( - "Verbose reporting is activated. Some processing information will be displayed using diagnostic kind {0}.", - Kind.NOTE - ) - ); - } - - return theValue; - } - } diff --git a/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheckFactory.java b/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheckFactory.java index 4fc6c55a91..8d153830c7 100644 --- a/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheckFactory.java +++ b/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/ConstraintCheckFactory.java @@ -58,7 +58,7 @@ public class ConstraintCheckFactory { private final static SingleValuedChecks NULL_CHECKS = new SingleValuedChecks(); - public ConstraintCheckFactory(Types typeUtils, ConstraintHelper constraintHelper, AnnotationApiHelper annotationApiHelper) { + public ConstraintCheckFactory(Types typeUtils, ConstraintHelper constraintHelper, AnnotationApiHelper annotationApiHelper, boolean methodConstraintsSupported) { this.constraintHelper = constraintHelper; @@ -80,16 +80,16 @@ public ConstraintCheckFactory(Types typeUtils, ConstraintHelper constraintHelper methodChecks = CollectionHelper.newHashMap(); methodChecks.put( AnnotationType.CONSTRAINT_ANNOTATION, - new SingleValuedChecks( new GetterCheck(), new StaticCheck(), new TypeCheck( constraintHelper ) ) + new SingleValuedChecks( new GetterCheck(methodConstraintsSupported), new StaticCheck(), new TypeCheck( constraintHelper ) ) ); methodChecks.put( AnnotationType.MULTI_VALUED_CONSTRAINT_ANNOTATION, new MultiValuedChecks( - constraintHelper, new GetterCheck(), new StaticCheck(), new TypeCheck( constraintHelper ) + constraintHelper, new GetterCheck(methodConstraintsSupported), new StaticCheck(), new TypeCheck( constraintHelper ) ) ); methodChecks.put( AnnotationType.GRAPH_VALIDATION_ANNOTATION, - new SingleValuedChecks( new GetterCheck(), new StaticCheck(), new PrimitiveCheck() ) + new SingleValuedChecks( new GetterCheck(methodConstraintsSupported), new StaticCheck(), new PrimitiveCheck() ) ); methodChecks.put( AnnotationType.NO_CONSTRAINT_ANNOTATION, NULL_CHECKS ); diff --git a/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/GetterCheck.java b/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/GetterCheck.java index 24cadb34c5..3977cfd1a3 100644 --- a/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/GetterCheck.java +++ b/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/checks/GetterCheck.java @@ -30,16 +30,30 @@ * @author Gunnar Morling */ public class GetterCheck extends AbstractConstraintCheck { + + private final boolean methodConstraintsSupported; + + public GetterCheck(boolean methodConstraintsSupported) { + this.methodConstraintsSupported = methodConstraintsSupported; + } + public Set checkMethod(ExecutableElement element, AnnotationMirror annotation) { - if ( !isGetterMethod( element ) ) { + if ( !methodConstraintsSupported && !isGetterMethod( element ) ) { return CollectionHelper.asSet( new ConstraintCheckError( element, annotation, "ONLY_GETTERS_MAY_BE_ANNOTATED" ) ); } + else if (!hasReturnValue(element)) { + return CollectionHelper.asSet( + new ConstraintCheckError( + element, annotation, "ONLY_NON_VOID_METHODS_MAY_BE_ANNOTATED" + ) + ); + } return Collections.emptySet(); } diff --git a/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/Configuration.java b/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/Configuration.java new file mode 100644 index 0000000000..c1f8e4a844 --- /dev/null +++ b/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/Configuration.java @@ -0,0 +1,149 @@ +/* +* JBoss, Home of Professional Open Source +* Copyright 2011, Red Hat, Inc. and/or its affiliates, 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.Map; +import javax.annotation.processing.Messager; +import javax.tools.Diagnostic.Kind; + +/** + * Provides access to the processor options supported by the Hibernate Validator + * annotation processor. + * + * @author Gunnar Morling + */ +public class Configuration { + + /** + * The name of the processor option for setting the diagnostic kind to be + * used when reporting errors during annotation processing. + */ + public final static String DIAGNOSTIC_KIND_PROCESSOR_OPTION = "diagnosticKind"; + + /** + * The name of the processor option for activating verbose message reporting. + */ + public final static String VERBOSE_PROCESSOR_OPTION = "verbose"; + + /** + * The name of the processor option for allowing constraints at methods + * other than getter methods. + */ + public final static String METHOD_CONSTRAINTS_SUPPORTED_PROCESSOR_OPTION = "methodConstraintsSupported"; + + /** + * 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; + + private final Kind diagnosticKind; + + private final boolean verbose; + + private final boolean methodConstraintsSupported; + + public Configuration(Map options, Messager messager) { + + this.diagnosticKind = getDiagnosticKindOption( options, messager ); + this.verbose = getVerboseOption( options, messager ); + this.methodConstraintsSupported = getMethodConstraintsSupportedOption( options ); + } + + /** + * Returns the diagnosticKind to be used when reporting failing constraint checks. + */ + public Kind getDiagnosticKind() { + return diagnosticKind; + } + + /** + * Whether logging information shall be put out in a verbose way or not. + */ + public boolean isVerbose() { + return verbose; + } + + /** + * Whether method constraints are allowed at any method (true) or only + * getter methods (false). + */ + public boolean methodConstraintsSupported() { + return methodConstraintsSupported; + } + + /** + * 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 {@link Kind#ERROR} will be returned. + */ + private Kind getDiagnosticKindOption(Map options, Messager messager) { + + String diagnosticKindFromOptions = options.get( DIAGNOSTIC_KIND_PROCESSOR_OPTION ); + + if ( diagnosticKindFromOptions != null ) { + try { + return Kind.valueOf( diagnosticKindFromOptions ); + } + catch ( IllegalArgumentException e ) { + messager.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; + } + + /** + * Retrieves the value for the "verbose" property from the options. + */ + private boolean getVerboseOption(Map options, Messager messager) { + + boolean theValue = Boolean.parseBoolean( options.get( VERBOSE_PROCESSOR_OPTION ) ); + + if ( theValue ) { + messager.printMessage( + Kind.NOTE, MessageFormat.format( + "Verbose reporting is activated. Some processing information will be displayed using diagnostic kind {0}.", + Kind.NOTE + ) + ); + } + + return theValue; + } + + /** + * Retrieves the value for the "methodConstraintsSupported" property from the options. + */ + private boolean getMethodConstraintsSupportedOption(Map options) { + + String methodConstraintsSupported = options.get( METHOD_CONSTRAINTS_SUPPORTED_PROCESSOR_OPTION ); + + //allow method constraints by default + if ( methodConstraintsSupported == null ) { + return true; + } + + return Boolean.parseBoolean( methodConstraintsSupported ); + } +} diff --git a/hibernate-validator-annotation-processor/src/main/resources/org/hibernate/validator/ap/ValidationProcessorMessages.properties b/hibernate-validator-annotation-processor/src/main/resources/org/hibernate/validator/ap/ValidationProcessorMessages.properties index 6d35255684..36fe35fe6f 100644 --- a/hibernate-validator-annotation-processor/src/main/resources/org/hibernate/validator/ap/ValidationProcessorMessages.properties +++ b/hibernate-validator-annotation-processor/src/main/resources/org/hibernate/validator/ap/ValidationProcessorMessages.properties @@ -25,3 +25,4 @@ GROUP_SEQUENCE_PROVIDER_ANNOTATION_NOT_ALLOWED_ON_CLASS_WITH_GROUP_SEQUENCE_ANNO GROUP_SEQUENCE_PROVIDER_ANNOTATION_VALUE_MUST_BE_AN_IMPLEMENTATION_CLASS=The annotation @GroupSequenceProvider must define an implementation or a concrete implementation of DefaultGroupSequenceProvider interface. GROUP_SEQUENCE_PROVIDER_ANNOTATION_VALUE_DEFINED_PROVIDER_CLASS_WITH_WRONG_TYPE=The annotation @GroupSequenceProvider defines a default group sequence provider for type {0} instead of {1} (super)type. GROUP_SEQUENCE_PROVIDER_ANNOTATION_VALUE_CLASS_MUST_HAVE_DEFAULT_CONSTRUCTOR=The annotation @GroupSequenceProvider defines a default group sequence provider class without a public default constructor. +ONLY_NON_VOID_METHODS_MAY_BE_ANNOTATED=Void methods may not be annotated with constraint annotations. diff --git a/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/ConstraintValidationProcessorTest.java b/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/ConstraintValidationProcessorTest.java index a02f2fb0a4..cedaf43fde 100644 --- a/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/ConstraintValidationProcessorTest.java +++ b/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/ConstraintValidationProcessorTest.java @@ -16,15 +16,12 @@ */ package org.hibernate.validator.ap; -import static org.hibernate.validator.ap.testutil.CompilerTestHelper.assertThatDiagnosticsMatch; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertTrue; - import java.io.File; - import javax.tools.Diagnostic; import javax.tools.Diagnostic.Kind; +import org.testng.annotations.Test; + import org.hibernate.validator.ap.testmodel.FieldLevelValidationUsingBuiltInConstraints; import org.hibernate.validator.ap.testmodel.MethodLevelValidationUsingBuiltInConstraints; import org.hibernate.validator.ap.testmodel.ModelWithJodaTypes; @@ -71,7 +68,9 @@ import org.hibernate.validator.ap.testmodel.nouniquevalidatorresolution.SizeValidatorForSet; import org.hibernate.validator.ap.util.DiagnosticExpectation; -import org.testng.annotations.Test; +import static org.hibernate.validator.ap.testutil.CompilerTestHelper.assertThatDiagnosticsMatch; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; /** * Miscellaneous tests for {@link ConstraintValidationProcessor}. @@ -103,7 +102,7 @@ public void testThatProcessorOptionsAreEvaluated() { // compile with -AdiagnosticKind=Kind.WARNING and -Averbose=true boolean compilationResult = compilerHelper.compile( - new ConstraintValidationProcessor(), diagnostics, Kind.WARNING, true, sourceFile + new ConstraintValidationProcessor(), diagnostics, Kind.WARNING, true, false, sourceFile ); // compilation succeeds as there are problems, but Kind.WARNING won't stop compilation @@ -167,7 +166,34 @@ public void methodLevelValidationUsingBuiltInConstraints() { File sourceFile = compilerHelper.getSourceFile( MethodLevelValidationUsingBuiltInConstraints.class ); boolean compilationResult = - compilerHelper.compile( new ConstraintValidationProcessor(), diagnostics, sourceFile ); + compilerHelper.compile( new ConstraintValidationProcessor(), diagnostics, false, false, sourceFile ); + + assertFalse( compilationResult ); + assertThatDiagnosticsMatch( + diagnostics, + new DiagnosticExpectation( Kind.ERROR, 31 ), + new DiagnosticExpectation( Kind.ERROR, 38 ), + new DiagnosticExpectation( Kind.ERROR, 46 ), + new DiagnosticExpectation( Kind.ERROR, 53 ), + new DiagnosticExpectation( Kind.ERROR, 61 ), + new DiagnosticExpectation( Kind.ERROR, 69 ), + new DiagnosticExpectation( Kind.ERROR, 77 ), + new DiagnosticExpectation( Kind.ERROR, 84 ) + ); + } + + /** + * Constraints are allowed at non-getters per processor option, but all other + * checks (no static methods allowed etc.) still apply. + */ + @Test + public void methodLevelConstraintsAllowedAtNonGetterMethods() { + + File sourceFile = compilerHelper.getSourceFile( MethodLevelValidationUsingBuiltInConstraints.class ); + + //compile with -AmethodConstraintsSupported + boolean compilationResult = + compilerHelper.compile( new ConstraintValidationProcessor(), diagnostics, false, true, sourceFile ); assertFalse( compilationResult ); assertThatDiagnosticsMatch( @@ -175,7 +201,10 @@ public void methodLevelValidationUsingBuiltInConstraints() { new DiagnosticExpectation( Kind.ERROR, 31 ), new DiagnosticExpectation( Kind.ERROR, 38 ), new DiagnosticExpectation( Kind.ERROR, 46 ), - new DiagnosticExpectation( Kind.ERROR, 53 ) + new DiagnosticExpectation( Kind.ERROR, 53 ), + new DiagnosticExpectation( Kind.ERROR, 69 ), + new DiagnosticExpectation( Kind.ERROR, 77 ), + new DiagnosticExpectation( Kind.ERROR, 84 ) ); } @@ -324,7 +353,7 @@ public void testThatAtValidAnnotationGivenAtNotSupportedTypesCausesCompilationEr File sourceFile1 = compilerHelper.getSourceFile( ValidationUsingAtValidAnnotation.class ); boolean compilationResult = compilerHelper.compile( - new ConstraintValidationProcessor(), diagnostics, sourceFile1 + new ConstraintValidationProcessor(), diagnostics, false, false, sourceFile1 ); assertFalse( compilationResult ); diff --git a/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/MethodLevelValidationUsingBuiltInConstraints.java b/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/MethodLevelValidationUsingBuiltInConstraints.java index c779d78344..1d21b25e2b 100644 --- a/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/MethodLevelValidationUsingBuiltInConstraints.java +++ b/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/MethodLevelValidationUsingBuiltInConstraints.java @@ -54,4 +54,36 @@ public void getAnotherString() { public static String getStringStatically() { return null; } + + /** + * No getter, but allowed with -AmethodConstraintsSupported. + */ + @Size(min = 10) + public String doSomething() { + return null; + } + + /** + * Also with -AmethodConstraintsSupported not allowed, as return type doesn't match. + */ + @Size(min = 10) + public Date doSomethingReturningDate() { + return null; + } + + /** + * Also with -AmethodConstraintsSupported not allowed. No return type. + */ + @Size(min = 10) + public void voidDoSomething() { + } + + /** + * Also with -AmethodConstraintsSupported not allowed. Static method. + */ + @Size(min = 10) + public static String staticDoSomething() { + return null; + } + } diff --git a/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testutil/CompilerTestHelper.java b/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testutil/CompilerTestHelper.java index 55a6cc51ce..75acdb34e8 100644 --- a/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testutil/CompilerTestHelper.java +++ b/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testutil/CompilerTestHelper.java @@ -16,15 +16,11 @@ */ package org.hibernate.validator.ap.testutil; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; - import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; - import javax.annotation.processing.Processor; import javax.tools.Diagnostic; import javax.tools.Diagnostic.Kind; @@ -34,8 +30,12 @@ import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; +import org.hibernate.validator.ap.util.Configuration; import org.hibernate.validator.ap.util.DiagnosticExpectation; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + /** * Infrastructure for unit tests based on the Java Compiler API. * @@ -79,30 +79,30 @@ public File getSourceFile(Class clazz) { } /** - * @see CompilerTestHelper#compile(Processor, DiagnosticCollector, Kind, Boolean, File...) + * @see CompilerTestHelper#compile(Processor, DiagnosticCollector, Kind, Boolean, Boolean, File...) */ public boolean compile( Processor annotationProcessor, DiagnosticCollector diagnostics, File... sourceFiles) { - return compile( annotationProcessor, diagnostics, null, null, sourceFiles ); + return compile( annotationProcessor, diagnostics, null, null, null, sourceFiles ); } /** - * @see CompilerTestHelper#compile(Processor, DiagnosticCollector, Kind, Boolean, File...) + * @see CompilerTestHelper#compile(Processor, DiagnosticCollector, Kind, Boolean, Boolean, File...) */ public boolean compile( Processor annotationProcessor, DiagnosticCollector diagnostics, Kind diagnosticKind, File... sourceFiles) { - return compile( annotationProcessor, diagnostics, diagnosticKind, null, sourceFiles ); + return compile( annotationProcessor, diagnostics, diagnosticKind, null, null, sourceFiles ); } /** - * @see CompilerTestHelper#compile(Processor, DiagnosticCollector, Kind, Boolean, File...) + * @see CompilerTestHelper#compile(Processor, DiagnosticCollector, Kind, Boolean, Boolean, File...) */ public boolean compile( - Processor annotationProcessor, DiagnosticCollector diagnostics, boolean verbose, File... sourceFiles) { + Processor annotationProcessor, DiagnosticCollector diagnostics, boolean verbose, boolean allowMethodConstraints, File... sourceFiles) { - return compile( annotationProcessor, diagnostics, null, verbose, sourceFiles ); + return compile( annotationProcessor, diagnostics, null, verbose, allowMethodConstraints, sourceFiles ); } /** @@ -119,7 +119,7 @@ public boolean compile( * any errors), false otherwise. */ public boolean compile( - Processor annotationProcessor, DiagnosticCollector diagnostics, Kind diagnosticKind, Boolean verbose, File... sourceFiles) { + Processor annotationProcessor, DiagnosticCollector diagnostics, Kind diagnosticKind, Boolean verbose, Boolean allowMethodConstraints, File... sourceFiles) { StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null ); @@ -131,11 +131,21 @@ public boolean compile( options.addAll( Arrays.asList( "-classpath", System.getProperty( "java.class.path" ), "-d", "target" ) ); if ( diagnosticKind != null ) { - options.add( "-AdiagnosticKind=" + diagnosticKind ); + options.add( String.format( "-A%s=%s", Configuration.DIAGNOSTIC_KIND_PROCESSOR_OPTION, diagnosticKind ) ); } if ( verbose != null ) { - options.add( "-Averbose=" + verbose.toString() ); + options.add( String.format( "-A%s=%b", Configuration.VERBOSE_PROCESSOR_OPTION, verbose ) ); + } + + if ( allowMethodConstraints != null ) { + options.add( + String.format( + "-A%s=%b", + Configuration.METHOD_CONSTRAINTS_SUPPORTED_PROCESSOR_OPTION, + allowMethodConstraints + ) + ); } CompilationTask task = compiler.getTask( null, fileManager, diagnostics, options, null, compilationUnits ); @@ -168,7 +178,10 @@ public static void assertThatDiagnosticsMatch(DiagnosticCollector oneDiagnostic : diagnosticsList ) { + System.out.println( oneDiagnostic ); + } } assertEquals( diagnosticsList.size(), expectations.length, "Wrong number of diagnostics." ); diff --git a/hibernate-validator/pom.xml b/hibernate-validator/pom.xml index 3ce12251f0..0b7b2f0d3f 100644 --- a/hibernate-validator/pom.xml +++ b/hibernate-validator/pom.xml @@ -274,6 +274,12 @@ zh-CN + + ${jdocbook.ignoreTranslations} ${basedir}/src/main/docbook/en-US/images diff --git a/hibernate-validator/src/main/docbook/en-US/modules/annotationprocessor.xml b/hibernate-validator/src/main/docbook/en-US/modules/annotationprocessor.xml index d50363a161..c6802a49f0 100644 --- a/hibernate-validator/src/main/docbook/en-US/modules/annotationprocessor.xml +++ b/hibernate-validator/src/main/docbook/en-US/modules/annotationprocessor.xml @@ -57,19 +57,6 @@ org.hibernate:hibernate-validator-annotation-processor. - - A first version of the Hibernate Validator Annotation Processor is - part of Hibernate Validator since release 4.1. It is currently still under - development and should therefore be considered as an experimental feature. - Some known issues can be found - at the end of this chapter. In case any problems arise when using the - processor feel free to ask for help at the forum or - create an issue within - JIRA. - -
    Prerequisites @@ -83,8 +70,8 @@
    Features - As of Hibernate Validator 4.1 the Hibernate Validator Annotation - Processor checks that: + As of Hibernate Validator &version; the Hibernate Validator + Annotation Processor checks that: @@ -93,18 +80,19 @@ - JavaBean getter methods are annotated in case of property - validation + only non-static fields or methods are annotated with constraint + annotations - only non-static fields or properties are annotated with - constraint annotations + only non-primitive fields or methods are annotated with + @Valid - only non-primitive fields or properties are annotated with - @Valid + only such methods are annotated with constraint annotations + which are valid JavaBeans getter methods (optionally, see + below) @@ -113,8 +101,8 @@ - definition of dynamic default group sequence with @GroupSequenceProvider - is valid + definition of dynamic default group sequence with + @GroupSequenceProvider is valid
    @@ -153,6 +141,17 @@ ERROR. + + methodConstraintsSupported + + Controls whether constraints are allowed at methods of any + kind. Must be set to true when working with + method level constraints as supported by Hibernate Validator. Can + be set to false to allow constraints only at + JavaBeans getter methods as defined by the Bean Validation API. + Defaults to true. + + verbose diff --git a/pom.xml b/pom.xml index 213c268497..63f0a9d5f9 100644 --- a/pom.xml +++ b/pom.xml @@ -55,6 +55,7 @@ 1.6.1 UTF-8 + false