Class PackageObjectFactory

  • All Implemented Interfaces:
    ModuleFactory

    public class PackageObjectFactory
    extends java.lang.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).
    • Constructor Detail

      • PackageObjectFactory

        public PackageObjectFactory​(java.util.Set<java.lang.String> packageNames,
                                    java.lang.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​(java.util.Set<java.lang.String> packageNames,
                                    java.lang.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:
        java.lang.IllegalArgumentException - if moduleClassLoader is null or packageNames contains null
      • PackageObjectFactory

        public PackageObjectFactory​(java.lang.String packageName,
                                    java.lang.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:
        java.lang.IllegalArgumentException - if moduleClassLoader is null or packageNames is null
    • Method Detail

      • createModule

        public java.lang.Object createModule​(java.lang.String name)
                                      throws CheckstyleException
        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

        private java.lang.Object createFromStandardCheckSet​(java.lang.String name)
                                                     throws CheckstyleException
        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.
      • createObjectFromMap

        private java.lang.Object createObjectFromMap​(java.lang.String name,
                                                     java.util.Map<java.lang.String,​java.util.Set<java.lang.String>> map)
                                              throws CheckstyleException
        Create object with the help of the supplied map.
        Parameters:
        name - name of module.
        map - the supplied map.
        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 java.lang.Object createObjectFromFullModuleNames​(java.lang.String name,
                                                                 java.util.Set<java.lang.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

        private java.util.Map<java.lang.String,​java.util.Set<java.lang.String>> generateThirdPartyNameToFullModuleName​(java.lang.ClassLoader loader)
        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 java.lang.String getShortFromFullModuleNames​(java.lang.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 java.lang.String joinPackageNamesWithClassName​(java.lang.String className,
                                                                      java.util.Set<java.lang.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 java.lang.Object createObject​(java.lang.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

        private java.lang.Object createModuleByTryInEachPackage​(java.lang.String name)
                                                         throws CheckstyleException
        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.
      • 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.