BasicDynaClass.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.lang.reflect.Constructor;
  19. import java.lang.reflect.InvocationTargetException;
  20. import java.util.HashMap;
  21. import java.util.Objects;

  22. /**
  23.  * <p>
  24.  * Minimal implementation of the {@code DynaClass} interface. Can be used as a convenience base class for more sophisticated implementations.
  25.  * </p>
  26.  * <p>
  27.  * <strong>IMPLEMENTATION NOTE</strong> - The {@code DynaBean} implementation class supplied to our constructor MUST have a one-argument constructor of its own
  28.  * that accepts a {@code DynaClass}. This is used to associate the DynaBean instance with this DynaClass.
  29.  * </p>
  30.  */
  31. public class BasicDynaClass implements DynaClass {

  32.     private static final long serialVersionUID = 1L;

  33.     /**
  34.      * The method signature of the constructor we will use to create new DynaBean instances.
  35.      */
  36.     private static final Class<?>[] CONSTRUCTOR_TYPES = { DynaClass.class };

  37.     /**
  38.      * The constructor of the {@code dynaBeanClass} that we will use for creating new instances.
  39.      */
  40.     protected transient Constructor<?> constructor;

  41.     /**
  42.      * The argument values to be passed to the constructor we will use to create new DynaBean instances.
  43.      */
  44.     protected Object[] constructorValues = { this };

  45.     /**
  46.      * The {@code DynaBean} implementation class we will use for creating new instances.
  47.      */
  48.     protected Class<?> dynaBeanClass = BasicDynaBean.class;

  49.     /**
  50.      * The "name" of this DynaBean class.
  51.      */
  52.     protected String name = this.getClass().getName();

  53.     /**
  54.      * The set of dynamic properties that are part of this DynaClass.
  55.      */
  56.     protected DynaProperty[] properties = DynaProperty.EMPTY_ARRAY;

  57.     /**
  58.      * 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
  59.      * those in the {@code properties} list.
  60.      */
  61.     protected HashMap<String, DynaProperty> propertiesMap = new HashMap<>();

  62.     /**
  63.      * Constructs a new BasicDynaClass with default parameters.
  64.      */
  65.     public BasicDynaClass() {
  66.         this(null, null, null);
  67.     }

  68.     /**
  69.      * Constructs a new BasicDynaClass with the specified parameters.
  70.      *
  71.      * @param name          Name of this DynaBean class
  72.      * @param dynaBeanClass The implementation class for new instances
  73.      */
  74.     public BasicDynaClass(final String name, final Class<?> dynaBeanClass) {
  75.         this(name, dynaBeanClass, null);
  76.     }

  77.     /**
  78.      * Constructs a new BasicDynaClass with the specified parameters.
  79.      *
  80.      * @param name          Name of this DynaBean class
  81.      * @param dynaBeanClass The implementation class for new instances
  82.      * @param properties    Property descriptors for the supported properties
  83.      */
  84.     public BasicDynaClass(final String name, Class<?> dynaBeanClass, final DynaProperty[] properties) {
  85.         if (name != null) {
  86.             this.name = name;
  87.         }
  88.         if (dynaBeanClass == null) {
  89.             dynaBeanClass = BasicDynaBean.class;
  90.         }
  91.         setDynaBeanClass(dynaBeanClass);
  92.         if (properties != null) {
  93.             setProperties(properties);
  94.         }
  95.     }

  96.     /**
  97.      * Gets the Class object we will use to create new instances in the {@code newInstance()} method. This Class <strong>MUST</strong> implement the
  98.      * {@code DynaBean} interface.
  99.      *
  100.      * @return The class of the {@link DynaBean}
  101.      */
  102.     public Class<?> getDynaBeanClass() {
  103.         return this.dynaBeanClass;
  104.     }

  105.     /**
  106.      * <p>
  107.      * Return an array of {@code PropertyDescriptor} for the properties currently defined in this DynaClass. If no properties are defined, a zero-length array
  108.      * will be returned.
  109.      * </p>
  110.      *
  111.      * <p>
  112.      * <strong>FIXME</strong> - Should we really be implementing {@code getBeanInfo()} instead, which returns property descriptors and a bunch of other stuff?
  113.      * </p>
  114.      *
  115.      * @return the set of properties for this DynaClass
  116.      */
  117.     @Override
  118.     public DynaProperty[] getDynaProperties() {
  119.         return properties.clone();
  120.     }

  121.     /**
  122.      * Gets a property descriptor for the specified property, if it exists; otherwise, return {@code null}.
  123.      *
  124.      * @param name Name of the dynamic property for which a descriptor is requested
  125.      * @return The descriptor for the specified property
  126.      * @throws IllegalArgumentException if no property name is specified
  127.      */
  128.     @Override
  129.     public DynaProperty getDynaProperty(final String name) {
  130.         return propertiesMap.get(Objects.requireNonNull(name, "name"));
  131.     }

  132.     /**
  133.      * Gets the name of this DynaClass (analogous to the {@code getName()} method of {@link Class}, which allows the same {@code DynaClass} implementation class
  134.      * to support different dynamic classes, with different sets of properties.
  135.      *
  136.      * @return the name of the DynaClass
  137.      */
  138.     @Override
  139.     public String getName() {
  140.         return this.name;
  141.     }

  142.     /**
  143.      * Instantiate and return a new DynaBean instance, associated with this DynaClass.
  144.      *
  145.      * @return A new {@code DynaBean} instance
  146.      * @throws IllegalAccessException if the Class or the appropriate constructor is not accessible
  147.      * @throws InstantiationException if this Class represents an abstract class, an array class, a primitive type, or void; or if instantiation fails for some
  148.      *                                other reason
  149.      */
  150.     @Override
  151.     public DynaBean newInstance() throws IllegalAccessException, InstantiationException {
  152.         try {
  153.             // Refind the constructor after a deserialization (if needed)
  154.             if (constructor == null) {
  155.                 setDynaBeanClass(this.dynaBeanClass);
  156.             }
  157.             // Invoke the constructor to create a new bean instance
  158.             return (DynaBean) constructor.newInstance(constructorValues);
  159.         } catch (final InvocationTargetException e) {
  160.             throw new InstantiationException(e.getTargetException().getMessage());
  161.         }
  162.     }

  163.     /**
  164.      * Sets the Class object we will use to create new instances in the {@code newInstance()} method. This Class <strong>MUST</strong> implement the
  165.      * {@code DynaBean} interface.
  166.      *
  167.      * @param dynaBeanClass The new Class object
  168.      * @throws IllegalArgumentException if the specified Class does not implement the {@code DynaBean} interface
  169.      */
  170.     protected void setDynaBeanClass(final Class<?> dynaBeanClass) {
  171.         // Validate the argument type specified
  172.         if (dynaBeanClass.isInterface()) {
  173.             throw new IllegalArgumentException("Class " + dynaBeanClass.getName() + " is an interface, not a class");
  174.         }
  175.         if (!DynaBean.class.isAssignableFrom(dynaBeanClass)) {
  176.             throw new IllegalArgumentException("Class " + dynaBeanClass.getName() + " does not implement DynaBean");
  177.         }

  178.         // Identify the Constructor we will use in newInstance()
  179.         try {
  180.             this.constructor = dynaBeanClass.getConstructor(CONSTRUCTOR_TYPES);
  181.         } catch (final NoSuchMethodException e) {
  182.             throw new IllegalArgumentException("Class " + dynaBeanClass.getName() + " does not have an appropriate constructor");
  183.         }
  184.         this.dynaBeanClass = dynaBeanClass;
  185.     }

  186.     /**
  187.      * Sets the list of dynamic properties supported by this DynaClass.
  188.      *
  189.      * @param properties List of dynamic properties to be supported
  190.      */
  191.     protected void setProperties(final DynaProperty[] properties) {
  192.         this.properties = Objects.requireNonNull(properties, "properties");
  193.         propertiesMap.clear();
  194.         for (final DynaProperty property : properties) {
  195.             propertiesMap.put(property.getName(), property);
  196.         }
  197.     }

  198. }