Hibernate SVN: r16149 - validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2009-03-12 15:55:18 -0400 (Thu, 12 Mar 2009)
New Revision: 16149
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java
Log:
adjust log level
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java 2009-03-12 18:41:53 UTC (rev 16148)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java 2009-03-12 19:55:18 UTC (rev 16149)
@@ -50,10 +50,10 @@
private void detectJPA() {
try {
ReflectionHelper.classForName( PERSISTENCE_UTIL_CLASS_NAME, this.getClass() );
- log.info( "Found {} on classpath.", PERSISTENCE_UTIL_CLASS_NAME );
+ log.debug( "Found {} on classpath.", PERSISTENCE_UTIL_CLASS_NAME );
}
catch ( ClassNotFoundException e ) {
- log.info(
+ log.debug(
"Cannot find {} on classpath. All properties will per default be traversable.",
PERSISTENCE_UTIL_CLASS_NAME
);
15 years, 9 months
Hibernate SVN: r16148 - validator/trunk/hibernate-validator/src/main/assembly.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-03-12 14:41:53 -0400 (Thu, 12 Mar 2009)
New Revision: 16148
Modified:
validator/trunk/hibernate-validator/src/main/assembly/dist.xml
Log:
HV-90 Updated the dist assembly to work with the shading of jtype.
Modified: validator/trunk/hibernate-validator/src/main/assembly/dist.xml
===================================================================
--- validator/trunk/hibernate-validator/src/main/assembly/dist.xml 2009-03-12 18:21:01 UTC (rev 16147)
+++ validator/trunk/hibernate-validator/src/main/assembly/dist.xml 2009-03-12 18:41:53 UTC (rev 16148)
@@ -31,6 +31,9 @@
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
+ <excludes>
+ <exclude>com.googlecode.jtype:jtype</exclude>
+ </excludes>
</dependencySet>
</dependencySets>
@@ -49,13 +52,13 @@
<fileSets>
<fileSet>
<directory>target</directory>
- <outputDirectory></outputDirectory>
+ <outputDirectory/>
<excludes>
<exclude>*-javadoc.jar</exclude>
<exclude>*-sources.jar</exclude>
</excludes>
<includes>
- <include>*.jar</include>
+ <include>hibernate-*.jar</include>
</includes>
</fileSet>
<fileSet>
@@ -68,9 +71,10 @@
</fileSet>
<fileSet>
<directory>.</directory>
- <outputDirectory></outputDirectory>
+ <outputDirectory/>
<useDefaultExcludes>true</useDefaultExcludes>
<excludes>
+ <exclude>dependency-reduced-pom.xml</exclude>
<exclude>*.txt</exclude>
<exclude>**/target/**</exclude>
<exclude>*.iml</exclude>
15 years, 9 months
Hibernate SVN: r16147 - in validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine: graphnavigation and 1 other directory.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-03-12 14:21:01 -0400 (Thu, 12 Mar 2009)
New Revision: 16147
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/Address.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/GraphNavigationTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/Order.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/OrderLine.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/User.java
Log:
HV-126 Added testcase. Still needs code changes.
Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/Address.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/Address.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/Address.java 2009-03-12 18:21:01 UTC (rev 16147)
@@ -0,0 +1,83 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, 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.validation.engine.graphnavigation;
+
+import javax.validation.Valid;
+import javax.validation.constraints.NotNull;
+
+import org.hibernate.validation.constraints.Length;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class Address {
+
+ @NotNull
+ @Length(max = 30)
+ private String addressline1;
+
+ private String zipCode;
+
+ @Length(max = 30)
+ @NotNull
+ private String city;
+
+ @Valid
+ private User inhabitant;
+
+ public Address() {
+ }
+
+ public Address(String addressline1, String zipCode, String city) {
+ this.addressline1 = addressline1;
+ this.zipCode = zipCode;
+ this.city = city;
+ }
+
+ public String getAddressline1() {
+ return addressline1;
+ }
+
+ public void setAddressline1(String addressline1) {
+ this.addressline1 = addressline1;
+ }
+
+ public String getZipCode() {
+ return zipCode;
+ }
+
+ public void setZipCode(String zipCode) {
+ this.zipCode = zipCode;
+ }
+
+ public String getCity() {
+ return city;
+ }
+
+ public void setCity(String city) {
+ this.city = city;
+ }
+
+ public User getInhabitant() {
+ return inhabitant;
+ }
+
+ public void setInhabitant(User inhabitant) {
+ this.inhabitant = inhabitant;
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/Address.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/GraphNavigationTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/GraphNavigationTest.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/GraphNavigationTest.java 2009-03-12 18:21:01 UTC (rev 16147)
@@ -0,0 +1,63 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, 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.validation.engine.graphnavigation;
+
+import java.util.Set;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+
+import static org.junit.Assert.assertEquals;
+import org.junit.Test;
+
+import org.hibernate.validation.util.TestUtil;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class GraphNavigationTest {
+
+ @Test
+ public void testGraphNavigationDeterminism() {
+ // build the test object graph
+ User user = new User( "John", "Doe" );
+
+ Address address1 = new Address( null, "11122", "Stockholm" );
+ address1.setInhabitant( user );
+
+ Address address2 = new Address( "Kungsgatan 5", "11122", "Stockholm" );
+ address2.setInhabitant( user );
+
+ user.addAddress( address1 );
+ user.addAddress( address2 );
+
+ Order order = new Order( 1 );
+ order.setShippingAddress( address1 );
+ order.setBillingAddress( address2 );
+ order.setCustomer( user );
+
+ OrderLine line1 = new OrderLine( order, 42 );
+ OrderLine line2 = new OrderLine( order, 101 );
+ order.addOrderLine( line1 );
+ order.addOrderLine( line2 );
+
+ Validator validator = TestUtil.getValidator();
+
+ Set<ConstraintViolation<Order>> constraintViolations = validator.validate( order );
+ assertEquals( "Wrong number of constraints", 1, constraintViolations.size() );
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/GraphNavigationTest.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/Order.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/Order.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/Order.java 2009-03-12 18:21:01 UTC (rev 16147)
@@ -0,0 +1,80 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, 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.validation.engine.graphnavigation;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import javax.validation.Valid;
+import javax.validation.constraints.NotNull;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class Order {
+ @NotNull
+ Integer orderId;
+
+ @Valid
+ private List<OrderLine> orderLines = new ArrayList<OrderLine>();
+
+ @Valid
+ private User customer;
+
+ @Valid
+ private Address shippingAddress;
+
+ @Valid
+ private Address billingAddress;
+
+ public Order(Integer id) {
+ orderId = id;
+ }
+
+ public void addOrderLine(OrderLine orderLine) {
+ orderLines.add( orderLine );
+ }
+
+ public List<OrderLine> getOrderLines() {
+ return Collections.unmodifiableList( orderLines );
+ }
+
+ public User getCustomer() {
+ return customer;
+ }
+
+ public void setCustomer(User customer) {
+ this.customer = customer;
+ }
+
+ public Address getShippingAddress() {
+ return shippingAddress;
+ }
+
+ public void setShippingAddress(Address shippingAddress) {
+ this.shippingAddress = shippingAddress;
+ }
+
+ public Address getBillingAddress() {
+ return billingAddress;
+ }
+
+ public void setBillingAddress(Address billingAddress) {
+ this.billingAddress = billingAddress;
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/Order.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/OrderLine.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/OrderLine.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/OrderLine.java 2009-03-12 18:21:01 UTC (rev 16147)
@@ -0,0 +1,45 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, 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.validation.engine.graphnavigation;
+
+import javax.validation.constraints.NotNull;
+import javax.validation.Valid;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class OrderLine {
+ @NotNull
+ Integer articleNumber;
+
+ @Valid
+ Order order;
+
+ public OrderLine(Order order, Integer articleNumber) {
+ this.articleNumber = articleNumber;
+ this.order = order;
+ }
+
+ public Integer getArticleNumber() {
+ return articleNumber;
+ }
+
+ public void setArticleNumber(Integer articleNumber) {
+ this.articleNumber = articleNumber;
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/OrderLine.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/User.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/User.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/User.java 2009-03-12 18:21:01 UTC (rev 16147)
@@ -0,0 +1,71 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, 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.validation.engine.graphnavigation;
+
+import java.util.HashSet;
+import java.util.Set;
+import javax.validation.constraints.NotNull;
+import javax.validation.groups.Default;
+import javax.validation.Valid;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class User {
+
+ @NotNull
+ private String firstName;
+
+ @NotNull(groups = Default.class)
+ private String lastName;
+
+ @Valid
+ private Set<Address> addresses = new HashSet<Address>();
+
+ public User() {
+ }
+
+ public User(String firstName, String lastName) {
+ this.firstName = firstName;
+ this.lastName = lastName;
+ }
+
+ public Set<Address> getAddresses() {
+ return addresses;
+ }
+
+ public void addAddress(Address address) {
+ addresses.add( address );
+ }
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/graphnavigation/User.java
___________________________________________________________________
Name: svn:keywords
+ Id
15 years, 9 months
Hibernate SVN: r16146 - in validator/trunk/hibernate-validator: src/main/java/org/hibernate/validation/engine and 3 other directories.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-03-12 14:11:00 -0400 (Thu, 12 Mar 2009)
New Revision: 16146
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeList.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeListValidatorForNumber.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeListValidatorForString.java
Modified:
validator/trunk/hibernate-validator/pom.xml
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ValidatorTypeHelper.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/Suburb.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/ValidatorResolutionTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ValidatorTypeTest.java
Log:
HV-90 - Added jtype 0.1 as dependency and use the maven shade plugin to include the class files directly into the jar file
Modified: validator/trunk/hibernate-validator/pom.xml
===================================================================
--- validator/trunk/hibernate-validator/pom.xml 2009-03-11 13:55:02 UTC (rev 16145)
+++ validator/trunk/hibernate-validator/pom.xml 2009-03-12 18:11:00 UTC (rev 16146)
@@ -28,6 +28,11 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
+ <dependency>
+ <groupId>com.googlecode.jtype</groupId>
+ <artifactId>jtype</artifactId>
+ <version>0.1.0</version>
+ </dependency>
<dependency>
<groupId>org.slf4j</groupId>
@@ -67,6 +72,28 @@
</resources>
<plugins>
<plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-shade-plugin</artifactId>
+ <executions>
+ <execution>
+ <phase>package</phase>
+ <goals>
+ <goal>shade</goal>
+ </goals>
+ <configuration>
+ <artifactSet>
+ <includes>
+ <include>com.googlecode.jtype:jtype</include>
+ </includes>
+ </artifactSet>
+ <transformers>
+ <transformer implementation="org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer"/>
+ </transformers>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
<groupId>org.jboss.maven.plugins</groupId>
<artifactId>maven-jdocbook-plugin</artifactId>
<version>2.1.0</version>
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java 2009-03-11 13:55:02 UTC (rev 16145)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java 2009-03-12 18:11:00 UTC (rev 16146)
@@ -19,6 +19,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
+import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -29,6 +30,7 @@
import javax.validation.UnexpectedTypeException;
import javax.validation.ValidationException;
+import com.googlecode.jtype.TypeUtils;
import org.slf4j.Logger;
import org.hibernate.validation.util.LoggerFactory;
@@ -36,7 +38,7 @@
/**
* Due to constraint conposition a single constraint annotation can lead to a whole constraint tree beeing validated.
- * This class encapsulates such a tree for a single constraint annotation.
+ * This class encapsulates such a tree.
*
* @author Hardy Ferentschik
*/
@@ -88,16 +90,27 @@
return descriptor;
}
- public <T, V> void validateConstraints(V value, ExecutionContext<T> executionContext, List<ConstraintViolationImpl<T>> constraintViolations) {
+ /**
+ * Validates the specified value.
+ *
+ * @param value The value to validate
+ * @param type The type of the value determined from the type the annotation was placed on.
+ * @param executionContext The current execution context.
+ * @param constraintViolations List of constraint violation into which to accumulate all constraint violation as we traverse
+ * this <code>ConstraintTree </code>.
+ * @param <T> Type of the root bean for the current validation.
+ * @param <V> Type of the value to be validated.
+ */
+ public <T, V> void validateConstraints(V value, Type type, ExecutionContext<T> executionContext, List<ConstraintViolationImpl<T>> constraintViolations) {
for ( ConstraintTree<?> tree : getChildren() ) {
- tree.validateConstraints( value, executionContext, constraintViolations );
+ tree.validateConstraints( value, type, executionContext, constraintViolations );
}
if ( log.isTraceEnabled() ) {
log.trace( "Validating value {} against constraint defined by {}", value, descriptor );
}
ConstraintValidator<A, V> validator = getInitalizedValidator(
- value, executionContext.getConstraintValidatorFactory()
+ value, type, executionContext.getConstraintValidatorFactory()
);
executionContext.setCurrentConstraintDescriptor( descriptor );
if ( !validator.isValid( value, executionContext ) ) {
@@ -119,58 +132,65 @@
/**
* @param value The value to be validated.
+ * @param type The type of the value to be validated (the type of the member/class the constraint was placed on).
* @param constraintFactory constraint factory used to instantiate the constraint validator.
*
* @return A initalized constraint validator matching the type of the value to be validated.
*/
- private <V> ConstraintValidator<A, V> getInitalizedValidator(Object value, ConstraintValidatorFactory constraintFactory) {
- Class<? extends ConstraintValidator<?, ?>> validatorClass;
- //FIXME This sounds really bad, why value can be null. Why are we deciding of the validator based on the value?
- if ( value == null ) {
- validatorClass = descriptor.getConstraintValidatorClasses().get( 0 );
- }
- else {
- validatorClass = findMatchingValidatorClass( value );
- }
+ private <V> ConstraintValidator<A, V> getInitalizedValidator(V value, Type type, ConstraintValidatorFactory constraintFactory) {
+ Class<? extends ConstraintValidator<?, ?>> validatorClass = findMatchingValidatorClass( value, type );
+
@SuppressWarnings("unchecked")
- ConstraintValidator<?, ?> constraintValidator =
- constraintFactory.getInstance( validatorClass );
+ ConstraintValidator<A, V> constraintValidator = ( ConstraintValidator<A, V> ) constraintFactory.getInstance(
+ validatorClass
+ );
initializeConstraint( descriptor, constraintValidator );
- return ( ConstraintValidator<A, V> ) constraintValidator;
+ return constraintValidator;
}
/**
* Runs the validator resolution algorithm.
*
- * @param value the object to validate
+ * @param value The value to be validated.
+ * @param type The type of the value to be validated (the type of the member/class the constraint was placed on).
*
* @return The class of a matching validator.
*/
- private Class<? extends ConstraintValidator<?, ?>> findMatchingValidatorClass(Object value) {
+ private Class<? extends ConstraintValidator<?, ?>> findMatchingValidatorClass(Object value, Type type) {
+ Map<Type, Class<? extends ConstraintValidator<?, ?>>> validatorsTypes =
+ ValidatorTypeHelper.getValidatorsTypes( descriptor.getConstraintValidatorClasses() );
- Class valueClass = determineValueClass( value );
+ List<Type> suitableTypes = new ArrayList<Type>();
+ findSuitableValidatorTypes( type, validatorsTypes, suitableTypes );
+ if ( value != null ) {
+ findSuitableValidatorTypes( determineValueClass( value ), validatorsTypes, suitableTypes );
+ }
- Map<Class<?>, Class<? extends ConstraintValidator<?, ?>>> validatorsTypes =
- ValidatorTypeHelper.getValidatorsTypes( ( List<Class<? extends ConstraintValidator<A, ?>>> ) descriptor.getConstraintValidatorClasses() );
- List<Class> assignableClasses = findAssignableClasses( valueClass, validatorsTypes );
+ resolveAssignableTypes( suitableTypes );
+ verifyResolveWasUnique( type, suitableTypes );
- resolveAssignableClasses( assignableClasses );
- verifyResolveWasUnique( valueClass, assignableClasses );
+ return validatorsTypes.get( suitableTypes.get( 0 ) );
+ }
- return validatorsTypes.get( assignableClasses.get( 0 ) );
+ private Class determineValueClass(Object value) {
+ Class valueClass = value.getClass();
+ if ( valueClass.isArray() ) {
+ valueClass = Array.class;
+ }
+ return valueClass;
}
- private void verifyResolveWasUnique(Class valueClass, List<Class> assignableClasses) {
+ private void verifyResolveWasUnique(Type valueClass, List<Type> assignableClasses) {
if ( assignableClasses.size() == 0 ) {
- throw new UnexpectedTypeException( "No validator could be found for type: " + valueClass.getName() );
+ throw new UnexpectedTypeException( "No validator could be found for type: " + valueClass );
}
else if ( assignableClasses.size() > 1 ) {
StringBuilder builder = new StringBuilder();
builder.append( "There are multiple validators which could validate the type " );
builder.append( valueClass );
builder.append( ". The validator classes are: " );
- for ( Class clazz : assignableClasses ) {
- builder.append( clazz.getName() );
+ for ( Type clazz : assignableClasses ) {
+ builder.append( clazz );
builder.append( ", " );
}
builder.delete( builder.length() - 2, builder.length() );
@@ -178,54 +198,44 @@
}
}
- private List<Class> findAssignableClasses(Class valueClass, Map<Class<?>, Class<? extends ConstraintValidator<?, ?>>> validatorsTypes) {
- List<Class> assignableClasses = new ArrayList<Class>();
- for ( Class clazz : validatorsTypes.keySet() ) {
- if ( clazz.isAssignableFrom( valueClass ) ) {
- assignableClasses.add( clazz );
+ private void findSuitableValidatorTypes(Type type, Map<Type, Class<? extends ConstraintValidator<?, ?>>> validatorsTypes, List<Type> suitableTypes) {
+ for ( Type validatorType : validatorsTypes.keySet() ) {
+ if ( TypeUtils.isAssignable( validatorType, type ) && !suitableTypes.contains( validatorType ) ) {
+ suitableTypes.add( validatorType );
}
}
- return assignableClasses;
}
- private Class determineValueClass(Object value) {
- Class valueClass = value.getClass();
- if ( valueClass.isArray() ) {
- valueClass = Array.class;
- }
- return valueClass;
- }
-
/**
* Tries to reduce all assignable classes down to a single class.
*
- * @param assignableClasses The set of all classes which are assignable to the class of the value to be validated and
+ * @param assignableTypes The set of all classes which are assignable to the class of the value to be validated and
* which are handled by at least one of the validators for the specified constraint.
*/
- private void resolveAssignableClasses(List<Class> assignableClasses) {
- if ( assignableClasses.size() == 0 || assignableClasses.size() == 1 ) {
+ private void resolveAssignableTypes(List<Type> assignableTypes) {
+ if ( assignableTypes.size() == 0 || assignableTypes.size() == 1 ) {
return;
}
- List<Class> classesToRemove = new ArrayList<Class>();
+ List<Type> typesToRemove = new ArrayList<Type>();
do {
- classesToRemove.clear();
- Class clazz = assignableClasses.get( 0 );
- for ( int i = 1; i < assignableClasses.size(); i++ ) {
- if ( clazz.isAssignableFrom( assignableClasses.get( i ) ) ) {
- classesToRemove.add( clazz );
+ typesToRemove.clear();
+ Type type = assignableTypes.get( 0 );
+ for ( int i = 1; i < assignableTypes.size(); i++ ) {
+ if ( TypeUtils.isAssignable( type, assignableTypes.get( i ) ) ) {
+ typesToRemove.add( type );
}
- else if ( assignableClasses.get( i ).isAssignableFrom( clazz ) ) {
- classesToRemove.add( assignableClasses.get( i ) );
+ else if ( TypeUtils.isAssignable( assignableTypes.get( i ), type ) ) {
+ typesToRemove.add( assignableTypes.get( i ) );
}
}
- assignableClasses.removeAll( classesToRemove );
- } while ( classesToRemove.size() > 0 );
+ assignableTypes.removeAll( typesToRemove );
+ } while ( typesToRemove.size() > 0 );
}
- private void initializeConstraint
+ private <V> void initializeConstraint
(ConstraintDescriptor<A>
- descriptor, ConstraintValidator
+ descriptor, ConstraintValidator<A, V>
constraintValidator) {
try {
constraintValidator.initialize( descriptor.getAnnotation() );
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java 2009-03-11 13:55:02 UTC (rev 16145)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java 2009-03-12 18:11:00 UTC (rev 16146)
@@ -19,14 +19,16 @@
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
+import java.lang.reflect.Array;
import java.lang.reflect.Field;
+import java.lang.reflect.GenericArrayType;
+import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.validation.ConstraintDescriptor;
-import javax.validation.ValidationException;
import org.hibernate.validation.util.ReflectionHelper;
@@ -44,24 +46,16 @@
private final ConstraintTree<A> constraintTree;
/**
- * The type (class) the constraint was defined on. <code>null</code> if the constraint was specified on method or
- * field level.
+ * The type the constraint was defined on.
*/
private final Type type;
/**
- * The method the constraint was defined on. <code>null</code> if the constraint was specified on class or
- * field level.
+ * The member the constraint was defined on.
*/
- private final Method method;
+ private final Member member;
/**
- * The field the constraint was defined on. <code>null</code> if the constraint was specified on class or
- * method level.
- */
- private final Field field;
-
- /**
* The JavaBeans name for this constraint.
*/
private final String propertyName;
@@ -77,32 +71,28 @@
*/
private final Class<T> beanClass;
- public MetaConstraint(Type t, ConstraintDescriptor<A> constraintDescriptor) {
- this( t, null, null, ElementType.TYPE, ( Class<T> ) t.getClass(), constraintDescriptor, "" );
+ public MetaConstraint(Type type, ConstraintDescriptor<A> constraintDescriptor) {
+ this.type = type;
+ this.elementType = ElementType.TYPE;
+ this.member = null;
+ this.propertyName = "";
+ this.beanClass = ( Class<T> ) type.getClass();
+ constraintTree = new ConstraintTree<A>( constraintDescriptor );
}
- public MetaConstraint(Method m, Class<T> beanClass, ConstraintDescriptor<A> constraintDescriptor) {
- this(
- null,
- m,
- null,
- ElementType.METHOD,
- beanClass,
- constraintDescriptor,
- ReflectionHelper.getPropertyName( m )
- );
- }
-
- public MetaConstraint(Field f, Class<T> beanClass, ConstraintDescriptor<A> constraintDescriptor) {
- this( null, null, f, ElementType.FIELD, beanClass, constraintDescriptor, f.getName() );
- }
-
- private MetaConstraint(Type t, Method m, Field f, ElementType elementType, Class<T> beanClass, ConstraintDescriptor<A> constraintDescriptor, String property) {
- this.type = t;
- this.method = m;
- this.field = f;
- this.elementType = elementType;
- this.propertyName = property;
+ public MetaConstraint(Member member, Class<T> beanClass, ConstraintDescriptor<A> constraintDescriptor) {
+ if ( member instanceof Method ) {
+ this.elementType = ElementType.METHOD;
+ }
+ else if ( member instanceof Field ) {
+ this.elementType = ElementType.FIELD;
+ }
+ else {
+ throw new IllegalArgumentException( "Non allowed member type: " + member );
+ }
+ this.type = null;
+ this.member = member;
+ this.propertyName = ReflectionHelper.getPropertyName( member );
this.beanClass = beanClass;
constraintTree = new ConstraintTree<A>( constraintDescriptor );
}
@@ -117,31 +107,6 @@
}
/**
- * @param o the object from which to retrieve the value.
- *
- * @return Returns the value for this constraint from the specified object. Depending on the type either the value itself
- * is returned of method or field access is used to access the value.
- */
- public Object getValue(Object o) {
- switch ( elementType ) {
- case TYPE: {
- return o;
- }
- case METHOD: {
- return ReflectionHelper.getValue( method, o );
- }
- case FIELD: {
- return ReflectionHelper.getValue( field, o );
- }
- default: {
- throw new ValidationException(
- "Invalid state of MetaConstraint. Parameter elementType has unexpected value - " + elementType
- );
- }
- }
- }
-
- /**
* @return Returns <code>true</code> in case the constraint is defined on a collection, <code>false</code>
* otherwise.
*/
@@ -163,18 +128,6 @@
return constraintTree.getDescriptor();
}
- public Method getMethod() {
- return method;
- }
-
- public Field getField() {
- return field;
- }
-
- public Type getType() {
- return type;
- }
-
public Class<T> getBeanClass() {
return beanClass;
}
@@ -195,7 +148,7 @@
final Object leafBeanInstance = executionContext.peekCurrentBean();
Object value = getValue( leafBeanInstance );
List<ConstraintViolationImpl<T>> constraintViolations = new ArrayList<ConstraintViolationImpl<T>>();
- constraintTree.validateConstraints( value, executionContext, constraintViolations );
+ constraintTree.validateConstraints( value, typeOfAnnoatedElement(), executionContext, constraintViolations );
if ( constraintViolations.size() > 0 ) {
executionContext.addConstraintFailures( constraintViolations );
return false;
@@ -205,7 +158,7 @@
public <T> boolean validateConstraint(Object value, ExecutionContext<T> executionContext) {
List<ConstraintViolationImpl<T>> constraintViolations = new ArrayList<ConstraintViolationImpl<T>>();
- constraintTree.validateConstraints( value, executionContext, constraintViolations );
+ constraintTree.validateConstraints( value, typeOfAnnoatedElement(), executionContext, constraintViolations );
if ( constraintViolations.size() > 0 ) {
executionContext.addConstraintFailures( constraintViolations );
return false;
@@ -220,20 +173,30 @@
t = type;
break;
}
- case METHOD: {
- t = ReflectionHelper.typeOf( method );
- break;
+ default: {
+ t = ReflectionHelper.typeOf( member );
+ if ( t instanceof Class && ((Class) t).isArray()) {
+ t = Array.class;
+ }
}
- case FIELD: {
- t = ReflectionHelper.typeOf( field );
- break;
+ }
+ return t;
+ }
+
+ /**
+ * @param o the object from which to retrieve the value.
+ *
+ * @return Returns the value for this constraint from the specified object. Depending on the type either the value itself
+ * is returned of method or field access is used to access the value.
+ */
+ private Object getValue(Object o) {
+ switch ( elementType ) {
+ case TYPE: {
+ return o;
}
default: {
- throw new ValidationException(
- "Invalid state of MetaConstraint. Parameter elementType has unexpected value - " + elementType
- );
+ return ReflectionHelper.getValue( member, o );
}
}
- return t;
}
}
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ValidatorTypeHelper.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ValidatorTypeHelper.java 2009-03-11 13:55:02 UTC (rev 16145)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ValidatorTypeHelper.java 2009-03-12 18:11:00 UTC (rev 16146)
@@ -45,14 +45,14 @@
* @return Return a Map<Class, Class<? extends ConstraintValidator>> where the map
* key is the type the validator accepts and value the validator class itself.
*/
- public static <T extends Annotation> Map<Class<?>, Class<? extends ConstraintValidator<?, ?>>> getValidatorsTypes(
+ public static <T extends Annotation> Map<Type, Class<? extends ConstraintValidator<?, ?>>> getValidatorsTypes(
List<Class<? extends ConstraintValidator<T, ?>>> validators) {
if ( validators == null || validators.size() == 0 ) {
throw new ValidationException( "No ConstraintValidators associated to @Constraint" );
}
else {
- Map<Class<?>, Class<? extends ConstraintValidator<?, ?>>> validatorsTypes =
- new HashMap<Class<?>, Class<? extends ConstraintValidator<?, ?>>>();
+ Map<Type, Class<? extends ConstraintValidator<?, ?>>> validatorsTypes =
+ new HashMap<Type, Class<? extends ConstraintValidator<?, ?>>>();
for ( Class<? extends ConstraintValidator<?, ?>> validator : validators ) {
validatorsTypes.put( extractType( validator ), validator );
}
@@ -60,7 +60,7 @@
}
}
- private static Class<?> extractType(Class<? extends ConstraintValidator<?, ?>> validator) {
+ private static Type extractType(Class<? extends ConstraintValidator<?, ?>> validator) {
Map<Type, Type> resolvedTypes = new HashMap<Type, Type>();
Type constraintValidatorType = resolveTypes( resolvedTypes, validator );
@@ -77,7 +77,7 @@
validatorType = resolvedTypes.get( validatorType );
}
//FIXME raise an exception if validatorType is not a class
- return ( Class<?> ) validatorType;
+ return validatorType;
}
private static Type resolveTypes(Map<Type, Type> resolvedTypes, Type type) {
Copied: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeList.java (from rev 16121, validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/Ambigious.java)
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeList.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeList.java 2009-03-12 18:11:00 UTC (rev 16146)
@@ -0,0 +1,42 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, 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.validation.engine.validatorresolution;
+
+import java.lang.annotation.Documented;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.TYPE;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+
+/**
+ * A test constraint which can lead to a error when trying to reslove the validator.
+ *
+ * @author Hardy Ferentschik
+ */
+@Constraint(validatedBy = { PostCodeListValidatorForString.class, PostCodeListValidatorForNumber.class })
+@Documented
+@Target({ METHOD, FIELD, TYPE })
+@Retention(RUNTIME)
+public @interface PostCodeList {
+ public abstract String message() default "foobar";
+
+ public abstract Class<?>[] groups() default { };
+}
\ No newline at end of file
Property changes on: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeList.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:mergeinfo
+
Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeListValidatorForNumber.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeListValidatorForNumber.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeListValidatorForNumber.java 2009-03-12 18:11:00 UTC (rev 16146)
@@ -0,0 +1,34 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, 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.validation.engine.validatorresolution;
+
+import java.util.Collection;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class PostCodeListValidatorForNumber implements ConstraintValidator<PostCodeList, Collection<? extends Number>> {
+ public void initialize(PostCodeList constraintAnnotation) {
+ }
+
+ public boolean isValid(Collection<? extends Number> value, ConstraintValidatorContext constraintValidatorContext) {
+ return true;
+ }
+}
Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeListValidatorForString.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeListValidatorForString.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeListValidatorForString.java 2009-03-12 18:11:00 UTC (rev 16146)
@@ -0,0 +1,37 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, 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.validation.engine.validatorresolution;
+
+import java.util.Collection;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class PostCodeListValidatorForString implements ConstraintValidator<PostCodeList, Collection<String>> {
+ public void initialize(PostCodeList constraintAnnotation) {
+ }
+
+ public boolean isValid(Collection<String> value, ConstraintValidatorContext constraintValidatorContext) {
+ if(value == null) {
+ return true;
+ }
+ return false;
+ }
+}
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/Suburb.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/Suburb.java 2009-03-11 13:55:02 UTC (rev 16145)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/Suburb.java 2009-03-12 18:11:00 UTC (rev 16146)
@@ -17,10 +17,12 @@
*/
package org.hibernate.validation.engine.validatorresolution;
+import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
+import java.util.ArrayList;
import javax.validation.constraints.Size;
/**
@@ -43,6 +45,17 @@
@Size(min = 4, max = 1000)
private Coordinate[] boundingBox;
+ @PostCodeList
+ private Collection<? extends Number> includedPostCodes;
+
+ public void setIncludedPostCodes(Collection<? extends Number> includedPostCodes) {
+ this.includedPostCodes = includedPostCodes;
+ }
+
+ public Collection<? extends Number> getIncludedPostcodes() {
+ return includedPostCodes;
+ }
+
public void setName(String name) {
this.name = name;
}
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/ValidatorResolutionTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/ValidatorResolutionTest.java 2009-03-11 13:55:02 UTC (rev 16145)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/ValidatorResolutionTest.java 2009-03-12 18:11:00 UTC (rev 16146)
@@ -18,6 +18,8 @@
package org.hibernate.validation.engine.validatorresolution;
import java.util.Set;
+import java.util.ArrayList;
+import java.util.List;
import javax.validation.ConstraintViolation;
import javax.validation.UnexpectedTypeException;
import javax.validation.Validator;
@@ -72,7 +74,7 @@
fail();
}
catch ( UnexpectedTypeException e ) {
- assertEquals( "No validator could be found for type: java.lang.Integer", e.getMessage() );
+ assertEquals( "No validator could be found for type: class java.lang.Integer", e.getMessage() );
}
}
@@ -82,6 +84,10 @@
Suburb suburb = new Suburb();
+ List<Integer> postcodes = new ArrayList<Integer>();
+ postcodes.add(12345);
+ suburb.setIncludedPostCodes( postcodes );
+
// all values are null and should pass
Set<ConstraintViolation<Suburb>> constraintViolations = validator.validate( suburb );
assertNumberOfViolations( constraintViolations, 0 );
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ValidatorTypeTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ValidatorTypeTest.java 2009-03-11 13:55:02 UTC (rev 16145)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ValidatorTypeTest.java 2009-03-12 18:11:00 UTC (rev 16146)
@@ -18,6 +18,7 @@
package org.hibernate.validation.util;
import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -41,7 +42,7 @@
List<Class<? extends ConstraintValidator<FrenchZipcode, ?>>> validators =
new ArrayList<Class<? extends ConstraintValidator<FrenchZipcode, ?>>>();
validators.add( FrenchZipcodeConstraintValidator.class );
- Map<Class<?>, Class<? extends ConstraintValidator<?, ?>>> validatorsTypes = ValidatorTypeHelper
+ Map<Type, Class<? extends ConstraintValidator<?, ?>>> validatorsTypes = ValidatorTypeHelper
.getValidatorsTypes( validators );
assertEquals( FrenchZipcodeConstraintValidator.class, validatorsTypes.get( String.class ) );
}
15 years, 9 months
Hibernate SVN: r16145 - beanvalidation/trunk/validation-api/src/main/java/javax/validation.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2009-03-11 09:55:02 -0400 (Wed, 11 Mar 2009)
New Revision: 16145
Modified:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/ConstraintViolation.java
beanvalidation/trunk/validation-api/src/main/java/javax/validation/Validator.java
Log:
Clean JavaDoc
Modified: beanvalidation/trunk/validation-api/src/main/java/javax/validation/ConstraintViolation.java
===================================================================
--- beanvalidation/trunk/validation-api/src/main/java/javax/validation/ConstraintViolation.java 2009-03-11 13:53:07 UTC (rev 16144)
+++ beanvalidation/trunk/validation-api/src/main/java/javax/validation/ConstraintViolation.java 2009-03-11 13:55:02 UTC (rev 16145)
@@ -40,7 +40,8 @@
String getMessageTemplate();
/**
- * @return The root bean being validated.
+ * @return The root bean being validated. Null when returned by
+ * {@link javax.validation.Validator#validateValue(Class, String, Object, Class[])}
*/
T getRootBean();
@@ -49,8 +50,8 @@
* If a property constraint, the bean instance hosting the property the
* constraint is applied on
*
- * @return the leaf bean the constraint is applied on or null if Validator#validateValue
- * is used
+ * @return the leaf bean the constraint is applied on. Null when returned by
+ * {@link javax.validation.Validator#validateValue(Class, String, Object, Class[])}
*/
Object getLeafBean();
Modified: beanvalidation/trunk/validation-api/src/main/java/javax/validation/Validator.java
===================================================================
--- beanvalidation/trunk/validation-api/src/main/java/javax/validation/Validator.java 2009-03-11 13:53:07 UTC (rev 16144)
+++ beanvalidation/trunk/validation-api/src/main/java/javax/validation/Validator.java 2009-03-11 13:55:02 UTC (rev 16145)
@@ -64,7 +64,8 @@
* Validates all constraints on <code>propertyName</code> property
* if the property value is <code>value</code>
* <p/>
- * TODO express limitations of ConstraintViolation in this case
+ * <code>ConstraintViolation</code> objects return null for
+ * {@link ConstraintViolation#getRootBean()} and {@link ConstraintViolation#getLeafBean()}
*
* @param beanType the bean type
* @param propertyName property to validate
15 years, 9 months
Hibernate SVN: r16144 - beanvalidation/trunk/validation-api/src/main/java/javax/validation/bootstrap and 1 other directories.
by hibernate-commits@lists.jboss.org
Author: epbernard
Date: 2009-03-11 09:53:07 -0400 (Wed, 11 Mar 2009)
New Revision: 16144
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultValidationProviderResolver.java
Removed:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/bootstrap/DefaultValidationProviderResolver.java
Modified:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/Validation.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java
Log:
BVAL-135 remove DefaultValidationProviderResolver from the API
Modified: beanvalidation/trunk/validation-api/src/main/java/javax/validation/Validation.java
===================================================================
--- beanvalidation/trunk/validation-api/src/main/java/javax/validation/Validation.java 2009-03-11 13:09:25 UTC (rev 16143)
+++ beanvalidation/trunk/validation-api/src/main/java/javax/validation/Validation.java 2009-03-11 13:53:07 UTC (rev 16144)
@@ -17,7 +17,16 @@
*/
package javax.validation;
-import javax.validation.bootstrap.DefaultValidationProviderResolver;
+import java.util.List;
+import java.util.Map;
+import java.util.WeakHashMap;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.net.URL;
+import java.io.InputStream;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.IOException;
import javax.validation.bootstrap.GenericBootstrap;
import javax.validation.bootstrap.ProviderSpecificBootstrap;
import javax.validation.spi.BootstrapState;
@@ -32,7 +41,7 @@
* <pre>
* ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
* </pre>
- * In this case {@link javax.validation.bootstrap.DefaultValidationProviderResolver}
+ * In this case, the default validation provider resolver
* will be used to locate available providers.
*
* The chosen provider is defined as followed:
@@ -86,7 +95,6 @@
*
* @author Emmanuel Bernard
* @author Hardy Feretnschik
- * @see DefaultValidationProviderResolver
*/
public class Validation {
@@ -95,8 +103,8 @@
* default Bean Validation provider and following the
* XML configuration.
* <p/>
- * The provider list is resolved using the
- * {@link javax.validation.bootstrap.DefaultValidationProviderResolver}.
+ * The provider list is resolved using the default validation provider resolver
+ * logic.
* <p/> The code is semantically equivalent to
* <code>Validation.byDefaultProvider().configure().buildValidatorFactory()</code>
*
@@ -231,4 +239,108 @@
return resolver.getValidationProviders().get( 0 ).createGenericConfiguration( this );
}
}
+
+ /**
+ * Find <code>ValidationProvider</code> according to the default <code>ValidationProviderResolver</code> defined in the
+ * Bean Validation specification. This implementation uses the current classloader or the classloader which has loaded
+ * the current class if the current class loader is unavailable. The classloader is used to retrieve the Service Provider files.
+ * <p>
+ * This class implements the Service Provider pattern described <a href="http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Service%20Provider">here</a>.
+ * Since we cannot rely on Java 6 we have to reimplement the <code>Service</code> functionality.
+ * </p>
+ *
+ * @author Emmanuel Bernard
+ * @author Hardy Ferentschik
+ */
+ private static class DefaultValidationProviderResolver implements ValidationProviderResolver {
+
+ //cache per classloader for an appropriate discovery
+ //keep them in a weak hashmap to avoid memory leaks and allow proper hot redeployment
+ //TODO use a WeakConcurrentHashMap
+ private static final Map<ClassLoader, List<ValidationProvider>> providersPerClassloader =
+ new WeakHashMap<ClassLoader, List<ValidationProvider>>();
+
+ private static final String SERVICES_FILE = "META-INF/services/" + ValidationProvider.class.getName();
+
+ public List<ValidationProvider> getValidationProviders() {
+ ClassLoader classloader = Thread.currentThread().getContextClassLoader();
+ if ( classloader == null ) {
+ classloader = DefaultValidationProviderResolver.class.getClassLoader();
+ }
+
+ List<ValidationProvider> providers;
+ synchronized ( providersPerClassloader ) {
+ providers = providersPerClassloader.get( classloader );
+ }
+
+ if ( providers == null ) {
+ providers = new ArrayList<ValidationProvider>();
+ String name = null;
+ try {
+ Enumeration<URL> providerDefinitions = classloader.getResources( SERVICES_FILE );
+ while ( providerDefinitions.hasMoreElements() ) {
+ URL url = providerDefinitions.nextElement();
+ InputStream stream = url.openStream();
+ try {
+ BufferedReader reader = new BufferedReader( new InputStreamReader( stream ), 100 );
+ name = reader.readLine();
+ while ( name != null ) {
+ name = name.trim();
+ if ( !name.startsWith( "#" ) ) {
+ final Class<?> providerClass = loadClass(
+ name,
+ DefaultValidationProviderResolver.class
+ );
+
+ providers.add(
+ ( ValidationProvider ) providerClass.newInstance()
+ );
+ }
+ name = reader.readLine();
+ }
+ }
+ finally {
+ stream.close();
+ }
+ }
+ }
+ catch ( IOException e ) {
+ throw new ValidationException( "Unable to read " + SERVICES_FILE, e );
+ }
+ catch ( ClassNotFoundException e ) {
+ //TODO is it better to not fail the whole loading because of a black sheep?
+ throw new ValidationException( "Unable to load Bean Validation provider " + name, e );
+ }
+ catch ( IllegalAccessException e ) {
+ throw new ValidationException( "Unable to instanciate Bean Validation provider" + name, e );
+ }
+ catch ( InstantiationException e ) {
+ throw new ValidationException( "Unable to instanciate Bean Validation provider" + name, e );
+ }
+
+ synchronized ( providersPerClassloader ) {
+ providersPerClassloader.put( classloader, providers );
+ }
+ }
+
+ return providers;
+ }
+
+ private static Class<?> loadClass(String name, Class caller) throws ClassNotFoundException {
+ try {
+ //try context classloader, if fails try caller classloader
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if ( loader != null ) {
+ return loader.loadClass( name );
+ }
+ }
+ catch ( ClassNotFoundException e ) {
+ //trying caller classloader
+ if ( caller == null ) {
+ throw e;
+ }
+ }
+ return Class.forName( name, true, caller.getClassLoader() );
+ }
+ }
}
Deleted: beanvalidation/trunk/validation-api/src/main/java/javax/validation/bootstrap/DefaultValidationProviderResolver.java
===================================================================
--- beanvalidation/trunk/validation-api/src/main/java/javax/validation/bootstrap/DefaultValidationProviderResolver.java 2009-03-11 13:09:25 UTC (rev 16143)
+++ beanvalidation/trunk/validation-api/src/main/java/javax/validation/bootstrap/DefaultValidationProviderResolver.java 2009-03-11 13:53:07 UTC (rev 16144)
@@ -1,136 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, 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 javax.validation.bootstrap;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Enumeration;
-import java.util.List;
-import java.util.Map;
-import java.util.WeakHashMap;
-import javax.validation.ValidationException;
-import javax.validation.ValidationProviderResolver;
-import javax.validation.spi.ValidationProvider;
-
-/**
- * Find <code>ValidationProvider</code> according to the default <code>ValidationProviderResolver</code> defined in the
- * Bean Validation specification. This implementation uses the current classloader or the classloader which has loaded
- * the current class if the current class loader is unavailable. The classloader is used to retrieve the Service Provider files.
- * <p>
- * This class implements the Service Provider pattern described <a href="http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Service%20Provider">here</a>.
- * Since we cannot rely on Java 6 we have to reimplement the <code>Service</code> functionality.
- * </p>
- *
- * @author Emmanuel Bernard
- * @author Hardy Ferentschik
- */
-public class DefaultValidationProviderResolver implements ValidationProviderResolver {
-
- //cache per classloader for an appropriate discovery
- //keep them in a weak hashmap to avoid memory leaks and allow proper hot redeployment
- //TODO use a WeakConcurrentHashMap
- private static final Map<ClassLoader, List<ValidationProvider>> providersPerClassloader =
- new WeakHashMap<ClassLoader, List<ValidationProvider>>();
-
- private static final String SERVICES_FILE = "META-INF/services/" + ValidationProvider.class.getName();
-
- public List<ValidationProvider> getValidationProviders() {
- ClassLoader classloader = Thread.currentThread().getContextClassLoader();
- if ( classloader == null ) {
- classloader = DefaultValidationProviderResolver.class.getClassLoader();
- }
-
- List<ValidationProvider> providers;
- synchronized ( providersPerClassloader ) {
- providers = providersPerClassloader.get( classloader );
- }
-
- if ( providers == null ) {
- providers = new ArrayList<ValidationProvider>();
- String name = null;
- try {
- Enumeration<URL> providerDefinitions = classloader.getResources( SERVICES_FILE );
- while ( providerDefinitions.hasMoreElements() ) {
- URL url = providerDefinitions.nextElement();
- InputStream stream = url.openStream();
- try {
- BufferedReader reader = new BufferedReader( new InputStreamReader( stream ), 100 );
- name = reader.readLine();
- while ( name != null ) {
- name = name.trim();
- if ( !name.startsWith( "#" ) ) {
- final Class<?> providerClass = loadClass(
- name,
- DefaultValidationProviderResolver.class
- );
-
- providers.add(
- ( ValidationProvider ) providerClass.newInstance()
- );
- }
- name = reader.readLine();
- }
- }
- finally {
- stream.close();
- }
- }
- }
- catch ( IOException e ) {
- throw new ValidationException( "Unable to read " + SERVICES_FILE, e );
- }
- catch ( ClassNotFoundException e ) {
- //TODO is it better to not fail the whole loading because of a black sheep?
- throw new ValidationException( "Unable to load Bean Validation provider " + name, e );
- }
- catch ( IllegalAccessException e ) {
- throw new ValidationException( "Unable to instanciate Bean Validation provider" + name, e );
- }
- catch ( InstantiationException e ) {
- throw new ValidationException( "Unable to instanciate Bean Validation provider" + name, e );
- }
-
- synchronized ( providersPerClassloader ) {
- providersPerClassloader.put( classloader, providers );
- }
- }
-
- return providers;
- }
-
- public static Class<?> loadClass(String name, Class caller) throws ClassNotFoundException {
- try {
- //try context classloader, if fails try caller classloader
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- if ( loader != null ) {
- return loader.loadClass( name );
- }
- }
- catch ( ClassNotFoundException e ) {
- //trying caller classloader
- if ( caller == null ) {
- throw e;
- }
- }
- return Class.forName( name, true, caller.getClassLoader() );
- }
-}
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java 2009-03-11 13:09:25 UTC (rev 16143)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java 2009-03-11 13:53:07 UTC (rev 16144)
@@ -28,7 +28,6 @@
import javax.validation.ValidationException;
import javax.validation.ValidationProviderResolver;
import javax.validation.ValidatorFactory;
-import javax.validation.bootstrap.DefaultValidationProviderResolver;
import javax.validation.spi.BootstrapState;
import javax.validation.spi.ConfigurationState;
import javax.validation.spi.ValidationProvider;
Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultValidationProviderResolver.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultValidationProviderResolver.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultValidationProviderResolver.java 2009-03-11 13:53:07 UTC (rev 16144)
@@ -0,0 +1,121 @@
+package org.hibernate.validation.engine;
+
+import java.util.List;
+import java.util.Map;
+import java.util.WeakHashMap;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.net.URL;
+import java.io.InputStream;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.IOException;
+import javax.validation.ValidationProviderResolver;
+import javax.validation.ValidationException;
+import javax.validation.spi.ValidationProvider;
+
+/**
+ * Find <code>ValidationProvider</code> according to the default <code>ValidationProviderResolver</code> defined in the
+ * Bean Validation specification. This implementation uses the current classloader or the classloader which has loaded
+ * the current class if the current class loader is unavailable. The classloader is used to retrieve the Service Provider files.
+ * <p>
+ * This class implements the Service Provider pattern described <a href="http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Service%20Provider">here</a>.
+ * Since we cannot rely on Java 6 we have to reimplement the <code>Service</code> functionality.
+ * </p>
+ *
+ * Duplicated from the private innner class Validation#DefaultValidationProviderResolver
+ *
+ * @author Emmanuel Bernard
+ * @author Hardy Ferentschik
+ */
+public class DefaultValidationProviderResolver implements ValidationProviderResolver {
+
+ //cache per classloader for an appropriate discovery
+ //keep them in a weak hashmap to avoid memory leaks and allow proper hot redeployment
+ //TODO use a WeakConcurrentHashMap
+ private static final Map<ClassLoader, List<ValidationProvider>> providersPerClassloader =
+ new WeakHashMap<ClassLoader, List<ValidationProvider>>();
+
+ private static final String SERVICES_FILE = "META-INF/services/" + ValidationProvider.class.getName();
+
+ public List<ValidationProvider> getValidationProviders() {
+ ClassLoader classloader = Thread.currentThread().getContextClassLoader();
+ if ( classloader == null ) {
+ classloader = DefaultValidationProviderResolver.class.getClassLoader();
+ }
+
+ List<ValidationProvider> providers;
+ synchronized ( providersPerClassloader ) {
+ providers = providersPerClassloader.get( classloader );
+ }
+
+ if ( providers == null ) {
+ providers = new ArrayList<ValidationProvider>();
+ String name = null;
+ try {
+ Enumeration<URL> providerDefinitions = classloader.getResources( SERVICES_FILE );
+ while ( providerDefinitions.hasMoreElements() ) {
+ URL url = providerDefinitions.nextElement();
+ InputStream stream = url.openStream();
+ try {
+ BufferedReader reader = new BufferedReader( new InputStreamReader( stream ), 100 );
+ name = reader.readLine();
+ while ( name != null ) {
+ name = name.trim();
+ if ( !name.startsWith( "#" ) ) {
+ final Class<?> providerClass = loadClass(
+ name,
+ DefaultValidationProviderResolver.class
+ );
+
+ providers.add(
+ ( ValidationProvider ) providerClass.newInstance()
+ );
+ }
+ name = reader.readLine();
+ }
+ }
+ finally {
+ stream.close();
+ }
+ }
+ }
+ catch ( IOException e ) {
+ throw new ValidationException( "Unable to read " + SERVICES_FILE, e );
+ }
+ catch ( ClassNotFoundException e ) {
+ //TODO is it better to not fail the whole loading because of a black sheep?
+ throw new ValidationException( "Unable to load Bean Validation provider " + name, e );
+ }
+ catch ( IllegalAccessException e ) {
+ throw new ValidationException( "Unable to instanciate Bean Validation provider" + name, e );
+ }
+ catch ( InstantiationException e ) {
+ throw new ValidationException( "Unable to instanciate Bean Validation provider" + name, e );
+ }
+
+ synchronized ( providersPerClassloader ) {
+ providersPerClassloader.put( classloader, providers );
+ }
+ }
+
+ return providers;
+ }
+
+ private static Class<?> loadClass(String name, Class caller) throws ClassNotFoundException {
+ try {
+ //try context classloader, if fails try caller classloader
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if ( loader != null ) {
+ return loader.loadClass( name );
+ }
+ }
+ catch ( ClassNotFoundException e ) {
+ //trying caller classloader
+ if ( caller == null ) {
+ throw e;
+ }
+ }
+ return Class.forName( name, true, caller.getClassLoader() );
+ }
+}
15 years, 9 months
Hibernate SVN: r16143 - in validator/trunk/hibernate-validator: src/main/docbook/en-US/modules and 5 other directories.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-03-11 09:09:25 -0400 (Wed, 11 Mar 2009)
New Revision: 16143
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/JPATraversableResolver.java
Removed:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/AmbiguousConstraintUsageException.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultTraversableResolver.java
Modified:
validator/trunk/hibernate-validator/pom.xml
validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/gettingstarted.xml
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/ValidatorResolutionTest.java
Log:
HV-104 Added dependecy to SNAPSHOT release of JPA2 spec. Also added JPATraversableResolver.
Removed obsolete AmbiguousConstraintUsageException.
Modified: validator/trunk/hibernate-validator/pom.xml
===================================================================
--- validator/trunk/hibernate-validator/pom.xml 2009-03-11 11:32:22 UTC (rev 16142)
+++ validator/trunk/hibernate-validator/pom.xml 2009-03-11 13:09:25 UTC (rev 16143)
@@ -1,7 +1,7 @@
<?xml version="1.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">
+ 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">
<parent>
<artifactId>hibernate-validator-parent</artifactId>
<groupId>org.hibernate</groupId>
@@ -28,13 +28,24 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
+
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
+ <!--
+ This is a temporary dependency. Do not use this dependecy in production.
+ -->
<dependency>
+ <groupId>javax.persistence</groupId>
+ <artifactId>jpa-api</artifactId>
+ <version>2.0.Beta1-SNAPSHOT</version>
+ <optional>true</optional>
+ </dependency>
+
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
@@ -125,7 +136,7 @@
</goals>
</execution>
</executions>
- </plugin>
+ </plugin>
</plugins>
</build>
<reporting>
Modified: validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/gettingstarted.xml
===================================================================
--- validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/gettingstarted.xml 2009-03-11 11:32:22 UTC (rev 16142)
+++ validator/trunk/hibernate-validator/src/main/docbook/en-US/modules/gettingstarted.xml 2009-03-11 13:09:25 UTC (rev 16143)
@@ -164,7 +164,6 @@
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
- <version>RELEASE</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/AmbiguousConstraintUsageException.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/AmbiguousConstraintUsageException.java 2009-03-11 11:32:22 UTC (rev 16142)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/AmbiguousConstraintUsageException.java 2009-03-11 13:09:25 UTC (rev 16143)
@@ -1,45 +0,0 @@
-// $Id: AmbiguousConstraintUsageException.java 15902 2009-02-05 11:53:49Z hardy.ferentschik $
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, 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.validation;
-
-import javax.validation.ValidationException;
-import javax.validation.UnexpectedTypeException;
-
-/**
- * Exception raised in the case that the constraint validator resolution returns more than one possible validators for
- * a given type.
- *
- * @author Hardy Ferentschik
- */
-public class AmbiguousConstraintUsageException extends UnexpectedTypeException {
- public AmbiguousConstraintUsageException(String message) {
- super( message );
- }
-
- public AmbiguousConstraintUsageException() {
- super();
- }
-
- public AmbiguousConstraintUsageException(String message, Throwable cause) {
- super( message, cause );
- }
-
- public AmbiguousConstraintUsageException(Throwable cause) {
- super( cause );
- }
-}
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java 2009-03-11 11:32:22 UTC (rev 16142)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java 2009-03-11 13:09:25 UTC (rev 16143)
@@ -34,6 +34,7 @@
import javax.validation.spi.ValidationProvider;
import org.hibernate.validation.util.Version;
+import org.hibernate.validation.engine.resolver.DefaultTraversableResolver;
/**
* @author Emmanuel Bernard
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java 2009-03-11 11:32:22 UTC (rev 16142)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java 2009-03-11 13:09:25 UTC (rev 16143)
@@ -31,7 +31,6 @@
import org.slf4j.Logger;
-import org.hibernate.validation.AmbiguousConstraintUsageException;
import org.hibernate.validation.util.LoggerFactory;
import org.hibernate.validation.util.ValidatorTypeHelper;
@@ -175,7 +174,7 @@
builder.append( ", " );
}
builder.delete( builder.length() - 2, builder.length() );
- throw new AmbiguousConstraintUsageException( builder.toString() );
+ throw new UnexpectedTypeException( builder.toString() );
}
}
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultTraversableResolver.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultTraversableResolver.java 2009-03-11 11:32:22 UTC (rev 16142)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultTraversableResolver.java 2009-03-11 13:09:25 UTC (rev 16143)
@@ -1,13 +0,0 @@
-package org.hibernate.validation.engine;
-
-import java.lang.annotation.ElementType;
-import javax.validation.TraversableResolver;
-
-/**
- * @author Emmanuel Bernard
- */
-public class DefaultTraversableResolver implements TraversableResolver {
- public boolean isTraversable(Object traversableObject, String traversableProperty, Class<?> rootBeanType, String pathToTraversableObject, ElementType elementType) {
- return true;
- }
-}
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java (from rev 16121, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/DefaultTraversableResolver.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java 2009-03-11 13:09:25 UTC (rev 16143)
@@ -0,0 +1,91 @@
+package org.hibernate.validation.engine.resolver;
+
+import java.lang.annotation.ElementType;
+import javax.validation.TraversableResolver;
+
+import org.slf4j.Logger;
+
+import org.hibernate.validation.util.LoggerFactory;
+import org.hibernate.validation.util.ReflectionHelper;
+
+/**
+ * A JPA 2 aware <code>TraversableResolver</code>.
+ *
+ * @author Emmanuel Bernard
+ * @author Hardy Ferentschik
+ */
+public class DefaultTraversableResolver implements TraversableResolver {
+
+ private static final Logger log = LoggerFactory.make();
+
+ /**
+ * Class to load to check whether JPA 2 is on the classpath.
+ */
+ private static final String PERSISTENCE_UTIL_CLASS_NAME = "javax.persistence.PersistenceUtil";
+
+ /**
+ * Class to instantiate in case JPA 2 is on the classpath.
+ */
+ private static final String JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME = "org.hibernate.validation.engine.resolver.JPATraversableResolver";
+
+ /**
+ * A JPA 2 aware traversable resolver.
+ */
+ private TraversableResolver jpaTraversableResolver;
+
+
+ public DefaultTraversableResolver() {
+ detectJPA();
+ }
+
+ public boolean isTraversable(Object traversableObject, String traversableProperty, Class<?> rootBeanType, String pathToTraversableObject, ElementType elementType) {
+ return jpaTraversableResolver == null || jpaTraversableResolver.isTraversable(
+ traversableObject, traversableProperty, rootBeanType, pathToTraversableObject, elementType
+ );
+ }
+
+ /**
+ * Tries to load detect and load JPA.
+ */
+ private void detectJPA() {
+ try {
+ ReflectionHelper.classForName( PERSISTENCE_UTIL_CLASS_NAME, this.getClass() );
+ log.info( "Found {} on classpath.", PERSISTENCE_UTIL_CLASS_NAME );
+ }
+ catch ( ClassNotFoundException e ) {
+ log.info(
+ "Cannot find {} on classpath. All properties will per default be traversable.",
+ PERSISTENCE_UTIL_CLASS_NAME
+ );
+ return;
+ }
+
+ try {
+ Class jpaAwareResolverClass = ReflectionHelper.classForName(
+ JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME, this.getClass()
+ );
+ jpaTraversableResolver = ( TraversableResolver ) jpaAwareResolverClass.newInstance();
+ log.info(
+ "Instantiated an instance of {}.", JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME
+ );
+ }
+ catch ( ClassNotFoundException e ) {
+ log.info(
+ "Unable to load JPA aware resolver {}. All properties will per default be traversable.",
+ JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME
+ );
+ }
+ catch ( IllegalAccessException e ) {
+ log.info(
+ "Unable to instantiate JPA aware resolver {}. All properties will per default be traversable.",
+ JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME
+ );
+ }
+ catch ( InstantiationException e ) {
+ log.info(
+ "Unable to instantiate JPA aware resolver {}. All properties will per default be traversable.",
+ JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME
+ );
+ }
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/DefaultTraversableResolver.java
___________________________________________________________________
Name: svn:keywords
+ Id
Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/JPATraversableResolver.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/JPATraversableResolver.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/JPATraversableResolver.java 2009-03-11 13:09:25 UTC (rev 16143)
@@ -0,0 +1,32 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, 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.validation.engine.resolver;
+
+import java.lang.annotation.ElementType;
+import javax.persistence.Persistence;
+import javax.validation.TraversableResolver;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class JPATraversableResolver implements TraversableResolver {
+
+ public boolean isTraversable(Object traversableObject, String traversableProperty, Class<?> rootBeanType, String pathToTraversableObject, ElementType elementType) {
+ return Persistence.getPersistenceUtil().isLoaded( traversableObject, traversableProperty );
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/resolver/JPATraversableResolver.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java 2009-03-11 11:32:22 UTC (rev 16142)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java 2009-03-11 13:09:25 UTC (rev 16143)
@@ -421,4 +421,19 @@
}
return false;
}
+
+ public static Class<?> classForName(String name, Class<?> caller) throws ClassNotFoundException {
+ try {
+ ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
+ if ( contextClassLoader != null ) {
+ return contextClassLoader.loadClass( name );
+ }
+ }
+ catch ( Throwable e ) {
+ // ignore
+ }
+ return Class.forName( name, true, caller.getClassLoader() );
+ }
+
+
}
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/ValidatorResolutionTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/ValidatorResolutionTest.java 2009-03-11 11:32:22 UTC (rev 16142)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/ValidatorResolutionTest.java 2009-03-11 13:09:25 UTC (rev 16143)
@@ -18,13 +18,12 @@
package org.hibernate.validation.engine.validatorresolution;
import java.util.Set;
-import org.hibernate.validation.AmbiguousConstraintUsageException;
import javax.validation.ConstraintViolation;
-import javax.validation.Validator;
import javax.validation.UnexpectedTypeException;
+import javax.validation.Validator;
-import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
@@ -58,7 +57,7 @@
validator.validate( foo );
fail();
}
- catch ( AmbiguousConstraintUsageException e ) {
+ catch ( UnexpectedTypeException e ) {
assertTrue( e.getMessage().startsWith( "There are multiple validators" ) );
}
}
15 years, 9 months
Hibernate SVN: r16142 - jpa-api/trunk/src/main/java/javax/persistence.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-03-11 07:32:22 -0400 (Wed, 11 Mar 2009)
New Revision: 16142
Modified:
jpa-api/trunk/src/main/java/javax/persistence/Persistence.java
Log:
Made Persistence.getPersistenceUtil static
Modified: jpa-api/trunk/src/main/java/javax/persistence/Persistence.java
===================================================================
--- jpa-api/trunk/src/main/java/javax/persistence/Persistence.java 2009-03-11 11:11:08 UTC (rev 16141)
+++ jpa-api/trunk/src/main/java/javax/persistence/Persistence.java 2009-03-11 11:32:22 UTC (rev 16142)
@@ -65,7 +65,7 @@
/**
* @return Returns a <code>PersistenceUtil</code> instance.
*/
- public PersistenceUtil getPersistenceUtil() {
+ public static PersistenceUtil getPersistenceUtil() {
// return a dummy implementation of PersistenceUtil. Introduced for Hibernate Validator (HV-104).
// Needs to be changed once we start updating EntityManager and introduce JPA2
return new PersistenceUtil() {
15 years, 9 months
Hibernate SVN: r16141 - jpa-api/trunk.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-03-11 07:11:08 -0400 (Wed, 11 Mar 2009)
New Revision: 16141
Modified:
jpa-api/trunk/pom.xml
Log:
Made the version a snapshot
Modified: jpa-api/trunk/pom.xml
===================================================================
--- jpa-api/trunk/pom.xml 2009-03-11 11:10:03 UTC (rev 16140)
+++ jpa-api/trunk/pom.xml 2009-03-11 11:11:08 UTC (rev 16141)
@@ -8,7 +8,7 @@
<artifactId>jpa-api</artifactId>
<version>2.0.Beta1-SNAPSHOT</version>
<packaging>jar</packaging>
- <name>Persistence API</name>
+ <name>JPA API</name>
<description>JSR 317: Java Persistence API 2.0</description>
15 years, 9 months
Hibernate SVN: r16140 - jpa-api/trunk.
by hibernate-commits@lists.jboss.org
Author: hardy.ferentschik
Date: 2009-03-11 07:10:03 -0400 (Wed, 11 Mar 2009)
New Revision: 16140
Modified:
jpa-api/trunk/pom.xml
Log:
Made the version a snapshot
Modified: jpa-api/trunk/pom.xml
===================================================================
--- jpa-api/trunk/pom.xml 2009-03-11 11:04:32 UTC (rev 16139)
+++ jpa-api/trunk/pom.xml 2009-03-11 11:10:03 UTC (rev 16140)
@@ -6,7 +6,7 @@
<groupId>javax.persistence</groupId>
<artifactId>jpa-api</artifactId>
- <version>2.0.Beta1</version>
+ <version>2.0.Beta1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Persistence API</name>
15 years, 9 months