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).
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String AMBIGUOUS_MODULE_NAME_EXCEPTION_MESSAGE
      Exception message when there is ambigugous module name in config file.
      static java.lang.String BASE_PACKAGE
      Base package of checkstyle modules checks.
      static java.lang.String CHECK_SUFFIX
      Suffix of checks.
      private java.lang.ClassLoader moduleClassLoader
      The class loader used to load Checkstyle core and custom modules.
      private static java.util.Map<java.lang.String,​java.lang.String> NAME_TO_FULL_MODULE_NAME
      Map of Checkstyle module names to their fully qualified names.
      static java.lang.String NULL_LOADER_MESSAGE
      Exception message when null class loader is given.
      static java.lang.String NULL_PACKAGE_MESSAGE
      Exception message when null package name is given.
      static java.lang.String PACKAGE_SEPARATOR
      Character separate package names in qualified name of java class.
      private java.util.Set<java.lang.String> packages
      A list of package names to prepend to class names.
      static java.lang.String STRING_SEPARATOR
      Separator to use in strings.
      private java.util.Map<java.lang.String,​java.util.Set<java.lang.String>> thirdPartyNameToFullModuleNames
      Map of third party Checkstyle module names to the set of their fully qualified names.
      static java.lang.String UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE
      Exception message when it is unable to create a class instance.
    • Constructor Summary

      Constructors 
      Constructor Description
      PackageObjectFactory​(java.lang.String packageName, java.lang.ClassLoader moduleClassLoader)
      Creates a new PackageObjectFactory instance.
      PackageObjectFactory​(java.util.Set<java.lang.String> packageNames, java.lang.ClassLoader moduleClassLoader)
      Creates a new PackageObjectFactory instance.
    • Field Detail

      • BASE_PACKAGE

        public static final java.lang.String BASE_PACKAGE
        Base package of checkstyle modules checks.
        See Also:
        Constant Field Values
      • UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE

        public static final java.lang.String UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE
        Exception message when it is unable to create a class instance.
        See Also:
        Constant Field Values
      • AMBIGUOUS_MODULE_NAME_EXCEPTION_MESSAGE

        public static final java.lang.String AMBIGUOUS_MODULE_NAME_EXCEPTION_MESSAGE
        Exception message when there is ambigugous module name in config file.
        See Also:
        Constant Field Values
      • CHECK_SUFFIX

        public static final java.lang.String CHECK_SUFFIX
        Suffix of checks.
        See Also:
        Constant Field Values
      • PACKAGE_SEPARATOR

        public static final java.lang.String PACKAGE_SEPARATOR
        Character separate package names in qualified name of java class.
        See Also:
        Constant Field Values
      • NULL_LOADER_MESSAGE

        public static final java.lang.String NULL_LOADER_MESSAGE
        Exception message when null class loader is given.
        See Also:
        Constant Field Values
      • NULL_PACKAGE_MESSAGE

        public static final java.lang.String NULL_PACKAGE_MESSAGE
        Exception message when null package name is given.
        See Also:
        Constant Field Values
      • STRING_SEPARATOR

        public static final java.lang.String STRING_SEPARATOR
        Separator to use in strings.
        See Also:
        Constant Field Values
      • NAME_TO_FULL_MODULE_NAME

        private static final java.util.Map<java.lang.String,​java.lang.String> NAME_TO_FULL_MODULE_NAME
        Map of Checkstyle module names to their fully qualified names.
      • packages

        private final java.util.Set<java.lang.String> packages
        A list of package names to prepend to class names.
      • moduleClassLoader

        private final java.lang.ClassLoader moduleClassLoader
        The class loader used to load Checkstyle core and custom modules.
      • thirdPartyNameToFullModuleNames

        private java.util.Map<java.lang.String,​java.util.Set<java.lang.String>> thirdPartyNameToFullModuleNames
        Map of third party Checkstyle module names to the set of their fully qualified names.
    • Constructor Detail

      • PackageObjectFactory

        public PackageObjectFactory​(java.util.Set<java.lang.String> packageNames,
                                    java.lang.ClassLoader moduleClassLoader)
        Creates a new PackageObjectFactory instance.
        Parameters:
        packageNames - the list of package names to use
        moduleClassLoader - class loader used to load Checkstyle core and custom modules
      • 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
    • 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.
      • 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
      • 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.
      • 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.