AbstractConverter.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.converters;

  18. import java.lang.reflect.Array;
  19. import java.util.Collection;
  20. import java.util.Locale;
  21. import java.util.Objects;

  22. import org.apache.commons.beanutils2.ConversionException;
  23. import org.apache.commons.beanutils2.ConvertUtils;
  24. import org.apache.commons.beanutils2.Converter;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;

  27. /**
  28.  * Base {@link Converter} implementation that provides the structure for handling conversion <strong>to</strong> and <strong>from</strong> a specified type.
  29.  * <p>
  30.  * This implementation provides the basic structure for converting to/from a specified type optionally using a default value or throwing a
  31.  * {@link ConversionException} if a conversion error occurs.
  32.  * </p>
  33.  * <p>
  34.  * Implementations should provide conversion to the specified type and from the specified type to a {@code String} value by implementing the following methods:
  35.  * </p>
  36.  * <ul>
  37.  * <li>{@code convertToString(value)} - convert to a String (default implementation uses the objects {@code toString()} method).</li>
  38.  * <li>{@code convertToType(Class, value)} - convert to the specified type</li>
  39.  * </ul>
  40.  * <p>
  41.  * The default value has to be compliant to the default type of this converter - which is enforced by the generic type parameter. If a conversion is not
  42.  * possible and a default value is set, the converter tries to transform the default value to the requested target type. If this fails, a
  43.  * {@code ConversionException} if thrown.
  44.  * </p>
  45.  *
  46.  * @param <D> The default value type.
  47.  * @since 1.8.0
  48.  */
  49. public abstract class AbstractConverter<D> implements Converter<D> {

  50.     /** Debug logging message to indicate default value configuration */
  51.     private static final String DEFAULT_CONFIG_MSG = "(N.B. Converters can be configured to use default values to avoid throwing exceptions)";

  52.     /** Current package name */
  53.     // getPackage() below returns null on some platforms/jvm versions during the unit tests.
  54.     // private static final String PACKAGE = AbstractConverter.class.getPackage().getName() + ".";
  55.     private static final String PACKAGE = "org.apache.commons.beanutils2.converters.";

  56.     /**
  57.      * Converts the given object to a lower-case string.
  58.      *
  59.      * @param value the input string.
  60.      * @return the given string trimmed and converter to lower-case.
  61.      */
  62.     protected static String toLowerCase(final Object value) {
  63.         return toString(value).toLowerCase(Locale.ROOT);
  64.     }

  65.     /**
  66.      * Converts the given object to a lower-case string.
  67.      *
  68.      * @param value the input string.
  69.      * @return the given string trimmed and converter to lower-case.
  70.      */
  71.     protected static String toString(final Object value) {
  72.         return Objects.requireNonNull(value, "value").toString();
  73.     }

  74.     /**
  75.      * Converts the given object to a lower-case string.
  76.      *
  77.      * @param value the input string.
  78.      * @return the given string trimmed and converter to lower-case.
  79.      */
  80.     protected static String toTrim(final Object value) {
  81.         return toString(value).trim();
  82.     }

  83.     /**
  84.      * Logging for this instance.
  85.      */
  86.     private transient Log log;

  87.     /**
  88.      * Should we return the default value on conversion errors?
  89.      */
  90.     private boolean useDefault;

  91.     /**
  92.      * The default value specified to our Constructor, if any.
  93.      */
  94.     private D defaultValue;

  95.     /**
  96.      * Constructs a <em>Converter</em> that throws a {@code ConversionException} if an error occurs.
  97.      */
  98.     public AbstractConverter() {
  99.     }

  100.     /**
  101.      * Constructs a <em>Converter</em> that returns a default value if an error occurs.
  102.      *
  103.      * @param defaultValue The default value to be returned if the value to be converted is missing or an error occurs converting the value.
  104.      */
  105.     public AbstractConverter(final D defaultValue) {
  106.         setDefaultValue(defaultValue);
  107.     }

  108.     /**
  109.      * Creates a standard conversion exception with a message indicating that the passed in value cannot be converted to the desired target type.
  110.      *
  111.      * @param type  the target type
  112.      * @param value the value to be converted
  113.      * @return a {@code ConversionException} with a standard message
  114.      * @since 1.9
  115.      */
  116.     protected ConversionException conversionException(final Class<?> type, final Object value) {
  117.         return ConversionException.format("Can't convert value '%s' to type %s", value, type);
  118.     }

  119.     /**
  120.      * Converts the input object into an output object of the specified type.
  121.      *
  122.      * @param type  Data type to which this value should be converted
  123.      * @param value The input value to be converted
  124.      * @return The converted value.
  125.      * @throws ConversionException if conversion cannot be performed successfully and no default is specified.
  126.      */
  127.     @Override
  128.     public <R> R convert(final Class<R> type, Object value) {
  129.         if (type == null) {
  130.             return convertToDefaultType(value);
  131.         }

  132.         Class<?> sourceType = value == null ? null : value.getClass();
  133.         final Class<R> targetType = ConvertUtils.primitiveToWrapper(type);

  134.         if (log().isDebugEnabled()) {
  135.             log().debug(
  136.                     "Converting" + (value == null ? "" : " '" + toString(sourceType) + "'") + " value '" + value + "' to type '" + toString(targetType) + "'");
  137.         }

  138.         value = convertArray(value);

  139.         // Missing Value
  140.         if (value == null) {
  141.             return handleMissing(targetType);
  142.         }

  143.         sourceType = value.getClass();

  144.         try {
  145.             // Convert --> String
  146.             if (targetType.equals(String.class)) {
  147.                 return targetType.cast(convertToString(value));

  148.                 // No conversion necessary
  149.             }
  150.             if (targetType.equals(sourceType)) {
  151.                 if (log().isDebugEnabled()) {
  152.                     log().debug("    No conversion required, value is already a " + toString(targetType));
  153.                 }
  154.                 return targetType.cast(value);

  155.                 // Convert --> Type
  156.             }
  157.             final Object result = convertToType(targetType, value);
  158.             if (log().isDebugEnabled()) {
  159.                 log().debug("    Converted to " + toString(targetType) + " value '" + result + "'");
  160.             }
  161.             return targetType.cast(result);
  162.         } catch (final Throwable t) {
  163.             return handleError(targetType, value, t);
  164.         }
  165.     }

  166.     /**
  167.      * Returns the first element from an Array (or Collection) or the value unchanged if not an Array (or Collection).
  168.      *
  169.      * N.B. This needs to be overridden for array/Collection converters.
  170.      *
  171.      * @param value The value to convert
  172.      * @return The first element in an Array (or Collection) or the value unchanged if not an Array (or Collection)
  173.      */
  174.     protected Object convertArray(final Object value) {
  175.         if (value == null) {
  176.             return null;
  177.         }
  178.         if (value.getClass().isArray()) {
  179.             if (Array.getLength(value) > 0) {
  180.                 return Array.get(value, 0);
  181.             }
  182.             return null;
  183.         }
  184.         if (value instanceof Collection) {
  185.             final Collection<?> collection = (Collection<?>) value;
  186.             if (!collection.isEmpty()) {
  187.                 return collection.iterator().next();
  188.             }
  189.             return null;
  190.         }
  191.         return value;
  192.     }

  193.     /**
  194.      * Converts to the default type. This method is called if we do not have a target class. In this case, the T parameter is not set. Therefore, we can cast to
  195.      * it (which is required to fulfill the contract of the method signature).
  196.      *
  197.      * @param value the value to be converted
  198.      * @param <T>   the type of the result object
  199.      * @return the converted value
  200.      */
  201.     @SuppressWarnings("unchecked")
  202.     private <T> T convertToDefaultType(final Object value) {
  203.         return (T) convert(getDefaultType(), value);
  204.     }

  205.     /**
  206.      * Converts the input object into a String.
  207.      * <p>
  208.      * <strong>N.B.</strong>This implementation simply uses the value's {@code toString()} method and should be overridden if a more sophisticated mechanism for
  209.      * <em>conversion to a String</em> is required.
  210.      * </p>
  211.      *
  212.      * @param value The input value to be converted.
  213.      * @return the converted String value.
  214.      * @throws IllegalArgumentException if an error occurs converting to a String
  215.      */
  216.     protected String convertToString(final Object value) {
  217.         return value.toString();
  218.     }

  219.     /**
  220.      * Converts the input object into an output object of the specified type.
  221.      * <p>
  222.      * Typical implementations will provide a minimum of {@code String --&gt; type} conversion.
  223.      * </p>
  224.      *
  225.      * @param <R>   Target type of the conversion.
  226.      * @param type  Data type to which this value should be converted.
  227.      * @param value The input value to be converted.
  228.      * @return The converted value.
  229.      * @throws Throwable if an error occurs converting to the specified type
  230.      */
  231.     protected abstract <R> R convertToType(Class<R> type, Object value) throws Throwable;

  232.     /**
  233.      * Gets the default value for conversions to the specified type.
  234.      *
  235.      * @param type Data type to which this value should be converted.
  236.      * @return The default value for the specified type.
  237.      */
  238.     protected Object getDefault(final Class<?> type) {
  239.         if (type.equals(String.class)) {
  240.             return null;
  241.         }
  242.         return defaultValue;
  243.     }

  244.     /**
  245.      * Gets the default type this {@code Converter} handles.
  246.      *
  247.      * @return The default type this {@code Converter} handles.
  248.      */
  249.     protected abstract Class<D> getDefaultType();

  250.     /**
  251.      * Handles Conversion Errors.
  252.      * <p>
  253.      * If a default value has been specified then it is returned otherwise a ConversionException is thrown.
  254.      * </p>
  255.      *
  256.      * @param <T>   Target type of the conversion.
  257.      * @param type  Data type to which this value should be converted.
  258.      * @param value The input value to be converted
  259.      * @param cause The exception thrown by the {@code convert} method
  260.      * @return The default value.
  261.      * @throws ConversionException if no default value has been specified for this {@link Converter}.
  262.      */
  263.     protected <T> T handleError(final Class<T> type, final Object value, final Throwable cause) {
  264.         if (log().isDebugEnabled()) {
  265.             if (cause instanceof ConversionException) {
  266.                 log().debug("    Conversion threw ConversionException: " + cause.getMessage());
  267.             } else {
  268.                 log().debug("    Conversion threw " + cause);
  269.             }
  270.         }
  271.         if (useDefault) {
  272.             return handleMissing(type);
  273.         }
  274.         ConversionException cex = null;
  275.         if (cause instanceof ConversionException) {
  276.             cex = (ConversionException) cause;
  277.             if (log().isDebugEnabled()) {
  278.                 log().debug("    Re-throwing ConversionException: " + cex.getMessage());
  279.                 log().debug("    " + DEFAULT_CONFIG_MSG);
  280.             }
  281.         } else {
  282.             final String msg = "Error converting from '" + toString(value.getClass()) + "' to '" + toString(type) + "' " + cause.getMessage();
  283.             cex = new ConversionException(msg, cause);
  284.             if (log().isDebugEnabled()) {
  285.                 log().debug("    Throwing ConversionException: " + msg);
  286.                 log().debug("    " + DEFAULT_CONFIG_MSG);
  287.             }
  288.         }
  289.         throw cex;
  290.     }

  291.     /**
  292.      * Handles missing values.
  293.      * <p>
  294.      * If a default value has been specified, then it is returned (after a cast to the desired target class); otherwise a ConversionException is thrown.
  295.      * </p>
  296.      *
  297.      * @param <T>  the desired target type
  298.      * @param type Data type to which this value should be converted.
  299.      * @return The default value.
  300.      * @throws ConversionException if no default value has been specified for this {@link Converter}.
  301.      */
  302.     protected <T> T handleMissing(final Class<T> type) {
  303.         if (useDefault || type.equals(String.class)) {
  304.             Object value = getDefault(type);
  305.             if (useDefault && value != null && !type.equals(value.getClass())) {
  306.                 try {
  307.                     value = convertToType(type, defaultValue);
  308.                 } catch (final Throwable t) {
  309.                     throw new ConversionException("Default conversion to " + toString(type) + " failed.", t);
  310.                 }
  311.             }
  312.             if (log().isDebugEnabled()) {
  313.                 log().debug("    Using default " + (value == null ? "" : toString(value.getClass()) + " ") + "value '" + defaultValue + "'");
  314.             }
  315.             // value is now either null or of the desired target type
  316.             return type.cast(value);
  317.         }

  318.         final ConversionException cex = ConversionException.format("No value specified for '%s'", toString(type));
  319.         if (log().isDebugEnabled()) {
  320.             log().debug("    Throwing ConversionException: " + cex.getMessage());
  321.             log().debug("    " + DEFAULT_CONFIG_MSG);
  322.         }
  323.         throw cex;
  324.     }

  325.     /**
  326.      * Tests whether a default value will be returned or exception thrown in the event of a conversion error.
  327.      *
  328.      * @return {@code true} if a default value will be returned for conversion errors or {@code false} if a {@link ConversionException} will be thrown.
  329.      */
  330.     public boolean isUseDefault() {
  331.         return useDefault;
  332.     }

  333.     /**
  334.      * Gets the Log instance.
  335.      * <p>
  336.      * The Log instance variable is transient and accessing it through this method ensures it is re-initialized when this instance is de-serialized.
  337.      * </p>
  338.      *
  339.      * @return The Log instance.
  340.      */
  341.     Log log() {
  342.         if (log == null) {
  343.             log = LogFactory.getLog(getClass());
  344.         }
  345.         return log;
  346.     }

  347.     /**
  348.      * Sets the default value, converting as required.
  349.      * <p>
  350.      * If the default value is different from the type the {@code Converter} handles, it will be converted to the handled type.
  351.      * </p>
  352.      *
  353.      * @param defaultValue The default value to be returned if the value to be converted is missing or an error occurs converting the value.
  354.      * @throws ConversionException if an error occurs converting the default value
  355.      */
  356.     protected void setDefaultValue(final D defaultValue) {
  357.         useDefault = false;
  358.         if (log().isDebugEnabled()) {
  359.             log().debug("Setting default value: " + defaultValue);
  360.         }
  361.         if (defaultValue == null) {
  362.             this.defaultValue = null;
  363.         } else {
  364.             this.defaultValue = convert(getDefaultType(), defaultValue);
  365.         }
  366.         useDefault = true;
  367.     }

  368.     /**
  369.      * Converts this instance to a String.
  370.      *
  371.      * @return A String representation of this converter
  372.      */
  373.     @Override
  374.     public String toString() {
  375.         return toString(getClass()) + "[UseDefault=" + useDefault + "]";
  376.     }

  377.     /**
  378.      * Converts a {@link Class} to a String.
  379.      *
  380.      * @param type The {@link Class}.
  381.      * @return The String representation.
  382.      */
  383.     String toString(final Class<?> type) {
  384.         String typeName = null;
  385.         if (type == null) {
  386.             typeName = "null";
  387.         } else if (type.isArray()) {
  388.             Class<?> elementType = type.getComponentType();
  389.             int count = 1;
  390.             while (elementType.isArray()) {
  391.                 elementType = elementType.getComponentType();
  392.                 count++;
  393.             }
  394.             final StringBuilder typeNameBuilder = new StringBuilder(elementType.getName());
  395.             for (int i = 0; i < count; i++) {
  396.                 typeNameBuilder.append("[]");
  397.             }
  398.             typeName = typeNameBuilder.toString();
  399.         } else {
  400.             typeName = type.getName();
  401.         }
  402.         if (typeName.startsWith("java.lang.") || typeName.startsWith("java.util.") || typeName.startsWith("java.math.")) {
  403.             typeName = typeName.substring("java.lang.".length());
  404.         } else if (typeName.startsWith(PACKAGE)) {
  405.             typeName = typeName.substring(PACKAGE.length());
  406.         }
  407.         return typeName;
  408.     }

  409. }