Package com.puppycrawl.tools.checkstyle
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).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
PackageObjectFactory.ModuleLoadOption
Enum class to define loading options.
-
Field Summary
Fields Modifier and Type Field Description static String
AMBIGUOUS_MODULE_NAME_EXCEPTION_MESSAGE
Exception message when there is ambiguous module name in config file.static String
BASE_PACKAGE
Base package of checkstyle modules checks.static String
CHECK_SUFFIX
Suffix of checks.private ClassLoader
moduleClassLoader
The class loader used to load Checkstyle core and custom modules.private PackageObjectFactory.ModuleLoadOption
moduleLoadOption
Module load option which defines class search type.private static Map<String,String>
NAME_TO_FULL_MODULE_NAME
Map of Checkstyle module names to their fully qualified names.static String
NULL_LOADER_MESSAGE
Exception message when null class loader is given.static String
NULL_PACKAGE_MESSAGE
Exception message when null package name is given.static String
PACKAGE_SEPARATOR
Character separate package names in qualified name of java class.private Set<String>
packages
Package names to prepend to class names.static String
STRING_SEPARATOR
Separator to use in strings.private Map<String,Set<String>>
thirdPartyNameToFullModuleNames
Map of third party Checkstyle module names to the set of their fully qualified names.static String
UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE
Exception message when it is unable to create a class instance.
-
Constructor Summary
Constructors Constructor Description PackageObjectFactory(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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private Object
createFromStandardCheckSet(String name)
Create object from one of Checkstyle module names.Object
createModule(String name)
Creates a new instance of a class from a given name, or that name concatenated with "Check".private Object
createModuleByTryInEachPackage(String name)
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
createObjectFromClassPath(String name)
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
fillChecksFromAnnotationPackage()
Fill short-to-full module names map with Checks from annotation package.private static void
fillChecksFromBlocksPackage()
Fill short-to-full module names map with Checks from blocks package.private static void
fillChecksFromCodingPackage()
Fill short-to-full module names map with Checks from coding package.private static void
fillChecksFromDesignPackage()
Fill short-to-full module names map with Checks from design package.private static void
fillChecksFromHeaderPackage()
Fill short-to-full module names map with Checks from header package.private static void
fillChecksFromImportsPackage()
Fill short-to-full module names map with Checks from imports package.private static void
fillChecksFromIndentationPackage()
Fill short-to-full module names map with Checks from indentation package.private static void
fillChecksFromJavadocPackage()
Fill short-to-full module names map with Checks from javadoc package.private static void
fillChecksFromMetricsPackage()
Fill short-to-full module names map with Checks from metrics package.private static void
fillChecksFromModifierPackage()
Fill short-to-full module names map with Checks from modifier package.private static void
fillChecksFromNamingPackage()
Fill short-to-full module names map with Checks from naming package.private static void
fillChecksFromRegexpPackage()
Fill short-to-full module names map with Checks from regexp package.private static void
fillChecksFromSizesPackage()
Fill short-to-full module names map with Checks from sizes package.private static void
fillChecksFromWhitespacePackage()
Fill short-to-full module names map with Checks from whitespace package.private static void
fillModulesFromChecksPackage()
Fill short-to-full module names map with modules from checks package.private static void
fillModulesFromCheckstylePackage()
Fill short-to-full module names map with modules from checkstyle package.private static void
fillModulesFromFilefiltersPackage()
Fill short-to-full module names map with modules from filefilters package.private static void
fillModulesFromFiltersPackage()
Fill short-to-full module names map with modules from filters package.private static void
fillShortToFullModuleNamesMap()
Fill short-to-full module names map.private Map<String,Set<String>>
generateThirdPartyNameToFullModuleName(ClassLoader loader)
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
lazyLoad(T object, Supplier<T> supplier)
Initialize object by supplier if object is null.
-
-
-
Field Detail
-
BASE_PACKAGE
public static final String BASE_PACKAGE
Base package of checkstyle modules checks.- See Also:
- Constant Field Values
-
UNABLE_TO_INSTANTIATE_EXCEPTION_MESSAGE
public static final 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 String AMBIGUOUS_MODULE_NAME_EXCEPTION_MESSAGE
Exception message when there is ambiguous module name in config file.- See Also:
- Constant Field Values
-
CHECK_SUFFIX
public static final String CHECK_SUFFIX
Suffix of checks.- See Also:
- Constant Field Values
-
PACKAGE_SEPARATOR
public static final String PACKAGE_SEPARATOR
Character separate package names in qualified name of java class.- See Also:
- Constant Field Values
-
NULL_LOADER_MESSAGE
public static final String NULL_LOADER_MESSAGE
Exception message when null class loader is given.- See Also:
- Constant Field Values
-
NULL_PACKAGE_MESSAGE
public static final String NULL_PACKAGE_MESSAGE
Exception message when null package name is given.- See Also:
- Constant Field Values
-
STRING_SEPARATOR
public static final String STRING_SEPARATOR
Separator to use in strings.- See Also:
- Constant Field Values
-
NAME_TO_FULL_MODULE_NAME
private static final Map<String,String> NAME_TO_FULL_MODULE_NAME
Map of Checkstyle module names to their fully qualified names.
-
moduleClassLoader
private final ClassLoader moduleClassLoader
The class loader used to load Checkstyle core and custom modules.
-
thirdPartyNameToFullModuleNames
private Map<String,Set<String>> thirdPartyNameToFullModuleNames
Map of third party Checkstyle module names to the set of their fully qualified names.
-
moduleLoadOption
private PackageObjectFactory.ModuleLoadOption moduleLoadOption
Module load option which defines class search type.
-
-
Constructor Detail
-
PackageObjectFactory
public PackageObjectFactory(Set<String> packageNames, ClassLoader moduleClassLoader)
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
public PackageObjectFactory(String packageName, ClassLoader moduleClassLoader)
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 Detail
-
createModule
public Object createModule(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 interfaceModuleFactory
- Parameters:
name
- the name of a class.- Returns:
- the
Object
created by loader. - Throws:
CheckstyleException
- if an error occurs.
-
createFromStandardCheckSet
private Object createFromStandardCheckSet(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.
-
createObjectFromClassPath
private Object createObjectFromClassPath(String name) throws CheckstyleException
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
private Map<String,Set<String>> generateThirdPartyNameToFullModuleName(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 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
private Object createModuleByTryInEachPackage(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.
-
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 initializesupplier
- 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.
-
-