Class ClassUtils

java.lang.Object
imagingbook.common.util.ClassUtils

public abstract class ClassUtils extends Object
Static utility methods related to classes.
Version:
2022/11/20
  • Method Details

    • listFields

      public static String listFields(Object obj)
      Lists all accessible public fields of the given object and returns the result as a string.
      Parameters:
      obj - a (non-null) object
      Returns:
      a string listing the names and values of the object's fields
    • getClassesInPackage

      public static List<Class<?>> getClassesInPackage(Package pkg)
      Collects and returns the list of classes defined in the specified package. The Package may be obtained from an existing Class object (clazz) by clazz.getPackage().
      Parameters:
      pkg - a Package instance, e.g., Package.getPackage("imagingbook.lib.ij.overlay")
      Returns:
      a list of classes contained in the package
    • getClassesInPackage

      public static List<Class<?>> getClassesInPackage(String pkgName)
      Collects and returns the list of classes defined in the specified package. Adapted from https://stackoverflow.com/a/58773271
      Parameters:
      pkgName - the full package name, e.g., "imagingbook.lib.ij.overlay"
      Returns:
      a list of classes contained in the package
    • getEnumConstantsSorted

      public static <E extends Enum<E>> E[] getEnumConstantsSorted(Class<E> enumClass)
      Returns an array of all constants defined by the specified enum class, sorted by their names.
      Type Parameters:
      E - generic type
      Parameters:
      enumClass - the enum class
      Returns:
      an array of enum items sorted by names
    • getComparator

      public static <T extends Comparable> Comparator<T> getComparator(Class<T> clazz)
      Returns the "natural order" Comparator instance for the specified Comparable class. Use Comparator.reversed() to obtain the comparator for sorting in reverse order.
      Type Parameters:
      T - the generic type
      Parameters:
      clazz - a class implementing Comparable
      Returns:
      the associated Comparator instance