1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.beanutils;
18  
19  import java.util.Map;
20  import java.beans.PropertyDescriptor;
21  import java.lang.reflect.InvocationTargetException;
22  
23  /***
24   * A PropertyUtilsBean which customises the behaviour of the
25   * setNestedProperty and getNestedProperty methods to look for
26   * simple properties in preference to map entries.
27   */
28  public class PropsFirstPropertyUtilsBean extends PropertyUtilsBean {
29  
30      public PropsFirstPropertyUtilsBean() {
31          super();
32      }
33      
34      /***
35       * Note: this is a *very rough* override of this method. In particular,
36       * it does not handle MAPPED_DELIM and INDEXED_DELIM chars in the
37       * propertyName, so propertyNames like "a(b)" or "a[3]" will not
38       * be correctly handled.
39       */
40      protected Object getPropertyOfMapBean(Map bean, String propertyName)
41      throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
42          
43          PropertyDescriptor descriptor = getPropertyDescriptor(bean, propertyName);
44          if (descriptor == null) {
45              // no simple property exists so return the value from the map
46              return bean.get(propertyName);
47          } else {
48              // a simple property exists so return its value instead.
49              return getSimpleProperty(bean, propertyName);
50          }
51      }
52  
53      /***
54       * Note: this is a *very rough* override of this method. In particular,
55       * it does not handle MAPPED_DELIM and INDEXED_DELIM chars in the
56       * propertyName, so propertyNames like "a(b)" or "a[3]" will not
57       * be correctly handled.
58       */
59      protected void setPropertyOfMapBean(Map bean, String propertyName, Object value)
60          throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
61          PropertyDescriptor descriptor = getPropertyDescriptor(bean, propertyName);
62          if (descriptor == null) {
63              // no simple property exists so put the value into the map
64              bean.put(propertyName, value);
65          } else {
66              // a simple property exists so set that instead.
67              setSimpleProperty(bean, propertyName, value);
68          }
69      }
70  }