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 * https://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.beanutils2;
18
19 import java.beans.PropertyDescriptor;
20 import java.lang.reflect.InvocationTargetException;
21 import java.util.Map;
22
23 /**
24 * A PropertyUtilsBean which customizes the behavior of the setNestedProperty and getNestedProperty methods to look for simple properties in preference to map
25 * entries.
26 */
27 public class PropsFirstPropertyUtilsBean extends PropertyUtilsBean {
28
29 /**
30 * Note: This is a *very rough* override of this method. In particular, it does not handle MAPPED_DELIM and INDEXED_DELIM chars in the propertyName, so
31 * propertyNames like "a(b)" or "a[3]" will not be correctly handled.
32 */
33 @Override
34 protected Object getPropertyOfMapBean(final Map<?, ?> bean, final String propertyName)
35 throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
36
37 final PropertyDescriptor descriptor = getPropertyDescriptor(bean, propertyName);
38 if (descriptor == null) {
39 // no simple property exists so return the value from the map
40 return bean.get(propertyName);
41 }
42 // a simple property exists so return its value instead.
43 return getSimpleProperty(bean, propertyName);
44 }
45
46 /**
47 * Note: This is a *very rough* override of this method. In particular, it does not handle MAPPED_DELIM and INDEXED_DELIM chars in the propertyName, so
48 * propertyNames like "a(b)" or "a[3]" will not be correctly handled.
49 */
50 @Override
51 protected void setPropertyOfMapBean(final Map<String, Object> bean, final String propertyName, final Object value)
52 throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
53 final PropertyDescriptor descriptor = getPropertyDescriptor(bean, propertyName);
54 if (descriptor == null) {
55 // no simple property exists so put the value into the map
56 bean.put(propertyName, value);
57 } else {
58 // a simple property exists so set that instead.
59 setSimpleProperty(bean, propertyName, value);
60 }
61 }
62 }