Skip to content

Commit 293b200

Browse files
committed
BVAL-31 refactored some code, imported AnnotationFactory from annotations commons, added explicit license to project
git-svn-id: https://svn.jboss.org/repos/hibernate/validator/trunk@15751 1b8cb986-b30d-0410-93ca-fae66ebed9b2
1 parent c0000ec commit 293b200

26 files changed

+756
-250
lines changed

hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaDataProvider.java renamed to hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*
3232
* @author Hardy Ferentschik
3333
*/
34-
public interface MetaDataProvider<T> {
34+
public interface BeanMetaData<T> {
3535

3636
/**
3737
* @return the class of the bean.

hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaDataProviderImpl.java renamed to hibernate-validator/src/main/java/org/hibernate/validation/engine/BeanMetaDataImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
* FIXME create an interface for MetadataProvider
5555
*/
5656

57-
public class MetaDataProviderImpl<T> implements MetaDataProvider<T> {
57+
public class BeanMetaDataImpl<T> implements BeanMetaData<T> {
5858

5959
private static final Logger log = LoggerFactory.make();
6060

@@ -99,7 +99,7 @@ public class MetaDataProviderImpl<T> implements MetaDataProvider<T> {
9999
*/
100100
private Map<Class<?>, List<Class<?>>> groupSequences = new HashMap<Class<?>, List<Class<?>>>();
101101

102-
public MetaDataProviderImpl(Class<T> beanClass, ConstraintFactory constraintFactory) {
102+
public BeanMetaDataImpl(Class<T> beanClass, ConstraintFactory constraintFactory) {
103103
this.beanClass = beanClass;
104104
this.constraintFactory = constraintFactory;
105105
createMetaData();

hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintContextImpl.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
// $Id$
2+
/*
3+
* JBoss, Home of Professional Open Source
4+
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
5+
* by the @authors tag. See the copyright.txt in the distribution for a
6+
* full listing of individual contributors.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
118
package org.hibernate.validation.engine;
219

320
import java.util.List;

hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.reflect.Method;
2323
import java.lang.reflect.Type;
2424
import javax.validation.ValidationException;
25+
import javax.validation.ConstraintDescriptor;
2526

2627
import org.hibernate.validation.impl.ConstraintDescriptorImpl;
2728
import org.hibernate.validation.util.ReflectionHelper;
@@ -138,7 +139,7 @@ public boolean isArray() {
138139
return ReflectionHelper.isArray( t );
139140
}
140141

141-
public ConstraintDescriptorImpl getDescriptor() {
142+
public ConstraintDescriptor getDescriptor() {
142143
return descriptor;
143144
}
144145

hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidationContext.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public class ValidationContext<T> {
7070
/**
7171
* Stack for keep track of the currently validated object.
7272
*/
73-
private Stack<ValidatedBean> validatedobjectStack = new Stack<ValidatedBean>();
73+
private Stack<ValidatedBean> validatedObjectStack = new Stack<ValidatedBean>();
7474

7575

7676
public ValidationContext(T object) {
@@ -79,26 +79,26 @@ public ValidationContext(T object) {
7979

8080
public ValidationContext(T rootBean, Object object) {
8181
this.rootBean = rootBean;
82-
validatedobjectStack.push( new ValidatedBean(object) );
82+
validatedObjectStack.push( new ValidatedBean(object) );
8383
processedObjects = new HashMap<Class<?>, IdentitySet>();
8484
propertyPath = "";
8585
failingConstraintViolations = new ArrayList<ConstraintViolationImpl<T>>();
8686
}
8787

8888
public Object peekValidatedObject() {
89-
return validatedobjectStack.peek().bean;
89+
return validatedObjectStack.peek().bean;
9090
}
9191

9292
public Class<?> peekValidatedObjectType() {
93-
return validatedobjectStack.peek().beanType;
93+
return validatedObjectStack.peek().beanType;
9494
}
9595

9696
public void pushValidatedObject(Object validatedObject) {
97-
validatedobjectStack.push( new ValidatedBean(validatedObject) );
97+
validatedObjectStack.push( new ValidatedBean(validatedObject) );
9898
}
9999

100100
public void popValidatedObject() {
101-
validatedobjectStack.pop();
101+
validatedObjectStack.pop();
102102
}
103103

104104
public T getRootBean() {
@@ -115,11 +115,11 @@ public void setCurrentGroup(Class<?> currentGroup) {
115115

116116
public void markProcessedForCurrentGroup() {
117117
if ( processedObjects.containsKey( currentGroup ) ) {
118-
processedObjects.get( currentGroup ).add( validatedobjectStack.peek().bean );
118+
processedObjects.get( currentGroup ).add( validatedObjectStack.peek().bean );
119119
}
120120
else {
121121
IdentitySet set = new IdentitySet();
122-
set.add( validatedobjectStack.peek().bean );
122+
set.add( validatedObjectStack.peek().bean );
123123
processedObjects.put( currentGroup, set );
124124
}
125125
}
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
// $Id$
2+
/*
3+
* JBoss, Home of Professional Open Source
4+
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
5+
* by the @authors tag. See the copyright.txt in the distribution for a
6+
* full listing of individual contributors.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
118
package org.hibernate.validation.engine;
219

320
import javax.validation.ValidatorFactory;
@@ -7,8 +24,8 @@
724
*/
825
public interface ValidatorFactoryImplementor extends ValidatorFactory {
926
/**
10-
* Gives access to the required parsed meta data.
11-
* This never returns an null object
27+
* @param clazz The bean class for which to retrieve the meta data.
28+
* @return Gives access to the required parsed meta data. This never returns an <code>null</code> object.
1229
*/
13-
<T> MetaDataProviderImpl<T> getMetadataProvider(Class<T> clazz);
30+
<T> BeanMetaDataImpl<T> getBeanMetaData(Class<T> clazz);
1431
}

hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorImpl.java

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@ public class ValidatorImpl implements Validator {
6464
Version.touch();
6565
}
6666

67-
//TODO remove
68-
/**
69-
* A map for caching validators for cascaded entities.
70-
*/
71-
//private final Map<Class<?>, ValidatorImpl> subValidators = new ConcurrentHashMap<Class<?>, ValidatorImpl>();
72-
73-
7467
/**
7568
* Message resolver used for interpolating error messages.
7669
*/
@@ -151,10 +144,10 @@ private <T> List<ConstraintViolationImpl<T>> validateInContext(ValidationContext
151144
private <T> void validateConstraints(ValidationContext<T> context) {
152145
//casting rely on the fact that root object is at the top of the stack
153146
@SuppressWarnings( "unchecked" )
154-
MetaDataProviderImpl<T> metaDataProvider =
155-
( MetaDataProviderImpl<T> ) factory.getMetadataProvider( context.peekValidatedObjectType() );
156-
for ( MetaConstraint metaConstraint : metaDataProvider.getConstraintMetaDataList() ) {
157-
ConstraintDescriptorImpl constraintDescriptor = metaConstraint.getDescriptor();
147+
BeanMetaData<T> beanMetaData =
148+
( BeanMetaData<T> ) factory.getBeanMetaData( context.peekValidatedObjectType() );
149+
for ( MetaConstraint metaConstraint : beanMetaData.getConstraintMetaDataList() ) {
150+
ConstraintDescriptorImpl constraintDescriptor = (ConstraintDescriptorImpl) metaConstraint.getDescriptor();
158151
context.pushProperty( metaConstraint.getPropertyName() );
159152

160153
if ( !context.needsValidation( constraintDescriptor.getGroups() ) ) {
@@ -178,7 +171,7 @@ private <T> void validateConstraints(ValidationContext<T> context) {
178171
message,
179172
interpolatedMessage,
180173
context.getRootBean(),
181-
metaDataProvider.getBeanClass(),
174+
beanMetaData.getBeanClass(),
182175
leafBeanInstance,
183176
value,
184177
context.peekPropertyPath(), //FIXME use error.getProperty()
@@ -223,7 +216,7 @@ else if ( ReflectionHelper.isArray( type ) ) {
223216
}
224217

225218
private <T> void validateCascadedConstraints(ValidationContext<T> context) {
226-
List<Member> cascadedMembers = factory.getMetadataProvider( context.peekValidatedObjectType() ).getCascadedMembers();
219+
List<Member> cascadedMembers = factory.getBeanMetaData( context.peekValidatedObjectType() ).getCascadedMembers();
227220
for ( Member member : cascadedMembers ) {
228221
Type type = ReflectionHelper.typeOf( member );
229222
context.pushProperty( ReflectionHelper.getPropertyName( member ) );
@@ -345,7 +338,7 @@ public <T> Set<ConstraintViolation<T>> validateValue(Class<T> beanType, String p
345338
}
346339

347340
public BeanDescriptor getConstraintsForClass(Class<?> clazz) {
348-
return factory.getMetadataProvider( clazz ).getBeanDescriptor();
341+
return factory.getBeanMetaData( clazz ).getBeanDescriptor();
349342
}
350343

351344

@@ -425,7 +418,7 @@ private ConstraintDescriptorImpl getConstraintDescriptorForPath(Class<?> clazz,
425418
propertyIter.split();
426419

427420
if ( !propertyIter.hasNext() ) {
428-
List<MetaConstraint> metaConstraintList = factory.getMetadataProvider(clazz).getConstraintMetaDataList();
421+
List<MetaConstraint> metaConstraintList = factory.getBeanMetaData(clazz).getConstraintMetaDataList();
429422
for ( MetaConstraint metaConstraint : metaConstraintList ) {
430423
ConstraintDescriptor constraintDescriptor = metaConstraint.getDescriptor();
431424
if ( metaConstraint.getPropertyName().equals( propertyIter.getHead() ) ) {
@@ -434,7 +427,7 @@ private ConstraintDescriptorImpl getConstraintDescriptorForPath(Class<?> clazz,
434427
}
435428
}
436429
else {
437-
List<Member> cascadedMembers = factory.getMetadataProvider(clazz).getCascadedMembers();
430+
List<Member> cascadedMembers = factory.getBeanMetaData(clazz).getCascadedMembers();
438431
for ( Member m : cascadedMembers ) {
439432
if ( ReflectionHelper.getPropertyName( m ).equals( propertyIter.getHead() ) ) {
440433
Type type = ReflectionHelper.typeOf( m );
@@ -463,7 +456,7 @@ private DesrciptorValueWrapper getConstraintDescriptorAndValueForPath(Class<?> c
463456

464457
// bottom out - there is only one token left
465458
if ( !propertyIter.hasNext() ) {
466-
List<MetaConstraint> metaConstraintList = factory.getMetadataProvider(clazz).getConstraintMetaDataList();
459+
List<MetaConstraint> metaConstraintList = factory.getBeanMetaData(clazz).getConstraintMetaDataList();
467460
for ( MetaConstraint metaConstraint : metaConstraintList ) {
468461
ConstraintDescriptor constraintDescriptor = metaConstraint.getDescriptor();
469462
if ( metaConstraint.getPropertyName().equals( propertyIter.getHead() ) ) {
@@ -474,7 +467,7 @@ private DesrciptorValueWrapper getConstraintDescriptorAndValueForPath(Class<?> c
474467
}
475468
}
476469
else {
477-
List<Member> cascadedMembers = factory.getMetadataProvider(clazz).getCascadedMembers();
470+
List<Member> cascadedMembers = factory.getBeanMetaData(clazz).getCascadedMembers();
478471
for ( Member m : cascadedMembers ) {
479472
if ( ReflectionHelper.getPropertyName( m ).equals( propertyIter.getHead() ) ) {
480473
ReflectionHelper.setAccessibility( m );
@@ -509,8 +502,9 @@ private <T> void addFailingConstraint(List<ConstraintViolationImpl<T>> failingCo
509502

510503
/**
511504
* Checks whether the provided group name is a group sequence and if so expands the group name and add the expanded
512-
* groups names to <code>expandedGroupName </code>
505+
* groups names to <code>expandedGroupName</code>.
513506
*
507+
* @param beanType The class for which to expand the group names.
514508
* @param group The group to expand
515509
* @param expandedGroups The exanded group names or just a list with the single provided group name id the name
516510
* was not expandable
@@ -523,7 +517,7 @@ private <T> boolean expandGroup(Class<T> beanType, Class<?> group, List<Class<?>
523517
}
524518

525519
boolean isGroupSequence;
526-
MetaDataProviderImpl<T> metaDataProvider = factory.getMetadataProvider( beanType );
520+
BeanMetaDataImpl<T> metaDataProvider = factory.getBeanMetaData( beanType );
527521
if ( metaDataProvider.getGroupSequences().containsKey( group ) ) {
528522
expandedGroups.addAll( metaDataProvider.getGroupSequences().get( group ) );
529523
isGroupSequence = true;

hibernate-validator/src/main/java/org/hibernate/validation/impl/BeanDescriptorImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,31 @@
55
import javax.validation.BeanDescriptor;
66
import javax.validation.PropertyDescriptor;
77

8-
import org.hibernate.validation.engine.MetaDataProvider;
8+
import org.hibernate.validation.engine.BeanMetaData;
99

1010
/**
1111
* @author Emmanuel Bernard
1212
*/
1313
public class BeanDescriptorImpl<T> extends ElementDescriptorImpl implements BeanDescriptor {
14-
private final MetaDataProvider<T> metadataProvider;
14+
private final BeanMetaData<T> metadataBean;
1515

16-
public BeanDescriptorImpl(Class<T> returnType, MetaDataProvider<T> metadataProvider) {
16+
public BeanDescriptorImpl(Class<T> returnType, BeanMetaData<T> metadataBean) {
1717
super(returnType, false, "");
18-
this.metadataProvider = metadataProvider;
18+
this.metadataBean = metadataBean;
1919
}
2020

2121
/**
2222
* @todo add child validation
2323
*/
2424
public boolean hasConstraints() {
25-
return metadataProvider.getConstraintMetaDataList().size() > 0;
25+
return metadataBean.getConstraintMetaDataList().size() > 0;
2626
}
2727

2828
public PropertyDescriptor getConstraintsForProperty(String propertyName) {
29-
return metadataProvider.getPropertyDescriptors().get( propertyName );
29+
return metadataBean.getPropertyDescriptors().get( propertyName );
3030
}
3131

3232
public Set<String> getPropertiesWithConstraints() {
33-
return Collections.unmodifiableSet( metadataProvider.getPropertyDescriptors().keySet() );
33+
return Collections.unmodifiableSet( metadataBean.getPropertyDescriptors().keySet() );
3434
}
3535
}

hibernate-validator/src/main/java/org/hibernate/validation/impl/ValidatorFactoryImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import javax.validation.ValidatorBuilder;
2727
import javax.validation.spi.ValidatorFactoryConfiguration;
2828

29-
import org.hibernate.validation.engine.MetaDataProviderImpl;
29+
import org.hibernate.validation.engine.BeanMetaDataImpl;
3030
import org.hibernate.validation.engine.ValidatorFactoryImplementor;
3131

3232
/**
@@ -40,8 +40,8 @@ public class ValidatorFactoryImpl implements ValidatorFactoryImplementor {
4040
private final ConstraintFactory constraintFactory;
4141

4242
//TODO is there a way to replace ? by so kind of <T> to express the correlation?
43-
private Map<Class<?>, MetaDataProviderImpl<?>> metadataProviders
44-
= new ConcurrentHashMap<Class<?>, MetaDataProviderImpl<?>>(10);
43+
private Map<Class<?>, BeanMetaDataImpl<?>> metadataProviders
44+
= new ConcurrentHashMap<Class<?>, BeanMetaDataImpl<?>>(10);
4545

4646

4747
public ValidatorFactoryImpl(ValidatorFactoryConfiguration configuration) {
@@ -66,13 +66,13 @@ public ValidatorBuilder defineValidatorState() {
6666
return new ValidatorBuilderImpl(this, messageResolver, traversableResolver);
6767
}
6868

69-
public <T> MetaDataProviderImpl<T> getMetadataProvider(Class<T> beanClass) {
69+
public <T> BeanMetaDataImpl<T> getBeanMetaData(Class<T> beanClass) {
7070
//FIXME make sure a optimized mock is provided when no constraints are present.
7171
if (beanClass == null) throw new IllegalArgumentException( "Class cannot be null" );
7272
@SuppressWarnings( "unchecked")
73-
MetaDataProviderImpl<T> metadata = ( MetaDataProviderImpl<T> ) metadataProviders.get(beanClass);
73+
BeanMetaDataImpl<T> metadata = ( BeanMetaDataImpl<T> ) metadataProviders.get(beanClass);
7474
if (metadata == null) {
75-
metadata = new MetaDataProviderImpl<T>(beanClass, constraintFactory);
75+
metadata = new BeanMetaDataImpl<T>(beanClass, constraintFactory);
7676
metadataProviders.put( beanClass, metadata );
7777
}
7878
return metadata;

hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ public static boolean isConstraintAnnotation(Annotation annotation) {
130130
return false;
131131
}
132132

133+
Method[] methods = annotation.getClass().getMethods();
134+
for ( Method m : methods ) {
135+
if ( m.getName().startsWith( "valid" ) ) {
136+
String msg = "Parameters starting with 'valid' are not allowed in a constraint.";
137+
log.warn( msg );
138+
return false;
139+
}
140+
}
141+
133142
return true;
134143
}
135144

@@ -140,7 +149,6 @@ public static boolean isConstraintAnnotation(Annotation annotation) {
140149
*
141150
* @return A list of constraint annotations or the empty list if <code>annotation</code> is not a multi constraint
142151
* annotation.
143-
*
144152
*/
145153
public static <A extends Annotation> List<Annotation> getMultiValueConstraints(A annotation) {
146154
List<Annotation> annotationList = new ArrayList<Annotation>();
@@ -149,8 +157,8 @@ public static <A extends Annotation> List<Annotation> getMultiValueConstraints(A
149157
Class returnType = m.getReturnType();
150158
if ( returnType.isArray() && returnType.getComponentType().isAnnotation() ) {
151159
Annotation[] annotations = ( Annotation[] ) m.invoke( annotation );
152-
for (Annotation a : annotations) {
153-
if( isConstraintAnnotation( a ) || isBuiltInConstraintAnnotation( a )) {
160+
for ( Annotation a : annotations ) {
161+
if ( isConstraintAnnotation( a ) || isBuiltInConstraintAnnotation( a ) ) {
154162
annotationList.add( a );
155163
}
156164
}

0 commit comments

Comments
 (0)