Class ClassPathUtils

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

public final class ClassPathUtils extends Object
Utility class for classpath related operations.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    The mode which determines if the classes should be included or excluded.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final org.apache.logging.log4j.Logger
    The logger for this class.
  • 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
    static @NotNull List<Class<?>>
    Gets all classes from the classpath.
    If an error occurs, the error will be logged and an empty list will be returned.
    static @NotNull List<Class<?>>
    getAnnotatedClasses(@NotNull Class<? extends Annotation> annotation)
    Gets all classes from the classpath which are annotated with the given annotation.
    static @NotNull List<Class<?>>
    getAnnotatedClasses(@Nullable String packageName, @NotNull Class<? extends Annotation> annotation)
    Gets all classes from the given package which are annotated with the given annotation.
    static @NotNull List<Field>
    getAnnotatedFields(@NotNull Class<? extends Annotation> annotation)
    Gets all fields from the classpath which are annotated with the given annotation.
    static @NotNull List<Field>
    getAnnotatedFields(@Nullable String packageName, @NotNull Class<? extends Annotation> annotation)
    Gets all fields from the classes in the given package which are annotated with the given annotation.
    static @NotNull List<Method>
    getAnnotatedMethods(@NotNull Class<? extends Annotation> annotation)
    Gets all methods from the classpath which are annotated with the given annotation.
    static @NotNull List<Method>
    getAnnotatedMethods(@Nullable String packageName, @NotNull Class<? extends Annotation> annotation)
    Gets all methods from the classes in the given package which are annotated with the given annotation.
    static @NotNull List<Class<?>>
    getClasses(@Nullable String packageName)
    Gets all classes from the given package.
    If the given package name is null, all classes will be returned.
    static @NotNull List<Class<?>>
    getClasses(@Nullable String packageName, @Nullable ClassPathUtils.Mode mode)
    Gets all classes from the given package.
    The mode determines if the classes should be included or excluded
    if the package name of the class starts with the given package name.
    static @NotNull List<Class<?>>
    Gets all classes from the classpath which are related to the project.
    Related classes are classes that are in the same package as the caller of this method.
    By default, the depth of the package which is used to filter the classes is 3.

    Methods inherited from class java.lang.Object

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

    • LOGGER

      private static final org.apache.logging.log4j.Logger LOGGER
      The logger for this class.
  • Constructor Details

    • ClassPathUtils

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

    • getAllClasses

      @NotNull public static @NotNull List<Class<?>> getAllClasses()
      Gets all classes from the classpath.
      If an error occurs, the error will be logged and an empty list will be returned.
      Returns:
      A list of all classes from the classpath
    • getProjectClasses

      @NotNull public static @NotNull List<Class<?>> getProjectClasses()
      Gets all classes from the classpath which are related to the project.
      Related classes are classes that are in the same package as the caller of this method.
      By default, the depth of the package which is used to filter the classes is 3.

      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'.

      If an error occurs, the error will be logged and an empty list will be returned.

      Returns:
      A list of all classes from the classpath which are related to the project
      See Also:
    • getClasses

      @NotNull public static @NotNull List<Class<?>> getClasses(@Nullable @Nullable String packageName)
      Gets all classes from the given package.
      If the given package name is null, all classes will be returned.
      Parameters:
      packageName - The package name in which the classes should be
      Returns:
      A list of all classes
    • getClasses

      @NotNull public static @NotNull List<Class<?>> getClasses(@Nullable @Nullable String packageName, @Nullable @Nullable ClassPathUtils.Mode mode)
      Gets all classes from the given package.
      The mode determines if the classes should be included or excluded
      if the package name of the class starts with the given package name.

      If the given package name is null, all classes will be returned.
      If the mode is null, the classes will be included.

      Parameters:
      packageName - The package name to filter the classes
      mode - Whether the classes should be included or excluded
      Returns:
      A list of all classes
    • getAnnotatedClasses

      @NotNull public static @NotNull List<Class<?>> getAnnotatedClasses(@NotNull @NotNull Class<? extends Annotation> annotation)
      Gets all classes from the classpath which are annotated with the given annotation.
      Parameters:
      annotation - The annotation which should be present on the classes
      Returns:
      A list of all classes
      Throws:
      NullPointerException - If the given annotation is null
    • getAnnotatedClasses

      @NotNull public static @NotNull List<Class<?>> getAnnotatedClasses(@Nullable @Nullable String packageName, @NotNull @NotNull Class<? extends Annotation> annotation)
      Gets all classes from the given package which are annotated with the given annotation.
      Parameters:
      packageName - The package name in which the classes should be
      annotation - The annotation which should be present on the classes
      Returns:
      A list of all classes
      Throws:
      NullPointerException - If the given annotation is null
    • getAnnotatedMethods

      @NotNull public static @NotNull List<Method> getAnnotatedMethods(@NotNull @NotNull Class<? extends Annotation> annotation)
      Gets all methods from the classpath which are annotated with the given annotation.
      Parameters:
      annotation - The annotation which should be present on the methods
      Returns:
      A list of all methods
      Throws:
      NullPointerException - If the given annotation is null
    • getAnnotatedMethods

      @NotNull public static @NotNull List<Method> getAnnotatedMethods(@Nullable @Nullable String packageName, @NotNull @NotNull Class<? extends Annotation> annotation)
      Gets all methods from the classes in the given package which are annotated with the given annotation.
      Parameters:
      packageName - The package name in which the classes should be
      annotation - The annotation which should be present on the methods
      Returns:
      A list of all methods
      Throws:
      NullPointerException - If the given annotation is null
    • getAnnotatedFields

      @NotNull public static @NotNull List<Field> getAnnotatedFields(@NotNull @NotNull Class<? extends Annotation> annotation)
      Gets all fields from the classpath which are annotated with the given annotation.
      Parameters:
      annotation - The annotation which should be present on the fields
      Returns:
      A list of all fields
      Throws:
      NullPointerException - If the given annotation is null
    • getAnnotatedFields

      @NotNull public static @NotNull List<Field> getAnnotatedFields(@Nullable @Nullable String packageName, @NotNull @NotNull Class<? extends Annotation> annotation)
      Gets all fields from the classes in the given package which are annotated with the given annotation.
      Parameters:
      packageName - The package name in which the classes should be
      annotation - The annotation which should be present on the fields
      Returns:
      A list of all fields
      Throws:
      NullPointerException - If the given annotation is null