Class RuntimeClassScanner

java.lang.Object
org.daiitech.naftah.utils.reflect.RuntimeClassScanner

public final class RuntimeClassScanner extends Object
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 Details

    • CLASS_PATH_PROPERTY

      public static final String CLASS_PATH_PROPERTY
      System property key for the Java class path.
      See Also:
    • CLASS_PATH

      public static final String CLASS_PATH
      The Java class path obtained from the system property java.class.path. This is a list of paths where classes and resources are searched for.
    • JAVA_HOME_PROPERTY

      public static final String JAVA_HOME_PROPERTY
      System property key for the Java installation directory (JAVA_HOME).
      See Also:
    • JAVA_HOME

      public static final String JAVA_HOME
      The Java home directory obtained from the system property java.home. Typically points to the directory where the JRE or JDK is installed.
    • SCAN_JDK

      public static final boolean SCAN_JDK
      Flag indicating whether to scan JDK classes for Naftah types, obtained from the system property naftah.scanJDK.
    • JAR_EXTENSION

      public static final String JAR_EXTENSION
      The file extension for Java archive files (JAR).
      See Also:
    • JMOD_EXTENSION

      public static final String JMOD_EXTENSION
      The file extension for Java module files (JMOD).
      See Also:
    • CLASS_EXTENSION

      public static final String CLASS_EXTENSION
      The file extension for compiled Java class files.
      See Also:
    • CLASS_EXTENSION_REGEX

      public static final String CLASS_EXTENSION_REGEX
      Regular expression pattern to match the class file extension .class. Used to identify class files in file names.
      See Also:
    • IGNORE

      public static final Set<String> 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

      public static final Set<String> IGNORE_CLASS
      A set of full class file names to ignore during scanning. This is the IGNORE set with the .class extension appended.
    • BASE_PACKAGES

      public static final String[] BASE_PACKAGES
      Array of common base package names to be used when scanning classes. Includes standard Java and popular top-level package prefixes.
    • PATHS

      public static final String[] 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

      public static final ClassLoader[] 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 a NaftahBugError when called.
  • Method Details

    • getPaths

      public static String[] getPaths(String[] tempPaths, List<String> ignoredJars)
      Filters out paths that match any of the ignored JARs.
      Parameters:
      tempPaths - the array of paths to filter
      ignoredJars - a list of JAR names to ignore
      Returns:
      a filtered array of paths
    • getPaths

      public static String[] getPaths(String[] tempPaths)
      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

      public static Map<String,ClassLoader> 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

      public static Map<String,ClassLoader> scanClasses(String[] paths, String packagePath)
      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

      public static Map<String,ClassLoader> scanClasses(String path, String packagePath)
      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 be null if default
    • scanPackageCLasses

      public static Map<String,ClassLoader> scanPackageCLasses(String packageName)
      Scans all classes within the given package.

      Uses the class loaders in CLASS_LOADERS to 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 loaders
      accessibleOnly - 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 loaders
      instantiableOnly - 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 loaders
      classLoaders - an array of ClassLoader instances to attempt loading classes from
      instantiableOnly - 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 Class objects
    • 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 started
      dir - current directory or file to scan
      Returns:
      a map of fully qualified class names to their corresponding class loaders (null here)
    • findClassesInJar

      public static Map<String,ClassLoader> findClassesInJar(File jarFile, String packagePath)
      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

      public static File jarEntryToTempFile(JarFile outerJar, JarEntry innerJarEntry) throws IOException
      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 entry
      innerJarEntry - 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