Class ClassPathHelper

java.lang.Object
net.luis.utils.util.unsafe.classpath.ClassPathHelper

final class ClassPathHelper extends Object
Internal helper class to get all classes from the classpath.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final String
    Constant for the system property 'unsafe.classes.ignored'.
    private static final String
    Constant for the system property 'unsafe.package.depth'.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Private constructor to prevent instantiation.
    This is a static helper class.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static @NotNull String
    convertToClass(@NotNull String fileName)
    Converts the given file name to a class name.
    (package private) static @NotNull List<Class<?>>
    getClasses(boolean includeDependencies, @NotNull Predicate<String> condition)
    Gets all classes from the classpath.
    The classes will be filtered by the given condition.
    private static @NotNull List<Class<?>>
    getClassesFromDirectory(@NotNull File directory, @NotNull Predicate<String> condition)
    Gets all classes from the given directory.
    This method will be called recursively if the given directory contains subdirectories.
    private static @NotNull List<Class<?>>
    getClassesFromJar(@NotNull File file, @NotNull Predicate<String> condition)
    Gets all classes from the given jar file.
    Any exceptions which will be thrown while trying to get the classes will be ignored.
    private static @NotNull List<File>
    Gets all files from the classpath.
    The files will be extracted from the system property 'java.class.path'.
    The system path separator will be used to split the classpath into single files.
    private static @NotNull Predicate<String>
    Gets a condition to filter the classes which should be ignored.
    The condition is based on the system property 'unsafe.classes.ignored'.
    private static @NotNull List<File>
    listFiles(@NotNull File directory, @Nullable FilenameFilter filter, boolean recurse)
    Gets all files from the given directory.
    If the given directory contains subdirectories, this method will be called recursively.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • UNSAFE_PACKAGE_DEPTH

      private static final String UNSAFE_PACKAGE_DEPTH
      Constant for the system property 'unsafe.package.depth'.

      The property is used in getClasses(boolean, Predicate) to filter the classes
      from the classpath by the package depth of the calling class.

      The default value is 2.

      See Also:
    • UNSAFE_CLASSES_IGNORED

      private static final String UNSAFE_CLASSES_IGNORED
      Constant for the system property 'unsafe.classes.ignored'.

      The property is used in to filter classes which should be fully ignored.
      All classes which are listed will not be loaded.

      The value of the property is a comma or semicolon separated list of fully qualified class names.
      If you want to ignore all classes from a package, you can use a wildcard at the end of the package name.
      Wildcard entries are only allowed at the end of the package name, the entry must end with .*.

      The default value is an empty string.

      See Also:
  • Constructor Details

    • ClassPathHelper

      private ClassPathHelper()
      Private constructor to prevent instantiation.
      This is a static helper class.
  • Method Details

    • getClasses

      @NotNull static @NotNull List<Class<?>> getClasses(boolean includeDependencies, @NotNull @NotNull Predicate<String> condition)
      Gets all classes from the classpath.
      The classes will be filtered by the given condition.

      If dependency classes should not be included, an 'and' clause is added to the condition.
      The clause checks uses the package name of the calling class to determine if the class is a dependency or not.

      For example, if the caller of this method is in the package "net.luis.utils.util",
      all classes in the packages "net.luis.utils" will be returned,
      because the depth is 3 which means that the package will be cut after the third dot.
      The depth can be changed by using the system property 'unsafe.package.depth'.

      Any exceptions which will be thrown while trying to get the classes will be ignored.

      Parameters:
      includeDependencies - Whether to include dependency classes or not
      condition - A condition to filter the classes
      Returns:
      A list of all classes
    • getIgnoreCondition

      @NotNull private static @NotNull Predicate<String> getIgnoreCondition()
      Gets a condition to filter the classes which should be ignored.
      The condition is based on the system property 'unsafe.classes.ignored'.

      The value of the property is a comma or semicolon separated list of fully qualified class names.
      If you want to ignore all classes from a package, you can use a wildcard at the end of the package name.
      Wildcard entries are only allowed at the end of the package name, the entry must end with .*.

      If the property is not set, the condition will always return false.
      This means that no classes will be ignored.

      Returns:
      A condition to filter the classes which should be ignored
    • getClassesFromJar

      @NotNull private static @NotNull List<Class<?>> getClassesFromJar(@NotNull @NotNull File file, @NotNull @NotNull Predicate<String> condition)
      Gets all classes from the given jar file.
      Any exceptions which will be thrown while trying to get the classes will be ignored.
      Parameters:
      file - The jar file
      condition - A condition to filter the classes
      Returns:
      A list of all classes in the given jar file
      Throws:
      NullPointerException - If the given file is null
    • getClassesFromDirectory

      @NotNull private static @NotNull List<Class<?>> getClassesFromDirectory(@NotNull @NotNull File directory, @NotNull @NotNull Predicate<String> condition)
      Gets all classes from the given directory.
      This method will be called recursively if the given directory contains subdirectories.

      If a jar file is found, getClassesFromJar(File, Predicate) will be called.
      Any exceptions which will be thrown while trying to get the classes will be ignored.
      This method will be mainly used to get all classes in ide environments, from the output directory.

      Parameters:
      directory - The directory to get the classes from
      condition - A condition to filter the classes
      Returns:
      A list of all classes in the given directory
      Throws:
      NullPointerException - If the given directory is null
    • listFiles

      @NotNull private static @NotNull List<File> listFiles(@NotNull @NotNull File directory, @Nullable @Nullable FilenameFilter filter, boolean recurse)
      Gets all files from the given directory.
      If the given directory contains subdirectories, this method will be called recursively.
      Parameters:
      directory - The directory to get the files from
      filter - A filter to filter the files
      recurse - If true, subdirectories will be included
      Returns:
      A list of all files in the given directory
      Throws:
      NullPointerException - If the given directory is null
    • getClassPathFiles

      @NotNull private static @NotNull List<File> getClassPathFiles()
      Gets all files from the classpath.
      The files will be extracted from the system property 'java.class.path'.
      The system path separator will be used to split the classpath into single files.
      Returns:
      A list of all files from the classpath
    • convertToClass

      @NotNull private static @NotNull String convertToClass(@NotNull @NotNull String fileName)
      Converts the given file name to a class name.
      Parameters:
      fileName - The file name to convert
      Returns:
      The converted class name
      Throws:
      NullPointerException - If the given file name is null