DefaultIntrospectionContext.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.PropertyDescriptor;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import java.util.Objects;
  22. import java.util.Set;

  23. /**
  24.  * <p>
  25.  * An implementation of the {@code IntrospectionContext} interface used by {@link PropertyUtilsBean} when doing introspection of a bean class.
  26.  * </p>
  27.  * <p>
  28.  * This class implements the methods required by the {@code IntrospectionContext} interface in a straight-forward manner based on a map. It is used internally
  29.  * only. It is not thread-safe.
  30.  * </p>
  31.  *
  32.  * @since 1.9
  33.  */
  34. final class DefaultIntrospectionContext implements IntrospectionContext {
  35.     /** The current class for introspection. */
  36.     private final Class<?> currentClass;

  37.     /** A map for storing the already added property descriptors. */
  38.     private final Map<String, PropertyDescriptor> descriptors;

  39.     /**
  40.      *
  41.      * Creates a new instance of {@code DefaultIntrospectionContext} and sets the current class for introspection.
  42.      *
  43.      * @param cls the current class
  44.      */
  45.     public DefaultIntrospectionContext(final Class<?> cls) {
  46.         currentClass = cls;
  47.         descriptors = new HashMap<>();
  48.     }

  49.     @Override
  50.     public void addPropertyDescriptor(final PropertyDescriptor desc) {
  51.         Objects.requireNonNull(desc, "desc");
  52.         descriptors.put(desc.getName(), desc);
  53.     }

  54.     @Override
  55.     public void addPropertyDescriptors(final PropertyDescriptor[] descs) {
  56.         Objects.requireNonNull(descs, "descs");
  57.         for (final PropertyDescriptor desc : descs) {
  58.             addPropertyDescriptor(desc);
  59.         }
  60.     }

  61.     @Override
  62.     public PropertyDescriptor getPropertyDescriptor(final String name) {
  63.         return descriptors.get(name);
  64.     }

  65.     /**
  66.      * Returns an array with all descriptors added to this context. This method is used to obtain the results of introspection.
  67.      *
  68.      * @return an array with all known property descriptors
  69.      */
  70.     public PropertyDescriptor[] getPropertyDescriptors() {
  71.         return descriptors.values().toArray(PropertyDescriptors.EMPTY_ARRAY);
  72.     }

  73.     @Override
  74.     public Class<?> getTargetClass() {
  75.         return currentClass;
  76.     }

  77.     @Override
  78.     public boolean hasProperty(final String name) {
  79.         return descriptors.containsKey(name);
  80.     }

  81.     @Override
  82.     public Set<String> propertyNames() {
  83.         return descriptors.keySet();
  84.     }

  85.     @Override
  86.     public void removePropertyDescriptor(final String name) {
  87.         descriptors.remove(name);
  88.     }
  89. }