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 * https://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.IntrospectionException; 020import java.util.Collection; 021import java.util.Collections; 022import java.util.HashSet; 023import java.util.Objects; 024import java.util.Set; 025 026/** 027 * <p> 028 * A specialized {@code BeanIntrospector} implementation which suppresses some properties. 029 * </p> 030 * <p> 031 * 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 032 * properties from the {@link IntrospectionContext}. So effectively, properties added by a different {@code BeanIntrospector} are removed again. 033 * </p> 034 * 035 * @since 1.9.2 036 */ 037public class SuppressPropertiesBeanIntrospector implements BeanIntrospector { 038 039 /** 040 * A specialized instance which is configured to suppress the special {@code class} properties of Java beans. Unintended access to the property 041 * {@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 042 * {@code BeanIntrospector} to an instance of {@code PropertyUtilsBean} suppresses the {@code class} property; it can then no longer be accessed. 043 */ 044 public static final SuppressPropertiesBeanIntrospector SUPPRESS_CLASS = new SuppressPropertiesBeanIntrospector(Collections.singleton("class")); 045 046 /** 047 * A specialized instance which is configured to suppress the special {@code class} properties of Java beans. Unintended access to the call for 048 * {@code declaringClass} (which is common to all Java {@code enum}) can be a security risk because it also allows access to the class loader. Adding this 049 * instance as {@code BeanIntrospector} to an instance of {@code PropertyUtilsBean} suppresses the {@code class} property; it can then no longer be 050 * accessed. 051 * 052 * @since 2.0.0-M2 053 */ 054 public static final SuppressPropertiesBeanIntrospector SUPPRESS_DECLARING_CLASS = new SuppressPropertiesBeanIntrospector( 055 Collections.singleton("declaringClass")); 056 057/** A set with the names of the properties to be suppressed. */ 058 private final Set<String> propertyNames; 059 060 /** 061 * Creates a new instance of {@code SuppressPropertiesBeanIntrospector} and sets the names of the properties to be suppressed. 062 * 063 * @param propertiesToSuppress the names of the properties to be suppressed (must not be <strong>null</strong>) 064 * @throws IllegalArgumentException if the collection with property names is <strong>null</strong> 065 */ 066 public SuppressPropertiesBeanIntrospector(final Collection<String> propertiesToSuppress) { 067 Objects.requireNonNull(propertiesToSuppress, "propertiesToSuppress"); 068 propertyNames = Collections.unmodifiableSet(new HashSet<>(propertiesToSuppress)); 069 } 070 071 /** 072 * Returns a (unmodifiable) set with the names of the properties which are suppressed by this {@code BeanIntrospector}. 073 * 074 * @return a set with the names of the suppressed properties 075 */ 076 public Set<String> getSuppressedProperties() { 077 return propertyNames; 078 } 079 080 /** 081 * {@inheritDoc} This implementation removes all properties from the given context it is configured for. 082 */ 083 @Override 084 public void introspect(final IntrospectionContext icontext) throws IntrospectionException { 085 getSuppressedProperties().forEach(icontext::removePropertyDescriptor); 086 } 087}