Permalink
Browse files

BVAL-31 refactored some code, imported AnnotationFactory from annotat…

…ions commons, added explicit license to project

git-svn-id: https://svn.jboss.org/repos/hibernate/validator/trunk@15751 1b8cb986-b30d-0410-93ca-fae66ebed9b2
  • Loading branch information...
1 parent c0000ec commit 293b2003b6b832f28d0e10d81cb048d98fb3217d @hferentschik hferentschik committed Jan 7, 2009
Showing with 756 additions and 250 deletions.
  1. +1 −1 ...lidator/src/main/java/org/hibernate/validation/engine/{MetaDataProvider.java → BeanMetaData.java}
  2. +2 −2 ...src/main/java/org/hibernate/validation/engine/{MetaDataProviderImpl.java → BeanMetaDataImpl.java}
  3. +17 −0 hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintContextImpl.java
  4. +2 −1 hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java
  5. +8 −8 hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidationContext.java
  6. +20 −3 hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImplementor.java
  7. +14 −20 hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java
  8. +7 −7 hibernate-validator/src/main/java/org/hibernate/validation/impl/BeanDescriptorImpl.java
  9. +6 −6 hibernate-validator/src/main/java/org/hibernate/validation/impl/ValidatorFactoryImpl.java
  10. +11 −3 hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java
  11. +64 −0 ...validator/src/main/java/org/hibernate/validation/util/annotationfactory/AnnotationDescriptor.java
  12. +59 −0 ...te-validator/src/main/java/org/hibernate/validation/util/annotationfactory/AnnotationFactory.java
  13. +128 −0 ...nate-validator/src/main/java/org/hibernate/validation/util/annotationfactory/AnnotationProxy.java
  14. +0 −188 hibernate-validator/src/test/java/org/hibernate/javadoc/JSRDoclet.java
  15. +2 −1 hibernate-validator/src/test/java/org/hibernate/validation/constraints/LengthConstraintTest.java
  16. +44 −0 hibernate-validator/src/test/java/org/hibernate/validation/eg/constraint/FrenchZipcode.java
  17. +34 −0 ...rnate-validator/src/test/java/org/hibernate/validation/eg/constraint/FrenchZipcodeConstraint.java
  18. +1 −1 hibernate-validator/src/test/java/org/hibernate/validation/eg/constraint/NoGroups.java
  19. +1 −1 hibernate-validator/src/test/java/org/hibernate/validation/eg/constraint/NoMessage.java
  20. +41 −0 hibernate-validator/src/test/java/org/hibernate/validation/eg/constraint/ValidProperty.java
  21. +28 −2 hibernate-validator/src/test/java/org/hibernate/validation/util/ReflectionHelperTest.java
  22. +58 −0 ...alidator/src/test/java/org/hibernate/validation/util/annotationfactory/AnnotationFactoryTest.java
  23. +202 −0 license.txt
  24. +1 −1 pom.xml
  25. +1 −1 validation-api/src/main/java/javax/validation/ConstraintContext.java
  26. +4 −4 validation-api/src/main/java/javax/validation/OverridesParameter.java
@@ -31,7 +31,7 @@
*
* @author Hardy Ferentschik
*/
-public interface MetaDataProvider<T> {
+public interface BeanMetaData<T> {
/**
* @return the class of the bean.
@@ -54,7 +54,7 @@
* FIXME create an interface for MetadataProvider
*/
-public class MetaDataProviderImpl<T> implements MetaDataProvider<T> {
+public class BeanMetaDataImpl<T> implements BeanMetaData<T> {
private static final Logger log = LoggerFactory.make();
@@ -99,7 +99,7 @@
*/
private Map<Class<?>, List<Class<?>>> groupSequences = new HashMap<Class<?>, List<Class<?>>>();
- public MetaDataProviderImpl(Class<T> beanClass, ConstraintFactory constraintFactory) {
+ public BeanMetaDataImpl(Class<T> beanClass, ConstraintFactory constraintFactory) {
this.beanClass = beanClass;
this.constraintFactory = constraintFactory;
createMetaData();
@@ -1,3 +1,20 @@
+// $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.util.List;
@@ -22,6 +22,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import javax.validation.ValidationException;
+import javax.validation.ConstraintDescriptor;
import org.hibernate.validation.impl.ConstraintDescriptorImpl;
import org.hibernate.validation.util.ReflectionHelper;
@@ -138,7 +139,7 @@ public boolean isArray() {
return ReflectionHelper.isArray( t );
}
- public ConstraintDescriptorImpl getDescriptor() {
+ public ConstraintDescriptor getDescriptor() {
return descriptor;
}
@@ -70,7 +70,7 @@
/**
* Stack for keep track of the currently validated object.
*/
- private Stack<ValidatedBean> validatedobjectStack = new Stack<ValidatedBean>();
+ private Stack<ValidatedBean> validatedObjectStack = new Stack<ValidatedBean>();
public ValidationContext(T object) {
@@ -79,26 +79,26 @@ public ValidationContext(T object) {
public ValidationContext(T rootBean, Object object) {
this.rootBean = rootBean;
- validatedobjectStack.push( new ValidatedBean(object) );
+ validatedObjectStack.push( new ValidatedBean(object) );
processedObjects = new HashMap<Class<?>, IdentitySet>();
propertyPath = "";
failingConstraintViolations = new ArrayList<ConstraintViolationImpl<T>>();
}
public Object peekValidatedObject() {
- return validatedobjectStack.peek().bean;
+ return validatedObjectStack.peek().bean;
}
public Class<?> peekValidatedObjectType() {
- return validatedobjectStack.peek().beanType;
+ return validatedObjectStack.peek().beanType;
}
public void pushValidatedObject(Object validatedObject) {
- validatedobjectStack.push( new ValidatedBean(validatedObject) );
+ validatedObjectStack.push( new ValidatedBean(validatedObject) );
}
public void popValidatedObject() {
- validatedobjectStack.pop();
+ validatedObjectStack.pop();
}
public T getRootBean() {
@@ -115,11 +115,11 @@ public void setCurrentGroup(Class<?> currentGroup) {
public void markProcessedForCurrentGroup() {
if ( processedObjects.containsKey( currentGroup ) ) {
- processedObjects.get( currentGroup ).add( validatedobjectStack.peek().bean );
+ processedObjects.get( currentGroup ).add( validatedObjectStack.peek().bean );
}
else {
IdentitySet set = new IdentitySet();
- set.add( validatedobjectStack.peek().bean );
+ set.add( validatedObjectStack.peek().bean );
processedObjects.put( currentGroup, set );
}
}
@@ -1,3 +1,20 @@
+// $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.ValidatorFactory;
@@ -7,8 +24,8 @@
*/
public interface ValidatorFactoryImplementor extends ValidatorFactory {
/**
- * Gives access to the required parsed meta data.
- * This never returns an null object
+ * @param clazz The bean class for which to retrieve the meta data.
+ * @return Gives access to the required parsed meta data. This never returns an <code>null</code> object.
*/
- <T> MetaDataProviderImpl<T> getMetadataProvider(Class<T> clazz);
+ <T> BeanMetaDataImpl<T> getBeanMetaData(Class<T> clazz);
}
@@ -64,13 +64,6 @@
Version.touch();
}
- //TODO remove
- /**
- * A map for caching validators for cascaded entities.
- */
- //private final Map<Class<?>, ValidatorImpl> subValidators = new ConcurrentHashMap<Class<?>, ValidatorImpl>();
-
-
/**
* Message resolver used for interpolating error messages.
*/
@@ -151,10 +144,10 @@ public ValidatorImpl(ValidatorFactoryImplementor factory, MessageResolver messag
private <T> void validateConstraints(ValidationContext<T> context) {
//casting rely on the fact that root object is at the top of the stack
@SuppressWarnings( "unchecked" )
- MetaDataProviderImpl<T> metaDataProvider =
- ( MetaDataProviderImpl<T> ) factory.getMetadataProvider( context.peekValidatedObjectType() );
- for ( MetaConstraint metaConstraint : metaDataProvider.getConstraintMetaDataList() ) {
- ConstraintDescriptorImpl constraintDescriptor = metaConstraint.getDescriptor();
+ BeanMetaData<T> beanMetaData =
+ ( BeanMetaData<T> ) factory.getBeanMetaData( context.peekValidatedObjectType() );
+ for ( MetaConstraint metaConstraint : beanMetaData.getConstraintMetaDataList() ) {
+ ConstraintDescriptorImpl constraintDescriptor = (ConstraintDescriptorImpl) metaConstraint.getDescriptor();
context.pushProperty( metaConstraint.getPropertyName() );
if ( !context.needsValidation( constraintDescriptor.getGroups() ) ) {
@@ -178,7 +171,7 @@ public ValidatorImpl(ValidatorFactoryImplementor factory, MessageResolver messag
message,
interpolatedMessage,
context.getRootBean(),
- metaDataProvider.getBeanClass(),
+ beanMetaData.getBeanClass(),
leafBeanInstance,
value,
context.peekPropertyPath(), //FIXME use error.getProperty()
@@ -223,7 +216,7 @@ else if ( ReflectionHelper.isArray( type ) ) {
}
private <T> void validateCascadedConstraints(ValidationContext<T> context) {
- List<Member> cascadedMembers = factory.getMetadataProvider( context.peekValidatedObjectType() ).getCascadedMembers();
+ List<Member> cascadedMembers = factory.getBeanMetaData( context.peekValidatedObjectType() ).getCascadedMembers();
for ( Member member : cascadedMembers ) {
Type type = ReflectionHelper.typeOf( member );
context.pushProperty( ReflectionHelper.getPropertyName( member ) );
@@ -345,7 +338,7 @@ else if ( ReflectionHelper.isArray( type ) ) {
}
public BeanDescriptor getConstraintsForClass(Class<?> clazz) {
- return factory.getMetadataProvider( clazz ).getBeanDescriptor();
+ return factory.getBeanMetaData( clazz ).getBeanDescriptor();
}
@@ -425,7 +418,7 @@ private ConstraintDescriptorImpl getConstraintDescriptorForPath(Class<?> clazz,
propertyIter.split();
if ( !propertyIter.hasNext() ) {
- List<MetaConstraint> metaConstraintList = factory.getMetadataProvider(clazz).getConstraintMetaDataList();
+ List<MetaConstraint> metaConstraintList = factory.getBeanMetaData(clazz).getConstraintMetaDataList();
for ( MetaConstraint metaConstraint : metaConstraintList ) {
ConstraintDescriptor constraintDescriptor = metaConstraint.getDescriptor();
if ( metaConstraint.getPropertyName().equals( propertyIter.getHead() ) ) {
@@ -434,7 +427,7 @@ private ConstraintDescriptorImpl getConstraintDescriptorForPath(Class<?> clazz,
}
}
else {
- List<Member> cascadedMembers = factory.getMetadataProvider(clazz).getCascadedMembers();
+ List<Member> cascadedMembers = factory.getBeanMetaData(clazz).getCascadedMembers();
for ( Member m : cascadedMembers ) {
if ( ReflectionHelper.getPropertyName( m ).equals( propertyIter.getHead() ) ) {
Type type = ReflectionHelper.typeOf( m );
@@ -463,7 +456,7 @@ private DesrciptorValueWrapper getConstraintDescriptorAndValueForPath(Class<?> c
// bottom out - there is only one token left
if ( !propertyIter.hasNext() ) {
- List<MetaConstraint> metaConstraintList = factory.getMetadataProvider(clazz).getConstraintMetaDataList();
+ List<MetaConstraint> metaConstraintList = factory.getBeanMetaData(clazz).getConstraintMetaDataList();
for ( MetaConstraint metaConstraint : metaConstraintList ) {
ConstraintDescriptor constraintDescriptor = metaConstraint.getDescriptor();
if ( metaConstraint.getPropertyName().equals( propertyIter.getHead() ) ) {
@@ -474,7 +467,7 @@ private DesrciptorValueWrapper getConstraintDescriptorAndValueForPath(Class<?> c
}
}
else {
- List<Member> cascadedMembers = factory.getMetadataProvider(clazz).getCascadedMembers();
+ List<Member> cascadedMembers = factory.getBeanMetaData(clazz).getCascadedMembers();
for ( Member m : cascadedMembers ) {
if ( ReflectionHelper.getPropertyName( m ).equals( propertyIter.getHead() ) ) {
ReflectionHelper.setAccessibility( m );
@@ -509,8 +502,9 @@ private DesrciptorValueWrapper getConstraintDescriptorAndValueForPath(Class<?> c
/**
* Checks whether the provided group name is a group sequence and if so expands the group name and add the expanded
- * groups names to <code>expandedGroupName </code>
+ * groups names to <code>expandedGroupName</code>.
*
+ * @param beanType The class for which to expand the group names.
* @param group The group to expand
* @param expandedGroups The exanded group names or just a list with the single provided group name id the name
* was not expandable
@@ -523,7 +517,7 @@ private DesrciptorValueWrapper getConstraintDescriptorAndValueForPath(Class<?> c
}
boolean isGroupSequence;
- MetaDataProviderImpl<T> metaDataProvider = factory.getMetadataProvider( beanType );
+ BeanMetaDataImpl<T> metaDataProvider = factory.getBeanMetaData( beanType );
if ( metaDataProvider.getGroupSequences().containsKey( group ) ) {
expandedGroups.addAll( metaDataProvider.getGroupSequences().get( group ) );
isGroupSequence = true;
@@ -5,31 +5,31 @@
import javax.validation.BeanDescriptor;
import javax.validation.PropertyDescriptor;
-import org.hibernate.validation.engine.MetaDataProvider;
+import org.hibernate.validation.engine.BeanMetaData;
/**
* @author Emmanuel Bernard
*/
public class BeanDescriptorImpl<T> extends ElementDescriptorImpl implements BeanDescriptor {
- private final MetaDataProvider<T> metadataProvider;
+ private final BeanMetaData<T> metadataBean;
- public BeanDescriptorImpl(Class<T> returnType, MetaDataProvider<T> metadataProvider) {
+ public BeanDescriptorImpl(Class<T> returnType, BeanMetaData<T> metadataBean) {
super(returnType, false, "");
- this.metadataProvider = metadataProvider;
+ this.metadataBean = metadataBean;
}
/**
* @todo add child validation
*/
public boolean hasConstraints() {
- return metadataProvider.getConstraintMetaDataList().size() > 0;
+ return metadataBean.getConstraintMetaDataList().size() > 0;
}
public PropertyDescriptor getConstraintsForProperty(String propertyName) {
- return metadataProvider.getPropertyDescriptors().get( propertyName );
+ return metadataBean.getPropertyDescriptors().get( propertyName );
}
public Set<String> getPropertiesWithConstraints() {
- return Collections.unmodifiableSet( metadataProvider.getPropertyDescriptors().keySet() );
+ return Collections.unmodifiableSet( metadataBean.getPropertyDescriptors().keySet() );
}
}
@@ -26,7 +26,7 @@
import javax.validation.ValidatorBuilder;
import javax.validation.spi.ValidatorFactoryConfiguration;
-import org.hibernate.validation.engine.MetaDataProviderImpl;
+import org.hibernate.validation.engine.BeanMetaDataImpl;
import org.hibernate.validation.engine.ValidatorFactoryImplementor;
/**
@@ -40,8 +40,8 @@
private final ConstraintFactory constraintFactory;
//TODO is there a way to replace ? by so kind of <T> to express the correlation?
- private Map<Class<?>, MetaDataProviderImpl<?>> metadataProviders
- = new ConcurrentHashMap<Class<?>, MetaDataProviderImpl<?>>(10);
+ private Map<Class<?>, BeanMetaDataImpl<?>> metadataProviders
+ = new ConcurrentHashMap<Class<?>, BeanMetaDataImpl<?>>(10);
public ValidatorFactoryImpl(ValidatorFactoryConfiguration configuration) {
@@ -66,13 +66,13 @@ public ValidatorBuilder defineValidatorState() {
return new ValidatorBuilderImpl(this, messageResolver, traversableResolver);
}
- public <T> MetaDataProviderImpl<T> getMetadataProvider(Class<T> beanClass) {
+ public <T> BeanMetaDataImpl<T> getBeanMetaData(Class<T> beanClass) {
//FIXME make sure a optimized mock is provided when no constraints are present.
if (beanClass == null) throw new IllegalArgumentException( "Class cannot be null" );
@SuppressWarnings( "unchecked")
- MetaDataProviderImpl<T> metadata = ( MetaDataProviderImpl<T> ) metadataProviders.get(beanClass);
+ BeanMetaDataImpl<T> metadata = ( BeanMetaDataImpl<T> ) metadataProviders.get(beanClass);
if (metadata == null) {
- metadata = new MetaDataProviderImpl<T>(beanClass, constraintFactory);
+ metadata = new BeanMetaDataImpl<T>(beanClass, constraintFactory);
metadataProviders.put( beanClass, metadata );
}
return metadata;
@@ -130,6 +130,15 @@ public static boolean isConstraintAnnotation(Annotation annotation) {
return false;
}
+ Method[] methods = annotation.getClass().getMethods();
+ for ( Method m : methods ) {
+ if ( m.getName().startsWith( "valid" ) ) {
+ String msg = "Parameters starting with 'valid' are not allowed in a constraint.";
+ log.warn( msg );
+ return false;
+ }
+ }
+
return true;
}
@@ -140,7 +149,6 @@ public static boolean isConstraintAnnotation(Annotation annotation) {
*
* @return A list of constraint annotations or the empty list if <code>annotation</code> is not a multi constraint
* annotation.
- *
*/
public static <A extends Annotation> List<Annotation> getMultiValueConstraints(A annotation) {
List<Annotation> annotationList = new ArrayList<Annotation>();
@@ -149,8 +157,8 @@ public static boolean isConstraintAnnotation(Annotation annotation) {
Class returnType = m.getReturnType();
if ( returnType.isArray() && returnType.getComponentType().isAnnotation() ) {
Annotation[] annotations = ( Annotation[] ) m.invoke( annotation );
- for (Annotation a : annotations) {
- if( isConstraintAnnotation( a ) || isBuiltInConstraintAnnotation( a )) {
+ for ( Annotation a : annotations ) {
+ if ( isConstraintAnnotation( a ) || isBuiltInConstraintAnnotation( a ) ) {
annotationList.add( a );
}
}
Oops, something went wrong.

0 comments on commit 293b200

Please sign in to comment.