Package host.anzo.commons.utils
Class ClassUtils
java.lang.Object
host.anzo.commons.utils.ClassUtils
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic @NotNull ClassLoaderAdds JAR files to the class loader dynamically.static <T> @Nullable Constructor<T> getConstructor(@NotNull Class<T> clazz, Class<?>... parameterTypes) Retrieves a constructor of the specified class that matches the given parameter types.getEnumValues(@NotNull Class<?> enumClass) Retrieves all the constants of the specified enum class as an unmodifiable list.getFieldValues(@NotNull Class<?> clazz) Retrieves the values of all static fields in the specified class.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.static booleanhasConstructor(@NotNull Class<?> clazz, Class<?>... parameterTypes) Checks if the specified class has a constructor that matches the given parameter types.static voidinitFieldsFromMap(Object object, @NotNull Map<String, Object> fieldValues) Initializes the fields of a given object using values from a provided map.static @Nullable Class<?> lookupClassQuietly(String name) Attempts to load a class by its name quietly, returning null if the class cannot be found.static ObjectsingletonInstance(Class<?> clazz) Retrieves the singleton instance of the specified class by invoking its static "getInstance" method.static ObjectsingletonInstanceMethod(Class<?> clazz, Method method) Invokes a specified method on the singleton instance of the given class.
-
Constructor Details
-
ClassUtils
public ClassUtils()
-
-
Method Details
-
singletonInstance
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
Invokes a specified method on the singleton instance of the given class.- Parameters:
clazz- the class containing the singleton instancemethod- 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 methodsannotation- the annotation to look for- Returns:
- a collection of methods annotated with the specified annotation
-
lookupClassQuietly
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
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 initializefieldValues- 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 constructorsparameterTypes- the parameter types to match- Returns:
- the matching constructor, or null if not found
-
hasConstructor
Checks if the specified class has a constructor that matches the given parameter types.- Parameters:
clazz- the class to inspect for constructorsparameterTypes- 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
-