Class BeanCopyUtil

java.lang.Object
com.ultikits.ultitools.utils.BeanCopyUtil

public final class BeanCopyUtil extends Object
Bean copy utility class.

Replaces hutool BeanUtil.copyProperties().

Since:
6.2.0
Author:
wisdomme
  • Field Details

    • PRIMITIVE_TO_WRAPPER

      private static final Map<Class<?>,Class<?>> PRIMITIVE_TO_WRAPPER
      Primitive type to its wrapper class; see boxed(Class).
  • Constructor Details

    • BeanCopyUtil

      private BeanCopyUtil()
  • Method Details

    • copyProperties

      public static void copyProperties(Object source, Object target)
      Copies properties.
      Parameters:
      source - the source object
      target - the target object
    • copyProperties

      public static void copyProperties(Object source, Object target, boolean ignoreNullValue)
      Copies properties.
      Parameters:
      source - the source object
      target - the target object
      ignoreNullValue - whether to ignore null values
    • copyProperties

      public static void copyProperties(Object source, Object target, String... ignoreFields)
      Copies properties, excluding the specified fields.
      Parameters:
      source - the source object
      target - the target object
      ignoreFields - the field names to exclude
    • copyProperties

      public static void copyProperties(Object source, Object target, boolean ignoreNullValue, String... ignoreFields)
      Copies properties.
      Parameters:
      source - the source object
      target - the target object
      ignoreNullValue - whether to ignore null values
      ignoreFields - the field names to exclude
    • shouldSkipField

      private static boolean shouldSkipField(Field field, Set<String> ignoreSet)
    • isStaticOrFinal

      private static boolean isStaticOrFinal(Field field)
    • findWritableTargetField

      private static Field findWritableTargetField(Class<?> targetClass, String fieldName)
    • copyFieldValue

      private static void copyFieldValue(Object source, Object target, Field sourceField, Field targetField, boolean ignoreNullValue)
    • copyToNewInstance

      public static <T> T copyToNewInstance(Object source, Class<T> targetClass)
      Copies to a new instance.
      Type Parameters:
      T - the target type
      Parameters:
      source - the source object
      targetClass - the target class
      Returns:
      the new instance
    • convertValue

      private static Object convertValue(Object value, Class<?> targetType)
      Converts a basic type.
    • boxed

      private static Class<?> boxed(Class<?> type)
      Returns the wrapper class of a primitive type, or the type itself otherwise.

      Field.get(java.lang.Object) always boxes, so a boolean field reads as a Boolean and boolean.class.isAssignableFrom(Boolean.class) is false. Deciding assignability against the primitive type sent every boolean and char value to convertValue(java.lang.Object, java.lang.Class<?>), which had no branch for either and dropped the write (#520). The numeric primitives only survived because convertNumber(java.lang.Number, java.lang.Class<?>) happens to cover them.

    • convertNumber

      private static Object convertNumber(Number num, Class<?> targetType)