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 class
Main.CliOptions
Helper structure to clear show what is required for Checker to run.
-
Field Summary
Fields Modifier and Type Field Description private static int
EXIT_WITH_CHECKSTYLE_EXCEPTION_CODE
Exit code returned when execution finishes withCheckstyleException
.private static int
HELP_WIDTH
Width of CLI help option.private static org.apache.commons.logging.Log
LOG
Logger for Main.private static java.lang.String
OPTION_C_NAME
Name for the option 'c'.private static java.lang.String
OPTION_CAPITAL_J_NAME
Name for the option '-J'.private static java.lang.String
OPTION_CAPITAL_T_NAME
Name for the option '-T'.private static java.lang.String
OPTION_D_NAME
Name for the option '-d'.private static java.lang.String
OPTION_DEBUG_NAME
Name for the option '--debug'.private static java.lang.String
OPTION_E_NAME
Name for the option 'e'.private static java.lang.String
OPTION_EXCLUDE_NAME
Name for the option '--exclude'.private static java.lang.String
OPTION_EXCLUDE_REGEXP_NAME
Name for the option '--exclude-regexp'.private static java.lang.String
OPTION_EXECUTE_IGNORED_MODULES_NAME
Name for the option '--executeIgnoredModules'.private static java.lang.String
OPTION_F_NAME
Name for the option 'f'.private static java.lang.String
OPTION_J_NAME
Name for the option '-j'.private static java.lang.String
OPTION_JAVADOC_TREE_NAME
Name for the option '--javadocTree'.private static java.lang.String
OPTION_O_NAME
Name for the option 'o'.private static java.lang.String
OPTION_P_NAME
Name for the option 'p'.private static java.lang.String
OPTION_T_NAME
Name for the option 't'.private static java.lang.String
OPTION_TREE_COMMENT_NAME
Name for the option '--treeWithComments'.private static java.lang.String
OPTION_TREE_JAVADOC_NAME
Name for the option '--treeWithJavadoc'.private static java.lang.String
OPTION_TREE_NAME
Name for the option '--tree'.private static java.lang.String
OPTION_V_NAME
Name for the option 'v'.private static java.lang.String
OPTION_X_NAME
Name for the option 'x'.private static java.lang.String
PLAIN_FORMAT_NAME
Name for 'plain' format.private static java.lang.String
XML_FORMAT_NAME
Name for 'xml' format.
-
Constructor Summary
Constructors Modifier Constructor Description private
Main()
Don't create instance of this class, usemain(String[])
method instead.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static org.apache.commons.cli.Options
buildOptions()
Builds and returns list of parameters supported by cli Checkstyle.private static Main.CliOptions
convertCliToPojo(org.apache.commons.cli.CommandLine cmdLine, java.util.List<java.io.File> filesToProcess)
Util method to convert CommandLine type to POJO object.private static AuditListener
createListener(java.lang.String format, java.lang.String outputLocation)
Creates the audit listener.private static java.util.List<java.util.regex.Pattern>
getExclusions(org.apache.commons.cli.CommandLine commandLine)
Gets the list of exclusions provided through the command line argument.private static java.util.List<java.io.File>
getFilesToProcess(java.util.List<java.util.regex.Pattern> patternsToExclude, java.lang.String... filesToProcess)
Determines the files to process.private static RootModule
getRootModule(java.lang.String name, java.lang.ClassLoader moduleClassLoader)
Creates a new instance of the root module that will control and run Checkstyle.private static boolean
isDirectoryExcluded(java.lang.String path, java.util.List<java.util.regex.Pattern> patternsToExclude)
Checks if a directorypath
should 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.Properties
loadProperties(java.io.File file)
Loads properties from a File.static void
main(java.lang.String... args)
Loops over the files specified checking them for errors.private static org.apache.commons.cli.CommandLine
parseCli(java.lang.String... args)
Parses and executes Checkstyle based on passed arguments.private static void
printUsage()
Prints the usage information.private static int
runCheckstyle(Main.CliOptions cliOptions)
Executes required Checkstyle actions based on passed parameters.private static int
runCli(org.apache.commons.cli.CommandLine commandLine, java.util.List<java.io.File> filesToProcess)
Do execution of CheckStyle based on Command line options.private static java.util.List<java.lang.String>
validateCli(org.apache.commons.cli.CommandLine cmdLine, java.util.List<java.io.File> filesToProcess)
Do validation of Command line options.
-
-
-
Field Detail
-
LOG
private static final org.apache.commons.logging.Log LOG
Logger for Main.
-
HELP_WIDTH
private static final int HELP_WIDTH
Width of CLI help option.- 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
-
OPTION_V_NAME
private static final java.lang.String OPTION_V_NAME
Name for the option 'v'.- See Also:
- Constant Field Values
-
OPTION_C_NAME
private static final java.lang.String OPTION_C_NAME
Name for the option 'c'.- See Also:
- Constant Field Values
-
OPTION_F_NAME
private static final java.lang.String OPTION_F_NAME
Name for the option 'f'.- See Also:
- Constant Field Values
-
OPTION_P_NAME
private static final java.lang.String OPTION_P_NAME
Name for the option 'p'.- See Also:
- Constant Field Values
-
OPTION_O_NAME
private static final java.lang.String OPTION_O_NAME
Name for the option 'o'.- See Also:
- Constant Field Values
-
OPTION_T_NAME
private static final java.lang.String OPTION_T_NAME
Name for the option 't'.- See Also:
- Constant Field Values
-
OPTION_TREE_NAME
private static final java.lang.String OPTION_TREE_NAME
Name for the option '--tree'.- See Also:
- Constant Field Values
-
OPTION_CAPITAL_T_NAME
private static final java.lang.String OPTION_CAPITAL_T_NAME
Name for the option '-T'.- See Also:
- Constant Field Values
-
OPTION_TREE_COMMENT_NAME
private static final java.lang.String OPTION_TREE_COMMENT_NAME
Name for the option '--treeWithComments'.- See Also:
- Constant Field Values
-
OPTION_J_NAME
private static final java.lang.String OPTION_J_NAME
Name for the option '-j'.- See Also:
- Constant Field Values
-
OPTION_JAVADOC_TREE_NAME
private static final java.lang.String OPTION_JAVADOC_TREE_NAME
Name for the option '--javadocTree'.- See Also:
- Constant Field Values
-
OPTION_CAPITAL_J_NAME
private static final java.lang.String OPTION_CAPITAL_J_NAME
Name for the option '-J'.- See Also:
- Constant Field Values
-
OPTION_TREE_JAVADOC_NAME
private static final java.lang.String OPTION_TREE_JAVADOC_NAME
Name for the option '--treeWithJavadoc'.- See Also:
- Constant Field Values
-
OPTION_D_NAME
private static final java.lang.String OPTION_D_NAME
Name for the option '-d'.- See Also:
- Constant Field Values
-
OPTION_DEBUG_NAME
private static final java.lang.String OPTION_DEBUG_NAME
Name for the option '--debug'.- See Also:
- Constant Field Values
-
OPTION_E_NAME
private static final java.lang.String OPTION_E_NAME
Name for the option 'e'.- See Also:
- Constant Field Values
-
OPTION_EXCLUDE_NAME
private static final java.lang.String OPTION_EXCLUDE_NAME
Name for the option '--exclude'.- See Also:
- Constant Field Values
-
OPTION_EXECUTE_IGNORED_MODULES_NAME
private static final java.lang.String OPTION_EXECUTE_IGNORED_MODULES_NAME
Name for the option '--executeIgnoredModules'.- See Also:
- Constant Field Values
-
OPTION_X_NAME
private static final java.lang.String OPTION_X_NAME
Name for the option 'x'.- See Also:
- Constant Field Values
-
OPTION_EXCLUDE_REGEXP_NAME
private static final java.lang.String OPTION_EXCLUDE_REGEXP_NAME
Name for the option '--exclude-regexp'.- See Also:
- Constant Field Values
-
XML_FORMAT_NAME
private static final java.lang.String XML_FORMAT_NAME
Name for 'xml' format.- See Also:
- Constant Field Values
-
PLAIN_FORMAT_NAME
private static final java.lang.String PLAIN_FORMAT_NAME
Name for 'plain' format.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Main
private Main()
Don't create instance of this class, 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
-
parseCli
private static org.apache.commons.cli.CommandLine parseCli(java.lang.String... args) throws org.apache.commons.cli.ParseException
Parses and executes Checkstyle based on passed arguments.- Parameters:
args
- command line parameters- Returns:
- parsed information about passed parameters
- Throws:
org.apache.commons.cli.ParseException
- when passed arguments are not valid
-
getExclusions
private static java.util.List<java.util.regex.Pattern> getExclusions(org.apache.commons.cli.CommandLine commandLine)
Gets the list of exclusions provided through the command line argument.- Parameters:
commandLine
- command line object- Returns:
- List of exclusion patterns.
-
validateCli
private static java.util.List<java.lang.String> validateCli(org.apache.commons.cli.CommandLine cmdLine, java.util.List<java.io.File> filesToProcess)
Do validation of Command line options.- Parameters:
cmdLine
- command line objectfilesToProcess
- List of files to process found from the command line.- Returns:
- list of violations
-
runCli
private static int runCli(org.apache.commons.cli.CommandLine commandLine, java.util.List<java.io.File> filesToProcess) throws java.io.IOException, CheckstyleException
Do execution of CheckStyle based on Command line options.- Parameters:
commandLine
- command line objectfilesToProcess
- List of files to process found from 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.
-
convertCliToPojo
private static Main.CliOptions convertCliToPojo(org.apache.commons.cli.CommandLine cmdLine, java.util.List<java.io.File> filesToProcess)
Util method to convert CommandLine type to POJO object.- Parameters:
cmdLine
- command line objectfilesToProcess
- List of files to process found from the command line.- Returns:
- command line option as POJO object
-
runCheckstyle
private static int runCheckstyle(Main.CliOptions cliOptions) throws CheckstyleException, java.io.FileNotFoundException
Executes required Checkstyle actions based on passed parameters.- Parameters:
cliOptions
- pojo object that contains all options- Returns:
- number of violations of ERROR level
- Throws:
java.io.FileNotFoundException
- when output file could not be foundCheckstyleException
- when properties file could not be loaded
-
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
-
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
-
createListener
private static AuditListener createListener(java.lang.String format, java.lang.String outputLocation) throws java.io.FileNotFoundException
Creates the audit listener.- Parameters:
format
- format of the audit listeneroutputLocation
- the location of output- Returns:
- a fresh new
AuditListener
- Throws:
java.io.FileNotFoundException
- when provided output location is not found
-
getFilesToProcess
private static java.util.List<java.io.File> getFilesToProcess(java.util.List<java.util.regex.Pattern> patternsToExclude, java.lang.String... filesToProcess)
Determines the files to process.- Parameters:
patternsToExclude
- The list of directory patterns to exclude from searching.filesToProcess
- arguments that were not processed yet but shall be- 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 directory patterns to exclude from searching.- Returns:
- found files
-
isDirectoryExcluded
private static boolean isDirectoryExcluded(java.lang.String path, java.util.List<java.util.regex.Pattern> patternsToExclude)
Checks if a directorypath
should be excluded based on if it matches one of the patterns supplied.- Parameters:
path
- The path of the directory to checkpatternsToExclude
- The list of directory patterns to exclude from searching.- Returns:
- True if the directory matches one of the patterns.
-
printUsage
private static void printUsage()
Prints the usage information.
-
buildOptions
private static org.apache.commons.cli.Options buildOptions()
Builds and returns list of parameters supported by cli Checkstyle.- Returns:
- available options
-
-