Package com.puppycrawl.tools.checkstyle
Class Checker
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.Checker
-
- All Implemented Interfaces:
Configurable
,Contextualizable
,MessageDispatcher
,RootModule
public class Checker extends AutomaticBean implements MessageDispatcher, RootModule
This class provides the functionality to check a set of files.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.String
basedir
The basedir to strip off in file names.private BeforeExecutionFileFilterSet
beforeExecutionFileFilters
The audit event before execution file filters.private PropertyCacheFile
cache
Cache file.private java.lang.String
charset
Name of a charset.private Context
childContext
The context of all child components.private java.lang.ClassLoader
classLoader
Class loader to resolve classes with.private SeverityLevelCounter
counter
Maintains error count.static java.lang.String
EXCEPTION_MSG
Message to use when an exception occurs and should be printed as a violation.private java.lang.String[]
fileExtensions
The file extensions that are accepted.private java.util.List<FileSetCheck>
fileSetChecks
Vector of fileset checks.private FilterSet
filters
The audit event filters.private boolean
haltOnException
Controls whether exceptions should halt execution or not.private java.util.List<AuditListener>
listeners
Vector of listeners.private java.lang.String
localeCountry
Locale country to report messages .private java.lang.String
localeLanguage
Locale language to report messages .private static org.apache.commons.logging.Log
LOG
Logger for Checker.private java.lang.ClassLoader
moduleClassLoader
The classloader used for loading Checkstyle module classes.private ModuleFactory
moduleFactory
The factory for instantiating submodules.private SeverityLevel
severityLevel
The severity level of any violations found by submodules.
-
Constructor Summary
Constructors Constructor Description Checker()
Creates a newChecker
instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private boolean
acceptFileStarted(java.lang.String fileName)
Check if all before execution file filters accept starting the file.void
addBeforeExecutionFileFilter(BeforeExecutionFileFilter filter)
Adds a before execution file filter to the end of the event chain.void
addFileSetCheck(FileSetCheck fileSetCheck)
Adds a FileSetCheck to the list of FileSetChecks that is executed in process().void
addFilter(Filter filter)
Adds a filter to the end of the audit event filter chain.void
addListener(AuditListener listener)
Add the listener that will be used to receive events from the audit.void
clearCache()
Clears the cache.void
destroy()
Cleans up the object.void
finishLocalSetup()
Provides a hook to finish the part of this component's setup that was not handled by the bean introspection.private void
fireAuditFinished()
Notify all listeners about the audit end.private void
fireAuditStarted()
Notify all listeners about the audit start.void
fireErrors(java.lang.String fileName, java.util.SortedSet<LocalizedMessage> errors)
Notify all listeners about the errors in a file.void
fireFileFinished(java.lang.String fileName)
Notify all listeners about the end of a file audit.void
fireFileStarted(java.lang.String fileName)
Notify all listeners about the beginning of a file audit.private java.util.Set<java.lang.String>
getExternalResourceLocations()
Returns a set of external configuration resource locations which are used by all file set checks and filters.int
process(java.util.List<java.io.File> files)
Processes a set of files.private java.util.SortedSet<LocalizedMessage>
processFile(java.io.File file)
Processes a file with all FileSetChecks.private void
processFiles(java.util.List<java.io.File> files)
Processes a list of files with all FileSetChecks.void
removeBeforeExecutionFileFilter(BeforeExecutionFileFilter filter)
Removes before execution file filter.void
removeFilter(Filter filter)
Removes filter.void
removeListener(AuditListener listener)
Removes a given listener.void
setBasedir(java.lang.String basedir)
Sets base directory.void
setCacheFile(java.lang.String fileName)
Sets cache file.void
setCharset(java.lang.String charset)
Sets a named charset.void
setClassLoader(java.lang.ClassLoader classLoader)
Sets the classloader that is used to contextualize fileset checks.void
setFileExtensions(java.lang.String... extensions)
Sets the file extensions that identify the files that pass the filter of this FileSetCheck.void
setHaltOnException(boolean haltOnException)
Sets the field haltOnException.void
setLocaleCountry(java.lang.String localeCountry)
Sets locale country.void
setLocaleLanguage(java.lang.String localeLanguage)
Sets locale language.void
setModuleClassLoader(java.lang.ClassLoader moduleClassLoader)
Sets the classloader used to load Checkstyle core and custom module classes when the module tree is being built up.void
setModuleFactory(ModuleFactory moduleFactory)
Sets the factory for creating submodules.void
setSeverity(java.lang.String severity)
Sets the severity level.protected void
setupChild(Configuration childConf)
Called by configure() for every child of this component's Configuration.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.puppycrawl.tools.checkstyle.api.Configurable
configure
-
-
-
-
Field Detail
-
EXCEPTION_MSG
public static final java.lang.String EXCEPTION_MSG
Message to use when an exception occurs and should be printed as a violation.- See Also:
- Constant Field Values
-
LOG
private static final org.apache.commons.logging.Log LOG
Logger for Checker.
-
counter
private final SeverityLevelCounter counter
Maintains error count.
-
listeners
private final java.util.List<AuditListener> listeners
Vector of listeners.
-
fileSetChecks
private final java.util.List<FileSetCheck> fileSetChecks
Vector of fileset checks.
-
beforeExecutionFileFilters
private final BeforeExecutionFileFilterSet beforeExecutionFileFilters
The audit event before execution file filters.
-
filters
private final FilterSet filters
The audit event filters.
-
classLoader
private java.lang.ClassLoader classLoader
Class loader to resolve classes with.
-
basedir
private java.lang.String basedir
The basedir to strip off in file names.
-
localeCountry
private java.lang.String localeCountry
Locale country to report messages .
-
localeLanguage
private java.lang.String localeLanguage
Locale language to report messages .
-
moduleFactory
private ModuleFactory moduleFactory
The factory for instantiating submodules.
-
moduleClassLoader
private java.lang.ClassLoader moduleClassLoader
The classloader used for loading Checkstyle module classes.
-
childContext
private Context childContext
The context of all child components.
-
fileExtensions
private java.lang.String[] fileExtensions
The file extensions that are accepted.
-
severityLevel
private SeverityLevel severityLevel
The severity level of any violations found by submodules. The value of this property is passed to submodules via contextualize().Note: Since the Checker is merely a container for modules it does not make sense to implement logging functionality here. Consequently Checker does not extend AbstractViolationReporter, leading to a bit of duplicated code for severity level setting.
-
charset
private java.lang.String charset
Name of a charset.
-
cache
private PropertyCacheFile cache
Cache file.
-
haltOnException
private boolean haltOnException
Controls whether exceptions should halt execution or not.
-
-
Method Detail
-
setCacheFile
public void setCacheFile(java.lang.String fileName) throws java.io.IOException
Sets cache file.- Parameters:
fileName
- the cache file.- Throws:
java.io.IOException
- if there are some problems with file loading.
-
removeBeforeExecutionFileFilter
public void removeBeforeExecutionFileFilter(BeforeExecutionFileFilter filter)
Removes before execution file filter.- Parameters:
filter
- before execution file filter to remove.
-
removeFilter
public void removeFilter(Filter filter)
Removes filter.- Parameters:
filter
- filter to remove.
-
destroy
public void destroy()
Description copied from interface:RootModule
Cleans up the object.- Specified by:
destroy
in interfaceRootModule
-
removeListener
public void removeListener(AuditListener listener)
Removes a given listener.- Parameters:
listener
- a listener to remove
-
setBasedir
public void setBasedir(java.lang.String basedir)
Sets base directory.- Parameters:
basedir
- the base directory to strip off in file names
-
process
public int process(java.util.List<java.io.File> files) throws CheckstyleException
Description copied from interface:RootModule
Processes a set of files. Once this is done, it is highly recommended to call for the destroy method to close and remove the listeners.- Specified by:
process
in interfaceRootModule
- Parameters:
files
- the list of files to be audited.- Returns:
- the total number of errors found
- Throws:
CheckstyleException
- if error condition within Checkstyle occurs- See Also:
RootModule.destroy()
-
getExternalResourceLocations
private java.util.Set<java.lang.String> getExternalResourceLocations()
Returns a set of external configuration resource locations which are used by all file set checks and filters.- Returns:
- a set of external configuration resource locations which are used by all file set checks and filters.
-
fireAuditStarted
private void fireAuditStarted()
Notify all listeners about the audit start.
-
fireAuditFinished
private void fireAuditFinished()
Notify all listeners about the audit end.
-
processFiles
private void processFiles(java.util.List<java.io.File> files) throws CheckstyleException
Processes a list of files with all FileSetChecks.- Parameters:
files
- a list of files to process.- Throws:
CheckstyleException
- if error condition within Checkstyle occurs.
-
processFile
private java.util.SortedSet<LocalizedMessage> processFile(java.io.File file) throws CheckstyleException
Processes a file with all FileSetChecks.- Parameters:
file
- a file to process.- Returns:
- a sorted set of messages to be logged.
- Throws:
CheckstyleException
- if error condition within Checkstyle occurs.
-
acceptFileStarted
private boolean acceptFileStarted(java.lang.String fileName)
Check if all before execution file filters accept starting the file.- Parameters:
fileName
- the file to be audited- Returns:
true
if the file is accepted.
-
fireFileStarted
public void fireFileStarted(java.lang.String fileName)
Notify all listeners about the beginning of a file audit.- Specified by:
fireFileStarted
in interfaceMessageDispatcher
- Parameters:
fileName
- the file to be audited
-
fireErrors
public void fireErrors(java.lang.String fileName, java.util.SortedSet<LocalizedMessage> errors)
Notify all listeners about the errors in a file.- Specified by:
fireErrors
in interfaceMessageDispatcher
- Parameters:
fileName
- the audited fileerrors
- the audit errors from the file
-
fireFileFinished
public void fireFileFinished(java.lang.String fileName)
Notify all listeners about the end of a file audit.- Specified by:
fireFileFinished
in interfaceMessageDispatcher
- Parameters:
fileName
- the audited file
-
finishLocalSetup
public void finishLocalSetup() throws CheckstyleException
Description copied from class:AutomaticBean
Provides a hook to finish the part of this component's setup that was not handled by the bean introspection.The default implementation does nothing.
- Overrides:
finishLocalSetup
in classAutomaticBean
- Throws:
CheckstyleException
- if there is a configuration error.
-
setupChild
protected void setupChild(Configuration childConf) throws CheckstyleException
Description copied from class:AutomaticBean
Called by configure() for every child of this component's Configuration.The default implementation throws
CheckstyleException
ifchildConf
isnull
because it doesn't support children. It must be overridden to validate and support children that are wanted.- Overrides:
setupChild
in classAutomaticBean
- Parameters:
childConf
- a child of this component's Configuration- Throws:
CheckstyleException
- if there is a configuration error.- See Also:
Configuration.getChildren()
-
addFileSetCheck
public void addFileSetCheck(FileSetCheck fileSetCheck)
Adds a FileSetCheck to the list of FileSetChecks that is executed in process().- Parameters:
fileSetCheck
- the additional FileSetCheck
-
addBeforeExecutionFileFilter
public void addBeforeExecutionFileFilter(BeforeExecutionFileFilter filter)
Adds a before execution file filter to the end of the event chain.- Parameters:
filter
- the additional filter
-
addFilter
public void addFilter(Filter filter)
Adds a filter to the end of the audit event filter chain.- Parameters:
filter
- the additional filter
-
addListener
public final void addListener(AuditListener listener)
Description copied from interface:RootModule
Add the listener that will be used to receive events from the audit.- Specified by:
addListener
in interfaceRootModule
- Parameters:
listener
- the nosy thing
-
setFileExtensions
public final void setFileExtensions(java.lang.String... extensions)
Sets the file extensions that identify the files that pass the filter of this FileSetCheck.- Parameters:
extensions
- the set of file extensions. A missing initial '.' character of an extension is automatically added.
-
setModuleFactory
public void setModuleFactory(ModuleFactory moduleFactory)
Sets the factory for creating submodules.- Parameters:
moduleFactory
- the factory for creating FileSetChecks
-
setLocaleCountry
public void setLocaleCountry(java.lang.String localeCountry)
Sets locale country.- Parameters:
localeCountry
- the country to report messages
-
setLocaleLanguage
public void setLocaleLanguage(java.lang.String localeLanguage)
Sets locale language.- Parameters:
localeLanguage
- the language to report messages
-
setSeverity
public final void setSeverity(java.lang.String severity)
Sets the severity level. The string should be one of the names defined in theSeverityLevel
class.- Parameters:
severity
- The new severity level- See Also:
SeverityLevel
-
setClassLoader
public final void setClassLoader(java.lang.ClassLoader classLoader)
Sets the classloader that is used to contextualize fileset checks. Some Check implementations will use that classloader to improve the quality of their reports, e.g. to load a class and then analyze it via reflection.- Parameters:
classLoader
- the new classloader
-
setModuleClassLoader
public final void setModuleClassLoader(java.lang.ClassLoader moduleClassLoader)
Description copied from interface:RootModule
Sets the classloader used to load Checkstyle core and custom module classes when the module tree is being built up. If no custom ModuleFactory is being set for the root module then this module classloader must be specified.- Specified by:
setModuleClassLoader
in interfaceRootModule
- Parameters:
moduleClassLoader
- the classloader used to load module classes
-
setCharset
public void setCharset(java.lang.String charset) throws java.io.UnsupportedEncodingException
Sets a named charset.- Parameters:
charset
- the name of a charset- Throws:
java.io.UnsupportedEncodingException
- if charset is unsupported.
-
setHaltOnException
public void setHaltOnException(boolean haltOnException)
Sets the field haltOnException.- Parameters:
haltOnException
- the new value.
-
clearCache
public void clearCache()
Clears the cache.
-
-