Class ClassUtil

java.lang.Object
com.helixframework.reflection.ClassUtil

public final class ClassUtil extends Object
Class reflection utilities.
  • Method Details

    • load

      public static <T> Class<T> load(T instance)
      Loads the class of the given instance.
      Type Parameters:
      T - the type of the instance
      Parameters:
      instance - the instance whose class should be loaded
      Returns:
      the class of the instance
      Throws:
      IllegalArgumentException - if the instance is null
    • createInstance

      public static <T> T createInstance(Class<T> clazz)
      Creates a new instance of the specified class.
      Type Parameters:
      T - the type of the class
      Parameters:
      clazz - the class to create an instance of
      Returns:
      a new instance of the specified class
      Throws:
      IllegalArgumentException - if the class is null
      ReflectionRuntimeException - if an error occurs while creating the instance
    • createInstance

      public static <T> T createInstance(Class<T> clazz, List<Class<?>> parameterTypes, List<Object> parameters)
      Creates a new instance of the specified class using the provided parameter types and parameters.
      Type Parameters:
      T - the type of the class
      Parameters:
      clazz - the class to create an instance of
      parameterTypes - a list of parameter types for the constructor
      parameters - a list of parameter values to be passed to the constructor
      Returns:
      a new instance of the specified class
      Throws:
      IllegalArgumentException - if the class, parameter types, or parameters are null, or if the number of parameter types does not match the number of supplied parameters
      ReflectionRuntimeException - if an error occurs while creating the instance
    • createInstance

      public static <T> T createInstance(String className)
      Creates a new instance of the specified class.
      Parameters:
      className - the fully qualified name of the class to create an instance of
      Returns:
      a new instance of the specified class
      Throws:
      IllegalArgumentException - if the className is null or blank
      ReflectionRuntimeException - if an error occurs while creating the instance
    • createInstance

      public static <T> T createInstance(String className, List<Class<?>> parameterTypes, List<Object> parameters)
      Creates a new instance of the specified class using the provided parameter types and parameters.
      Type Parameters:
      T - the type of the class
      Parameters:
      className - the fully qualified name of the class to create an instance of
      parameterTypes - a list of parameter types for the constructor
      parameters - a list of parameter values to be passed to the constructor
      Returns:
      a new instance of the specified class
      Throws:
      IllegalArgumentException - if the class, parameter types, or parameters are null, or if the number of parameter types does not match the number of supplied parameters
      ReflectionRuntimeException - if an error occurs while creating the instance
    • isAssignable

      public static boolean isAssignable(Class<?> type, Class<?>... classes)
      Determines if the given type is assignable to all the specified classes.
      Parameters:
      type - the type to check
      classes - the classes to check against
      Returns:
      true if the type is assignable to all the classes, false otherwise
      Throws:
      IllegalArgumentException - if the type or classes are null
    • isPrimitive

      public static boolean isPrimitive(Class<?> clazz)
      Determines if the given class is a primitive type.
      Parameters:
      clazz - the class to check
      Returns:
      true if the given class is a primitive type, false otherwise
    • isRecord

      public static boolean isRecord(Object instance)
      Determines if the given object is a record.
      Parameters:
      instance - the object to check
      Returns:
      true if the object is a record, false otherwise
    • isRecord

      public static boolean isRecord(Class<?> clazz)
      Determines if the given class is a record.
      Parameters:
      clazz - the class to check
      Returns:
      true if the class is a record, false otherwise
    • isArraysArrayList

      public static boolean isArraysArrayList(Class<?> clazz)
      Determines if the given class is an instance of Arrays$ArrayList.

      java.util.Arrays$ArrayList is a nested class inside the Arrays class that implements the List interface. This implementation has a fixed length and is backed by an array. The method Arrays.asList returns an instance of this class.

      Parameters:
      clazz - the class to check
      Returns:
      true if the given class is an instance of Arrays$ArrayList, false otherwise
    • getReadableProperties

      public static List<PropertyDescriptor> getReadableProperties(Class<?> instanceType)
      Retrieves a list of readable property descriptors for the given class.
      Parameters:
      instanceType - the class for which to retrieve the readable property descriptors
      Returns:
      a list of readable property descriptors for the given class
    • getReadablePropertiesAsMap

      public static Map<String,PropertyDescriptor> getReadablePropertiesAsMap(Class<?> instanceType)
      Retrieves readable property descriptors for the given class as a map.
      Parameters:
      instanceType - the class for which to retrieve the readable property descriptors
      Returns:
      a map of property names to property descriptors for the given class
    • getReadableProperty

      public static PropertyDescriptor getReadableProperty(String name, Class<?> instanceType)
      Retrieves a readable property descriptor for the given class and property name.
      Parameters:
      name - the name of the property
      instanceType - the class for which to retrieve the property descriptor
      Returns:
      the readable property descriptor for the given class and property name
    • getFieldsUpTo

      public static Iterable<Field> getFieldsUpTo(Class<?> startClass, Class<?> exclusiveParent)
      Retrieves all fields of a class and its superclasses up to an exclusive parent class.
      Parameters:
      startClass - the class to start retrieving fields from
      exclusiveParent - the exclusive parent class, fields from this class will not be included
      Returns:
      an iterable containing all the fields
    • getFieldValue

      public static Object getFieldValue(Object target, String fieldName)
      Retrieves the value of a field from the target object.
      Parameters:
      target - the object from which to retrieve the field value
      fieldName - the name of the field to retrieve
      Returns:
      the value of the field from the target object
      Throws:
      IllegalArgumentException - if the target is null or the field name is null or blank
      ReflectionRuntimeException - if an error occurs while retrieving the field value
    • setFieldValue

      public static void setFieldValue(Object target, String fieldName, Object value)
      Sets the value of a field on the target object.
      Parameters:
      target - the object on which to set the field value
      fieldName - the name of the field to set
      value - the value to set
      Throws:
      IllegalArgumentException - if the target is null or the field name is null or blank
      ReflectionRuntimeException - if an error occurs while setting the field value