1 package org.apache.commons.beanutils2;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.apache.commons.beanutils2.Assertions.checkArgument;
23 import static org.apache.commons.beanutils2.Assertions.checkNotNull;
24
25 import java.beans.IndexedPropertyDescriptor;
26 import java.beans.IntrospectionException;
27 import java.beans.PropertyDescriptor;
28 import java.lang.reflect.Method;
29 import java.util.Map;
30
31 final class DefaultBeanProperties<B>
32 implements BeanProperties<B>
33 {
34
35 private final PropertyDescriptorsRegistry registry = PropertyDescriptorsRegistry.getInstance();
36
37 private final Class<B> beanClass;
38
39 DefaultBeanProperties( Class<B> beanClass )
40 {
41 this.beanClass = beanClass;
42 }
43
44
45
46 public boolean hasProperty( String propertyName )
47 {
48 checkNotNull( propertyName, "Parameter 'propertyName' must not be null!" );
49 try
50 {
51 getPropertyDescriptor( propertyName );
52 }
53 catch ( NoSuchPropertyException e )
54 {
55 return false;
56 }
57 return true;
58 }
59
60
61
62
63 public boolean isReadable( String propertyName )
64 {
65 checkNotNull( propertyName, "Parameter 'propertyName' must not be null!" );
66 PropertyDescriptor propertyDescriptor = getPropertyDescriptor( propertyName );
67
68 Method getter;
69 if ( propertyDescriptor instanceof IndexedPropertyDescriptor )
70 {
71 IndexedPropertyDescriptor indexed = (IndexedPropertyDescriptor) propertyDescriptor;
72 getter = indexed.getIndexedReadMethod();
73 }
74 else if ( propertyDescriptor instanceof MappedPropertyDescriptor )
75 {
76 MappedPropertyDescriptor mapped = (MappedPropertyDescriptor) propertyDescriptor;
77 getter = mapped.getMappedReadMethod();
78 }
79 else
80 {
81 getter = propertyDescriptor.getReadMethod();
82 }
83
84 return getter != null ? true : false;
85 }
86
87
88
89
90 public boolean isWritable( String propertyName )
91 {
92 checkNotNull( propertyName, "Parameter 'propertyName' must not be null!" );
93 PropertyDescriptor propertyDescriptor = getPropertyDescriptor( propertyName );
94
95 Method setter;
96 if ( propertyDescriptor instanceof IndexedPropertyDescriptor )
97 {
98 IndexedPropertyDescriptor indexed = (IndexedPropertyDescriptor) propertyDescriptor;
99 setter = indexed.getIndexedWriteMethod();
100 }
101 else if ( propertyDescriptor instanceof MappedPropertyDescriptor )
102 {
103 MappedPropertyDescriptor mapped = (MappedPropertyDescriptor) propertyDescriptor;
104 setter = mapped.getMappedWriteMethod();
105 }
106 else
107 {
108 setter = propertyDescriptor.getWriteMethod();
109 }
110
111 return setter != null ? true : false;
112 }
113
114
115
116
117 public PropertyDescriptor getPropertyDescriptor( String propertyName )
118 {
119 PropertyDescriptor propertyDescriptor = null;
120 try
121 {
122 propertyDescriptor = registry.getPropertyDescriptor( beanClass, propertyName );
123 }
124 catch ( IntrospectionException ex )
125 {
126 throw new NoSuchPropertyException( propertyName, beanClass, ex );
127 }
128
129 if ( propertyDescriptor == null )
130 {
131 throw new NoSuchPropertyException( propertyName, beanClass, null );
132 }
133
134 return propertyDescriptor;
135 }
136
137
138
139
140 public Method getReadPropertyMethod( String propertyName )
141 {
142 PropertyDescriptor propertyDescriptor = getPropertyDescriptor( propertyName );
143
144 return checkGetterMethod( propertyDescriptor.getReadMethod(), propertyName );
145 }
146
147
148
149
150 public Method getWriteMethod( String name )
151 {
152 PropertyDescriptor propertyDescriptor = getPropertyDescriptor( name );
153
154 return checkSetterMethod( propertyDescriptor.getWriteMethod(), name );
155 }
156
157
158
159
160 public IndexedPropertyDescriptor getIndexedPropertyDescriptor( String propertyName )
161 {
162 PropertyDescriptor propertyDescriptor = getPropertyDescriptor( propertyName );
163 checkArgument( propertyDescriptor instanceof IndexedPropertyDescriptor,
164 "Property '%s' in bean of type %s is not an indexed property", propertyName, beanClass.getName() );
165 return (IndexedPropertyDescriptor) propertyDescriptor;
166 }
167
168
169
170
171 public Method getIndexedReadMethod( String propertyName )
172 {
173 IndexedPropertyDescriptor indexedPropertyDescriptor = getIndexedPropertyDescriptor( propertyName );
174
175 return checkGetterMethod( indexedPropertyDescriptor.getIndexedReadMethod(), propertyName );
176 }
177
178
179
180
181 public Method getIndexedWriteMethod( String propertyName )
182 {
183 IndexedPropertyDescriptor indexedPropertyDescriptor = getIndexedPropertyDescriptor( propertyName );
184
185 return checkSetterMethod( indexedPropertyDescriptor.getIndexedWriteMethod(), propertyName );
186 }
187
188 private Method checkSetterMethod( Method method, String propertyName )
189 {
190 if ( method == null )
191 {
192 throw new PropertyNotWritableException( propertyName, beanClass, null );
193 }
194
195 return method;
196 }
197
198 private Method checkGetterMethod( Method method, String propertyName )
199 {
200 if ( method == null )
201 {
202 throw new PropertyNotReadableException( propertyName, beanClass, null );
203 }
204
205 return method;
206 }
207
208
209
210
211 public MappedPropertyDescriptor getMappedPropertyDescriptor( String propertyName )
212 {
213 PropertyDescriptor propertyDescriptor = getPropertyDescriptor( propertyName );
214 checkArgument( propertyDescriptor instanceof MappedPropertyDescriptor,
215 "Property '%s' in bean of type %s is not a mapped property", propertyName, beanClass.getName() );
216 return (MappedPropertyDescriptor) propertyDescriptor;
217 }
218
219
220
221
222 public Method getMappedReadMethod( String propertyName )
223 {
224 MappedPropertyDescriptor mappedPropertyDescriptor = getMappedPropertyDescriptor( propertyName );
225
226 return checkGetterMethod( mappedPropertyDescriptor.getMappedReadMethod(), propertyName );
227 }
228
229
230
231
232 public Map<String, PropertyDescriptor> getPropertiesIndex()
233 {
234 try
235 {
236 return registry.getPropertiesIndex( beanClass );
237 }
238 catch ( IntrospectionException e )
239 {
240 throw new BeanReflectionException( e, beanClass );
241 }
242 }
243
244 public Method getMappedWriteMethod( String propertyName )
245 {
246 MappedPropertyDescriptor mappedPropertyDescriptor = getMappedPropertyDescriptor( propertyName );
247
248 return checkSetterMethod( mappedPropertyDescriptor.getMappedWriteMethod(), propertyName );
249 }
250
251 }