Author: hardy.ferentschik
Date: 2009-04-09 08:23:07 -0400 (Thu, 09 Apr 2009)
New Revision: 16286
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/UserType.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/ConsistentUserInformation.java
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml
Log:
HV-112
Enum and Annotation types in constraint definitions.
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java 2009-04-09
11:45:43 UTC (rev 16285)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java 2009-04-09
12:23:07 UTC (rev 16286)
@@ -55,6 +55,7 @@
import org.hibernate.validation.util.ReflectionHelper;
import org.hibernate.validation.util.annotationfactory.AnnotationDescriptor;
import org.hibernate.validation.util.annotationfactory.AnnotationFactory;
+import org.hibernate.validation.xml.AnnotationType;
import org.hibernate.validation.xml.BeanType;
import org.hibernate.validation.xml.ClassType;
import org.hibernate.validation.xml.ConstraintMappingsType;
@@ -241,7 +242,7 @@
}
// group sequence
- List<Class<?>> groupSequence = createGroupSequence(
classType.getGroupSequence(), beanClass, defaultPackage );
+ List<Class<?>> groupSequence = createGroupSequence(
classType.getGroupSequence(), defaultPackage );
if ( !groupSequence.isEmpty() ) {
defaultSequences.put( beanClass, groupSequence );
}
@@ -253,7 +254,7 @@
}
}
- private List<Class<?>> createGroupSequence(GroupSequenceType
groupSequenceType, Class<?> beanClass, String defaultPackage) {
+ private List<Class<?>> createGroupSequence(GroupSequenceType
groupSequenceType, String defaultPackage) {
List<Class<?>> groupSequence = new ArrayList<Class<?>>();
for ( String groupName : groupSequenceType.getValue() ) {
Class<?> group = getClass( groupName, defaultPackage );
@@ -364,25 +365,52 @@
}
private Object getSingleValue(Serializable serializable, Class<?> returnType) {
- String value;
+
+ Object returnValue;
if ( serializable instanceof String ) {
- value = ( String ) serializable;
+ String value = ( String ) serializable;
+ returnValue = convertStringToReturnType( returnType, value );
}
- else if ( serializable instanceof JAXBElement ) {
+ else if ( serializable instanceof JAXBElement && ( ( JAXBElement ) serializable
).getDeclaredType()
+ .equals( String.class ) ) {
JAXBElement<?> elem = ( JAXBElement<?> ) serializable;
- // this is safe due to the underlying schema
- value = ( String ) elem.getValue();
+ String value = ( String ) elem.getValue();
+ returnValue = convertStringToReturnType( returnType, value );
}
+ else if ( serializable instanceof JAXBElement && ( ( JAXBElement ) serializable
).getDeclaredType()
+ .equals( AnnotationType.class ) ) {
+ JAXBElement<?> elem = ( JAXBElement<?> ) serializable;
+ AnnotationType annotationType = ( AnnotationType ) elem.getValue();
+ try {
+ @SuppressWarnings("unchecked")
+ Class<Annotation> annotationClass = ( Class<Annotation> ) returnType;
+ returnValue = createAnnotation( annotationType, annotationClass );
+ }
+ catch ( ClassCastException e ) {
+ throw new ValidationException( "Unexpected paramter value" );
+ }
+ }
else {
throw new ValidationException( "Unexpected paramter value" );
}
+ return returnValue;
- return convertStringToReturnType( returnType, value );
}
+ private <A extends Annotation> Annotation createAnnotation(AnnotationType
annotationType, Class<A> returnType) {
+ AnnotationDescriptor<A> annotationDescriptor = new AnnotationDescriptor<A>(
returnType );
+ for ( ElementType elementType : annotationType.getElement() ) {
+ String name = elementType.getName();
+ Class<?> paramterType = getAnnotationParamterType( returnType, name );
+ Object elementValue = getElementValue( elementType, paramterType );
+ annotationDescriptor.setValue( name, elementValue );
+ }
+ return AnnotationFactory.create( annotationDescriptor );
+ }
+
private Object convertStringToReturnType(Class<?> returnType, String value) {
Object returnValue;
- if ( returnType.isPrimitive() && returnType.getName().equals(
byte.class.getName() ) ) {
+ if ( returnType.getName().equals( byte.class.getName() ) ) {
try {
returnValue = Byte.parseByte( value );
}
@@ -390,7 +418,7 @@
throw new ValidationException( "Invalid byte format", e );
}
}
- else if ( returnType.isPrimitive() && returnType.getName().equals(
short.class.getName() ) ) {
+ else if ( returnType.getName().equals( short.class.getName() ) ) {
try {
returnValue = Short.parseShort( value );
}
@@ -398,7 +426,7 @@
throw new ValidationException( "Invalid short format", e );
}
}
- else if ( returnType.isPrimitive() && returnType.getName().equals(
int.class.getName() ) ) {
+ else if ( returnType.getName().equals( int.class.getName() ) ) {
try {
returnValue = Integer.parseInt( value );
}
@@ -406,7 +434,7 @@
throw new ValidationException( "Invalid int format", e );
}
}
- else if ( returnType.isPrimitive() && returnType.getName().equals(
long.class.getName() ) ) {
+ else if ( returnType.getName().equals( long.class.getName() ) ) {
try {
returnValue = Long.parseLong( value );
}
@@ -414,7 +442,7 @@
throw new ValidationException( "Invalid long format", e );
}
}
- else if ( returnType.isPrimitive() && returnType.getName().equals(
float.class.getName() ) ) {
+ else if ( returnType.getName().equals( float.class.getName() ) ) {
try {
returnValue = Float.parseFloat( value );
}
@@ -422,7 +450,7 @@
throw new ValidationException( "Invalid float format", e );
}
}
- else if ( returnType.isPrimitive() && returnType.getName().equals(
double.class.getName() ) ) {
+ else if ( returnType.getName().equals( double.class.getName() ) ) {
try {
returnValue = Double.parseDouble( value );
}
@@ -430,10 +458,10 @@
throw new ValidationException( "Invalid double format", e );
}
}
- else if ( returnType.isPrimitive() && returnType.getName().equals(
boolean.class.getName() ) ) {
+ else if ( returnType.getName().equals( boolean.class.getName() ) ) {
returnValue = Boolean.parseBoolean( value );
}
- else if ( returnType.isPrimitive() && returnType.getName().equals(
char.class.getName() ) ) {
+ else if ( returnType.getName().equals( char.class.getName() ) ) {
if ( value.length() != 1 ) {
throw new ValidationException( "Invalid char value: " + value );
}
@@ -451,7 +479,14 @@
}
}
else {
- throw new ValidationException( "Invalid return type: " + returnType );
+ try {
+ @SuppressWarnings("unchecked")
+ Class<Enum> enumClass = ( Class<Enum> ) returnType;
+ returnValue = Enum.valueOf( enumClass, value );
+ }
+ catch ( ClassCastException e ) {
+ throw new ValidationException( "Invalid return type: " + returnType +
". Should be a enumeration type." );
+ }
}
return returnValue;
}
@@ -508,7 +543,6 @@
}
private void initBeanMetaData() {
-
for ( Class<?> beanClass : processedClasses ) {
BeanMetaDataImpl<?> metaData = new BeanMetaDataImpl( beanClass,
constraintHelper, annotationIgnores );
for ( MetaConstraint<?, ?> constraint : constraintMap.get( beanClass ) ) {
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/ConsistentUserInformation.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/ConsistentUserInformation.java 2009-04-09
11:45:43 UTC (rev 16285)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/ConsistentUserInformation.java 2009-04-09
12:23:07 UTC (rev 16286)
@@ -25,6 +25,7 @@
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
import javax.validation.Constraint;
+import javax.validation.constraints.Pattern;
/**
@@ -44,4 +45,8 @@
public abstract String[] stringArrayParam() default { };
public abstract int intParam() default 0;
+
+ public abstract Pattern[] patterns();
+
+ public abstract UserType userType() default UserType.BUYER;
}
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/UserType.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/UserType.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/UserType.java 2009-04-09
12:23:07 UTC (rev 16286)
@@ -0,0 +1,26 @@
+// $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.xml;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public enum UserType {
+ BUYER,
+ SELLER
+}
Property changes on:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/UserType.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified:
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml
===================================================================
---
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml 2009-04-09
11:45:43 UTC (rev 16285)
+++
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml 2009-04-09
12:23:07 UTC (rev 16286)
@@ -21,23 +21,21 @@
<element name="intParam">
<value>42</value>
</element>
+ <element name="patterns">
+ <annotation>
+ <element name="regexp">myRegExp1</element>
+ </annotation>
+ <annotation>
+ <element name="regexp">myRegExp2</element>
+ </annotation>
+ </element>
+ <element name="userType">SELLER</element>
</constraint>
</class>
<field name="lastname">
<constraint
annotation="javax.validation.constraints.Pattern">
<message>Last name has to start with with a capital
letter.</message>
<element name="regexp">^[A-Z][a-z]+</element>
- <!--element name="patterns">
- <annotation>
- <element name="value">myRegExp</element>
- <element name="flag">
- <value>INSENSITIVE</value>
- </element>
- </annotation>
- <annotation>
- <element
name="value">my2ndRegExp</element>
- </annotation>
- </element-->
</constraint>
</field>
<field name="creditcard">