Class RuntimeClassScanner
java.lang.Object
org.daiitech.naftah.utils.reflect.RuntimeClassScanner
Utility class to scan and load classes at runtime from the classpath, directories, and JAR files.
Provides methods to find class names and load classes using different class loaders.
This class is not meant to be instantiated.
- Author:
- Chakib Daii
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String[]Array of common base package names to be used when scanning classes.static final StringThe file extension for compiled Java class files.static final StringRegular expression pattern to match the class file extension.class.static final ClassLoader[]Array of ClassLoaders used when attempting to load classes.static final StringThe Java class path obtained from the system propertyjava.class.path.static final StringSystem property key for the Java class path.A set of class file base names to ignore during scanning.A set of full class file names to ignore during scanning.static final StringThe file extension for Java archive files (JAR).static final StringThe Java home directory obtained from the system propertyjava.home.static final StringSystem property key for the Java installation directory (JAVA_HOME).static final StringThe file extension for Java module files (JMOD).static final String[]Array of filesystem paths (directories or archives) used as scan roots.static final booleanFlag indicating whether to scan JDK classes for Naftah types, obtained from the system propertynaftah.scanJDK. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor to prevent instantiation. -
Method Summary
Modifier and TypeMethodDescriptionstatic Map<String,ClassLoader> findClassesInDirectory(File root, File dir, String packagePath) Recursively scans for classes inside a directory.static Map<String,ClassLoader> findClassesInJar(File jarFile, String packagePath) Scans for classes inside a JAR or JMOD file.static String[]Returns classpath paths while excluding ignored JARs defined in the "original-dependencies" file.static String[]Filters out paths that match any of the ignored JARs.static FilejarEntryToTempFile(JarFile outerJar, JarEntry innerJarEntry) Extracts a nested JAR entry from a JAR file and writes it to a temporary file.loadClasses(Map<String, ClassLoader> classNames, boolean instantiableOnly) Loads classes from the given map of class names and their associated class loaders, optionally filtering by whether they are instantiable.loadClasses(Map<String, ClassLoader> classNames, ClassLoader[] classLoaders, boolean instantiableOnly) Loads classes from the given map of class names and their associated class loaders using the specified array of class loaders, optionally filtering by instantiability.loadClassSet(Map<String, ClassLoader> classNames, boolean accessibleOnly) Loads classes from the given map of class names and their associated class loaders, returning a set of Class objects.static Map<String,ClassLoader> Scans for classes in the default classpath and java home paths.static Map<String,ClassLoader> scanClasses(String[] paths, String packagePath) Scans for classes in the given paths.static Map<String,ClassLoader> scanClasses(String path, String packagePath) Scans a single path (directory or JAR/JMOD file) for classes.static Map<String,ClassLoader> scanPackageCLasses(String packageName) Scans all classes within the given package.
-
Field Details
-
CLASS_PATH_PROPERTY
System property key for the Java class path.- See Also:
-
CLASS_PATH
The Java class path obtained from the system propertyjava.class.path. This is a list of paths where classes and resources are searched for. -
JAVA_HOME_PROPERTY
System property key for the Java installation directory (JAVA_HOME).- See Also:
-
JAVA_HOME
The Java home directory obtained from the system propertyjava.home. Typically points to the directory where the JRE or JDK is installed. -
SCAN_JDK
public static final boolean SCAN_JDKFlag indicating whether to scan JDK classes for Naftah types, obtained from the system propertynaftah.scanJDK. -
JAR_EXTENSION
The file extension for Java archive files (JAR).- See Also:
-
JMOD_EXTENSION
The file extension for Java module files (JMOD).- See Also:
-
CLASS_EXTENSION
The file extension for compiled Java class files.- See Also:
-
CLASS_EXTENSION_REGEX
Regular expression pattern to match the class file extension.class. Used to identify class files in file names.- See Also:
-
IGNORE
A set of class file base names to ignore during scanning. Typically these include special files like module-info and package-info. -
IGNORE_CLASS
A set of full class file names to ignore during scanning. This is theIGNOREset with the.classextension appended. -
BASE_PACKAGES
Array of common base package names to be used when scanning classes. Includes standard Java and popular top-level package prefixes. -
PATHS
Array of filesystem paths (directories or archives) used as scan roots. Initialized statically by combining the classpath and Java home paths, filtering out ignored dependencies if available. -
CLASS_LOADERS
Array of ClassLoaders used when attempting to load classes. Includes the system class loader, the platform class loader, and the bootstrap class loader.
-
-
Constructor Details
-
RuntimeClassScanner
private RuntimeClassScanner()Private constructor to prevent instantiation. Always throws aNaftahBugErrorwhen called.
-
-
Method Details
-
getPaths
Filters out paths that match any of the ignored JARs.- Parameters:
tempPaths- the array of paths to filterignoredJars- a list of JAR names to ignore- Returns:
- a filtered array of paths
-
getPaths
Returns classpath paths while excluding ignored JARs defined in the "original-dependencies" file.- Parameters:
tempPaths- the array of paths to filter- Returns:
- a filtered array of paths
-
scanClasses
Scans for classes in the default classpath and java home paths.- Returns:
- a map of fully qualified class names to their corresponding class loaders (may be null if default)
-
scanClasses
Scans for classes in the given paths.- Parameters:
paths- an array of file system paths (directories or JAR files) to scan for classes- Returns:
- a map of fully qualified class names to their corresponding class loaders (may be null if default)
-
scanClasses
Scans a single path (directory or JAR/JMOD file) for classes.- Parameters:
path- the file system path to scan- Returns:
- a map of fully qualified class names to their corresponding
ClassLoaders; may benullif default
-
scanPackageCLasses
Scans all classes within the given package.Uses the class loaders in
CLASS_LOADERSto locate the package resource, then scans the resolved path for classes.- Parameters:
packageName- the fully qualified package name to scan- Returns:
- a map of fully qualified class names to their corresponding
ClassLoaders - Throws:
NullPointerException- if the package cannot be found in any class loader
-
loadClassSet
public static Set<Class<?>> loadClassSet(Map<String, ClassLoader> classNames, boolean accessibleOnly) Loads classes from the given map of class names and their associated class loaders, returning a set of Class objects.- Parameters:
classNames- map of class names to their class loadersaccessibleOnly- if true, only loads accessible (public) classes; if false, loads all classes- Returns:
- a set of loaded Class objects
-
loadClasses
public static Map<String,Class<?>> loadClasses(Map<String, ClassLoader> classNames, boolean instantiableOnly) Loads classes from the given map of class names and their associated class loaders, optionally filtering by whether they are instantiable.- Parameters:
classNames- map of class names to their class loadersinstantiableOnly- if true, only includes classes that can be instantiated; if false, includes all- Returns:
- a map of qualified class names to their loaded Class objects
-
loadClasses
public static Map<String,Class<?>> loadClasses(Map<String, ClassLoader> classNames, ClassLoader[] classLoaders, boolean instantiableOnly) Loads classes from the given map of class names and their associated class loaders using the specified array of class loaders, optionally filtering by instantiability.- Parameters:
classNames- a map of class names to their respective class loadersclassLoaders- an array ofClassLoaderinstances to attempt loading classes frominstantiableOnly- if true, only includes classes that can be instantiated; if false, includes all classes- Returns:
- a map of fully qualified class names to their loaded
Classobjects
-
findClassesInDirectory
public static Map<String,ClassLoader> findClassesInDirectory(File root, File dir, String packagePath) Recursively scans for classes inside a directory.- Parameters:
root- root directory where scanning starteddir- current directory or file to scan- Returns:
- a map of fully qualified class names to their corresponding class loaders (null here)
-
findClassesInJar
Scans for classes inside a JAR or JMOD file.- Parameters:
jarFile- the JAR or JMOD file to scan- Returns:
- a map of fully qualified class names to their associated class loaders (null or URLClassLoader for nested jars)
-
jarEntryToTempFile
Extracts a nested JAR entry from a JAR file and writes it to a temporary file. The temporary file is deleted on JVM exit.- Parameters:
outerJar- the outer JAR file containing the nested JAR entryinnerJarEntry- the nested JAR entry inside the outer JAR- Returns:
- a temporary File representing the extracted nested JAR
- Throws:
IOException- if an I/O error occurs during extraction
-