[hibernate-commits] Hibernate SVN: r15377 - in validator/trunk: hibernate-validator/src/main/java/org/hibernate/validation/impl and 1 other directories.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Thu Oct 23 03:57:42 EDT 2008
Author: hardy.ferentschik
Date: 2008-10-23 03:57:41 -0400 (Thu, 23 Oct 2008)
New Revision: 15377
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaDataProvider.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaDataProviderImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/ConstraintDescriptorImpl.java
validator/trunk/validation-api/src/main/java/javax/validation/ConstraintDescriptor.java
validator/trunk/validation-api/src/main/java/javax/validation/ConstraintFactory.java
Log:
Some javadoc cleanups and refactories
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java (from rev 15369, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorMetaData.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java 2008-10-23 07:57:41 UTC (rev 15377)
@@ -0,0 +1,188 @@
+// $Id$// $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;
+
+import java.lang.annotation.ElementType;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Type;
+import javax.validation.ValidationException;
+
+import org.hibernate.validation.impl.ConstraintDescriptorImpl;
+import org.hibernate.validation.util.ReflectionHelper;
+
+/**
+ * Instances of this class abstract the constraint type (class, method or field constraint) and gives access to
+ * meta data about the constraint. This allows a unified handling of constraints in the validator imlpementation.
+ *
+ * @author Hardy Ferentschik
+ */
+public class MetaConstraint {
+
+ /**
+ * The constraint specific meta data.
+ */
+ private final ConstraintDescriptorImpl descriptor;
+
+ /**
+ * The type (class) the constraint was defined on. <code>null</code> if the constraint was specified on method or
+ * field level.
+ */
+ private final Type type;
+
+ /**
+ * The method the constraint was defined on. <code>null</code> if the constraint was specified on class or
+ * field level.
+ */
+ private final Method method;
+
+ /**
+ * 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;
+
+ /**
+ * Describes on which level (<code>TYPE</code>, <code>METHOD</code>, <code>FIELD</code>) the constraint was
+ * defined on.
+ */
+ private final ElementType elementType;
+
+ public MetaConstraint(Type t, ConstraintDescriptorImpl constraintDescriptor) {
+ this.type = t;
+ this.method = null;
+ this.field = null;
+ this.descriptor = constraintDescriptor;
+ this.elementType = ElementType.TYPE;
+ this.propertyName = "";
+ }
+
+ public MetaConstraint(Method m, ConstraintDescriptorImpl constraintDescriptor) {
+ this.method = m;
+ this.type = null;
+ this.field = null;
+ this.descriptor = constraintDescriptor;
+ this.elementType = ElementType.METHOD;
+ this.propertyName = ReflectionHelper.getPropertyName( m );
+ }
+
+ public MetaConstraint(Field f, ConstraintDescriptorImpl constraintDescriptor) {
+ this.field = f;
+ this.method = null;
+ this.type = null;
+ this.descriptor = constraintDescriptor;
+ this.elementType = ElementType.FIELD;
+ this.propertyName = f.getName();
+ }
+
+ /**
+ * @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.
+ */
+ public boolean isCollection() {
+ Type t = typeOfAnnoatedElement();
+ return ReflectionHelper.isCollection( t );
+ }
+
+ /**
+ * @return Returns <code>true</code> in case the constraint is defined on an array, <code>false</code>
+ * otherwise.
+ */
+ public boolean isArray() {
+ Type t = typeOfAnnoatedElement();
+ return ReflectionHelper.isArray( t );
+ }
+
+ public ConstraintDescriptorImpl getDescriptor() {
+ return descriptor;
+ }
+
+ public Method getMethod() {
+ return method;
+ }
+
+ public Field getField() {
+ return field;
+ }
+
+ public Type getType() {
+ return type;
+ }
+
+ public String getPropertyName() {
+ return propertyName;
+ }
+
+ public ElementType getElementType() {
+ return elementType;
+ }
+
+ private Type typeOfAnnoatedElement() {
+ Type t;
+ switch ( elementType ) {
+ case TYPE: {
+ t = type;
+ break;
+ }
+ case METHOD: {
+ t = ReflectionHelper.typeOf( method );
+ break;
+ }
+ case FIELD: {
+ t = ReflectionHelper.typeOf( field );
+ break;
+ }
+ default: {
+ throw new ValidationException(
+ "Invalid state of MetaConstraint. Parameter elementType has unexpected value - " + elementType
+ );
+ }
+ }
+ return t;
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaDataProvider.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaDataProvider.java 2008-10-23 02:15:50 UTC (rev 15376)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaDataProvider.java 2008-10-23 07:57:41 UTC (rev 15377)
@@ -65,7 +65,7 @@
* @return A list of <code>ValidatorMetaData</code> instances encapsulating the information of all the constraints
* defined on the bean.
*/
- List<ValidatorMetaData> getConstraintMetaDataList();
+ List<MetaConstraint> getConstraintMetaDataList();
/**
* @return A map keying the property name of a constraint to its <code>ElementDescriptor</code>.
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaDataProviderImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaDataProviderImpl.java 2008-10-23 02:15:50 UTC (rev 15376)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaDataProviderImpl.java 2008-10-23 07:57:41 UTC (rev 15377)
@@ -18,7 +18,6 @@
package org.hibernate.validation.engine;
import java.lang.annotation.Annotation;
-import java.lang.annotation.ElementType;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
@@ -69,7 +68,7 @@
/**
* List of constraints.
*/
- private List<ValidatorMetaData> constraintMetaDataList = new ArrayList<ValidatorMetaData>();
+ private List<MetaConstraint> metaConstraintList = new ArrayList<MetaConstraint>();
/**
* List of cascaded fields.
@@ -119,7 +118,7 @@
/**
* Get all superclasses and interfaces recursively.
*
- * @param clazz The class to start the seatch with.
+ * @param clazz The class to start the search with.
* @param classes List of classes to which to add all found super classes and interfaces.
*/
private void computeClassHierarchy(Class clazz, List<Class> classes) {
@@ -195,11 +194,11 @@
private void initFieldConstraints(Class clazz) {
for ( Field field : clazz.getDeclaredFields() ) {
- List<ConstraintDescriptorImpl> fieldMetadata = getFieldLevelMetadata( field );
+ List<ConstraintDescriptorImpl> fieldMetadata = findFieldLevelConstraints( field );
for ( ConstraintDescriptorImpl constraintDescription : fieldMetadata ) {
ReflectionHelper.setAccessibility( field );
- ValidatorMetaData metaData = new ValidatorMetaData( field, constraintDescription );
- constraintMetaDataList.add( metaData );
+ MetaConstraint metaConstraint = new MetaConstraint( field, constraintDescription );
+ metaConstraintList.add( metaConstraint );
}
if ( field.isAnnotationPresent( Valid.class ) ) {
cascadedFields.add( field );
@@ -209,11 +208,11 @@
private void initMethodConstraints(Class clazz) {
for ( Method method : clazz.getDeclaredMethods() ) {
- List<ConstraintDescriptorImpl> methodMetadata = getMethodLevelMetadata( method );
+ List<ConstraintDescriptorImpl> methodMetadata = findMethodLevelConstraints( method );
for ( ConstraintDescriptorImpl constraintDescription : methodMetadata ) {
ReflectionHelper.setAccessibility( method );
- ValidatorMetaData metaData = new ValidatorMetaData( method, constraintDescription );
- constraintMetaDataList.add( metaData );
+ MetaConstraint metaConstraint = new MetaConstraint( method, constraintDescription );
+ metaConstraintList.add( metaConstraint );
}
if ( method.isAnnotationPresent( Valid.class ) ) {
cascadedMethods.add( method );
@@ -222,10 +221,10 @@
}
private void initClassConstraints(Class clazz) {
- List<ConstraintDescriptorImpl> classMetadata = getClassLevelMetadata( clazz );
+ List<ConstraintDescriptorImpl> classMetadata = findClassLevelConstraints( clazz );
for ( ConstraintDescriptorImpl constraintDescription : classMetadata ) {
- ValidatorMetaData metaData = new ValidatorMetaData( clazz, constraintDescription );
- constraintMetaDataList.add( metaData );
+ MetaConstraint metaConstraint = new MetaConstraint( clazz, constraintDescription );
+ metaConstraintList.add( metaConstraint );
}
}
@@ -350,7 +349,7 @@
*
* @todo inject XML data here, probably externalizing the process
*/
- private List<ConstraintDescriptorImpl> getClassLevelMetadata(Class beanClass) {
+ private List<ConstraintDescriptorImpl> findClassLevelConstraints(Class beanClass) {
List<ConstraintDescriptorImpl> metadata = new ArrayList<ConstraintDescriptorImpl>();
for ( Annotation annotation : beanClass.getAnnotations() ) {
metadata.addAll( findConstraintAnnotations( annotation ) );
@@ -371,7 +370,7 @@
*
* @todo inject XML data here, probably externalizing the process
*/
- private List<ConstraintDescriptorImpl> getMethodLevelMetadata(Method method) {
+ private List<ConstraintDescriptorImpl> findMethodLevelConstraints(Method method) {
List<ConstraintDescriptorImpl> metadata = new ArrayList<ConstraintDescriptorImpl>();
for ( Annotation annotation : method.getAnnotations() ) {
metadata.addAll( findConstraintAnnotations( annotation ) );
@@ -408,7 +407,7 @@
*
* @todo inject XML data here, probably externalizing the process
*/
- private List<ConstraintDescriptorImpl> getFieldLevelMetadata(Field field) {
+ private List<ConstraintDescriptorImpl> findFieldLevelConstraints(Field field) {
List<ConstraintDescriptorImpl> metadata = new ArrayList<ConstraintDescriptorImpl>();
for ( Annotation annotation : field.getAnnotations() ) {
metadata.addAll( findConstraintAnnotations( annotation ) );
@@ -457,8 +456,8 @@
return groupSequences;
}
- public List<ValidatorMetaData> getConstraintMetaDataList() {
- return constraintMetaDataList;
+ public List<MetaConstraint> getConstraintMetaDataList() {
+ return metaConstraintList;
}
public Map<String, ElementDescriptor> getPropertyDescriptors() {
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java 2008-10-23 02:15:50 UTC (rev 15376)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java 2008-10-23 07:57:41 UTC (rev 15377)
@@ -158,9 +158,9 @@
* @param context The current validation context.
*/
private void validateConstraints(ValidationContext<T> context) {
- for ( ValidatorMetaData metaData : metaDataProvider.getConstraintMetaDataList() ) {
- ConstraintDescriptorImpl constraintDescriptor = metaData.getDescriptor();
- context.pushProperty( metaData.getPropertyName() );
+ for ( MetaConstraint metaConstraint : metaDataProvider.getConstraintMetaDataList() ) {
+ ConstraintDescriptorImpl constraintDescriptor = metaConstraint.getDescriptor();
+ context.pushProperty( metaConstraint.getPropertyName() );
if ( !context.needsValidation( constraintDescriptor.getGroups() ) ) {
context.popProperty();
@@ -168,7 +168,7 @@
}
final Object leafBeanInstance = context.peekValidatedObject();
- Object value = metaData.getValue( leafBeanInstance );
+ Object value = metaConstraint.getValue( leafBeanInstance );
ContextImpl contextImpl = new ContextImpl(constraintDescriptor);
if ( !constraintDescriptor.getConstraintImplementation().isValid( value, contextImpl ) ) {
@@ -414,10 +414,10 @@
propertyIter.split();
if ( !propertyIter.hasNext() ) {
- List<ValidatorMetaData> metaDataList = validator.getMetaDataProvider().getConstraintMetaDataList();
- for ( ValidatorMetaData metaData : metaDataList ) {
- ConstraintDescriptor constraintDescriptor = metaData.getDescriptor();
- if ( metaData.getPropertyName().equals( propertyIter.getHead() ) ) {
+ List<MetaConstraint> metaConstraintList = validator.getMetaDataProvider().getConstraintMetaDataList();
+ for ( MetaConstraint metaConstraint : metaConstraintList ) {
+ ConstraintDescriptor constraintDescriptor = metaConstraint.getDescriptor();
+ if ( metaConstraint.getPropertyName().equals( propertyIter.getHead() ) ) {
matchingConstraintDescriptor = ( ConstraintDescriptorImpl ) constraintDescriptor;
}
}
@@ -453,12 +453,12 @@
// bottom out - there is only one token left
if ( !propertyIter.hasNext() ) {
- List<ValidatorMetaData> metaDataList = validator.getMetaDataProvider().getConstraintMetaDataList();
- for ( ValidatorMetaData metaData : metaDataList ) {
- ConstraintDescriptor constraintDescriptor = metaData.getDescriptor();
- if ( metaData.getPropertyName().equals( propertyIter.getHead() ) ) {
+ List<MetaConstraint> metaConstraintList = validator.getMetaDataProvider().getConstraintMetaDataList();
+ for ( MetaConstraint metaConstraint : metaConstraintList ) {
+ ConstraintDescriptor constraintDescriptor = metaConstraint.getDescriptor();
+ if ( metaConstraint.getPropertyName().equals( propertyIter.getHead() ) ) {
return new DesrciptorValueWrapper(
- ( ConstraintDescriptorImpl ) constraintDescriptor, metaData.getValue( value )
+ ( ConstraintDescriptorImpl ) constraintDescriptor, metaConstraint.getValue( value )
);
}
}
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/ConstraintDescriptorImpl.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/ConstraintDescriptorImpl.java 2008-10-23 02:15:50 UTC (rev 15376)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/impl/ConstraintDescriptorImpl.java 2008-10-23 07:57:41 UTC (rev 15377)
@@ -28,29 +28,30 @@
import java.util.Set;
import javax.validation.Constraint;
import javax.validation.ConstraintDescriptor;
+import javax.validation.ReportAsSingleInvalidConstraint;
import javax.validation.ValidationException;
-import javax.validation.ReportAsSingleInvalidConstraint;
/**
* Describe a single constraint.
*
* @author Emmanuel Bernard
+ * @author Hardy Ferentschik
*/
public class ConstraintDescriptorImpl implements ConstraintDescriptor {
private final Annotation annotation;
private final Constraint constraintImplementation;
private final Class<? extends Constraint> constraintClass;
- private final Set<String> contexts;
+ private final Set<String> groups;
private final Map<String, Object> parameters;
private final boolean isReportAsSingleInvalidConstraint;
- public ConstraintDescriptorImpl(Annotation annotation, String[] contexts, Constraint validator, Class<? extends Constraint> constraintClass) {
+ public ConstraintDescriptorImpl(Annotation annotation, String[] groups, Constraint validator, Class<? extends Constraint> constraintClass) {
this.annotation = annotation;
- if ( contexts.length == 0 ) {
- contexts = new String[] { "default" };
+ if ( groups.length == 0 ) {
+ groups = new String[] { "default" };
}
- this.contexts = new HashSet<String>();
- this.contexts.addAll( Arrays.asList( contexts ) );
+ this.groups = new HashSet<String>();
+ this.groups.addAll( Arrays.asList( groups ) );
this.constraintImplementation = validator;
this.parameters = getAnnotationParameters( annotation );
this.isReportAsSingleInvalidConstraint = annotation.annotationType().isAnnotationPresent(
@@ -59,45 +60,60 @@
this.constraintClass = constraintClass;
}
+
/**
- * Constraint declaration annotation
+ * {@inheritDoc}
*/
public Annotation getAnnotation() {
return annotation;
}
/**
- * What are the contexts the constraint is applied on
+ * {@inheritDoc}
*/
public Set<String> getGroups() {
- return contexts;
+ return groups;
}
+ /**
+ * {@inheritDoc}
+ */
public Class<? extends Constraint> getContstraintClass() {
return constraintClass;
}
public boolean isInGroups(String group) {
- return contexts.contains( group );
+ return groups.contains( group );
}
/**
- * Return the constraint implementation routine
- * //FIXME should we get rid of that and call the ConstraintFactory each time??
+ * @return the constraint's implementation.
+ *
+ * @todo should we get rid of that and call the ConstraintFactory each time??
*/
public Constraint getConstraintImplementation() {
return constraintImplementation;
}
+ /**
+ * {@inheritDoc}
+ */
public Map<String, Object> getParameters() {
return parameters;
}
+ /**
+ * {@inheritDoc}
+ *
+ * @todo Implement
+ */
public Set<ConstraintDescriptor> getComposingConstraints() {
- //FIXME implement
return Collections.emptySet();
}
+ /**
+ * {@inheritDoc}
+ */
public boolean isReportAsSingleInvalidConstraint() {
return isReportAsSingleInvalidConstraint;
}
Modified: validator/trunk/validation-api/src/main/java/javax/validation/ConstraintDescriptor.java
===================================================================
--- validator/trunk/validation-api/src/main/java/javax/validation/ConstraintDescriptor.java 2008-10-23 02:15:50 UTC (rev 15376)
+++ validator/trunk/validation-api/src/main/java/javax/validation/ConstraintDescriptor.java 2008-10-23 07:57:41 UTC (rev 15377)
@@ -22,7 +22,7 @@
import java.util.Set;
/**
- * Describes a single constraint.
+ * Describes a single constraint and its composing constraints.
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
@@ -48,10 +48,10 @@
Class<? extends Constraint> getContstraintClass();
/**
- * Returns a map containing the annotation paramter names as keys and the annotation parameter values
+ * Returns a map containing the annotation parameter names as keys and the annotation parameter values
* as value.
- * If a composing constraint, parameter values are reflecting
- * the overridden parameters form the main constraint
+ * If this constraint is used as part of a composed constraint, parameter values are reflecting
+ * the overridden parameters form the main constraint.
*
* @return Returns a map containing the annotation paramter names as keys and the annotation parameter values
* as value.
@@ -59,11 +59,11 @@
Map<String, Object> getParameters();
/**
- * Return a set of ConstraintDescriptors. Each ConstraintDescriptor describes a composing
- * constraint. ConstraintDescriptor instances of composing constraints reflect overridden
- * parameter values in #getParameters() and #getAnnotation()
+ * Return a set of composing <code>ConstraintDescriptor</code>s where each descriptor describes a composing
+ * constraint. <code>ConstraintDescriptor</code> instances of composing constraints reflect overridden
+ * parameter values in {@link #getParameters()} and {@link #getAnnotation()}.
*
- * @return a set of ConstraintDescriptor object or an empty set
+ * @return a set of <code>ConstraintDescriptor<code> objects or an empty set in case there are no composing constraints.
*/
Set<ConstraintDescriptor> getComposingConstraints();
Modified: validator/trunk/validation-api/src/main/java/javax/validation/ConstraintFactory.java
===================================================================
--- validator/trunk/validation-api/src/main/java/javax/validation/ConstraintFactory.java 2008-10-23 02:15:50 UTC (rev 15376)
+++ validator/trunk/validation-api/src/main/java/javax/validation/ConstraintFactory.java 2008-10-23 07:57:41 UTC (rev 15377)
@@ -26,5 +26,11 @@
* @author Hardy Ferentschik
*/
public interface ConstraintFactory {
+
+ /**
+ * @param key The class of the constraint to instantiate.
+ *
+ * @return An constraint instance of the specified class.
+ */
<T extends Constraint> T getInstance(Class<T> key);
}
More information about the hibernate-commits
mailing list