SuppressPropertiesBeanIntrospector.java

  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.beanutils2;

  18. import java.beans.IntrospectionException;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.HashSet;
  22. import java.util.Objects;
  23. import java.util.Set;

  24. /**
  25.  * <p>
  26.  * A specialized {@code BeanIntrospector} implementation which suppresses some properties.
  27.  * </p>
  28.  * <p>
  29.  * An instance of this class is passed a set with the names of the properties it should process. During introspection of a bean class it removes all these
  30.  * properties from the {@link IntrospectionContext}. So effectively, properties added by a different {@code BeanIntrospector} are removed again.
  31.  * </p>
  32.  *
  33.  * @since 1.9.2
  34.  */
  35. public class SuppressPropertiesBeanIntrospector implements BeanIntrospector {
  36.     /**
  37.      * A specialized instance which is configured to suppress the special {@code class} properties of Java beans. Unintended access to the property
  38.      * {@code class} (which is common to all Java objects) can be a security risk because it also allows access to the class loader. Adding this instance as
  39.      * {@code BeanIntrospector} to an instance of {@code PropertyUtilsBean} suppresses the {@code class} property; it can then no longer be accessed.
  40.      */
  41.     public static final SuppressPropertiesBeanIntrospector SUPPRESS_CLASS = new SuppressPropertiesBeanIntrospector(Collections.singleton("class"));

  42.     /** A set with the names of the properties to be suppressed. */
  43.     private final Set<String> propertyNames;

  44.     /**
  45.      * Creates a new instance of {@code SuppressPropertiesBeanIntrospector} and sets the names of the properties to be suppressed.
  46.      *
  47.      * @param propertiesToSuppress the names of the properties to be suppressed (must not be <strong>null</strong>)
  48.      * @throws IllegalArgumentException if the collection with property names is <strong>null</strong>
  49.      */
  50.     public SuppressPropertiesBeanIntrospector(final Collection<String> propertiesToSuppress) {
  51.         Objects.requireNonNull(propertiesToSuppress, "propertiesToSuppress");
  52.         propertyNames = Collections.unmodifiableSet(new HashSet<>(propertiesToSuppress));
  53.     }

  54.     /**
  55.      * Returns a (unmodifiable) set with the names of the properties which are suppressed by this {@code BeanIntrospector}.
  56.      *
  57.      * @return a set with the names of the suppressed properties
  58.      */
  59.     public Set<String> getSuppressedProperties() {
  60.         return propertyNames;
  61.     }

  62.     /**
  63.      * {@inheritDoc} This implementation removes all properties from the given context it is configured for.
  64.      */
  65.     @Override
  66.     public void introspect(final IntrospectionContext icontext) throws IntrospectionException {
  67.         getSuppressedProperties().forEach(icontext::removePropertyDescriptor);
  68.     }
  69. }