Author: hardy.ferentschik
Date: 2009-03-06 17:23:09 -0500 (Fri, 06 Mar 2009)
New Revision: 16102
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/PropertyDescriptorImpl.java
Modified:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/BeanDescriptor.java
beanvalidation/trunk/validation-api/src/main/java/javax/validation/ElementDescriptor.java
beanvalidation/trunk/validation-api/src/main/java/javax/validation/PropertyDescriptor.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanDescriptorImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaData.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ElementDescriptorImpl.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java
Log:
HV-84 Introduced a proper implementation for PropertyDescriptor and refactored code.
Also made some javadoc changes to the api.
Modified:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/BeanDescriptor.java
===================================================================
---
beanvalidation/trunk/validation-api/src/main/java/javax/validation/BeanDescriptor.java 2009-03-06
20:34:24 UTC (rev 16101)
+++
beanvalidation/trunk/validation-api/src/main/java/javax/validation/BeanDescriptor.java 2009-03-06
22:23:09 UTC (rev 16102)
@@ -21,18 +21,19 @@
/**
* Describe a constrained Java Bean and the constraints associated to it.
- *
+ *
* @author Emmanuel Bernard
*/
public interface BeanDescriptor extends ElementDescriptor {
/**
* Returns true if the bean involves validation:
- * - a constraint is hosted on the bean itself
- * - a constraint is hosted on one of the bean properties
- * - or a bean property is marked for cascade (@Valid)
+ * <ul>
+ * <li> a constraint is hosted on the bean itself </li>
+ * <li> a constraint is hosted on one of the bean properties </li>
+ * <li> or a bean property is marked for cascade (@Valid) </li>
+ * </ul>
*
- * @return true if the bean nvolves validation
- *
+ * @return <code>true</code> if the bean involves validation,
<code>false</code> otherwise.
*/
boolean isBeanConstrained();
@@ -40,17 +41,19 @@
* Return the property descriptor for a given property.
* Return null if the property does not exist or has no
* constraint nor is marked as cascaded (see {@link #getConstrainedProperties()} )
- *
+ * <p/>
* The returned object (and associated objects including ConstraintDescriptors)
- * are immutable.
+ * are immutable.
*
* @param propertyName property evaludated
+ *
+ * @return the property descriptor for a given property.
*/
PropertyDescriptor getConstraintsForProperty(String propertyName);
/**
- * return the property names having at least a constraint defined or marked
- * as cascaded (@Valid)
+ * @return the property names having at least one constraint defined or which are
marked
+ * as cascaded (@Valid).
*/
Set<String> getConstrainedProperties();
}
Modified:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/ElementDescriptor.java
===================================================================
---
beanvalidation/trunk/validation-api/src/main/java/javax/validation/ElementDescriptor.java 2009-03-06
20:34:24 UTC (rev 16101)
+++
beanvalidation/trunk/validation-api/src/main/java/javax/validation/ElementDescriptor.java 2009-03-06
22:23:09 UTC (rev 16102)
@@ -28,7 +28,7 @@
public interface ElementDescriptor {
/**
- * return true if at least one constraint declaration is present on the element.
+ * @return <code>true</code> if at least one constraint declaration is
present on the element, <code>false</code> otherwise.
*/
boolean hasConstraints();
Modified:
beanvalidation/trunk/validation-api/src/main/java/javax/validation/PropertyDescriptor.java
===================================================================
---
beanvalidation/trunk/validation-api/src/main/java/javax/validation/PropertyDescriptor.java 2009-03-06
20:34:24 UTC (rev 16101)
+++
beanvalidation/trunk/validation-api/src/main/java/javax/validation/PropertyDescriptor.java 2009-03-06
22:23:09 UTC (rev 16102)
@@ -11,13 +11,13 @@
public interface PropertyDescriptor extends ElementDescriptor {
/**
* Is the property marked by the <code>@Valid</code> annotation.
- * @return true if the annotation is present
+ * @return <code>true</code> if the annotation is present,
<code>false</code> otherwise.
*/
boolean isCascaded();
/**
* Name of the property acording to the Java Bean specification.
- * @return property name
+ * @return property name.
*/
String getPropertyName();
}
\ No newline at end of file
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanDescriptorImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanDescriptorImpl.java 2009-03-06
20:34:24 UTC (rev 16101)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanDescriptorImpl.java 2009-03-06
22:23:09 UTC (rev 16102)
@@ -1,33 +1,30 @@
package org.hibernate.validation.engine;
-import java.util.Collections;
import java.util.Set;
import javax.validation.BeanDescriptor;
import javax.validation.PropertyDescriptor;
/**
* @author Emmanuel Bernard
+ * @author Hardy Ferentschik
*/
public class BeanDescriptorImpl<T> extends ElementDescriptorImpl implements
BeanDescriptor {
private final BeanMetaData<T> metadataBean;
public BeanDescriptorImpl(Class<T> returnType, BeanMetaData<T> metadataBean)
{
- super(returnType, false, "");
+ super( returnType );
this.metadataBean = metadataBean;
}
- /**
- * @todo add child validation
- */
public boolean isBeanConstrained() {
- return metadataBean.geMetaConstraintList().size() > 0;
+ return metadataBean.getConstrainedProperties().size() > 0;
}
public PropertyDescriptor getConstraintsForProperty(String propertyName) {
- return metadataBean.getPropertyDescriptors().get( propertyName );
+ return metadataBean.getPropertyDescriptor( propertyName );
}
public Set<String> getConstrainedProperties() {
- return Collections.unmodifiableSet( metadataBean.getPropertyDescriptors().keySet() );
+ return metadataBean.getConstrainedProperties();
}
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaData.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaData.java 2009-03-06
20:34:24 UTC (rev 16101)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaData.java 2009-03-06
22:23:09 UTC (rev 16102)
@@ -21,9 +21,8 @@
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.List;
-import java.util.Map;
+import java.util.Set;
import javax.validation.BeanDescriptor;
-import javax.validation.ElementDescriptor;
import javax.validation.PropertyDescriptor;
/**
@@ -70,12 +69,18 @@
List<MetaConstraint<T, ?>> geMetaConstraintList();
/**
- * @return A map keying the property name of a constraint to its
<code>ElementDescriptor</code>.
+ * Return <code>PropertyDescriptor</code> for the given property.
*
- * @todo Maybe needs to be removed since this data structure is ambigious. There could
be conflicts between field and
- * methods.
+ * @param property the property for which to retrieve the descriptor.
+ *
+ * @return Returns the <code>PropertyDescriptor</code> for the given
property or <code>null</code> in case the
+ * property does not have a descriptor.
*/
- Map<String, PropertyDescriptor> getPropertyDescriptors();
+ PropertyDescriptor getPropertyDescriptor(String property);
- ElementDescriptor getPropertyDescriptors(String property);
+ /**
+ * @return the property names having at least one constraint defined or which are
marked
+ * as cascaded (@Valid).
+ */
+ Set<String> getConstrainedProperties();
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java 2009-03-06
20:34:24 UTC (rev 16101)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java 2009-03-06
22:23:09 UTC (rev 16102)
@@ -18,14 +18,17 @@
package org.hibernate.validation.engine;
import java.lang.annotation.Annotation;
+import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import javax.validation.BeanDescriptor;
import javax.validation.GroupSequence;
import javax.validation.PropertyDescriptor;
@@ -155,7 +158,7 @@
defaultGroupSequence.add( Default.class );
}
else if ( group.getName().equals( Default.class.getName() ) ) {
- throw new ValidationException( "'Default.class' cannot appear in
default group sequence list." );
+ throw new ValidationException( "'Default.class' cannot appear in
default group sequence list." );
}
else {
defaultGroupSequence.add( group );
@@ -171,17 +174,21 @@
}
}
- private <A extends Annotation >void initFieldConstraints(Class clazz) {
+ private <A extends Annotation> void initFieldConstraints(Class clazz) {
for ( Field field : clazz.getDeclaredFields() ) {
List<ConstraintDescriptorImpl> fieldMetadata = findFieldLevelConstraints( field
);
for ( ConstraintDescriptorImpl constraintDescription : fieldMetadata ) {
ReflectionHelper.setAccessibility( field );
- MetaConstraint<T, A> metaConstraint = new MetaConstraint<T, A>( field,
beanClass, constraintDescription );
+ MetaConstraint<T, A> metaConstraint = new MetaConstraint<T, A>(
+ field, beanClass, constraintDescription
+ );
metaConstraintList.add( metaConstraint );
}
if ( field.isAnnotationPresent( Valid.class ) ) {
ReflectionHelper.setAccessibility( field );
+ String name = field.getName();
cascadedFields.add( field );
+ addPropertyDescriptorForMember( field );
}
}
}
@@ -191,16 +198,35 @@
List<ConstraintDescriptorImpl> methodMetadata = findMethodLevelConstraints(
method );
for ( ConstraintDescriptorImpl constraintDescription : methodMetadata ) {
ReflectionHelper.setAccessibility( method );
- MetaConstraint<T, A> metaConstraint = new MetaConstraint<T, A>( method,
beanClass, constraintDescription );
+ MetaConstraint<T, A> metaConstraint = new MetaConstraint<T, A>(
+ method, beanClass, constraintDescription
+ );
metaConstraintList.add( metaConstraint );
}
if ( method.isAnnotationPresent( Valid.class ) ) {
ReflectionHelper.setAccessibility( method );
cascadedMethods.add( method );
+ addPropertyDescriptorForMember( method );
}
}
}
+ private PropertyDescriptorImpl addPropertyDescriptorForMember(Member member) {
+ String name = ReflectionHelper.getPropertyName( member );
+ PropertyDescriptorImpl propertyDescriptor = ( PropertyDescriptorImpl )
propertyDescriptors.get(
+ name
+ );
+ if ( propertyDescriptor == null ) {
+ propertyDescriptor = new PropertyDescriptorImpl(
+ ReflectionHelper.getType( member ),
+ ( ( AnnotatedElement ) member ).isAnnotationPresent( Valid.class ),
+ name
+ );
+ propertyDescriptors.put( name, propertyDescriptor );
+ }
+ return propertyDescriptor;
+ }
+
private <A extends Annotation> void initClassConstraints(Class clazz) {
List<ConstraintDescriptorImpl> classMetadata = findClassLevelConstraints( clazz
);
for ( ConstraintDescriptorImpl constraintDescription : classMetadata ) {
@@ -282,16 +308,11 @@
"Annotated methods must follow the JavaBeans naming convention. " +
method.getName() + "() does not."
);
}
- ElementDescriptorImpl elementDescriptor = ( ElementDescriptorImpl )
propertyDescriptors.get( methodName );
- if ( elementDescriptor == null ) {
- elementDescriptor = new ElementDescriptorImpl(
- method.getReturnType(),
- method.isAnnotationPresent( Valid.class ),
- methodName
- );
- propertyDescriptors.put( methodName, elementDescriptor );
+ PropertyDescriptorImpl propertyDescriptor = ( PropertyDescriptorImpl )
propertyDescriptors.get( methodName );
+ if ( propertyDescriptor == null ) {
+ propertyDescriptor = addPropertyDescriptorForMember( method );
}
- elementDescriptor.addConstraintDescriptor( constraintDescriptor );
+ propertyDescriptor.addConstraintDescriptor( constraintDescriptor );
}
return metadata;
}
@@ -312,16 +333,11 @@
String fieldName = field.getName();
for ( ConstraintDescriptorImpl constraintDescriptor : metadata ) {
- ElementDescriptorImpl elementDescriptor = ( ElementDescriptorImpl )
propertyDescriptors.get( fieldName );
- if ( elementDescriptor == null ) {
- elementDescriptor = new ElementDescriptorImpl(
- field.getType(),
- field.isAnnotationPresent( Valid.class ),
- fieldName
- );
- propertyDescriptors.put( field.getName(), elementDescriptor );
+ PropertyDescriptorImpl propertyDescriptor = ( PropertyDescriptorImpl )
propertyDescriptors.get( fieldName );
+ if ( propertyDescriptor == null ) {
+ propertyDescriptor = addPropertyDescriptorForMember( field );
}
- elementDescriptor.addConstraintDescriptor( constraintDescriptor );
+ propertyDescriptor.addConstraintDescriptor( constraintDescriptor );
}
return metadata;
}
@@ -353,15 +369,15 @@
return metaConstraintList;
}
- public Map<String, PropertyDescriptor> getPropertyDescriptors() {
- return propertyDescriptors;
- }
-
- public PropertyDescriptor getPropertyDescriptors(String property) {
+ public PropertyDescriptor getPropertyDescriptor(String property) {
return propertyDescriptors.get( property );
}
public List<Class<?>> getDefaultGroupSequence() {
return defaultGroupSequence;
}
+
+ public Set<String> getConstrainedProperties() {
+ return Collections.unmodifiableSet( propertyDescriptors.keySet() );
+ }
}
\ No newline at end of file
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ElementDescriptorImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ElementDescriptorImpl.java 2009-03-06
20:34:24 UTC (rev 16101)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ElementDescriptorImpl.java 2009-03-06
22:23:09 UTC (rev 16102)
@@ -21,28 +21,20 @@
import java.util.HashSet;
import java.util.Set;
import javax.validation.ConstraintDescriptor;
-import javax.validation.PropertyDescriptor;
+import javax.validation.ElementDescriptor;
/**
* Describe a validated element (class, field or property).
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
- * @todo Should returnType be renamed to type?
- * @todo Handle problem in descirbing cyclic dependecies for propertyPath
*/
-//FIXME I implement both interfaces on the same object as a quick hack, we need to fix
that.
-public class ElementDescriptorImpl implements PropertyDescriptor {
- private final Class<?> returnType;
- private final boolean cascaded;
+public class ElementDescriptorImpl implements ElementDescriptor {
+ private final Class<?> type;
private final Set<ConstraintDescriptor<?>> constraintDescriptors = new
HashSet<ConstraintDescriptor<?>>();
- private final String propertyPath;
-
- public ElementDescriptorImpl(Class<?> returnType, boolean cascaded, String
propertyPath) {
- this.returnType = returnType;
- this.cascaded = cascaded;
- this.propertyPath = propertyPath;
+ public ElementDescriptorImpl(Class<?> type) {
+ this.type = type;
}
public void addConstraintDescriptor(ConstraintDescriptorImpl constraintDescriptor) {
@@ -55,31 +47,15 @@
/**
* {@inheritDoc}
- *
- * @todo Generic type or regular type?
*/
- public Class getType() {
- return returnType;
+ public Class<?> getType() {
+ return type;
}
/**
* {@inheritDoc}
*/
- public boolean isCascaded() {
- return cascaded;
- }
-
- /**
- * {@inheritDoc}
- */
public Set<ConstraintDescriptor<?>> getConstraintDescriptors() {
return Collections.unmodifiableSet( constraintDescriptors );
}
-
- /**
- * {@inheritDoc}
- */
- public String getPropertyName() {
- return propertyPath;
- }
}
Copied:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/PropertyDescriptorImpl.java
(from rev 16096,
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ElementDescriptorImpl.java)
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/PropertyDescriptorImpl.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/PropertyDescriptorImpl.java 2009-03-06
22:23:09 UTC (rev 16102)
@@ -0,0 +1,52 @@
+// $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 javax.validation.PropertyDescriptor;
+
+/**
+ * Describe a validated element (class, field or property).
+ *
+ * @author Emmanuel Bernard
+ * @author Hardy Ferentschik
+ */
+public class PropertyDescriptorImpl extends ElementDescriptorImpl implements
PropertyDescriptor {
+ private final boolean cascaded;
+ private final String property;
+
+
+ public PropertyDescriptorImpl(Class<?> returnType, boolean cascaded, String
property) {
+ super( returnType );
+ this.cascaded = cascaded;
+ this.property = property;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean isCascaded() {
+ return cascaded;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public String getPropertyName() {
+ return property;
+ }
+}
\ No newline at end of file
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-06
20:34:24 UTC (rev 16101)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java 2009-03-06
22:23:09 UTC (rev 16102)
@@ -122,6 +122,26 @@
}
/**
+ * Returns the type of the field of return type of a method.
+ *
+ * @param member the member for which to get the type.
+ *
+ * @return Returns the type of the field of return type of a method.
+ */
+ public static Class<?> getType(Member member) {
+
+ Class<?> type = null;
+ if ( member instanceof Field ) {
+ type = ( ( Field ) member ).getType();
+ }
+
+ if ( member instanceof Method ) {
+ type = ( ( Method ) member ).getReturnType();
+ }
+ return type;
+ }
+
+ /**
* @param member The <code>Member</code> instance for which to retrieve the
type.
*
* @return Retrurns the <code>Type</code> of the given
<code>Field</code> or <code>Method</code>.