1 /*
2  * Hibernate Validator, declare and validate application constraints
3  *
4  * License: Apache License, Version 2.0
5  * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
6  */

7 package org.hibernate.validator.internal.metadata.descriptor;
8
9 import static org.hibernate.validator.internal.util.CollectionHelper.newHashSet;
10 import static org.hibernate.validator.internal.util.Contracts.assertNotNull;
11 import static org.hibernate.validator.internal.util.logging.Messages.MESSAGES;
12
13 import java.lang.reflect.Type;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17
18 import javax.validation.metadata.BeanDescriptor;
19 import javax.validation.metadata.ConstructorDescriptor;
20 import javax.validation.metadata.MethodDescriptor;
21 import javax.validation.metadata.MethodType;
22 import javax.validation.metadata.PropertyDescriptor;
23
24 import org.hibernate.validator.internal.properties.Signature;
25 import org.hibernate.validator.internal.util.CollectionHelper;
26 import org.hibernate.validator.internal.util.Contracts;
27 import org.hibernate.validator.internal.util.ExecutableHelper;
28 import org.hibernate.validator.internal.util.stereotypes.Immutable;
29
30 /**
31  * Describes a validated bean.
32  *
33  * @author Emmanuel Bernard
34  * @author Hardy Ferentschik
35  * @author Gunnar Morling
36  */

37 public class BeanDescriptorImpl extends ElementDescriptorImpl implements BeanDescriptor {
38     @Immutable
39     private final Map<String, PropertyDescriptor> constrainedProperties;
40     @Immutable
41     private final Map<Signature, ExecutableDescriptorImpl> constrainedMethods;
42     @Immutable
43     private final Map<Signature, ConstructorDescriptor> constrainedConstructors;
44
45     public BeanDescriptorImpl(Type beanClass,
46                               Set<ConstraintDescriptorImpl<?>> classLevelConstraints,
47                               Map<String, PropertyDescriptor> constrainedProperties,
48                               Map<Signature, ExecutableDescriptorImpl> constrainedMethods,
49                               Map<Signature, ConstructorDescriptor> constrainedConstructors,
50                               boolean defaultGroupSequenceRedefined,
51                               List<Class<?>> defaultGroupSequence) {
52         super( beanClass, classLevelConstraints, defaultGroupSequenceRedefined, defaultGroupSequence );
53
54         this.constrainedProperties = CollectionHelper.toImmutableMap( constrainedProperties );
55         this.constrainedMethods = CollectionHelper.toImmutableMap( constrainedMethods );
56         this.constrainedConstructors = CollectionHelper.toImmutableMap( constrainedConstructors );
57     }
58
59     @Override
60     public final boolean isBeanConstrained() {
61         return hasConstraints() || !constrainedProperties.isEmpty();
62     }
63
64     @Override
65     public final PropertyDescriptor getConstraintsForProperty(String propertyName) {
66         assertNotNull( propertyName, "The property name cannot be null" );
67         return constrainedProperties.get( propertyName );
68     }
69
70     @Override
71     public final Set<PropertyDescriptor> getConstrainedProperties() {
72         return newHashSet( constrainedProperties.values() );
73     }
74
75     @Override
76     public ConstructorDescriptor getConstraintsForConstructor(Class<?>... parameterTypes) {
77         return constrainedConstructors.get( ExecutableHelper.getSignature( getElementClass().getSimpleName(), parameterTypes ) );
78     }
79
80     @Override
81     public Set<ConstructorDescriptor> getConstrainedConstructors() {
82         return newHashSet( constrainedConstructors.values() );
83     }
84
85     @Override
86     public Set<MethodDescriptor> getConstrainedMethods(MethodType methodType, MethodType... methodTypes) {
87         boolean includeGetters = MethodType.GETTER.equals( methodType );
88         boolean includeNonGetters = MethodType.NON_GETTER.equals( methodType );
89         if ( methodTypes != null ) {
90             for ( MethodType type : methodTypes ) {
91                 if ( MethodType.GETTER.equals( type ) ) {
92                     includeGetters = true;
93                 }
94                 if ( MethodType.NON_GETTER.equals( type ) ) {
95                     includeNonGetters = true;
96                 }
97             }
98         }
99
100         Set<MethodDescriptor> matchingMethodDescriptors = newHashSet();
101         for ( ExecutableDescriptorImpl constrainedMethod : constrainedMethods.values() ) {
102             boolean addToSet = false;
103             if ( ( constrainedMethod.isGetter() && includeGetters ) || ( !constrainedMethod.isGetter() && includeNonGetters ) ) {
104                 addToSet = true;
105             }
106
107             if ( addToSet ) {
108                 matchingMethodDescriptors.add( constrainedMethod );
109             }
110         }
111
112         return matchingMethodDescriptors;
113     }
114
115     @Override
116     public MethodDescriptor getConstraintsForMethod(String methodName, Class<?>... parameterTypes) {
117         Contracts.assertNotNull( methodName, MESSAGES.methodNameMustNotBeNull() );
118         return constrainedMethods.get( ExecutableHelper.getSignature( methodName, parameterTypes ) );
119     }
120
121     @Override
122     public String toString() {
123         final StringBuilder sb = new StringBuilder();
124         sb.append( "BeanDescriptorImpl" );
125         sb.append( "{class='" );
126         sb.append( getElementClass().getSimpleName() );
127         sb.append( "'}" );
128         return sb.toString();
129     }
130 }
131