Skip to content
Commits on Source (39)
# Repository specific GIT options
# Set default handling of line terminators for all non explicitly listed file types:
* text=auto
# Force LF as internal repository format for all following files;
# this overrides user settings to enforce the *right format* :
*.java text
*.xml text
*.txt text
*.md text
*.html text
*.properties text
*.rb text
*.pot text
*.po text
*.xsd text
*.header text
*.groovy text
*.css text
# Specify we want Java-friendly readable chunk headers for diff:
*.java diff=java
# Typically *NIX text editors, by default, append '~' to files on saving to make backups
*~
# Our build scripts - necessary for our release jobs
hibernate-noorm-release-scripts
# Gradle work directory
.gradle
buildSrc/.gradle
# Build output directories
/target
*/target
/build
*/build
/bin
*/bin
target/
build/
bin/
# Test output directories
test-output
*/test-output
test-output/
# Jmeter directory from perf test
*/jmeter
jmeter/
# IntelliJ specific files/directories
/out
out/
.idea
*.ipr
*.iws
......@@ -33,6 +32,7 @@ atlassian-ide-plugin.xml
.project
.settings
.factorypath
.checkstyle
.externalToolBuilders
maven-eclipse.xml
......
sudo: required
dist: trusty
language: java
jdk:
- oraclejdk8
addons:
apt:
packages:
- oracle-java8-installer
# might be useful to push reports to an S3 bucket
# artifacts:
# paths:
# - $(find $HOME -name surefire-reports | tr "\n" ":")
# - $(find $HOME -name failsafe-reports | tr "\n" ":")
# s3_region: 'us-west-2'
cache:
directories:
- $HOME/.m2
install:
# The Maven install provided by Travis is outdated, use Maven wrapper to get the latest version
- mvn -N io.takari:maven:wrapper
- ./mvnw -v
# first run to download all the Maven dependencies without logging
- travis_wait ./mvnw -s settings-example.xml -B -q -DskipTests=true install
before_script:
script:
- ./mvnw -s settings-example.xml clean verify
Guidelines for contributing to Hibernate Validator
====
Contributions from the community are essential in keeping Hibernate Validator strong and successful.
This guide focuses on how to contribute back to Hibernate Validator using GitHub pull requests.
If you need help with cloning, compiling or setting the project up in an IDE please refer to
[this page](http://hibernate.org/validator/contribute/).
## Getting Started
If you are just getting started with Git, GitHub and/or contributing to Hibernate Validator there are a
few prerequisite steps:
* Make sure you have a [Hibernate JIRA account](https://hibernate.atlassian.net)
* Make sure you have a [GitHub account](https://github.com/signup/free)
* [Fork](http://help.github.com/fork-a-repo) the Hibernate Validator [repository](https://github.com/hibernate/hibernate-validator).
As discussed in the linked page, this also includes:
* [Setting](https://help.github.com/articles/set-up-git) up your local git install
* Cloning your fork
## Create a test case
If you have opened a JIRA issue but are not comfortable enough to contribute code directly, creating a self
contained test case is a good first step towards contributing.
As part of our efforts to simplify access to new contributors, we provide [test case templates for the Hibernate family
projects](https://github.com/hibernate/hibernate-test-case-templates).
Just fork this repository, build your test case and attach it as an archive to a JIRA issue.
## Create a topic branch
Create a "topic" branch on which you will work. The convention is to name the branch
using the JIRA issue key. If there is not already a JIRA issue covering the work you
want to do, create one. Assuming you will be working from the master branch and working
on the JIRA HV-123 :
git checkout -b HV-123 master
## Code
Code away...
## Formatting rules and style conventions
The Hibernate family projects share the same style conventions. You can download the appropriate configuration
files for your IDE from [the IDE codestyles GitHub repository](https://github.com/hibernate/hibernate-ide-codestyles).
You can very quickly check that you have respected the formatting rules by running Checkstyle:
```shell
mvn checkstyle:check
```
## Commit
* Make commits of logical units.
* Be sure to start the commit messages with the JIRA issue key you are working on. This is how JIRA will pick
up the related commits and display them on the JIRA issue.
* Avoid formatting changes to existing code as much as possible: they make the intent of your patch less clear.
* Make sure you have added the necessary tests for your changes.
* Run _all_ the tests to assure nothing else was accidentally broken:
```shell
mvn verify
```
_Prior to committing, if you want to pull in the latest upstream changes (highly
appreciated by the way), please use rebasing rather than merging (see instructions below). Merging creates
"merge commits" that really muck up the project timeline._
Add the original Hibernate Validator repository as a remote repository called upstream:
```shell
git remote add upstream https://github.com/hibernate/hibernate-validator.git
```
If you want to rebase your branch on top of the master branch, you can use the following git command:
```shell
git pull --rebase upstream master
```
## Submit
* Sign the [Contributor License Agreement](https://cla.jboss.org/) for the Hibernate Validator project.
* Push your changes to a topic branch in your fork of the repository.
* Initiate a [pull request](http://help.github.com/send-pull-requests/).
* Update the JIRA issue, adding a comment including a link to the created pull request.
# Hibernate Validator
*Version: 4.3.4.Final, 29.05.2018*
*Version: 5.3.6.Final - 19-10-2017*
## What is it?
This is the reference implementation of JSR 303 - Bean Validation.
Bean Validation defines a metadata model and API for JavaBean validation.
The default metadata source is annotations, with the ability to override and extend
This is the reference implementation of [JSR-349 - Bean Validation 1.1](http://beanvalidation.org/).
Bean Validation defines a metadata model and API for JavaBean as well as method validation.
The default metadata source are annotations, with the ability to override and extend
the meta-data through the use of XML validation descriptors.
## A bit of history
Prior to version 4.x Hibernate Validators was based on a different source base which
is not based on JSR 303. This code can be accessed via [this](https://github.com/hibernate/hibernate-validator/tree/pre-validator3-removal/hibernate-validator-legacy) GitHub tag.
## Documentation
The documentation for this release is included in the docs directory of distribution package or can be accessed [online](http://hibernate.org/validator/documentation/).
The documentation for this release is included in the _docs_ directory of the distribution package or can be accessed [online](http://hibernate.org/validator/documentation/).
## Release Notes
......@@ -29,24 +24,47 @@ JDK 1.6 or above.
## Using Hibernate Validator
* In case you use the distribution archive from the download site, copy dist/hibernate-validator-<version>.jar together with all
jar files from dist/lib/required into the classpath of your application. For the purposes of logging, Hibernate Validator uses
* In case you use the distribution archive from the download site, copy _dist/hibernate-validator-&lt;version&gt;.jar_ together with all
jar files from _dist/lib/required_ into the classpath of your application. For the purposes of logging, Hibernate Validator uses
the JBoss Logging API, an abstraction layer which supports several logging solutions such (e.g. log4j or the logging framework
provided by the JDK) as implementation. Just add a supported logging library to the classpath (e.g. log4j-<version>.jar) and JBoss
provided by the JDK) as implementation. Just add a supported logging library to the classpath (e.g. _log4j-&lt;version&gt;.jar_) and JBoss
Logging will delegate any log requests to that provider.
* Add the following to your maven or ivy dependency list (Hibernate Validator can be found in the [JBoss Maven repository](http://repository.jboss.org/nexus/content/groups/public-jboss)):
* Add the following to your Maven or Ivy dependency list
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.4.Final</version>
<version>5.3.6.Final</version>
</dependency>
You also need an API and implementation of the Unified Expression Language. These dependencies must be explicitly added in an SE environment.
In an EE environment they are often already provided.
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>
* Bean Validation defines integration points with [CDI](http://jcp.org/en/jsr/detail?id=346). If your application runs
in an environment which does not provide this integration out of the box, you may use the Hibernate Validator CDI portable
extension by adding the following dependency:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-cdi</artifactId>
<version>5.3.6.Final</version>
</dependency>
*hibernate-validator-annotation-processor-<version>.jar* is an optional jar which can be integrated with your build
environment respectively IDE to verify that constraint annotations are correctly used. Refer to the online
documentation for more information.
* _hibernate-validator-annotation-processor-&lt;version&gt;.jar_ is an optional jar which can be integrated with your build
environment respectively IDE to verify that constraint annotations are correctly used. Refer to the [online
documentation](http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html/validator-annotation-processor.html) for more information.
## Licensing
......@@ -55,22 +73,43 @@ the Apache Software License 2.0. Refer to license.txt for more information.
## Build from Source
You can build Hibernate Validator from source by cloning the git repository git://github.com/hibernate/hibernate-validator.git
You will also need a JDK 6 or 7 and a Maven 3. With these prerequisites in place you can compile the source via
You can build Hibernate Validator from source by cloning the git repository git://github.com/hibernate/hibernate-validator.git.
You will also need a JDK 8 and Maven 3 (>= 3.0.3). With these prerequisites in place you can compile the source via
mvn clean install -s settings-example.xml
mvn -s settings-example.xml clean install
The documentation module requires an additional tool called po2xml. If you don't have po2xml installed you can
skip the building of the documentation via:
There are more build options available as well. For more information refer to [Contributing to Hibernate Validator](http://hibernate.org/validator/contribute/).
mvn clean install -DdisableDocumentationBuild=true -s settings-example.xml
### Build on JDK 9
There are more build options available as well. For more information refer to [Contributing to Hibernate Validator](http://hibernate.org/validator/contribute/).
To build Hibernate Validator with JDK 9, export the following environment variable:
export MAVEN_OPTS="--add-opens=java.base/java.security=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED"
Then the build can be started like this:
mvn -s settings-example.xml clean install
Also the OSGi integration tests will fail on Java 9 currently, hence the "osgi" module is excluded automatically when building on JDK 9. We are waiting for the release of a Karaf version supporting the latest Java 9 builds.
Here are the reasons why we added the various --add-opens options:
* java.security: required by wildfly-maven-plugin:execute-commands (for the WildFly integration tests and the TCK runner running in container mode)
* java.lang: required by JRuby for Asciidoc processing
* java.util: required by the gmavenplus script used to start WildFly
## Continuous Integration
The official Continuous Integration service for the project is hosted on [ci.hibernate.org](http://ci.hibernate.org/view/Validator/).
We provide a `.travis.yml` file so that you can enable CI for your GitHub fork by enabling the build in [your Travis CI account](https://travis-ci.org/).
## Hibernate Validator URLs
* [Home Page](http://hibernate.org/validator/)
* [Downloads](http://hibernate.org/validator/releases/4.3/)
* [Community Info](http://hibernate.org/community/)
* [Source Code](git://github.com/hibernate/hibernate-validator.git)
* [Issue Tracking](https://hibernate.atlassian.net/projects/HV)
* [Bean Validation Home](http://beanvalidation.org)
* [Downloads](http://hibernate.org/validator/downloads/)
* [Mailing Lists](http://hibernate.org/community/)
* [Issue Tracking](https://hibernate.atlassian.net/browse/HV)
* [Continuous Integration](http://ci.hibernate.org/view/Validator/) | [![Build Status](http://ci.hibernate.org/view/Validator/job/hibernate-validator-master/badge/icon)](http://ci.hibernate.org/view/Validator/job/hibernate-validator-master/)
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
~ Hibernate Validator, declare and validate application constraints
~
~ 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.
~ License: Apache License, Version 2.0
~ See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
......@@ -21,7 +11,7 @@
<parent>
<artifactId>hibernate-validator-parent</artifactId>
<groupId>org.hibernate</groupId>
<version>4.3.4.Final</version>
<version>5.3.6.Final</version>
<relativePath>../pom.xml</relativePath>
</parent>
......@@ -72,9 +62,17 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument>-proc:none</compilerArgument>
<proc>none</proc>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
......
/*
* 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.
* Hibernate Validator, declare and validate application constraints
*
* 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.
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.ap;
......
/*
* 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.
* Hibernate Validator, declare and validate application constraints
*
* 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.
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.ap;
......@@ -52,6 +42,7 @@
* set to <code>false</code> in order to allow only getter based property
* constraints but not method level constraints as supported by Hibernate
* Validator. Default is <code>true</code>.</li>
* </ul>
*
* @author Hardy Ferentschik
* @author 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.
* Hibernate Validator, declare and validate application constraints
*
* 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.
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.ap.checks;
......@@ -42,22 +32,26 @@
*/
public class AbstractConstraintCheck implements ConstraintCheck {
@Override
public Set<ConstraintCheckError> checkField(VariableElement element, AnnotationMirror annotation) {
return Collections.emptySet();
}
@Override
public Set<ConstraintCheckError> checkMethod(ExecutableElement element, AnnotationMirror annotation) {
return Collections.emptySet();
}
@Override
public Set<ConstraintCheckError> checkAnnotationType(TypeElement element,
AnnotationMirror annotation) {
return Collections.emptySet();
}
@Override
public Set<ConstraintCheckError> checkNonAnnotationType(
TypeElement element, AnnotationMirror annotation) {
......
/*
* 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.
* Hibernate Validator, declare and validate application constraints
*
* 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.
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.ap.checks;
......
/*
* 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.
* Hibernate Validator, declare and validate application constraints
*
* 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.
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.ap.checks;
......
/*
* 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.
* Hibernate Validator, declare and validate application constraints
*
* 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.
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.ap.checks;
......
/*
* 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.
* Hibernate Validator, declare and validate application constraints
*
* 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.
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.ap.checks;
......@@ -43,8 +33,10 @@ public class 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/>
* <p>
* <code>org.hibernate.validator.ap.ValidationProcessorMessages.</code>
* </p>
*
* @param messageParameters An array with values to put into the error message template
* using {@link java.text.MessageFormat}. The number of elements must match
* the number of place holders in the message template.
......
/*
* 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.
* Hibernate Validator, declare and validate application constraints
*
* 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.
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.ap.checks;
......@@ -56,7 +46,7 @@ public class ConstraintCheckFactory {
private ConstraintHelper constraintHelper;
private final static SingleValuedChecks NULL_CHECKS = new SingleValuedChecks();
private static final SingleValuedChecks NULL_CHECKS = new SingleValuedChecks();
public ConstraintCheckFactory(Types typeUtils, ConstraintHelper constraintHelper, AnnotationApiHelper annotationApiHelper, boolean methodConstraintsSupported) {
......@@ -80,16 +70,16 @@ public ConstraintCheckFactory(Types typeUtils, ConstraintHelper constraintHelper
methodChecks = CollectionHelper.newHashMap();
methodChecks.put(
AnnotationType.CONSTRAINT_ANNOTATION,
new SingleValuedChecks( new GetterCheck(methodConstraintsSupported), new StaticCheck(), new TypeCheck( constraintHelper ) )
new SingleValuedChecks( new GetterCheck(methodConstraintsSupported), new StaticCheck(), new MethodAnnotationCheck(constraintHelper), new TypeCheck( constraintHelper ) )
);
methodChecks.put(
AnnotationType.MULTI_VALUED_CONSTRAINT_ANNOTATION, new MultiValuedChecks(
constraintHelper, new GetterCheck(methodConstraintsSupported), new StaticCheck(), new TypeCheck( constraintHelper )
constraintHelper, new GetterCheck(methodConstraintsSupported), new StaticCheck(), new MethodAnnotationCheck(constraintHelper), new TypeCheck( constraintHelper )
)
);
methodChecks.put(
AnnotationType.GRAPH_VALIDATION_ANNOTATION,
new SingleValuedChecks( new GetterCheck(methodConstraintsSupported), new StaticCheck(), new PrimitiveCheck() )
new SingleValuedChecks( new GetterCheck(methodConstraintsSupported), new StaticCheck(), new MethodAnnotationCheck(constraintHelper), new PrimitiveCheck() )
);
methodChecks.put( AnnotationType.NO_CONSTRAINT_ANNOTATION, NULL_CHECKS );
......@@ -108,7 +98,8 @@ constraintHelper, new GetterCheck(methodConstraintsSupported), new StaticCheck()
new RetentionPolicyCheck( annotationApiHelper ),
new TargetCheck( annotationApiHelper ),
new ConstraintValidatorCheck( constraintHelper, annotationApiHelper ),
new AnnotationTypeMemberCheck( annotationApiHelper, typeUtils )
new AnnotationTypeMemberCheck( annotationApiHelper, typeUtils ),
new CrossParameterConstraintCheck(annotationApiHelper, constraintHelper, typeUtils)
)
);
annotationTypeChecks.put( AnnotationType.NO_CONSTRAINT_ANNOTATION, NULL_CHECKS );
......
/*
* 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.
* Hibernate Validator, declare and validate application constraints
*
* 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.
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.ap.checks;
......
/*
* 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.
* Hibernate Validator, declare and validate application constraints
*
* 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.
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.ap.checks;
......
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
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.DeclaredType;
import javax.lang.model.util.ElementKindVisitor6;
import javax.lang.model.util.SimpleAnnotationValueVisitor6;
import javax.lang.model.util.TypeKindVisitor6;
import javax.lang.model.util.Types;
import org.hibernate.validator.ap.util.AnnotationApiHelper;
import org.hibernate.validator.ap.util.CollectionHelper;
import org.hibernate.validator.ap.util.ConstraintHelper;
import org.hibernate.validator.ap.util.ConstraintHelper.AnnotationProcessorValidationTarget;
import org.hibernate.validator.ap.util.ConstraintHelper.ConstraintCheckResult;
import org.hibernate.validator.ap.util.TypeNames.BeanValidationTypes;
/**
* Checks that a cross-parameter constraint is defined correctly with reference to the specifications.
* <p>
* <ul>
* <li>It must have at most one cross-parameter validator.</li>
* <li>The cross-parameter validator must resolve to Object or Object[].</li>
* <li>If the constraint is both normal and cross-parameter, it must define a 'validationAppliesTo()' attribute.</li>
* <li>The 'validationAppliesTo' method, if any, must return a {@code ConstraintTarget}.</li>
* <li>The 'validationAppliesTo' method, if any, must declare {@code ConstraintTarget#IMPLICIT} as default return value.</li>
* </ul>
*
* @author Nicola Ferraro
*/
public class CrossParameterConstraintCheck extends AbstractConstraintCheck {
private final AnnotationApiHelper annotationApiHelper;
private final ConstraintHelper constraintHelper;
private final Types typeUtils;
public CrossParameterConstraintCheck(AnnotationApiHelper annotationApiHelper, ConstraintHelper constraintHelper, Types typeUtils) {
this.annotationApiHelper = annotationApiHelper;
this.constraintHelper = constraintHelper;
this.typeUtils = typeUtils;
}
@Override
public Set<ConstraintCheckError> checkAnnotationType(TypeElement element, AnnotationMirror annotation) {
// this check applies to constraint annotations
if ( !constraintHelper.isConstraintAnnotation( element ) ) {
return Collections.emptySet();
}
DeclaredType elementType = element.asType().accept( new TypeKindVisitor6<DeclaredType, Void>() {
@Override
public DeclaredType visitDeclared(DeclaredType t, Void p) {
return t;
}
}, null );
Set<AnnotationProcessorValidationTarget> targets = constraintHelper.getSupportedValidationTargets( elementType );
if ( !targets.contains( AnnotationProcessorValidationTarget.PARAMETERS ) ) {
return Collections.emptySet();
}
// Check cross parameter validators
ConstraintCheckResult res = constraintHelper.checkCrossParameterTypes( elementType );
if ( res == ConstraintCheckResult.MULTIPLE_VALIDATORS_FOUND ) {
return CollectionHelper.asSet(
new ConstraintCheckError(
element,
annotation,
"CROSS_PARAMETER_CONSTRAINT_MULTIPLE_VALIDATORS",
element.getSimpleName().toString() ) );
}
else if ( res == ConstraintCheckResult.DISALLOWED ) {
return CollectionHelper.asSet(
new ConstraintCheckError(
element,
annotation,
"CROSS_PARAMETER_CONSTRAINT_VALIDATOR_HAS_INVALID_TYPE",
element.getSimpleName() ) );
}
// Check validationAppliesTo method
ExecutableElement validationAppliesTo = getValidationAppliesToMethod( element );
if ( validationAppliesTo == null && targets.size() > 1 ) {
// validationAppliesTo is required to let the user specify the constraint target
return CollectionHelper.asSet(
new ConstraintCheckError(
element,
annotation,
"CROSS_PARAMETER_VALIDATION_APPLIES_TO_REQUIRED",
element.getSimpleName() ) );
}
if ( validationAppliesTo != null ) {
if ( !checkValidationAppliesToReturnType( validationAppliesTo ) ) {
return CollectionHelper.asSet(
new ConstraintCheckError(
element,
annotation,
"CROSS_PARAMETER_VALIDATION_APPLIES_TO_MUST_HAVE_CONSTRAINT_TARGET_RETURN_TYPE",
element.getSimpleName() ) );
}
else if ( !checkValidationAppliesToDefaultValue( validationAppliesTo ) ) {
return CollectionHelper.asSet(
new ConstraintCheckError(
element,
annotation,
"CROSS_PARAMETER_VALIDATION_APPLIES_TO_MUST_HAVE_IMPLICIT_DEFAULT_VALUE",
element.getSimpleName() ) );
}
}
return Collections.emptySet();
}
private boolean checkValidationAppliesToReturnType(ExecutableElement validationAppliesToMethod) {
final DeclaredType constraintTargetType = annotationApiHelper.getDeclaredTypeByName( BeanValidationTypes.CONSTRAINT_TARGET );
// Check the return type
return validationAppliesToMethod.getReturnType().accept( new TypeKindVisitor6<Boolean, Void>() {
@Override
public Boolean visitDeclared(DeclaredType t, Void p) {
if ( typeUtils.isSameType( constraintTargetType, t ) ) {
return true;
}
return false;
}
}, null );
}
private boolean checkValidationAppliesToDefaultValue(ExecutableElement validationAppliesToMethod) {
final DeclaredType constraintTargetType = annotationApiHelper.getDeclaredTypeByName( BeanValidationTypes.CONSTRAINT_TARGET );
// Check the return type
return validationAppliesToMethod.getDefaultValue().accept( new SimpleAnnotationValueVisitor6<Boolean, Void>() {
@Override
public Boolean visitEnumConstant(VariableElement c, Void p) {
if ( typeUtils.isSameType( constraintTargetType, c.asType() ) ) {
return c.getSimpleName().contentEquals( "IMPLICIT" );
}
return false;
}
}, null );
}
private ExecutableElement getValidationAppliesToMethod(Element annotation) {
for ( Element e : annotation.getEnclosedElements() ) {
ExecutableElement method = e.accept( new ElementKindVisitor6<ExecutableElement, Void>() {
@Override
public ExecutableElement visitExecutableAsMethod(ExecutableElement e, Void p) {
if ( e.getSimpleName().contentEquals( "validationAppliesTo" ) ) {
return e;
}
return null;
}
}, null );
if ( method != null ) {
return method;
}
}
return null;
}
}
/*
* 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.
* Hibernate Validator, declare and validate application constraints
*
* 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.
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.ap.checks;
......@@ -37,6 +27,7 @@ public GetterCheck(boolean methodConstraintsSupported) {
this.methodConstraintsSupported = methodConstraintsSupported;
}
@Override
public Set<ConstraintCheckError> checkMethod(ExecutableElement element,
AnnotationMirror annotation) {
......@@ -47,13 +38,8 @@ public Set<ConstraintCheckError> checkMethod(ExecutableElement element,
)
);
}
else if (!hasReturnValue(element)) {
return CollectionHelper.asSet(
new ConstraintCheckError(
element, annotation, "ONLY_NON_VOID_METHODS_MAY_BE_ANNOTATED"
)
);
}
// HV-864: void methods support cross-parameter constraints. We do not enforce the check here.
return Collections.emptySet();
}
......
/*
* 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.
* Hibernate Validator, declare and validate application constraints
*
* 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.
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.ap.checks;
......@@ -38,19 +28,23 @@
import org.hibernate.validator.ap.util.TypeNames.HibernateValidatorTypes;
/**
* Checks that the {@link org.hibernate.validator.group.GroupSequenceProvider} annotation definition is valid.
* <p>
* Checks that the {@link org.hibernate.validator.group.GroupSequenceProvider}
* annotation definition is valid.
* <br>
* This check ensure that :
* <ul>
* <li>The annotation is not defined on an interface.</li>
* <li>The annotation defines an implementation class of {@link org.hibernate.validator.group.DefaultGroupSequenceProvider}, not an interface or an abstract class.</li>
* <li>The annotation defines an implementation class of
* {@link org.hibernate.validator.spi.group.DefaultGroupSequenceProvider}, not
* an interface or an abstract class.</li>
* <li>The annotation defines a class with a public default constructor.</li>
* <li>The annotation defines a default group sequence provider class for a (super-)type of the annotated class.</li>
* <li>The class hosting the annotation is not already annotated with {@link javax.validation.GroupSequence}.</li>
* <li>The annotation defines a default group sequence provider class for a
* (super-)type of the annotated class.</li>
* <li>The class hosting the annotation is not already annotated with
* {@link javax.validation.GroupSequence}.</li>
* </ul>
* </p>
*
* @author Kevin Pollet <kevin.pollet@serli.com> (C) 2011 SERLI
* @author Kevin Pollet &lt;kevin.pollet@serli.com&gt; (C) 2011 SERLI
*/
public class GroupSequenceProviderCheck extends AbstractConstraintCheck {
......
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
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;
import org.hibernate.validator.ap.util.ConstraintHelper;
import org.hibernate.validator.ap.util.ConstraintHelper.AnnotationProcessorValidationTarget;
/**
* Checks whether a method is correctly annotated with a valid constraint involving the return type or the method
* parameters (cross-parameters).
*
* @author Nicola Ferraro
*/
public class MethodAnnotationCheck extends AbstractConstraintCheck {
private ConstraintHelper constraintHelper;
public MethodAnnotationCheck(ConstraintHelper constraintHelper) {
this.constraintHelper = constraintHelper;
}
@Override
public Set<ConstraintCheckError> checkMethod(ExecutableElement element,
AnnotationMirror annotation) {
// Annotations on methods/constructors can refer to return type or parameters (not both)
AnnotationProcessorValidationTarget target;
// Constraint annotations can define a different validation target
if ( constraintHelper.isConstraintAnnotation( annotation.getAnnotationType().asElement() ) ) {
Set<AnnotationProcessorValidationTarget> supportedTargets = constraintHelper.getSupportedValidationTargets( annotation.getAnnotationType() );
// at least one target is always returned
if ( supportedTargets.size() != 1 ) {
// when multiple targets are supported, the actual target must be disambiguated using a
// 'validationAppliesTo' property
// resolve the actual target depending on the element on which it is applied
target = constraintHelper.resolveValidationTarget( element, annotation );
if ( target == null ) {
return CollectionHelper.asSet(
new ConstraintCheckError(
element, annotation, "CROSS_PARAMETER_TARGET_NOT_INFERABLE",
annotation.getAnnotationType().asElement().getSimpleName() ) );
}
}
else {
// get the single validation target
target = supportedTargets.toArray( new AnnotationProcessorValidationTarget[1] )[0];
}
}
else {
target = AnnotationProcessorValidationTarget.ANNOTATED_ELEMENT;
}
if ( target == AnnotationProcessorValidationTarget.ANNOTATED_ELEMENT && !hasReturnValue( element ) ) {
return CollectionHelper.asSet(
new ConstraintCheckError(
element, annotation, "ONLY_NON_VOID_METHODS_MAY_BE_ANNOTATED" ) );
}
if ( target == AnnotationProcessorValidationTarget.PARAMETERS && !hasParameters( element ) ) {
return CollectionHelper.asSet(
new ConstraintCheckError(
element, annotation, "CROSS_PARAMETER_VALIDATION_ON_PARAMETERLESS_METHOD",
annotation.getAnnotationType().asElement().getSimpleName() ) );
}
return Collections.emptySet();
}
private boolean hasParameters(ExecutableElement method) {
return method.getParameters().size() > 0;
}
private boolean hasReturnValue(ExecutableElement method) {
return method.getReturnType().getKind() != TypeKind.VOID;
}
}