View Javadoc
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.BeanInfo;
20  import java.beans.IndexedPropertyDescriptor;
21  import java.beans.IntrospectionException;
22  import java.beans.Introspector;
23  import java.beans.PropertyDescriptor;
24  import java.lang.reflect.Method;
25  import java.util.List;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   * <p>
32   * The default {@link BeanIntrospector} implementation.
33   * </p>
34   * <p>
35   * This class implements a default bean introspection algorithm based on the JDK
36   * classes in the <code>java.beans</code> package. It discovers properties
37   * conforming to the Java Beans specification.
38   * </p>
39   * <p>
40   * This class is a singleton. The single instance can be obtained using the
41   * {@code INSTANCE} field. It does not define any state and thus can be
42   * shared by arbitrary clients. {@link PropertyUtils} per default uses this
43   * instance as its only {@code BeanIntrospector} object.
44   * </p>
45   *
46   * @version $Id$
47   * @since 1.9
48   */
49  public class DefaultBeanIntrospector implements BeanIntrospector {
50      /** The singleton instance of this class. */
51      public static final BeanIntrospector INSTANCE = new DefaultBeanIntrospector();
52  
53      /** Constant for argument types of a method that expects no arguments. */
54      private static final Class<?>[] EMPTY_CLASS_PARAMETERS = new Class[0];
55  
56      /** Constant for arguments types of a method that expects a list argument. */
57      private static final Class<?>[] LIST_CLASS_PARAMETER = new Class[] { java.util.List.class };
58  
59      /** Log instance */
60      private final Log log = LogFactory.getLog(getClass());
61  
62      /**
63       * Private constructor so that no instances can be created.
64       */
65      private DefaultBeanIntrospector() {
66      }
67  
68      /**
69       * Performs introspection of a specific Java class. This implementation uses
70       * the {@code java.beans.Introspector.getBeanInfo()} method to obtain
71       * all property descriptors for the current class and adds them to the
72       * passed in introspection context.
73       *
74       * @param icontext the introspection context
75       */
76      public void introspect(final IntrospectionContext icontext) {
77          BeanInfo beanInfo = null;
78          try {
79              beanInfo = Introspector.getBeanInfo(icontext.getTargetClass());
80          } catch (final IntrospectionException e) {
81              // no descriptors are added to the context
82              log.error(
83                      "Error when inspecting class " + icontext.getTargetClass(),
84                      e);
85              return;
86          }
87  
88          PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
89          if (descriptors == null) {
90              descriptors = new PropertyDescriptor[0];
91          }
92  
93          handleIndexedPropertyDescriptors(icontext.getTargetClass(),
94                  descriptors);
95          icontext.addPropertyDescriptors(descriptors);
96      }
97  
98      /**
99       * This method fixes an issue where IndexedPropertyDescriptor behaves
100      * differently in different versions of the JDK for 'indexed' properties
101      * which use java.util.List (rather than an array). It implements a
102      * workaround for Bug 28358. If you have a Bean with the following
103      * getters/setters for an indexed property:
104      *
105      * <pre>
106      * public List getFoo()
107      * public Object getFoo(int index)
108      * public void setFoo(List foo)
109      * public void setFoo(int index, Object foo)
110      * </pre>
111      *
112      * then the IndexedPropertyDescriptor's getReadMethod() and getWriteMethod()
113      * behave as follows:
114      * <ul>
115      * <li>JDK 1.3.1_04: returns valid Method objects from these methods.</li>
116      * <li>JDK 1.4.2_05: returns null from these methods.</li>
117      * </ul>
118      *
119      * @param beanClass the current class to be inspected
120      * @param descriptors the array with property descriptors
121      */
122     private void handleIndexedPropertyDescriptors(final Class<?> beanClass,
123             final PropertyDescriptor[] descriptors) {
124         for (final PropertyDescriptor pd : descriptors) {
125             if (pd instanceof IndexedPropertyDescriptor) {
126                 final IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) pd;
127                 final String propName = descriptor.getName().substring(0, 1)
128                         .toUpperCase()
129                         + descriptor.getName().substring(1);
130 
131                 if (descriptor.getReadMethod() == null) {
132                     final String methodName = descriptor.getIndexedReadMethod() != null ? descriptor
133                             .getIndexedReadMethod().getName() : "get"
134                             + propName;
135                     final Method readMethod = MethodUtils
136                             .getMatchingAccessibleMethod(beanClass, methodName,
137                                     EMPTY_CLASS_PARAMETERS);
138                     if (readMethod != null) {
139                         try {
140                             descriptor.setReadMethod(readMethod);
141                         } catch (final Exception e) {
142                             log.error(
143                                     "Error setting indexed property read method",
144                                     e);
145                         }
146                     }
147                 }
148                 if (descriptor.getWriteMethod() == null) {
149                     final String methodName = descriptor.getIndexedWriteMethod() != null ? descriptor
150                             .getIndexedWriteMethod().getName() : "set"
151                             + propName;
152                     Method writeMethod = MethodUtils
153                             .getMatchingAccessibleMethod(beanClass, methodName,
154                                     LIST_CLASS_PARAMETER);
155                     if (writeMethod == null) {
156                         for (final Method m : beanClass.getMethods()) {
157                             if (m.getName().equals(methodName)) {
158                                 final Class<?>[] parameterTypes = m.getParameterTypes();
159                                 if (parameterTypes.length == 1
160                                         && List.class
161                                                 .isAssignableFrom(parameterTypes[0])) {
162                                     writeMethod = m;
163                                     break;
164                                 }
165                             }
166                         }
167                     }
168                     if (writeMethod != null) {
169                         try {
170                             descriptor.setWriteMethod(writeMethod);
171                         } catch (final Exception e) {
172                             log.error(
173                                     "Error setting indexed property write method",
174                                     e);
175                         }
176                     }
177                 }
178             }
179         }
180     }
181 }