Class ClassScanner

java.lang.Object
com.zetaplugins.zetacore.scan.ClassScanner

public class ClassScanner extends Object
Scans a package for classes using a specific class loader.

Class files are read without loading them; only classes matching a query are loaded (without being initialized). The package is scanned once, on first use, and the result is reused for every query. Share one instance between the ServiceRegistry and the registrars instead of letting each of them scan on its own.

If ZetaCore is shaded and relocated into the scanned package, its own classes are excluded. Classes that cannot be loaded (for example because they depend on a plugin that is not installed) are skipped.

  • Constructor Details

    • ClassScanner

      public ClassScanner(String packagePrefix, ClassLoader classLoader)
      Creates a scanner for the given package.
      Parameters:
      packagePrefix - The package to scan, including all sub-packages.
      classLoader - The class loader that can see the classes in the package (usually the plugin's class loader).
  • Method Details

    • getPackagePrefix

      public String getPackagePrefix()
      Gets the scanned package.
      Returns:
      The package prefix.
    • getClassLoader

      public ClassLoader getClassLoader()
      Gets the class loader used for scanning and loading classes.
      Returns:
      The class loader.
    • getTypesAnnotatedWith

      public Set<Class<?>> getTypesAnnotatedWith(Class<? extends Annotation> annotation)
      Finds all classes annotated with the given annotation, including subclasses of annotated classes and classes annotated with an annotation that is itself annotated with it.
      Parameters:
      annotation - The annotation to look for.
      Returns:
      The annotated classes, sorted by name.
    • getSubTypesOf

      public <T> Set<Class<? extends T>> getSubTypesOf(Class<T> type)
      Finds all subtypes of the given type.
      Type Parameters:
      T - The supertype.
      Parameters:
      type - The supertype.
      Returns:
      The subtypes, not including the type itself, sorted by name.
    • getTypesWithMethodsAnnotatedWith

      public Set<Class<?>> getTypesWithMethodsAnnotatedWith(Class<? extends Annotation> annotation)
      Finds all classes that declare at least one method annotated with the given annotation.
      Parameters:
      annotation - The method annotation to look for.
      Returns:
      The declaring classes, sorted by name.