All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.commons.beanutils2.SuppressPropertiesBeanIntrospector Maven / Gradle / Ivy

Go to download

Apache Commons BeanUtils provides an easy-to-use but flexible wrapper around reflection and introspection.

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.beanutils2;

import java.beans.IntrospectionException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * 

* A specialized {@code BeanIntrospector} implementation which suppresses some properties. *

*

* 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 * properties from the {@link IntrospectionContext}. So effectively, properties added by a different {@code BeanIntrospector} are removed again. *

* * @since 1.9.2 */ public class SuppressPropertiesBeanIntrospector implements BeanIntrospector { /** * A specialized instance which is configured to suppress the special {@code class} properties of Java beans. Unintended access to the property * {@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 * {@code BeanIntrospector} to an instance of {@code PropertyUtilsBean} suppresses the {@code class} property; it can then no longer be accessed. */ public static final SuppressPropertiesBeanIntrospector SUPPRESS_CLASS = new SuppressPropertiesBeanIntrospector(Collections.singleton("class")); /** * A specialized instance which is configured to suppress the special {@code class} properties of Java beans. Unintended access to the call for * {@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 * instance as {@code BeanIntrospector} to an instance of {@code PropertyUtilsBean} suppresses the {@code class} property; it can then no longer be * accessed. * * @since 2.0.0-M2 */ public static final SuppressPropertiesBeanIntrospector SUPPRESS_DECLARING_CLASS = new SuppressPropertiesBeanIntrospector( Collections.singleton("declaringClass")); /** A set with the names of the properties to be suppressed. */ private final Set propertyNames; /** * Creates a new instance of {@code SuppressPropertiesBeanIntrospector} and sets the names of the properties to be suppressed. * * @param propertiesToSuppress the names of the properties to be suppressed (must not be null) * @throws IllegalArgumentException if the collection with property names is null */ public SuppressPropertiesBeanIntrospector(final Collection propertiesToSuppress) { Objects.requireNonNull(propertiesToSuppress, "propertiesToSuppress"); propertyNames = Collections.unmodifiableSet(new HashSet<>(propertiesToSuppress)); } /** * Returns a (unmodifiable) set with the names of the properties which are suppressed by this {@code BeanIntrospector}. * * @return a set with the names of the suppressed properties */ public Set getSuppressedProperties() { return propertyNames; } /** * {@inheritDoc} This implementation removes all properties from the given context it is configured for. */ @Override public void introspect(final IntrospectionContext icontext) throws IntrospectionException { getSuppressedProperties().forEach(icontext::removePropertyDescriptor); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy