Class ClassUtils

java.lang.Object
org.apache.empire.commons.ClassUtils

public final class ClassUtils extends Object
  • Field Details

    • EMPTY_ARGS

      public static final Object[] EMPTY_ARGS
  • Method Details

    • testSerialization

      public static <T> T testSerialization(Class<T> clazz, T objToSerialize)
      Used to test Serialization
      Type Parameters:
      T - the class type
      Parameters:
      clazz - class to serialize
      objToSerialize - objedt to serialize
      Returns:
      the unserializedObject
    • isImmutableClass

      public static boolean isImmutableClass(Class<?> clazz)
      Checks if a class is an immutable class such as a wrapper class of a primitive type String and Class are also considered to be a immutable classes
      Parameters:
      clazz - the class to check
      Returns:
      true if the class is immutable
    • copy

      public static boolean[] copy(boolean[] other)
      Copies a boolean array
      Parameters:
      other - array to copy
      Returns:
      the copy
    • copy

      public static Object[] copy(Object[] other)
      Copies an Object array
      Parameters:
      other - array to copy
      Returns:
      the copy
    • copy

      public static String[] copy(String[] other)
      Copies a String array
      Parameters:
      other - array to copy
      Returns:
      the copy
    • copy

      public static <T> T copy(T obj)
      Makes a copy of an object if possible or returns the object itself if copy is not supported
      Type Parameters:
      T - the class type
      Parameters:
      obj - the object to copy
      Returns:
      either a copy of the object or the object itself if copy is not supported
    • copy

      public static <T> T copy(T obj, int flags)
      Makes a copy of an object if possible or returns null or self (depending on flags)
      Type Parameters:
      T - the class type
      Parameters:
      obj - the object to copy
      flags - options for the copy
      Returns:
      either a copy of the object or null the object itself
    • copySmart

      public static <T> T copySmart(T obj)
      Makes a copy of an object if the object is an array or a collection (List,Set,Map,...) Otherwise the object itself is returned
      Type Parameters:
      T - the class type
      Parameters:
      obj - the object to copy
      Returns:
      either a copy of the object or the object itself if copy is not supported
    • getFieldValue

      public static Object getFieldValue(Class<?> clazz, Object object, String property, boolean includePrivateFields)
      Retrieve a field value using reflection
      Parameters:
      clazz - the class from which to obtain the field
      object - the object instance from which to obtain the field
      property - the property to obtain
      includePrivateFields - flag whether or not to include private fields
      Returns:
      the property value
    • getFieldValue

      public static Object getFieldValue(Object object, String property)
      Retrieve a field value using reflection The field accessor must be public
      Parameters:
      object - the object instance from which to obtain the field
      property - the property to obtain
      Returns:
      the property value
    • getPrivateFieldValue

      public static Object getPrivateFieldValue(Object object, String property)
      Retrieve a field value using reflection
      Parameters:
      object - the object instance from which to obtain the field
      property - the property to obatin
      Returns:
      the property value
    • setPrivateFieldValue

      public static void setPrivateFieldValue(Class<?> clazz, Object object, String property, Object value)
      Modifies a private field value using reflection
      Parameters:
      clazz - the class of the object
      object - the object or null if static fields are to be changed
      property - the field name
      value - the field value
    • setPrivateFieldValue

      public static void setPrivateFieldValue(Object object, String property, Object value)
      Modifies a private field value using reflection
      Parameters:
      object - the object or null if static fields are to be changed
      property - the field name
      value - the field value
    • newInstance

      public static <T> T newInstance(Class<T> typeClass)
      Creates a new Object instance
      Type Parameters:
      T - the class type
      Parameters:
      typeClass - the class of the object to instantiate
      Returns:
      the instance
    • newInstance

      public static <T> T newInstance(Class<T> typeClass, Class<?> paramClass, Object paramValue)
      Creates a new Object instance with a single parameter constructor
      Type Parameters:
      T - the type of the new object
      Parameters:
      typeClass - the type of the object to instantiate
      paramClass - the type of the constructor parameter
      paramValue - the value of the constructor parameter
      Returns:
      the instance
    • newInstance

      public static <T> T newInstance(Constructor<T> typeConstructor, Object... params)
      Creates a new Object instance
      Type Parameters:
      T - the type of the new object
      Parameters:
      typeConstructor - the constructor of the object to instantiate
      params - the constructor params
      Returns:
      the instance
    • findMatchingConstructor

      public static <T> Constructor<T> findMatchingConstructor(Class<T> clazz, int minParams, Class<?>... parameterTypes)
      copied from org.apache.commons.beanutils.ConstructorUtils since it's private there
      Type Parameters:
      T - the class type
      Parameters:
      clazz - the class of the object
      minParams - minimum number of params
      parameterTypes - the param types
      Returns:
      the constructor or null
    • invokeMethod

      public static Object invokeMethod(Class<?> clazz, Object object, String methodName, Class<?>[] paramTypes, Object[] paramValues, boolean makeAccessible)
      Invoke a method on an object using reflection
      Parameters:
      clazz - the class from which to obtain the field
      object - the object instance on which to invoke the method
      methodName - the name of the method to invoke
      paramTypes - the list of parameter types
      paramValues - the list of parameter values
      makeAccessible - flag whether to make private methods accessible
      Returns:
      the return value of the method
    • invokeMethod

      public static Object invokeMethod(Object object, String methodName, Class<?> paramType, Object paramValue)
      Invokes a method with one parameter on an object using reflection
      Parameters:
      object - the object instance on which to invoke the method
      methodName - the name of the method to invoke
      paramType - the method parameter type
      paramValue - the method parameter value
      Returns:
      the return value of the method
    • invokeSimpleMethod

      public static Object invokeSimpleMethod(Class<?> clazz, Object object, String methodName, boolean makeAccessible)
      Invoke a simple method (without parameters) on an object using reflection
      Parameters:
      clazz - the class on which to invoke the method
      object - the object instance on which to invoke the method
      methodName - the name of the method to invoke
      makeAccessible - flag whether or not to make the method accessible (eg. if private)
      Returns:
      the return value of the method
    • invokeSimpleMethod

      public static Object invokeSimpleMethod(Object object, String methodName)
      Invoke a simple method (without parameters) on an object using reflection
      Parameters:
      object - the object instance on which to invoke the method
      methodName - the name of the method to invoke
      Returns:
      the return value of the method
    • invokeSimplePrivateMethod

      public static Object invokeSimplePrivateMethod(Object object, String methodName)
      Invoke a simple method (without parameters) on an object using reflection
      Parameters:
      object - the object instance on which to invoke the method
      methodName - the name of the method to invoke
      Returns:
      the return value of the method
    • invokeSimpleMethod

      public static Object invokeSimpleMethod(Object object, Method method)
      Invoke a simple method (without parameters) on an object
      Parameters:
      object - the object on which to invoke the method
      method - the method
      Returns:
      the return value
    • invokeMethod

      public static Object invokeMethod(Object object, Method method, Object... params)
      Invoke a method with parameters on an object
      Parameters:
      object - the object on which to invoke the method
      method - the method
      params - the method params
      Returns:
      the return value
    • getImplemenationJarName

      public static String getImplemenationJarName(Class<?> clazz)
      Returns the JAR name that contains the implementation of a specific class
      Parameters:
      clazz - the class
      Returns:
      the JAR File Name
    • getImplemenationJarName

      public static String getImplemenationJarName(String className)
      Returns the JAR name that contains the implementation of a specific class
      Parameters:
      className - the class Name of the class
      Returns:
      the JAR File Name or "[ClassNotFoundException]" if the class is not available