Package org.apache.commons.beanutils2

# Class ConvertUtils

java.lang.Object
org.apache.commons.beanutils2.ConvertUtils

public final class ConvertUtils extends Object

Utility methods for converting String scalar values to objects of the specified Class, String arrays to arrays of the specified Class.

For more details, see `ConvertUtilsBean` which provides the implementations for these methods.

See Also:
  - `ConvertUtilsBean`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static String`
  `convert(Object value)`
  Converts the specified value into a String.
  `static Object`
  `convert(Object value, Class<?> targetType)`
  Converts the value to an object of the specified class (if possible).
  `static Object`
  `convert(String[] values, Class<?> clazz)`
  Convert an array of specified values to an array of objects of the specified class (if possible).
  `static Object`
  `convert(String value, Class<?> clazz)`
  Converts the specified value to an object of the specified class (if possible).
  `static void`
  `deregister()`
  Remove all registered `Converter`s, and re-establish the standard Converters.
  `static void`
  `deregister(Class<?> clazz)`
  Remove any registered `Converter` for the specified destination `Class`.
  `static <T> Converter<T>`
  `lookup(Class<?> sourceType, Class<T> targetType)`
  Look up and return any registered `Converter` for the specified source and destination class; if there is no registered Converter, return `null`.
  `static <T> Converter<T>`
  `lookup(Class<T> clazz)`
  Look up and return any registered `Converter` for the specified destination class; if there is no registered Converter, return `null`.
  `static <T> Class<T>`
  `primitiveToWrapper(Class<T> type)`
  Change primitive Class types to the associated wrapper class.
  `static <T> void`
  `register(Converter<T> converter, Class<T> clazz)`
  Register a custom `Converter` for the specified destination `Class`, replacing any previously registered Converter.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### convert

    public static String convert(Object value)

    Converts the specified value into a String.

    For more details see `ConvertUtilsBean`.

    Parameters:
      `value` - Value to be converted (may be null)
    Returns:
      The converted String value or null if value is null
    See Also:
      - `ConvertUtilsBean.convert(Object)`

  - ### convert

    public static Object convert(Object value, Class<?> targetType)

    Converts the value to an object of the specified class (if possible).

    Parameters:
      `value` - Value to be converted (may be null)
      `targetType` - Class of the value to be converted to (must not be null)
    Returns:
      The converted value
    Throws:
      `ConversionException` - if thrown by an underlying Converter

  - ### convert

    public static Object convert(String value, Class<?> clazz)

    Converts the specified value to an object of the specified class (if possible). Otherwise, return a String representation of the value.

    For more details see `ConvertUtilsBean`.

    Parameters:
      `value` - Value to be converted (may be null)
      `clazz` - Java class to be converted to (must not be null)
    Returns:
      The converted value
    See Also:
      - `ConvertUtilsBean.convert(String, Class)`

  - ### convert

    public static Object convert(String[] values, Class<?> clazz)

    Convert an array of specified values to an array of objects of the specified class (if possible).

    For more details see `ConvertUtilsBean`.

    Parameters:
      `values` - Array of values to be converted
      `clazz` - Java array or element class to be converted to (must not be null)
    Returns:
      The converted value
    See Also:
      - `ConvertUtilsBean.convert(String[], Class)`

  - ### deregister

    public static void deregister()

    Remove all registered `Converter`s, and re-establish the standard Converters.

    For more details see `ConvertUtilsBean`.

    See Also:
      - `ConvertUtilsBean.deregister()`

  - ### deregister

    public static void deregister(Class<?> clazz)

    Remove any registered `Converter` for the specified destination `Class`.

    For more details see `ConvertUtilsBean`.

    Parameters:
      `clazz` - Class for which to remove a registered Converter
    See Also:
      - `ConvertUtilsBean.deregister(Class)`

  - ### lookup

    public static <T> Converter<T> lookup(Class<?> sourceType, Class<T> targetType)
    Look up and return any registered `Converter` for the specified source and destination class; if there is no registered Converter, return `null`.
    Type Parameters:
      `T` - The converter type.
    Parameters:
      `sourceType` - Class of the value being converted
      `targetType` - Class of the value to be converted to
    Returns:
      The registered `Converter` or `null` if not found

  - ### lookup

    public static <T> Converter<T> lookup(Class<T> clazz)

    Look up and return any registered `Converter` for the specified destination class; if there is no registered Converter, return `null`.

    For more details see `ConvertUtilsBean`.

    Type Parameters:
      `T` - The converter type.
    Parameters:
      `clazz` - Class for which to return a registered Converter
    Returns:
      The registered `Converter` or `null` if not found
    See Also:
      - `ConvertUtilsBean.lookup(Class)`

  - ### primitiveToWrapper

    public static <T> Class<T> primitiveToWrapper(Class<T> type)
    Change primitive Class types to the associated wrapper class. This is useful for concrete converter implementations which typically treat primitive types like their corresponding wrapper types.
    Type Parameters:
      `T` - The type to be checked.
    Parameters:
      `type` - The class type to check.
    Returns:
      The converted type.
    Since:
      1.9

  - ### register

    public static <T> void register(Converter<T> converter, Class<T> clazz)

    Register a custom `Converter` for the specified destination `Class`, replacing any previously registered Converter.

    For more details see `ConvertUtilsBean`.

    Type Parameters:
      `T` - The converter type.
    Parameters:
      `converter` - Converter to be registered
      `clazz` - Destination class for conversions performed by this Converter
    See Also:
      - `ConvertUtilsBean.register(Converter, Class)`