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.beans.PropertyDescriptor; 20 import java.lang.reflect.InvocationTargetException; 21 import java.util.Map; 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 * @version $Id$ 29 */ 30 public class PropsFirstPropertyUtilsBean extends PropertyUtilsBean { 31 32 public PropsFirstPropertyUtilsBean() { 33 super(); 34 } 35 36 /** 37 * Note: this is a *very rough* override of this method. In particular, 38 * it does not handle MAPPED_DELIM and INDEXED_DELIM chars in the 39 * propertyName, so propertyNames like "a(b)" or "a[3]" will not 40 * be correctly handled. 41 */ 42 @Override 43 protected Object getPropertyOfMapBean(final Map<?, ?> bean, final String propertyName) 44 throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { 45 46 final PropertyDescriptor descriptor = getPropertyDescriptor(bean, propertyName); 47 if (descriptor == null) { 48 // no simple property exists so return the value from the map 49 return bean.get(propertyName); 50 } else { 51 // a simple property exists so return its value instead. 52 return getSimpleProperty(bean, propertyName); 53 } 54 } 55 56 /** 57 * Note: this is a *very rough* override of this method. In particular, 58 * it does not handle MAPPED_DELIM and INDEXED_DELIM chars in the 59 * propertyName, so propertyNames like "a(b)" or "a[3]" will not 60 * be correctly handled. 61 */ 62 @Override 63 protected void setPropertyOfMapBean(final Map<String, Object> bean, final String propertyName, final Object value) 64 throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { 65 final PropertyDescriptor descriptor = getPropertyDescriptor(bean, 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 }