Class ClassUtils

java.lang.Object
host.anzo.commons.utils.ClassUtils

public class ClassUtils extends Object
Utility class for performing various operations related to Java classes, including singleton instance retrieval, method and field reflection, and dynamic class loading.
Since:
12.09.2013
  • Constructor Details

    • ClassUtils

      public ClassUtils()
  • Method Details

    • singletonInstance

      public static Object singletonInstance(Class<?> clazz)
      Retrieves the singleton instance of the specified class by invoking its static "getInstance" method.
      Parameters:
      clazz - the class to retrieve the singleton instance from
      Returns:
      the singleton instance of the specified class
    • singletonInstanceMethod

      public static Object singletonInstanceMethod(Class<?> clazz, Method method)
      Invokes a specified method on the singleton instance of the given class.
      Parameters:
      clazz - the class containing the singleton instance
      method - the method to invoke on the singleton instance
      Returns:
      the result of the method invocation
    • getMethodsAnnotatedWith

      @NotNull public static @NotNull Collection<Method> getMethodsAnnotatedWith(Class<?> type, Class<? extends Annotation> annotation)
      Retrieves all methods annotated with a specified annotation from the given class and its superclasses and interfaces.
      Parameters:
      type - the class to inspect for annotated methods
      annotation - the annotation to look for
      Returns:
      a collection of methods annotated with the specified annotation
    • lookupClassQuietly

      @Nullable public static @Nullable Class<?> lookupClassQuietly(String name)
      Attempts to load a class by its name quietly, returning null if the class cannot be found.
      Parameters:
      name - the name of the class to load
      Returns:
      the loaded class, or null if not found
    • getFieldValues

      @NotNull public static @NotNull Map<String,Object> getFieldValues(@NotNull @NotNull Class<?> clazz)
      Retrieves the values of all static fields in the specified class.
      Parameters:
      clazz - the class to inspect for field values
      Returns:
      a map of field names to their corresponding values
    • initFieldsFromMap

      public static void initFieldsFromMap(Object object, @NotNull @NotNull Map<String,Object> fieldValues)
      Initializes the fields of a given object using values from a provided map.
      Parameters:
      object - the object to initialize
      fieldValues - a map of field names and their corresponding values
    • getConstructor

      @Nullable public static <T> @Nullable Constructor<T> getConstructor(@NotNull @NotNull Class<T> clazz, Class<?>... parameterTypes)
      Retrieves a constructor of the specified class that matches the given parameter types.
      Type Parameters:
      T - the type of the class
      Parameters:
      clazz - the class to inspect for constructors
      parameterTypes - the parameter types to match
      Returns:
      the matching constructor, or null if not found
    • hasConstructor

      public static boolean hasConstructor(@NotNull @NotNull Class<?> clazz, Class<?>... parameterTypes)
      Checks if the specified class has a constructor that matches the given parameter types.
      Parameters:
      clazz - the class to inspect for constructors
      parameterTypes - the parameter types to match
      Returns:
      true if a matching constructor exists, false otherwise
    • addJars

      @NotNull public static @NotNull ClassLoader addJars(@NotNull @NotNull List<File> jarFiles) throws IOException
      Adds JAR files to the class loader dynamically.
      Parameters:
      jarFiles - a list of JAR files to add
      Returns:
      the updated ClassLoader
      Throws:
      IOException - if an I/O error occurs while adding JARs
    • getEnumValues

      @NotNull public static @NotNull @Unmodifiable List<Enum<?>> getEnumValues(@NotNull @NotNull Class<?> enumClass)
      Retrieves all the constants of the specified enum class as an unmodifiable list.
      Parameters:
      enumClass - the class object of the enum type from which to retrieve the constants
      Returns:
      a list containing all the constants of the specified enum class
      Throws:
      IllegalArgumentException - if the provided class is not an enum