WrapDynaClass.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.lang.ref.Reference;
  20. import java.lang.ref.SoftReference;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.Objects;
  24. import java.util.WeakHashMap;

  25. /**
  26.  * Implements {@link DynaClass} to wrap standard JavaBean instances.
  27.  * <p>
  28.  * This class should not usually need to be used directly to create new {@link WrapDynaBean} instances - it's usually better to call the {@link WrapDynaBean}
  29.  * constructor. For example:
  30.  * </p>
  31.  *
  32.  * <pre>
  33.  *   Object javaBean = ...;
  34.  *   DynaBean wrapper = new WrapDynaBean(javaBean);
  35.  * </pre>
  36.  */
  37. public class WrapDynaClass implements DynaClass {

  38.     /**
  39.      * A class representing the combined key for the cache of {@code WrapDynaClass} instances. A single key consists of a bean class and an instance of
  40.      * {@code PropertyUtilsBean}. Instances are immutable.
  41.      */
  42.     private static final class CacheKey {

  43.         /** The bean class. */
  44.         private final Class<?> beanClass;

  45.         /** The instance of PropertyUtilsBean. */
  46.         private final PropertyUtilsBean propUtils;

  47.         /**
  48.          * Creates a new instance of {@code CacheKey}.
  49.          *
  50.          * @param beanCls the bean class
  51.          * @param pu      the instance of {@code PropertyUtilsBean}
  52.          */
  53.         public CacheKey(final Class<?> beanCls, final PropertyUtilsBean pu) {
  54.             beanClass = beanCls;
  55.             propUtils = pu;
  56.         }

  57.         @Override
  58.         public boolean equals(final Object obj) {
  59.             if (this == obj) {
  60.                 return true;
  61.             }
  62.             if (!(obj instanceof CacheKey)) {
  63.                 return false;
  64.             }

  65.             final CacheKey c = (CacheKey) obj;
  66.             return beanClass.equals(c.beanClass) && propUtils.equals(c.propUtils);
  67.         }

  68.         @Override
  69.         public int hashCode() {
  70.             final int factor = 31;
  71.             int result = 17;
  72.             result = factor * beanClass.hashCode() + result;
  73.             result = factor * propUtils.hashCode() + result;
  74.             return result;
  75.         }
  76.     }

  77.     private static final ContextClassLoaderLocal<Map<CacheKey, WrapDynaClass>> CLASSLOADER_CACHE = new ContextClassLoaderLocal<Map<CacheKey, WrapDynaClass>>() {
  78.         @Override
  79.         protected Map<CacheKey, WrapDynaClass> initialValue() {
  80.             return new WeakHashMap<>();
  81.         }
  82.     };

  83.     /**
  84.      * Clear our cache of WrapDynaClass instances.
  85.      */
  86.     public static void clear() {
  87.         getClassesCache().clear();
  88.     }

  89.     /**
  90.      * Create (if necessary) and return a new {@code WrapDynaClass} instance for the specified bean class.
  91.      *
  92.      * @param beanClass Bean class for which a WrapDynaClass is requested
  93.      * @return A new <em>Wrap</em> {@link DynaClass}
  94.      */
  95.     public static WrapDynaClass createDynaClass(final Class<?> beanClass) {
  96.         return createDynaClass(beanClass, null);
  97.     }

  98.     /**
  99.      * Create (if necessary) and return a new {@code WrapDynaClass} instance for the specified bean class using the given {@code PropertyUtilsBean} instance for
  100.      * introspection. Using this method a specially configured {@code PropertyUtilsBean} instance can be hooked into the introspection mechanism of the managed
  101.      * bean. The argument is optional; if no {@code PropertyUtilsBean} object is provided, the default instance is used.
  102.      *
  103.      * @param beanClass Bean class for which a WrapDynaClass is requested
  104.      * @param pu        the optional {@code PropertyUtilsBean} to be used for introspection
  105.      * @return A new <em>Wrap</em> {@link DynaClass}
  106.      * @since 1.9
  107.      */
  108.     public static WrapDynaClass createDynaClass(final Class<?> beanClass, final PropertyUtilsBean pu) {
  109.         final PropertyUtilsBean propUtils = pu != null ? pu : PropertyUtilsBean.getInstance();
  110.         final CacheKey key = new CacheKey(beanClass, propUtils);
  111.         return getClassesCache().computeIfAbsent(key, k -> new WrapDynaClass(beanClass, propUtils));
  112.     }

  113.     /**
  114.      * Returns the cache for the already created class instances. For each combination of bean class and {@code PropertyUtilsBean} instance an entry is created
  115.      * in the cache.
  116.      *
  117.      * @return the cache for already created {@code WrapDynaClass} instances
  118.      */
  119.     private static Map<CacheKey, WrapDynaClass> getClassesCache() {
  120.         return CLASSLOADER_CACHE.get();
  121.     }

  122.     /**
  123.      * Name of the JavaBean class represented by this WrapDynaClass.
  124.      */
  125.     private final String beanClassName;

  126.     /**
  127.      * Reference to the JavaBean class represented by this WrapDynaClass.
  128.      */
  129.     private final Reference<Class<?>> beanClassRef;

  130.     /** Stores the associated {@code PropertyUtilsBean} instance. */
  131.     private final PropertyUtilsBean propertyUtilsBean;

  132.     /**
  133.      * The set of PropertyDescriptors for this bean class, keyed by the property name. Individual descriptor instances will be the same instances as those in
  134.      * the {@code descriptors} list.
  135.      */
  136.     protected HashMap<String, PropertyDescriptor> descriptorsMap = new HashMap<>();

  137.     /**
  138.      * The set of dynamic properties that are part of this DynaClass.
  139.      */
  140.     protected DynaProperty[] properties;

  141.     /**
  142.      * The set of dynamic properties that are part of this DynaClass, keyed by the property name. Individual descriptor instances will be the same instances as
  143.      * those in the {@code properties} list.
  144.      */
  145.     protected HashMap<String, DynaProperty> propertiesMap = new HashMap<>();

  146.     /**
  147.      * Constructs a new WrapDynaClass for the specified JavaBean class. This constructor is private; WrapDynaClass instances will be created as needed via calls
  148.      * to the {@code createDynaClass(Class)} method.
  149.      *
  150.      * @param beanClass JavaBean class to be introspected around
  151.      * @param propUtils the {@code PropertyUtilsBean} associated with this class
  152.      */
  153.     private WrapDynaClass(final Class<?> beanClass, final PropertyUtilsBean propUtils) {
  154.         this.beanClassRef = new SoftReference<>(beanClass);
  155.         this.beanClassName = beanClass.getName();
  156.         this.propertyUtilsBean = propUtils;
  157.         introspect();
  158.     }

  159.     /**
  160.      * Gets the class of the underlying wrapped bean.
  161.      *
  162.      * @return the class of the underlying wrapped bean
  163.      * @since 1.8.0
  164.      */
  165.     protected Class<?> getBeanClass() {
  166.         return beanClassRef.get();
  167.     }

  168.     /**
  169.      * <p>
  170.      * Return an array of {@code PropertyDescriptor} for the properties currently defined in this DynaClass. If no properties are defined, a zero-length array
  171.      * will be returned.
  172.      * </p>
  173.      *
  174.      * <p>
  175.      * <strong>FIXME</strong> - Should we really be implementing {@code getBeanInfo()} instead, which returns property descriptors and a bunch of other stuff?
  176.      * </p>
  177.      *
  178.      * @return the set of properties for this DynaClass
  179.      */
  180.     @Override
  181.     public DynaProperty[] getDynaProperties() {
  182.         return properties.clone();
  183.     }

  184.     /**
  185.      * Gets a property descriptor for the specified property, if it exists; otherwise, return {@code null}.
  186.      *
  187.      * @param name Name of the dynamic property for which a descriptor is requested
  188.      * @return The descriptor for the specified property
  189.      * @throws IllegalArgumentException if no property name is specified
  190.      */
  191.     @Override
  192.     public DynaProperty getDynaProperty(final String name) {
  193.         return propertiesMap.get(Objects.requireNonNull(name, "name"));
  194.     }

  195.     /**
  196.      * Gets the name of this DynaClass (analogous to the {@code getName()} method of {@link Class}, which allows the same {@code DynaClass} implementation class
  197.      * to support different dynamic classes, with different sets of properties.
  198.      *
  199.      * @return the name of the DynaClass
  200.      */
  201.     @Override
  202.     public String getName() {
  203.         return beanClassName;
  204.     }

  205.     /**
  206.      * Gets the PropertyDescriptor for the specified property name, if any; otherwise return {@code null}.
  207.      *
  208.      * @param name Name of the property to be retrieved
  209.      * @return The descriptor for the specified property
  210.      */
  211.     public PropertyDescriptor getPropertyDescriptor(final String name) {
  212.         return descriptorsMap.get(name);
  213.     }

  214.     /**
  215.      * Returns the {@code PropertyUtilsBean} instance associated with this class. This bean is used for introspection.
  216.      *
  217.      * @return the associated {@code PropertyUtilsBean} instance
  218.      * @since 1.9
  219.      */
  220.     protected PropertyUtilsBean getPropertyUtilsBean() {
  221.         return propertyUtilsBean;
  222.     }

  223.     /**
  224.      * Introspect our bean class to identify the supported properties.
  225.      */
  226.     protected void introspect() {
  227.         // Look up the property descriptors for this bean class
  228.         final Class<?> beanClass = getBeanClass();
  229.         PropertyDescriptor[] regulars = getPropertyUtilsBean().getPropertyDescriptors(beanClass);
  230.         if (regulars == null) {
  231.             regulars = PropertyDescriptors.EMPTY_ARRAY;
  232.         }
  233.         Map<?, ?> mappeds = PropertyUtils.getMappedPropertyDescriptors(beanClass);
  234.         if (mappeds == null) {
  235.             mappeds = new HashMap<>();
  236.         }

  237.         // Construct corresponding DynaProperty information
  238.         properties = new DynaProperty[regulars.length + mappeds.size()];
  239.         for (int i = 0; i < regulars.length; i++) {
  240.             descriptorsMap.put(regulars[i].getName(), regulars[i]);
  241.             properties[i] = new DynaProperty(regulars[i].getName(), regulars[i].getPropertyType());
  242.             propertiesMap.put(properties[i].getName(), properties[i]);
  243.         }

  244.         int j = regulars.length;

  245.         for (final Object value : mappeds.values()) {
  246.             final PropertyDescriptor descriptor = (PropertyDescriptor) value;
  247.             properties[j] = new DynaProperty(descriptor.getName(), Map.class);
  248.             propertiesMap.put(properties[j].getName(), properties[j]);
  249.             j++;
  250.         }
  251.     }

  252.     /**
  253.      * <p>
  254.      * Instantiates a new standard JavaBean instance associated with this DynaClass and return it wrapped in a new WrapDynaBean instance. <strong>NOTE</strong>
  255.      * the JavaBean should have a no argument constructor.
  256.      * </p>
  257.      *
  258.      * <p>
  259.      * <strong>NOTE</strong> - Most common use cases should not need to use this method. It is usually better to create new {@code WrapDynaBean} instances by
  260.      * calling its constructor. For example:
  261.      * </p>
  262.      *
  263.      * <pre>{@code
  264.      *   Object javaBean = ...;
  265.      *   DynaBean wrapper = new WrapDynaBean(javaBean);
  266.      * }</pre>
  267.      * <p>
  268.      * (This method is needed for some kinds of {@code DynaBean} framework.)
  269.      * </p>
  270.      *
  271.      * @return A new {@code DynaBean} instance
  272.      * @throws IllegalAccessException if the Class or the appropriate constructor is not accessible
  273.      * @throws InstantiationException if this Class represents an abstract class, an array class, a primitive type, or void; or if instantiation fails for some
  274.      *                                other reason
  275.      */
  276.     @Override
  277.     public DynaBean newInstance() throws IllegalAccessException, InstantiationException {
  278.         return new WrapDynaBean(getBeanClass().newInstance());
  279.     }
  280. }