01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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,
42: NoSuchMethodException {
43:
44: PropertyDescriptor descriptor = getPropertyDescriptor(bean,
45: propertyName);
46: if (descriptor == null) {
47: // no simple property exists so return the value from the map
48: return bean.get(propertyName);
49: } else {
50: // a simple property exists so return its value instead.
51: return getSimpleProperty(bean, propertyName);
52: }
53: }
54:
55: /**
56: * Note: this is a *very rough* override of this method. In particular,
57: * it does not handle MAPPED_DELIM and INDEXED_DELIM chars in the
58: * propertyName, so propertyNames like "a(b)" or "a[3]" will not
59: * be correctly handled.
60: */
61: protected void setPropertyOfMapBean(Map bean, String propertyName,
62: Object value) throws IllegalAccessException,
63: InvocationTargetException, NoSuchMethodException {
64: PropertyDescriptor descriptor = getPropertyDescriptor(bean,
65: propertyName);
66: if (descriptor == null) {
67: // no simple property exists so put the value into the map
68: bean.put(propertyName, value);
69: } else {
70: // a simple property exists so set that instead.
71: setSimpleProperty(bean, propertyName, value);
72: }
73: }
74: }
|