Package com.puppycrawl.tools.checkstyle
Class PackageObjectFactory
java.lang.Object
com.puppycrawl.tools.checkstyle.PackageObjectFactory
- All Implemented Interfaces:
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).
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Enum class to define loading options. -
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Exception message when there is ambiguous module name in config file.static final String
Base package of checkstyle modules checks.static final String
Suffix of checks.private final ClassLoader
The class loader used to load Checkstyle core and custom modules.Module load option which defines class search type.Map of Checkstyle module names to their fully qualified names.static final String
Exception message when null class loader is given.static final String
Exception message when null package name is given.static final String
Character separate package names in qualified name of java class.Package names to prepend to class names.static final String
Separator to use in strings.Map of third party Checkstyle module names to the set of their fully qualified names.static final String
Exception message when it is unable to create a class instance. -
Constructor Summary
ConstructorDescriptionPackageObjectFactory
(String packageName, ClassLoader moduleClassLoader) Creates a newPackageObjectFactory
instance.PackageObjectFactory
(Set<String> packageNames, ClassLoader moduleClassLoader) Creates a newPackageObjectFactory
instance.PackageObjectFactory
(Set<String> packageNames, ClassLoader moduleClassLoader, PackageObjectFactory.ModuleLoadOption moduleLoadOption) Creates a newPackageObjectFactory
instance. -
Method Summary
Modifier and TypeMethodDescriptionprivate Object
Create object from one of Checkstyle module names.createModule
(String name) Creates a new instance of a class from a given name, or that name concatenated with "Check".private Object
Searching to class with given name (or name concatenated with "Check") in existing packages.private Object
createObject
(String className) Creates a new instance of a named class.private Object
Create object with the help of the classpath.private Object
createObjectFromFullModuleNames
(String name, Set<String> fullModuleNames) Create Object from optional full module names.private static void
Fill short-to-full module names map with Checks from annotation package.private static void
Fill short-to-full module names map with Checks from blocks package.private static void
Fill short-to-full module names map with Checks from coding package.private static void
Fill short-to-full module names map with Checks from design package.private static void
Fill short-to-full module names map with Checks from header package.private static void
Fill short-to-full module names map with Checks from imports package.private static void
Fill short-to-full module names map with Checks from indentation package.private static void
Fill short-to-full module names map with Checks from javadoc package.private static void
Fill short-to-full module names map with Checks from metrics package.private static void
Fill short-to-full module names map with Checks from modifier package.private static void
Fill short-to-full module names map with Checks from naming package.private static void
Fill short-to-full module names map with Checks from regexp package.private static void
Fill short-to-full module names map with Checks from sizes package.private static void
Fill short-to-full module names map with Checks from whitespace package.private static void
Fill short-to-full module names map with modules from checks package.private static void
Fill short-to-full module names map with modules from checkstyle package.private static void
Fill short-to-full module names map with modules from filefilters package.private static void
Fill short-to-full module names map with modules from filters package.private static void
Fill short-to-full module names map.Generate the map of third party Checkstyle module names to the set of their fully qualified names.static String
getShortFromFullModuleNames
(String fullName) Returns simple check name from full modules names map.private static String
joinPackageNamesWithClassName
(String className, Set<String> packages) Creates a string by joining package names with a class name.private static <T> T
Initialize object by supplier if object is null.
-
Field Details
-
BASE_PACKAGE
Base package of checkstyle modules checks.- See Also:
-
UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE
Exception message when it is unable to create a class instance.- See Also:
-
AMBIGUOUS_MODULE_NAME_EXCEPTION_MESSAGE
Exception message when there is ambiguous module name in config file.- See Also:
-
CHECK_SUFFIX
Suffix of checks.- See Also:
-
PACKAGE_SEPARATOR
Character separate package names in qualified name of java class.- See Also:
-
NULL_LOADER_MESSAGE
Exception message when null class loader is given.- See Also:
-
NULL_PACKAGE_MESSAGE
Exception message when null package name is given.- See Also:
-
STRING_SEPARATOR
Separator to use in strings.- See Also:
-
NAME_TO_FULL_MODULE_NAME
Map of Checkstyle module names to their fully qualified names. -
packages
Package names to prepend to class names. -
moduleClassLoader
The class loader used to load Checkstyle core and custom modules. -
thirdPartyNameToFullModuleNames
Map of third party Checkstyle module names to the set of their fully qualified names. -
moduleLoadOption
Module load option which defines class search type.
-
-
Constructor Details
-
PackageObjectFactory
Creates a newPackageObjectFactory
instance.- Parameters:
packageNames
- package names to usemoduleClassLoader
- class loader used to load Checkstyle core and custom modules
-
PackageObjectFactory
public PackageObjectFactory(Set<String> packageNames, ClassLoader moduleClassLoader, PackageObjectFactory.ModuleLoadOption moduleLoadOption) Creates a newPackageObjectFactory
instance.- Parameters:
packageNames
- package names to usemoduleClassLoader
- class loader used to load Checkstyle core and custom modulesmoduleLoadOption
- loading option- Throws:
IllegalArgumentException
- if moduleClassLoader is null or packageNames contains null
-
PackageObjectFactory
Creates a newPackageObjectFactory
instance.- Parameters:
packageName
- The package name to usemoduleClassLoader
- 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 interfaceModuleFactory
- 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 infullModuleName
, otherwise an exception would be thrown.- Parameters:
name
- name of modulefullModuleNames
- 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 infullModuleName
-
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
Returns simple check name from full modules names map.- Parameters:
fullName
- name of the class for joining.- Returns:
- simple check name.
-
joinPackageNamesWithClassName
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
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
Initialize object by supplier if object is null.- Type Parameters:
T
- type of object- Parameters:
object
- object to initializesupplier
- function to initialize if object is null- Returns:
- object as it was provided in method or initialized
-
fillShortToFullModuleNamesMap
Fill short-to-full module names map. -
fillChecksFromAnnotationPackage
Fill short-to-full module names map with Checks from annotation package. -
fillChecksFromBlocksPackage
Fill short-to-full module names map with Checks from blocks package. -
fillChecksFromCodingPackage
Fill short-to-full module names map with Checks from coding package. -
fillChecksFromDesignPackage
Fill short-to-full module names map with Checks from design package. -
fillChecksFromHeaderPackage
Fill short-to-full module names map with Checks from header package. -
fillChecksFromImportsPackage
Fill short-to-full module names map with Checks from imports package. -
fillChecksFromIndentationPackage
Fill short-to-full module names map with Checks from indentation package. -
fillChecksFromJavadocPackage
Fill short-to-full module names map with Checks from javadoc package. -
fillChecksFromMetricsPackage
Fill short-to-full module names map with Checks from metrics package. -
fillChecksFromModifierPackage
Fill short-to-full module names map with Checks from modifier package. -
fillChecksFromNamingPackage
Fill short-to-full module names map with Checks from naming package. -
fillChecksFromRegexpPackage
Fill short-to-full module names map with Checks from regexp package. -
fillChecksFromSizesPackage
Fill short-to-full module names map with Checks from sizes package. -
fillChecksFromWhitespacePackage
Fill short-to-full module names map with Checks from whitespace package. -
fillModulesFromChecksPackage
Fill short-to-full module names map with modules from checks package. -
fillModulesFromFilefiltersPackage
Fill short-to-full module names map with modules from filefilters package. -
fillModulesFromFiltersPackage
Fill short-to-full module names map with modules from filters package. -
fillModulesFromCheckstylePackage
Fill short-to-full module names map with modules from checkstyle package.
-