View Javadoc
1   package org.apache.commons.beanutils2;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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      // checks
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       * {@inheritDoc}
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       * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
139      */
140     public Method getReadPropertyMethod( String propertyName )
141     {
142         PropertyDescriptor propertyDescriptor = getPropertyDescriptor( propertyName );
143 
144         return checkGetterMethod( propertyDescriptor.getReadMethod(), propertyName );
145     }
146 
147     /**
148      * {@inheritDoc}
149      */
150     public Method getWriteMethod( String name )
151     {
152         PropertyDescriptor propertyDescriptor = getPropertyDescriptor( name );
153 
154         return checkSetterMethod( propertyDescriptor.getWriteMethod(), name );
155     }
156 
157     /**
158      * {@inheritDoc}
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      * {@inheritDoc}
170      */
171     public Method getIndexedReadMethod( String propertyName )
172     {
173         IndexedPropertyDescriptor indexedPropertyDescriptor = getIndexedPropertyDescriptor( propertyName );
174 
175         return checkGetterMethod( indexedPropertyDescriptor.getIndexedReadMethod(), propertyName );
176     }
177 
178     /**
179      * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
221      */
222     public Method getMappedReadMethod( String propertyName )
223     {
224         MappedPropertyDescriptor mappedPropertyDescriptor = getMappedPropertyDescriptor( propertyName );
225 
226         return checkGetterMethod( mappedPropertyDescriptor.getMappedReadMethod(), propertyName );
227     }
228 
229     /**
230      * {@inheritDoc}
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 }