Class ClassUtils

java.lang.Object
org.litebridgedb.commons.ClassUtils

public final class ClassUtils extends Object
Utility class for working with Java reflection.
  • Method Details

    • getAllFields

      public static List<Field> getAllFields(Class<?> type, MethodHandles.Lookup lookup)
      Retrieves all non-static fields from a given class, including fields declared in its superclasses (except for fields the `Object` class). Equivalent to getAllFields(type, false).
      Parameters:
      type - the class from which to retrieve all declared fields
      Returns:
      a list of all fields declared in the given class and its superclasses
    • getAllFields

      public static List<Field> getAllFields(Class<?> type, boolean includeStatic, MethodHandles.Lookup lookup)
      Retrieves all fields from a given class, including fields declared in its superclasses (except for fields of the `Object` class) and (optionally) static fields.
      Parameters:
      type - the class from which to retrieve all declared fields
      Returns:
      a list of all fields declared in the given class and its superclasses
    • getField

      public static Field getField(Class<?> cls, String fieldName)
      Retrieves a specific field from the specified class or its superclasses.

      This method attempts to find a field in the given class by its name. If the field is not found in the current class, it recursively checks the superclasses up to (but not including) the `Object` class.

      Parameters:
      cls - the class to search for the field
      fieldName - the name of the field to retrieve
      Returns:
      the Field object representing the specified field if found
      Throws:
      IllegalArgumentException - if the field cannot be found in the specified class or its superclasses
    • isBasicType

      public static boolean isBasicType(Class<?> type)
      Determines if the provided class type represents a basic type.

      A basic type is defined as one of the following: - A primitive type (e.g., int, double). - An enum. - A type that is a subclass or implementation of CharSequence. - A type that is a subclass of Number. - A type that is a subclass of Boolean. - A byte[] type.

      Parameters:
      type - the Class object to check for being a basic type
      Returns:
      true if the provided class type is considered a basic type, false otherwise
    • getGenericType

      public static Class<?> getGenericType(Field field)
      Retrieves the first generic type argument of a given Field, if the field is parameterized.

      For example, if the field is of type List<String>, this method will return String.class. However, if the field is of type Map<Long, String>, it will return Long.class.

      Parameters:
      field - the field from which to retrieve the first generic type argument
      Returns:
      the Class<?> object representing the first generic type argument of the field
    • getGenericTypes

      public static Class<?>[] getGenericTypes(Field field)
      Retrieves the generic type arguments of a given Field, if the field is parameterized.

      For example, if the field is of type List<String>, this method will return an array containing String.class.

      Parameters:
      field - the field from which to retrieve the generic type arguments
      Returns:
      an array of Class<?> objects representing the generic type arguments of the field
    • getGenericTypes

      public static Class<?>[] getGenericTypes(Type genericType)
      Retrieves the generic type arguments of a given Type, if it is parameterized.

      For example, if the generic type is List<String>, this method will return an array containing String.class.

      Parameters:
      genericType - the type for which to retrieve the generic type arguments
      Returns:
      an array of Class<?> objects representing the generic type arguments
      Throws:
      IllegalArgumentException - if the generic type is not parameterized or if a concrete class cannot be determined for one of the arguments
    • newInstance

      public static <DTO> DTO newInstance(Class<DTO> dtoClass)
      Creates a new instance of the specified class type using its no-argument constructor.

      This method allows instantiating classes even if their constructors are not publicly accessible.

      Type Parameters:
      DTO - the type of the object to be created
      Parameters:
      dtoClass - the class object representing the type to instantiate
      Returns:
      a new instance of the specified class type
      Throws:
      IllegalStateException - if the instantiation fails due to an exception (e.g., no accessible constructor, security restriction)
    • newInstance

      public static <DTO> DTO newInstance(Class<DTO> dtoClass, Constructor<DTO> constructor, Object... args)
    • getConstructors

      public static <DTO> Constructor<DTO>[] getConstructors(Class<DTO> dtoClass)