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.IntrospectionException;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.HashSet;
23  import java.util.Set;
24  
25  /**
26   * <p>
27   * A specialized {@code BeanIntrospector} implementation which suppresses some properties.
28   * </p>
29   * <p>
30   * An instance of this class is passed a set with the names of the properties it should
31   * process. During introspection of a bean class it removes all these properties from the
32   * {@link IntrospectionContext}. So effectively, properties added by a different
33   * {@code BeanIntrospector} are removed again.
34   * </p>
35   *
36   * @version $Id$
37   * @since 1.9.2
38   */
39  public class SuppressPropertiesBeanIntrospector implements BeanIntrospector {
40      /**
41       * A specialized instance which is configured to suppress the special {@code class}
42       * properties of Java beans. Unintended access to the property {@code class} (which is
43       * common to all Java objects) can be a security risk because it also allows access to
44       * the class loader. Adding this instance as {@code BeanIntrospector} to an instance
45       * of {@code PropertyUtilsBean} suppresses the {@code class} property; it can then no
46       * longer be accessed.
47       */
48      public static final SuppressPropertiesBeanIntrospector SUPPRESS_CLASS =
49              new SuppressPropertiesBeanIntrospector(Collections.singleton("class"));
50  
51      /** A set with the names of the properties to be suppressed. */
52      private final Set<String> propertyNames;
53  
54      /**
55       * Creates a new instance of {@code SuppressPropertiesBeanIntrospector} and sets the
56       * names of the properties to be suppressed.
57       *
58       * @param propertiesToSuppress the names of the properties to be suppressed (must not
59       * be <b>null</b>)
60       * @throws IllegalArgumentException if the collection with property names is
61       * <b>null</b>
62       */
63      public SuppressPropertiesBeanIntrospector(final Collection<String> propertiesToSuppress) {
64          if (propertiesToSuppress == null) {
65              throw new IllegalArgumentException("Property names must not be null!");
66          }
67  
68          propertyNames = Collections.unmodifiableSet(new HashSet<String>(
69                  propertiesToSuppress));
70      }
71  
72      /**
73       * Returns a (unmodifiable) set with the names of the properties which are suppressed
74       * by this {@code BeanIntrospector}.
75       *
76       * @return a set with the names of the suppressed properties
77       */
78      public Set<String> getSuppressedProperties() {
79          return propertyNames;
80      }
81  
82      /**
83       * {@inheritDoc} This implementation removes all properties from the given context it
84       * is configured for.
85       */
86      public void introspect(final IntrospectionContext icontext) throws IntrospectionException {
87          for (final String property : getSuppressedProperties()) {
88              icontext.removePropertyDescriptor(property);
89          }
90      }
91  }