Package com.puppycrawl.tools.checkstyle
Class Main
java.lang.Object
com.puppycrawl.tools.checkstyle.Main
Wrapper command line program for the Checker.
-
Nested Class Summary
Modifier and TypeClassDescriptionprivate static final class
Command line options.private static final class
Log Filter used in debug mode.(package private) static enum
Enumeration over the possible output formats. -
Field Summary
Modifier and TypeFieldDescriptionstatic final String
A key pointing to the create listener exception message in the "messages.properties" file.static final String
A key pointing to the error counter message in the "messages.properties" file.private static final int
Exit code returned when execution finishes withCheckstyleException
.private static final int
Exit code returned when user specified invalid command line arguments.static final String
A key pointing to the load properties exception message in the "messages.properties" file.private static final org.apache.commons.logging.Log
Logger for Main. -
Constructor Summary
ModifierConstructorDescriptionprivate
Main()
Client code should not create instances of this class, but usemain(String[])
method instead. -
Method Summary
Modifier and TypeMethodDescriptionprivate static AuditListener
createListener
(Main.OutputFormat format, Path outputLocation) This method creates in AuditListener an open stream for validation data, it must be closed byRootModule
(default implementation isChecker
) by callingAuditListener.auditFinished(AuditEvent)
.private static int
execute
(picocli.CommandLine.ParseResult parseResult, Main.CliOptions options) Validates the user input and returns -1 if invalid, otherwise executes CheckStyle and returns the number of violations.getFilesToProcess
(Main.CliOptions options) Determines the files to process.private static OutputStream
getOutputStream
(Path outputPath) Create output stream or return System.out.private static AbstractAutomaticBean.OutputStreamOptions
getOutputStreamOptions
(Path outputPath) CreateAbstractAutomaticBean.OutputStreamOptions
for the given location.private static RootModule
getRootModule
(String name, ClassLoader moduleClassLoader) Creates a new instance of the root module that will control and run Checkstyle.private static Configuration
getTreeWalkerConfig
(Configuration config) ReturnsTreeWalker
module configuration.private static String
Returns the version string printed when the user requests version help (--version or -V).private static boolean
isPathExcluded
(String path, Iterable<Pattern> patternsToExclude) Checks if a directory/filepath
should be excluded based on if it matches one of the patterns supplied.Traverses a specified node looking for files to check.private static Properties
loadProperties
(File file) Loads properties from a File.static void
Loops over the files specified checking them for errors.private static int
runCheckstyle
(Main.CliOptions options, List<File> filesToProcess) Executes required Checkstyle actions based on passed parameters.private static int
runCli
(Main.CliOptions options, List<File> filesToProcess) Do execution of CheckStyle based on Command line options.
-
Field Details
-
ERROR_COUNTER
A key pointing to the error counter message in the "messages.properties" file.- See Also:
-
LOAD_PROPERTIES_EXCEPTION
A key pointing to the load properties exception message in the "messages.properties" file.- See Also:
-
CREATE_LISTENER_EXCEPTION
A key pointing to the create listener exception message in the "messages.properties" file.- See Also:
-
LOG
Logger for Main. -
EXIT_WITH_INVALID_USER_INPUT_CODE
Exit code returned when user specified invalid command line arguments.- See Also:
-
EXIT_WITH_CHECKSTYLE_EXCEPTION_CODE
Exit code returned when execution finishes withCheckstyleException
.- See Also:
-
-
Constructor Details
-
Main
private Main()Client code should not create instances of this class, but usemain(String[])
method instead.
-
-
Method Details
-
main
Loops over the files specified checking them for errors. The exit code is the number of errors found in all the files.- Parameters:
args
- the command line arguments.- Throws:
IOException
- if there is a problem with files access
-
getVersionString
Returns the version string printed when the user requests version help (--version or -V).- Returns:
- a version string based on the package implementation version
-
execute
private static int execute(picocli.CommandLine.ParseResult parseResult, Main.CliOptions options) throws IOException, CheckstyleException Validates the user input and returns -1 if invalid, otherwise executes CheckStyle and returns the number of violations.- Parameters:
parseResult
- generic access to options and parameters found on the command lineoptions
- encapsulates options and parameters specified on the command line- Returns:
- number of violations
- Throws:
IOException
- if a file could not be read.CheckstyleException
- if something happens processing the files.
-
getFilesToProcess
Determines the files to process.- Parameters:
options
- the user-specified options- Returns:
- list of files to process
-
listFiles
Traverses a specified node looking for files to check. Found files are added to a specified list. Subdirectories are also traversed.- Parameters:
node
- the node to processpatternsToExclude
- The list of patterns to exclude from searching or being added as files.- Returns:
- found files
-
isPathExcluded
Checks if a directory/filepath
should be excluded based on if it matches one of the patterns supplied.- Parameters:
path
- The path of the directory/file to checkpatternsToExclude
- The collection of patterns to exclude from searching or being added as files.- Returns:
- True if the directory/file matches one of the patterns.
-
runCli
private static int runCli(Main.CliOptions options, List<File> filesToProcess) throws IOException, CheckstyleException Do execution of CheckStyle based on Command line options.- Parameters:
options
- user-specified optionsfilesToProcess
- the list of files whose style to check- Returns:
- number of violations
- Throws:
IOException
- if a file could not be read.CheckstyleException
- if something happens processing the files.
-
runCheckstyle
private static int runCheckstyle(Main.CliOptions options, List<File> filesToProcess) throws CheckstyleException, IOException Executes required Checkstyle actions based on passed parameters.- Parameters:
options
- user-specified optionsfilesToProcess
- the list of files whose style to check- Returns:
- number of violations of ERROR level
- Throws:
IOException
- when output file could not be foundCheckstyleException
- when properties file could not be loaded
-
loadProperties
Loads properties from a File.- Parameters:
file
- the properties file- Returns:
- the properties in file
- Throws:
CheckstyleException
- when could not load properties file
-
getRootModule
private static RootModule getRootModule(String name, ClassLoader moduleClassLoader) throws CheckstyleException Creates a new instance of the root module that will control and run Checkstyle.- Parameters:
name
- The name of the module. This will either be a short name that will have to be found or the complete package name.moduleClassLoader
- Class loader used to load the root module.- Returns:
- The new instance of the root module.
- Throws:
CheckstyleException
- if no module can be instantiated from name
-
getTreeWalkerConfig
ReturnsTreeWalker
module configuration.- Parameters:
config
- The configuration object.- Returns:
- The
TreeWalker
module configuration.
-
createListener
private static AuditListener createListener(Main.OutputFormat format, Path outputLocation) throws IOException This method creates in AuditListener an open stream for validation data, it must be closed byRootModule
(default implementation isChecker
) by callingAuditListener.auditFinished(AuditEvent)
.- Parameters:
format
- format of the audit listeneroutputLocation
- the location of output- Returns:
- a fresh new
AuditListener
- Throws:
IOException
- when provided output location is not found
-
getOutputStream
Create output stream or return System.out.- Parameters:
outputPath
- output location- Returns:
- output stream
- Throws:
IOException
- might happen
-
getOutputStreamOptions
CreateAbstractAutomaticBean.OutputStreamOptions
for the given location.- Parameters:
outputPath
- output location- Returns:
- output stream options
-