Class PackageObjectFactory

java.lang.Object
com.puppycrawl.tools.checkstyle.PackageObjectFactory
All Implemented Interfaces:
ModuleFactory

public class PackageObjectFactory extends Object implements ModuleFactory
A factory for creating objects from package names and names. Consider the below example for better understanding.
  • module name - name of java class that represents module;
  • module full name - fully qualifies name of java class that represents module;
  • check module short name - name of Check without 'Check' suffix;
  • check module name - name of java class that represents Check (with 'Check' suffix);
  • check module full name - fully qualifies name of java class that represents Check (with 'Check' suffix).
  • Field Details

  • Constructor Details

    • PackageObjectFactory

      public PackageObjectFactory(Set<String> packageNames, ClassLoader moduleClassLoader)
      Creates a new PackageObjectFactory instance.
      Parameters:
      packageNames - package names to use
      moduleClassLoader - class loader used to load Checkstyle core and custom modules
    • PackageObjectFactory

      public PackageObjectFactory(Set<String> packageNames, ClassLoader moduleClassLoader, PackageObjectFactory.ModuleLoadOption moduleLoadOption)
      Creates a new PackageObjectFactory instance.
      Parameters:
      packageNames - package names to use
      moduleClassLoader - class loader used to load Checkstyle core and custom modules
      moduleLoadOption - loading option
      Throws:
      IllegalArgumentException - if moduleClassLoader is null or packageNames contains null
    • PackageObjectFactory

      public PackageObjectFactory(String packageName, ClassLoader moduleClassLoader)
      Creates a new PackageObjectFactory instance.
      Parameters:
      packageName - The package name to use
      moduleClassLoader - class loader used to load Checkstyle core and custom modules
      Throws:
      IllegalArgumentException - if moduleClassLoader is null or packageNames is null
  • Method Details

    • createModule

      Creates a new instance of a class from a given name, or that name concatenated with "Check". If the name is a class name, creates an instance of the named class. Otherwise, creates an instance of a class name obtained by concatenating the given name to a package name from a given list of package names.
      Specified by:
      createModule in interface ModuleFactory
      Parameters:
      name - the name of a class.
      Returns:
      the Object created by loader.
      Throws:
      CheckstyleException - if an error occurs.
    • createFromStandardCheckSet

      Create object from one of Checkstyle module names.
      Parameters:
      name - name of module.
      Returns:
      instance of module.
      Throws:
      CheckstyleException - if the class fails to instantiate or there are ambiguous classes.
    • createObjectFromClassPath

      Create object with the help of the classpath.
      Parameters:
      name - name of module.
      Returns:
      instance of module if it is found in modules map and no ambiguous classes exist.
      Throws:
      CheckstyleException - if the class fails to instantiate or there are ambiguous classes.
    • createObjectFromFullModuleNames

      private Object createObjectFromFullModuleNames(String name, Set<String> fullModuleNames) throws CheckstyleException
      Create Object from optional full module names. In most case, there should be only one element in fullModuleName, otherwise an exception would be thrown.
      Parameters:
      name - name of module
      fullModuleNames - the supplied full module names set
      Returns:
      instance of module if there is only one element in fullModuleName
      Throws:
      CheckstyleException - if the class fails to instantiate or there are more than one element in fullModuleName
    • generateThirdPartyNameToFullModuleName

      Generate the map of third party Checkstyle module names to the set of their fully qualified names.
      Parameters:
      loader - the class loader used to load Checkstyle package names
      Returns:
      the map of third party Checkstyle module names to the set of their fully qualified names
    • getShortFromFullModuleNames

      public static String getShortFromFullModuleNames(String fullName)
      Returns simple check name from full modules names map.
      Parameters:
      fullName - name of the class for joining.
      Returns:
      simple check name.
    • joinPackageNamesWithClassName

      private static String joinPackageNamesWithClassName(String className, Set<String> packages)
      Creates a string by joining package names with a class name.
      Parameters:
      className - name of the class for joining.
      packages - packages names.
      Returns:
      a string which is obtained by joining package names with a class name.
    • createObject

      private Object createObject(String className) throws CheckstyleException
      Creates a new instance of a named class.
      Parameters:
      className - the name of the class to instantiate.
      Returns:
      the Object created by loader or null.
      Throws:
      CheckstyleException - if the class fails to instantiate.
    • createModuleByTryInEachPackage

      Searching to class with given name (or name concatenated with "Check") in existing packages. Returns instance if class found or, otherwise, null.
      Parameters:
      name - the name of a class.
      Returns:
      the Object created by loader.
      Throws:
      CheckstyleException - if an error occurs.
    • lazyLoad

      private static <T> T lazyLoad(T object, Supplier<T> supplier)
      Initialize object by supplier if object is null.
      Type Parameters:
      T - type of object
      Parameters:
      object - object to initialize
      supplier - function to initialize if object is null
      Returns:
      object as it was provided in method or initialized
    • fillShortToFullModuleNamesMap

      private static void fillShortToFullModuleNamesMap()
      Fill short-to-full module names map.
    • fillChecksFromAnnotationPackage

      private static void fillChecksFromAnnotationPackage()
      Fill short-to-full module names map with Checks from annotation package.
    • fillChecksFromBlocksPackage

      private static void fillChecksFromBlocksPackage()
      Fill short-to-full module names map with Checks from blocks package.
    • fillChecksFromCodingPackage

      private static void fillChecksFromCodingPackage()
      Fill short-to-full module names map with Checks from coding package.
    • fillChecksFromDesignPackage

      private static void fillChecksFromDesignPackage()
      Fill short-to-full module names map with Checks from design package.
    • fillChecksFromHeaderPackage

      private static void fillChecksFromHeaderPackage()
      Fill short-to-full module names map with Checks from header package.
    • fillChecksFromImportsPackage

      private static void fillChecksFromImportsPackage()
      Fill short-to-full module names map with Checks from imports package.
    • fillChecksFromIndentationPackage

      private static void fillChecksFromIndentationPackage()
      Fill short-to-full module names map with Checks from indentation package.
    • fillChecksFromJavadocPackage

      private static void fillChecksFromJavadocPackage()
      Fill short-to-full module names map with Checks from javadoc package.
    • fillChecksFromMetricsPackage

      private static void fillChecksFromMetricsPackage()
      Fill short-to-full module names map with Checks from metrics package.
    • fillChecksFromModifierPackage

      private static void fillChecksFromModifierPackage()
      Fill short-to-full module names map with Checks from modifier package.
    • fillChecksFromNamingPackage

      private static void fillChecksFromNamingPackage()
      Fill short-to-full module names map with Checks from naming package.
    • fillChecksFromRegexpPackage

      private static void fillChecksFromRegexpPackage()
      Fill short-to-full module names map with Checks from regexp package.
    • fillChecksFromSizesPackage

      private static void fillChecksFromSizesPackage()
      Fill short-to-full module names map with Checks from sizes package.
    • fillChecksFromWhitespacePackage

      private static void fillChecksFromWhitespacePackage()
      Fill short-to-full module names map with Checks from whitespace package.
    • fillModulesFromChecksPackage

      private static void fillModulesFromChecksPackage()
      Fill short-to-full module names map with modules from checks package.
    • fillModulesFromFilefiltersPackage

      private static void fillModulesFromFilefiltersPackage()
      Fill short-to-full module names map with modules from filefilters package.
    • fillModulesFromFiltersPackage

      private static void fillModulesFromFiltersPackage()
      Fill short-to-full module names map with modules from filters package.
    • fillModulesFromCheckstylePackage

      private static void fillModulesFromCheckstylePackage()
      Fill short-to-full module names map with modules from checkstyle package.