Package com.puppycrawl.tools.checkstyle
Class Main
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.Main
-
public final class Main extends java.lang.Object
Wrapper command line program for the Checker.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classMain.CliOptionsCommand line options.private static classMain.OnlyCheckstyleLoggersFilterLog Filter used in debug mode.(package private) static classMain.OutputFormatEnumeration over the possible output formats.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringCREATE_LISTENER_EXCEPTIONA key pointing to the create listener exception message in the "messages.properties" file.static java.lang.StringERROR_COUNTERA key pointing to the error counter message in the "messages.properties" file.private static intEXIT_WITH_CHECKSTYLE_EXCEPTION_CODEExit code returned when execution finishes withCheckstyleException.private static intEXIT_WITH_INVALID_USER_INPUT_CODEExit code returned when user specified invalid command line arguments.static java.lang.StringLOAD_PROPERTIES_EXCEPTIONA key pointing to the load properties exception message in the "messages.properties" file.private static org.apache.commons.logging.LogLOGLogger for Main.
-
Constructor Summary
Constructors Modifier Constructor Description privateMain()Client code should not create instances of this class, but usemain(String[])method instead.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static AuditListenercreateListener(Main.OutputFormat format, java.nio.file.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 intexecute(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.private static java.util.List<java.io.File>getFilesToProcess(Main.CliOptions options)Determines the files to process.private static java.io.OutputStreamgetOutputStream(java.nio.file.Path outputPath)Create output stream or return System.outprivate static AutomaticBean.OutputStreamOptionsgetOutputStreamOptions(java.nio.file.Path outputPath)CreateAutomaticBean.OutputStreamOptionsfor the given location.private static RootModulegetRootModule(java.lang.String name, java.lang.ClassLoader moduleClassLoader)Creates a new instance of the root module that will control and run Checkstyle.private static ConfigurationgetTreeWalkerConfig(Configuration config)ReturnsTreeWalkermodule configuration.private static java.lang.StringgetVersionString()Returns the version string printed when the user requests version help (--version or -V).private static booleanisPathExcluded(java.lang.String path, java.util.List<java.util.regex.Pattern> patternsToExclude)Checks if a directory/filepathshould be excluded based on if it matches one of the patterns supplied.private static java.util.List<java.io.File>listFiles(java.io.File node, java.util.List<java.util.regex.Pattern> patternsToExclude)Traverses a specified node looking for files to check.private static java.util.PropertiesloadProperties(java.io.File file)Loads properties from a File.static voidmain(java.lang.String... args)Loops over the files specified checking them for errors.private static intrunCheckstyle(Main.CliOptions options, java.util.List<java.io.File> filesToProcess)Executes required Checkstyle actions based on passed parameters.private static intrunCli(Main.CliOptions options, java.util.List<java.io.File> filesToProcess)Do execution of CheckStyle based on Command line options.
-
-
-
Field Detail
-
ERROR_COUNTER
public static final java.lang.String ERROR_COUNTER
A key pointing to the error counter message in the "messages.properties" file.- See Also:
- Constant Field Values
-
LOAD_PROPERTIES_EXCEPTION
public static final java.lang.String LOAD_PROPERTIES_EXCEPTION
A key pointing to the load properties exception message in the "messages.properties" file.- See Also:
- Constant Field Values
-
CREATE_LISTENER_EXCEPTION
public static final java.lang.String CREATE_LISTENER_EXCEPTION
A key pointing to the create listener exception message in the "messages.properties" file.- See Also:
- Constant Field Values
-
LOG
private static final org.apache.commons.logging.Log LOG
Logger for Main.
-
EXIT_WITH_INVALID_USER_INPUT_CODE
private static final int EXIT_WITH_INVALID_USER_INPUT_CODE
Exit code returned when user specified invalid command line arguments.- See Also:
- Constant Field Values
-
EXIT_WITH_CHECKSTYLE_EXCEPTION_CODE
private static final int EXIT_WITH_CHECKSTYLE_EXCEPTION_CODE
Exit code returned when execution finishes withCheckstyleException.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Main
private Main()
Client code should not create instances of this class, but usemain(String[])method instead.
-
-
Method Detail
-
main
public static void main(java.lang.String... args) throws java.io.IOException
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:
java.io.IOException- if there is a problem with files access
-
getVersionString
private static java.lang.String 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 java.io.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:
java.io.IOException- if a file could not be read.CheckstyleException- if something happens processing the files.
-
getFilesToProcess
private static java.util.List<java.io.File> getFilesToProcess(Main.CliOptions options)
Determines the files to process.- Parameters:
options- the user-specified options- Returns:
- list of files to process
-
listFiles
private static java.util.List<java.io.File> listFiles(java.io.File node, java.util.List<java.util.regex.Pattern> patternsToExclude)
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
private static boolean isPathExcluded(java.lang.String path, java.util.List<java.util.regex.Pattern> patternsToExclude)
Checks if a directory/filepathshould be excluded based on if it matches one of the patterns supplied.- Parameters:
path- The path of the directory/file to checkpatternsToExclude- The list 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, java.util.List<java.io.File> filesToProcess) throws java.io.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:
java.io.IOException- if a file could not be read.CheckstyleException- if something happens processing the files.
-
runCheckstyle
private static int runCheckstyle(Main.CliOptions options, java.util.List<java.io.File> filesToProcess) throws CheckstyleException, java.io.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:
java.io.IOException- when output file could not be foundCheckstyleException- when properties file could not be loaded
-
loadProperties
private static java.util.Properties loadProperties(java.io.File file) throws CheckstyleException
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(java.lang.String name, java.lang.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
private static Configuration getTreeWalkerConfig(Configuration config)
ReturnsTreeWalkermodule configuration.- Parameters:
config- The configuration object.- Returns:
- The
TreeWalkermodule configuration.
-
createListener
private static AuditListener createListener(Main.OutputFormat format, java.nio.file.Path outputLocation) throws java.io.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:
java.io.IOException- when provided output location is not found
-
getOutputStream
private static java.io.OutputStream getOutputStream(java.nio.file.Path outputPath) throws java.io.IOException
Create output stream or return System.out- Parameters:
outputPath- output location- Returns:
- output stream
- Throws:
java.io.IOException- might happen
-
getOutputStreamOptions
private static AutomaticBean.OutputStreamOptions getOutputStreamOptions(java.nio.file.Path outputPath)
CreateAutomaticBean.OutputStreamOptionsfor the given location.- Parameters:
outputPath- output location- Returns:
- output stream options
-
-