Class CheckstyleAntTask

  • All Implemented Interfaces:
    java.lang.Cloneable

    public class CheckstyleAntTask
    extends org.apache.tools.ant.Task
    An implementation of a ANT task for calling checkstyle. See the documentation of the task for usage.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private org.apache.tools.ant.types.Path classpath
      Class path to locate class files.
      private java.lang.String config
      Config file containing configuration.
      private static java.lang.String E_PLAIN
      Poor man's enum for an plain formatter.
      private static java.lang.String E_XML
      Poor man's enum for an xml formatter.
      private boolean executeIgnoredModules
      Whether to execute ignored modules - some modules may log above their severity depending on their configuration (e.g.
      private boolean failOnViolation
      Whether to fail build on violations.
      private java.lang.String failureProperty
      Property to set on violations.
      private java.lang.String fileName
      Name of file to check.
      private java.util.List<org.apache.tools.ant.types.FileSet> fileSets
      Contains the filesets to process.
      private java.util.List<CheckstyleAntTask.Formatter> formatters
      Contains the formatters to log to.
      private int maxErrors
      The maximum number of errors that are tolerated.
      private int maxWarnings
      The maximum number of warnings that are tolerated.
      private java.util.List<CheckstyleAntTask.Property> overrideProps
      Contains the Properties to override.
      private java.util.List<org.apache.tools.ant.types.Path> paths
      Contains the paths to process.
      private java.io.File properties
      The name of the properties file.
      private static java.lang.String TIME_SUFFIX
      Suffix for time string.
      • Fields inherited from class org.apache.tools.ant.Task

        target, taskName, taskType, wrapper
      • Fields inherited from class org.apache.tools.ant.ProjectComponent

        description, location, project
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addFileset​(org.apache.tools.ant.types.FileSet fileSet)
      Adds set of files (nested fileset attribute).
      void addFormatter​(CheckstyleAntTask.Formatter formatter)
      Add a formatter.
      void addPath​(org.apache.tools.ant.types.Path path)
      Adds a path.
      void addProperty​(CheckstyleAntTask.Property property)
      Add an override property.
      org.apache.tools.ant.types.Path createClasspath()
      Creates classpath.
      private java.util.Properties createOverridingProperties()
      Create the Properties object based on the arguments specified to the ANT task.
      private RootModule createRootModule()
      Creates new instance of the root module.
      void execute()  
      private java.util.List<java.io.File> getFilesToCheck()
      Returns the list of files (full path name) to process.
      private AuditListener[] getListeners()
      Return the list of listeners set in this task.
      private void processFiles​(RootModule rootModule, SeverityLevelCounter warningCounter, java.lang.String checkstyleVersion)
      Scans and processes files by means given root module.
      private void realExecute​(java.lang.String checkstyleVersion)
      Helper implementation to perform execution.
      private java.util.List<java.io.File> retrieveAllScannedFiles​(org.apache.tools.ant.DirectoryScanner scanner, int logIndex)
      Retrieves all matched files from the given scanner.
      protected java.util.List<java.io.File> scanFileSets()
      Returns the list of files (full path name) to process.
      private java.util.List<java.io.File> scanPath​(org.apache.tools.ant.types.Path path, int pathIndex)
      Scans the given path and retrieves all files for the given path.
      private java.util.List<java.io.File> scanPaths()
      Retrieves all files from the defined paths.
      void setClasspath​(org.apache.tools.ant.types.Path classpath)
      Set the class path.
      void setClasspathRef​(org.apache.tools.ant.types.Reference classpathRef)
      Set the class path from a reference defined elsewhere.
      void setConfig​(java.lang.String configuration)
      Sets configuration file.
      void setExecuteIgnoredModules​(boolean omit)
      Sets flag - whether to execute ignored modules.
      void setFailOnViolation​(boolean fail)
      Sets flag - whether to fail if a violation is found.
      void setFailureProperty​(java.lang.String propertyName)
      Tells this task to write failure message to the named property when there is a violation.
      void setFile​(java.io.File file)
      Sets file to be checked.
      void setMaxErrors​(int maxErrors)
      Sets the maximum number of errors allowed.
      void setMaxWarnings​(int maxWarnings)
      Sets the maximum number of warnings allowed.
      void setProperties​(java.io.File props)
      Sets a properties file for use instead of individually setting them.
      • Methods inherited from class org.apache.tools.ant.Task

        bindToOwner, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, getTaskType, getWrapper, handleErrorFlush, handleErrorOutput, handleFlush, handleInput, handleOutput, init, isInvalid, log, log, log, log, maybeConfigure, perform, reconfigure, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName, setTaskType
      • Methods inherited from class org.apache.tools.ant.ProjectComponent

        clone, getDescription, getLocation, getProject, setDescription, setLocation, setProject
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • paths

        private final java.util.List<org.apache.tools.ant.types.Path> paths
        Contains the paths to process.
      • fileSets

        private final java.util.List<org.apache.tools.ant.types.FileSet> fileSets
        Contains the filesets to process.
      • classpath

        private org.apache.tools.ant.types.Path classpath
        Class path to locate class files.
      • fileName

        private java.lang.String fileName
        Name of file to check.
      • config

        private java.lang.String config
        Config file containing configuration.
      • failOnViolation

        private boolean failOnViolation
        Whether to fail build on violations.
      • failureProperty

        private java.lang.String failureProperty
        Property to set on violations.
      • properties

        private java.io.File properties
        The name of the properties file.
      • maxErrors

        private int maxErrors
        The maximum number of errors that are tolerated.
      • maxWarnings

        private int maxWarnings
        The maximum number of warnings that are tolerated.
      • executeIgnoredModules

        private boolean executeIgnoredModules
        Whether to execute ignored modules - some modules may log above their severity depending on their configuration (e.g. WriteTag) so need to be included
    • Method Detail

      • setFailureProperty

        public void setFailureProperty​(java.lang.String propertyName)
        Tells this task to write failure message to the named property when there is a violation.
        Parameters:
        propertyName - the name of the property to set in the event of an failure.
      • setFailOnViolation

        public void setFailOnViolation​(boolean fail)
        Sets flag - whether to fail if a violation is found.
        Parameters:
        fail - whether to fail if a violation is found
      • setMaxErrors

        public void setMaxErrors​(int maxErrors)
        Sets the maximum number of errors allowed. Default is 0.
        Parameters:
        maxErrors - the maximum number of errors allowed.
      • setMaxWarnings

        public void setMaxWarnings​(int maxWarnings)
        Sets the maximum number of warnings allowed. Default is Integer.MAX_VALUE.
        Parameters:
        maxWarnings - the maximum number of warnings allowed.
      • addPath

        public void addPath​(org.apache.tools.ant.types.Path path)
        Adds a path.
        Parameters:
        path - the path to add.
      • addFileset

        public void addFileset​(org.apache.tools.ant.types.FileSet fileSet)
        Adds set of files (nested fileset attribute).
        Parameters:
        fileSet - the file set to add
      • setClasspath

        public void setClasspath​(org.apache.tools.ant.types.Path classpath)
        Set the class path.
        Parameters:
        classpath - the path to locate classes
      • setClasspathRef

        public void setClasspathRef​(org.apache.tools.ant.types.Reference classpathRef)
        Set the class path from a reference defined elsewhere.
        Parameters:
        classpathRef - the reference to an instance defining the classpath
      • createClasspath

        public org.apache.tools.ant.types.Path createClasspath()
        Creates classpath.
        Returns:
        a created path for locating classes
      • setFile

        public void setFile​(java.io.File file)
        Sets file to be checked.
        Parameters:
        file - the file to be checked
      • setConfig

        public void setConfig​(java.lang.String configuration)
        Sets configuration file.
        Parameters:
        configuration - the configuration file, URL, or resource to use
        Throws:
        org.apache.tools.ant.BuildException - when config was already set
      • setExecuteIgnoredModules

        public void setExecuteIgnoredModules​(boolean omit)
        Sets flag - whether to execute ignored modules.
        Parameters:
        omit - whether to execute ignored modules
      • setProperties

        public void setProperties​(java.io.File props)
        Sets a properties file for use instead of individually setting them.
        Parameters:
        props - the properties File to use
      • execute

        public void execute()
        Overrides:
        execute in class org.apache.tools.ant.Task
      • realExecute

        private void realExecute​(java.lang.String checkstyleVersion)
        Helper implementation to perform execution.
        Parameters:
        checkstyleVersion - Checkstyle compile version.
      • processFiles

        private void processFiles​(RootModule rootModule,
                                  SeverityLevelCounter warningCounter,
                                  java.lang.String checkstyleVersion)
        Scans and processes files by means given root module.
        Parameters:
        rootModule - Root module to process files
        warningCounter - Root Module's counter of warnings
        checkstyleVersion - Checkstyle compile version
        Throws:
        org.apache.tools.ant.BuildException - if the files could not be processed, or if the build failed due to violations.
      • createRootModule

        private RootModule createRootModule()
        Creates new instance of the root module.
        Returns:
        new instance of the root module
        Throws:
        org.apache.tools.ant.BuildException - if the root module could not be created.
      • createOverridingProperties

        private java.util.Properties createOverridingProperties()
        Create the Properties object based on the arguments specified to the ANT task.
        Returns:
        the properties for property expansion expansion
        Throws:
        org.apache.tools.ant.BuildException - if the properties file could not be loaded.
      • getListeners

        private AuditListener[] getListeners()
        Return the list of listeners set in this task.
        Returns:
        the list of listeners.
        Throws:
        org.apache.tools.ant.BuildException - if the listeners could not be created.
      • getFilesToCheck

        private java.util.List<java.io.File> getFilesToCheck()
        Returns the list of files (full path name) to process.
        Returns:
        the list of files included via the fileName, filesets and paths.
      • scanPaths

        private java.util.List<java.io.File> scanPaths()
        Retrieves all files from the defined paths.
        Returns:
        a list of files defined via paths.
      • scanPath

        private java.util.List<java.io.File> scanPath​(org.apache.tools.ant.types.Path path,
                                                      int pathIndex)
        Scans the given path and retrieves all files for the given path.
        Parameters:
        path - A path to scan.
        pathIndex - The index of the given path. Used in log messages only.
        Returns:
        A list of files, extracted from the given path.
      • scanFileSets

        protected java.util.List<java.io.File> scanFileSets()
        Returns the list of files (full path name) to process.
        Returns:
        the list of files included via the filesets.
      • retrieveAllScannedFiles

        private java.util.List<java.io.File> retrieveAllScannedFiles​(org.apache.tools.ant.DirectoryScanner scanner,
                                                                     int logIndex)
        Retrieves all matched files from the given scanner.
        Parameters:
        scanner - A directory scanner. Note, that DirectoryScanner.scan() must be called before calling this method.
        logIndex - A log entry index. Used only for log messages.
        Returns:
        A list of files, retrieved from the given scanner.