001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.beanutils2;
018
019import java.beans.BeanInfo;
020import java.beans.IndexedPropertyDescriptor;
021import java.beans.IntrospectionException;
022import java.beans.Introspector;
023import java.beans.PropertyDescriptor;
024import java.lang.reflect.Method;
025import java.util.List;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029
030/**
031 * <p>
032 * The default {@link BeanIntrospector} implementation.
033 * </p>
034 * <p>
035 * This class implements a default bean introspection algorithm based on the JDK classes in the {@link java.beans} package. It discovers properties conforming
036 * to the Java Beans specification.
037 * </p>
038 * <p>
039 * This class is a singleton. The single instance can be obtained using the {@code INSTANCE} field. It does not define any state and thus can be shared by
040 * arbitrary clients. {@link PropertyUtils} per default uses this instance as its only {@code BeanIntrospector} object.
041 * </p>
042 *
043 * @since 1.9
044 */
045public class DefaultBeanIntrospector implements BeanIntrospector {
046
047    /** The singleton instance of this class. */
048    public static final BeanIntrospector INSTANCE = new DefaultBeanIntrospector();
049
050    /** Constant for arguments types of a method that expects a list argument. */
051    private static final Class<?>[] LIST_CLASS_PARAMETER = new Class[] { java.util.List.class };
052
053    /** For logging. Each subclass gets its own log instance. */
054    private final Log log = LogFactory.getLog(getClass());
055
056    /**
057     * Private constructor so that no instances can be created.
058     */
059    private DefaultBeanIntrospector() {
060    }
061
062    /**
063     * This method fixes an issue where IndexedPropertyDescriptor behaves differently in different versions of the JDK for 'indexed' properties which use
064     * java.util.List (rather than an array). It implements a workaround for Bug 28358. If you have a Bean with the following getters/setters for an indexed
065     * property:
066     *
067     * <pre>
068     * public List getFoo()
069     * public Object getFoo(int index)
070     * public void setFoo(List foo)
071     * public void setFoo(int index, Object foo)
072     * </pre>
073     *
074     * then the IndexedPropertyDescriptor's getReadMethod() and getWriteMethod() behave as follows:
075     * <ul>
076     * <li>JDK 1.3.1_04: returns valid Method objects from these methods.</li>
077     * <li>JDK 1.4.2_05: returns null from these methods.</li>
078     * </ul>
079     *
080     * @param beanClass   the current class to be inspected
081     * @param descriptors the array with property descriptors
082     */
083    private void handleIndexedPropertyDescriptors(final Class<?> beanClass, final PropertyDescriptor[] descriptors) {
084        for (final PropertyDescriptor pd : descriptors) {
085            if (pd instanceof IndexedPropertyDescriptor) {
086                final IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) pd;
087                final String propName = descriptor.getName().substring(0, 1).toUpperCase() + descriptor.getName().substring(1);
088
089                if (descriptor.getReadMethod() == null) {
090                    final String methodName = descriptor.getIndexedReadMethod() != null ? descriptor.getIndexedReadMethod().getName() : "get" + propName;
091                    final Method readMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName, BeanUtils.EMPTY_CLASS_ARRAY);
092                    if (readMethod != null) {
093                        try {
094                            descriptor.setReadMethod(readMethod);
095                        } catch (final Exception e) {
096                            log.error("Error setting indexed property read method", e);
097                        }
098                    }
099                }
100                if (descriptor.getWriteMethod() == null) {
101                    final String methodName = descriptor.getIndexedWriteMethod() != null ? descriptor.getIndexedWriteMethod().getName() : "set" + propName;
102                    Method writeMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName, LIST_CLASS_PARAMETER);
103                    if (writeMethod == null) {
104                        for (final Method m : beanClass.getMethods()) {
105                            if (m.getName().equals(methodName)) {
106                                final Class<?>[] parameterTypes = m.getParameterTypes();
107                                if (parameterTypes.length == 1 && List.class.isAssignableFrom(parameterTypes[0])) {
108                                    writeMethod = m;
109                                    break;
110                                }
111                            }
112                        }
113                    }
114                    if (writeMethod != null) {
115                        try {
116                            descriptor.setWriteMethod(writeMethod);
117                        } catch (final Exception e) {
118                            log.error("Error setting indexed property write method", e);
119                        }
120                    }
121                }
122            }
123        }
124    }
125
126    /**
127     * Performs introspection of a specific Java class. This implementation uses the {@code java.beans.Introspector.getBeanInfo()} method to obtain all property
128     * descriptors for the current class and adds them to the passed in introspection context.
129     *
130     * @param icontext the introspection context
131     */
132    @Override
133    public void introspect(final IntrospectionContext icontext) {
134        BeanInfo beanInfo = null;
135        try {
136            beanInfo = Introspector.getBeanInfo(icontext.getTargetClass());
137        } catch (final IntrospectionException e) {
138            // no descriptors are added to the context
139            log.error("Error when inspecting class " + icontext.getTargetClass(), e);
140            return;
141        }
142
143        PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
144        if (descriptors == null) {
145            descriptors = PropertyDescriptors.EMPTY_ARRAY;
146        }
147
148        handleIndexedPropertyDescriptors(icontext.getTargetClass(), descriptors);
149        icontext.addPropertyDescriptors(descriptors);
150    }
151}