
ru.progrm_jarvis.javacommons.classloading.ClassLoadingUtil Maven / Gradle / Ivy
package ru.progrm_jarvis.javacommons.classloading;
import lombok.NonNull;
import lombok.experimental.UtilityClass;
import java.util.Optional;
/**
* General purpose utility for {@link Class}-related operations.
*/
@UtilityClass
public class ClassLoadingUtil {
/**
* Attempts to find a class by the given name wrapping it in an {@link Optional}.
*
* @param className name of the class to search for
* @param loadIfNeeded flag indicating whether the class should be loaded if it is available but not loaded yet
* @param classLoader class-loader to use for lookup
* @param type of the class
* @return optional containing the class if it was found or an empty one otherwise
*/
@SuppressWarnings("unchecked")
public Optional> getClass(final @NonNull String className,
final boolean loadIfNeeded,
final @NonNull ClassLoader classLoader) {
try {
return Optional.of((Class extends T>) Class.forName(className, loadIfNeeded, classLoader));
} catch (final ClassNotFoundException e) {
return Optional.empty();
}
}
/**
* Attempts to find a class by the given name using current class-loader wrapping it in an {@link Optional}.
*
* @param className name of the class to search for
* @param loadIfNeeded flag indicating whether the class should be loaded if it is available but not loaded yet
* @param type of the class
* @return optional containing the class if it was found or an empty one otherwise
*/
public Optional> getClass(final @NonNull String className,
final boolean loadIfNeeded) {
return getClass(className, loadIfNeeded, ClassLoadingUtil.class.getClassLoader());
}
/**
* Attempts to find a class by the given name using current class-loader wrapping it in an {@link Optional}. This
* will load the class if needed.
*
* @param className name of the class to search for
* @param type of the class
* @return optional containing the class if it was found or an empty one otherwise
*/
@SuppressWarnings("unchecked")
public Optional> getClass(final @NonNull String className) {
try {
return Optional.of((Class extends T>) Class.forName(className));
} catch (final ClassNotFoundException e) {
return Optional.empty();
}
}
/**
* Attempts to find a class by the given name.
*
* @param className name of the class to search for
* @param loadIfNeeded flag indicating whether the class should be loaded if it is available but not loaded yet
* @param classLoader class-loader to use for lookup
* @param type of the class
* @return the class if it was found or {@code null}
*/
@SuppressWarnings("unchecked")
public Class extends T> getNullableClass(final @NonNull String className,
final boolean loadIfNeeded,
final @NonNull ClassLoader classLoader) {
try {
return (Class extends T>) Class.forName(className, loadIfNeeded, classLoader);
} catch (final ClassNotFoundException e) {
return null;
}
}
/**
* Attempts to find a class by the given name using current class-loader.
*
* @param className name of the class to search for
* @param loadIfNeeded flag indicating whether the class should be loaded if it is available but not loaded yet
* @param type of the class
* @return the class if it was found or {@code null}
*/
public Class extends T> getNullableClass(final @NonNull String className,
final boolean loadIfNeeded) {
return getNullableClass(className, loadIfNeeded, ClassLoadingUtil.class.getClassLoader());
}
/**
* Attempts to find a class by the given name using current class-loader.
* This will load the class if needed.
*
* @param className name of the class to search for
* @param type of the class
* @return the class if it was found or {@code null}
*/
@SuppressWarnings("unchecked")
public Class extends T> getNullableClass(final @NonNull String className) {
try {
return (Class extends T>) Class.forName(className);
} catch (final ClassNotFoundException e) {
return null;
}
}
/**
* Checks if the class is available.
*
* @param className name of the class to search for
* @param loadIfNeeded flag indicating whether the class should be loaded if it is available but not loaded yet
* @param classLoader class-loader to use for lookup
* @return {@code true} if the class is available and {@code false} otherwise
*/
public boolean isClassAvailable(final @NonNull String className,
final boolean loadIfNeeded,
final @NonNull ClassLoader classLoader) {
try {
Class.forName(className, loadIfNeeded, classLoader);
} catch (final ClassNotFoundException e) {
return false;
}
return true;
}
/**
* Checks if the class is available using current class-loader.
*
* @param className name of the class to search for
* @param loadIfNeeded flag indicating whether the class should be loaded if it is available but not loaded yet
* @return {@code true} if the class is available and {@code false} otherwise
*/
public boolean isClassAvailable(final @NonNull String className,
final boolean loadIfNeeded) {
return isClassAvailable(className, loadIfNeeded, ClassLoadingUtil.class.getClassLoader());
}
/**
* Checks if the class is available using current class-loader. This will load the class if needed.
*
* @param className name of the class to search for
* @return {@code true} if the class is available and {@code false} otherwise
*/
public boolean isClassAvailable(final @NonNull String className) {
try {
Class.forName(className);
} catch (final ClassNotFoundException e) {
return false;
}
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy