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.PropertyDescriptor;
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.Set;
23  
24  /**
25   * <p>
26   * An implementation of the {@code IntrospectionContext} interface used by
27   * {@link PropertyUtilsBean} when doing introspection of a bean class.
28   * </p>
29   * <p>
30   * This class implements the methods required by the
31   * {@code IntrospectionContext} interface in a straight-forward manner
32   * based on a map. It is used internally only. It is not thread-safe.
33   * </p>
34   *
35   * @version $Id$
36   * @since 1.9
37   */
38  class DefaultIntrospectionContext implements IntrospectionContext {
39      /** Constant for an empty array of property descriptors. */
40      private static final PropertyDescriptor[] EMPTY_DESCRIPTORS = new PropertyDescriptor[0];
41  
42      /** The current class for introspection. */
43      private final Class<?> currentClass;
44  
45      /** A map for storing the already added property descriptors. */
46      private final Map<String, PropertyDescriptor> descriptors;
47  
48      /**
49       *
50       * Creates a new instance of <code>DefaultIntrospectionContext</code> and sets
51       * the current class for introspection.
52       *
53       * @param cls the current class
54       */
55      public DefaultIntrospectionContext(final Class<?> cls) {
56          currentClass = cls;
57          descriptors = new HashMap<String, PropertyDescriptor>();
58      }
59  
60      public Class<?> getTargetClass() {
61          return currentClass;
62      }
63  
64      public void addPropertyDescriptor(final PropertyDescriptor desc) {
65          if (desc == null) {
66              throw new IllegalArgumentException(
67                      "Property descriptor must not be null!");
68          }
69          descriptors.put(desc.getName(), desc);
70      }
71  
72      public void addPropertyDescriptors(final PropertyDescriptor[] descs) {
73          if (descs == null) {
74              throw new IllegalArgumentException(
75                      "Array with descriptors must not be null!");
76          }
77  
78          for (PropertyDescriptor desc : descs) {
79              addPropertyDescriptor(desc);
80          }
81      }
82  
83      public boolean hasProperty(final String name) {
84          return descriptors.containsKey(name);
85      }
86  
87      public PropertyDescriptor getPropertyDescriptor(final String name) {
88          return descriptors.get(name);
89      }
90  
91      public void removePropertyDescriptor(final String name) {
92          descriptors.remove(name);
93      }
94  
95      public Set<String> propertyNames() {
96          return descriptors.keySet();
97      }
98  
99      /**
100      * Returns an array with all descriptors added to this context. This method
101      * is used to obtain the results of introspection.
102      *
103      * @return an array with all known property descriptors
104      */
105     public PropertyDescriptor[] getPropertyDescriptors() {
106         return descriptors.values().toArray(EMPTY_DESCRIPTORS);
107     }
108 }