001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.ant; 021 022import java.io.File; 023import java.io.IOException; 024import java.io.InputStream; 025import java.io.OutputStream; 026import java.nio.file.Files; 027import java.util.ArrayList; 028import java.util.Arrays; 029import java.util.List; 030import java.util.Locale; 031import java.util.Map; 032import java.util.Objects; 033import java.util.Properties; 034import java.util.stream.Collectors; 035 036import org.apache.tools.ant.BuildException; 037import org.apache.tools.ant.DirectoryScanner; 038import org.apache.tools.ant.Project; 039import org.apache.tools.ant.Task; 040import org.apache.tools.ant.taskdefs.LogOutputStream; 041import org.apache.tools.ant.types.EnumeratedAttribute; 042import org.apache.tools.ant.types.FileSet; 043import org.apache.tools.ant.types.Path; 044import org.apache.tools.ant.types.Reference; 045 046import com.puppycrawl.tools.checkstyle.Checker; 047import com.puppycrawl.tools.checkstyle.ConfigurationLoader; 048import com.puppycrawl.tools.checkstyle.DefaultLogger; 049import com.puppycrawl.tools.checkstyle.ModuleFactory; 050import com.puppycrawl.tools.checkstyle.PackageObjectFactory; 051import com.puppycrawl.tools.checkstyle.PropertiesExpander; 052import com.puppycrawl.tools.checkstyle.ThreadModeSettings; 053import com.puppycrawl.tools.checkstyle.XMLLogger; 054import com.puppycrawl.tools.checkstyle.api.AuditListener; 055import com.puppycrawl.tools.checkstyle.api.AutomaticBean; 056import com.puppycrawl.tools.checkstyle.api.CheckstyleException; 057import com.puppycrawl.tools.checkstyle.api.Configuration; 058import com.puppycrawl.tools.checkstyle.api.RootModule; 059import com.puppycrawl.tools.checkstyle.api.SeverityLevel; 060import com.puppycrawl.tools.checkstyle.api.SeverityLevelCounter; 061 062/** 063 * An implementation of a ANT task for calling checkstyle. See the documentation 064 * of the task for usage. 065 */ 066public class CheckstyleAntTask extends Task { 067 068 /** Poor man's enum for an xml formatter. */ 069 private static final String E_XML = "xml"; 070 /** Poor man's enum for an plain formatter. */ 071 private static final String E_PLAIN = "plain"; 072 073 /** Suffix for time string. */ 074 private static final String TIME_SUFFIX = " ms."; 075 076 /** Contains the paths to process. */ 077 private final List<Path> paths = new ArrayList<>(); 078 079 /** Contains the filesets to process. */ 080 private final List<FileSet> fileSets = new ArrayList<>(); 081 082 /** Contains the formatters to log to. */ 083 private final List<Formatter> formatters = new ArrayList<>(); 084 085 /** Contains the Properties to override. */ 086 private final List<Property> overrideProps = new ArrayList<>(); 087 088 /** Class path to locate class files. */ 089 private Path classpath; 090 091 /** Name of file to check. */ 092 private String fileName; 093 094 /** Config file containing configuration. */ 095 private String config; 096 097 /** Whether to fail build on violations. */ 098 private boolean failOnViolation = true; 099 100 /** Property to set on violations. */ 101 private String failureProperty; 102 103 /** The name of the properties file. */ 104 private File properties; 105 106 /** The maximum number of errors that are tolerated. */ 107 private int maxErrors; 108 109 /** The maximum number of warnings that are tolerated. */ 110 private int maxWarnings = Integer.MAX_VALUE; 111 112 /** 113 * Whether to execute ignored modules - some modules may log above 114 * their severity depending on their configuration (e.g. WriteTag) so 115 * need to be included 116 */ 117 private boolean executeIgnoredModules; 118 119 //////////////////////////////////////////////////////////////////////////// 120 // Setters for ANT specific attributes 121 //////////////////////////////////////////////////////////////////////////// 122 123 /** 124 * Tells this task to write failure message to the named property when there 125 * is a violation. 126 * @param propertyName the name of the property to set 127 * in the event of an failure. 128 */ 129 public void setFailureProperty(String propertyName) { 130 failureProperty = propertyName; 131 } 132 133 /** 134 * Sets flag - whether to fail if a violation is found. 135 * @param fail whether to fail if a violation is found 136 */ 137 public void setFailOnViolation(boolean fail) { 138 failOnViolation = fail; 139 } 140 141 /** 142 * Sets the maximum number of errors allowed. Default is 0. 143 * @param maxErrors the maximum number of errors allowed. 144 */ 145 public void setMaxErrors(int maxErrors) { 146 this.maxErrors = maxErrors; 147 } 148 149 /** 150 * Sets the maximum number of warnings allowed. Default is 151 * {@link Integer#MAX_VALUE}. 152 * @param maxWarnings the maximum number of warnings allowed. 153 */ 154 public void setMaxWarnings(int maxWarnings) { 155 this.maxWarnings = maxWarnings; 156 } 157 158 /** 159 * Adds a path. 160 * @param path the path to add. 161 */ 162 public void addPath(Path path) { 163 paths.add(path); 164 } 165 166 /** 167 * Adds set of files (nested fileset attribute). 168 * @param fileSet the file set to add 169 */ 170 public void addFileset(FileSet fileSet) { 171 fileSets.add(fileSet); 172 } 173 174 /** 175 * Add a formatter. 176 * @param formatter the formatter to add for logging. 177 */ 178 public void addFormatter(Formatter formatter) { 179 formatters.add(formatter); 180 } 181 182 /** 183 * Add an override property. 184 * @param property the property to add 185 */ 186 public void addProperty(Property property) { 187 overrideProps.add(property); 188 } 189 190 /** 191 * Set the class path. 192 * @param classpath the path to locate classes 193 */ 194 public void setClasspath(Path classpath) { 195 if (this.classpath == null) { 196 this.classpath = classpath; 197 } 198 else { 199 this.classpath.append(classpath); 200 } 201 } 202 203 /** 204 * Set the class path from a reference defined elsewhere. 205 * @param classpathRef the reference to an instance defining the classpath 206 */ 207 public void setClasspathRef(Reference classpathRef) { 208 createClasspath().setRefid(classpathRef); 209 } 210 211 /** 212 * Creates classpath. 213 * @return a created path for locating classes 214 */ 215 public Path createClasspath() { 216 if (classpath == null) { 217 classpath = new Path(getProject()); 218 } 219 return classpath.createPath(); 220 } 221 222 /** 223 * Sets file to be checked. 224 * @param file the file to be checked 225 */ 226 public void setFile(File file) { 227 fileName = file.getAbsolutePath(); 228 } 229 230 /** 231 * Sets configuration file. 232 * @param configuration the configuration file, URL, or resource to use 233 */ 234 public void setConfig(String configuration) { 235 if (config != null) { 236 throw new BuildException("Attribute 'config' has already been set"); 237 } 238 config = configuration; 239 } 240 241 /** 242 * Sets flag - whether to execute ignored modules. 243 * @param omit whether to execute ignored modules 244 */ 245 public void setExecuteIgnoredModules(boolean omit) { 246 executeIgnoredModules = omit; 247 } 248 249 //////////////////////////////////////////////////////////////////////////// 250 // Setters for Root Module's configuration attributes 251 //////////////////////////////////////////////////////////////////////////// 252 253 /** 254 * Sets a properties file for use instead 255 * of individually setting them. 256 * @param props the properties File to use 257 */ 258 public void setProperties(File props) { 259 properties = props; 260 } 261 262 //////////////////////////////////////////////////////////////////////////// 263 // The doers 264 //////////////////////////////////////////////////////////////////////////// 265 266 @Override 267 public void execute() { 268 final long startTime = System.currentTimeMillis(); 269 270 try { 271 final String version = CheckstyleAntTask.class.getPackage().getImplementationVersion(); 272 273 log("checkstyle version " + version, Project.MSG_VERBOSE); 274 275 // Check for no arguments 276 if (fileName == null 277 && fileSets.isEmpty() 278 && paths.isEmpty()) { 279 throw new BuildException( 280 "Must specify at least one of 'file' or nested 'fileset' or 'path'.", 281 getLocation()); 282 } 283 if (config == null) { 284 throw new BuildException("Must specify 'config'.", getLocation()); 285 } 286 realExecute(version); 287 } 288 finally { 289 final long endTime = System.currentTimeMillis(); 290 log("Total execution took " + (endTime - startTime) + TIME_SUFFIX, 291 Project.MSG_VERBOSE); 292 } 293 } 294 295 /** 296 * Helper implementation to perform execution. 297 * @param checkstyleVersion Checkstyle compile version. 298 */ 299 private void realExecute(String checkstyleVersion) { 300 // Create the root module 301 RootModule rootModule = null; 302 try { 303 rootModule = createRootModule(); 304 305 // setup the listeners 306 final AuditListener[] listeners = getListeners(); 307 for (AuditListener element : listeners) { 308 rootModule.addListener(element); 309 } 310 final SeverityLevelCounter warningCounter = 311 new SeverityLevelCounter(SeverityLevel.WARNING); 312 rootModule.addListener(warningCounter); 313 314 processFiles(rootModule, warningCounter, checkstyleVersion); 315 } 316 finally { 317 if (rootModule != null) { 318 rootModule.destroy(); 319 } 320 } 321 } 322 323 /** 324 * Scans and processes files by means given root module. 325 * @param rootModule Root module to process files 326 * @param warningCounter Root Module's counter of warnings 327 * @param checkstyleVersion Checkstyle compile version 328 */ 329 private void processFiles(RootModule rootModule, final SeverityLevelCounter warningCounter, 330 final String checkstyleVersion) { 331 final long startTime = System.currentTimeMillis(); 332 final List<File> files = getFilesToCheck(); 333 final long endTime = System.currentTimeMillis(); 334 log("To locate the files took " + (endTime - startTime) + TIME_SUFFIX, 335 Project.MSG_VERBOSE); 336 337 log("Running Checkstyle " 338 + Objects.toString(checkstyleVersion, "") 339 + " on " + files.size() 340 + " files", Project.MSG_INFO); 341 log("Using configuration " + config, Project.MSG_VERBOSE); 342 343 final int numErrs; 344 345 try { 346 final long processingStartTime = System.currentTimeMillis(); 347 numErrs = rootModule.process(files); 348 final long processingEndTime = System.currentTimeMillis(); 349 log("To process the files took " + (processingEndTime - processingStartTime) 350 + TIME_SUFFIX, Project.MSG_VERBOSE); 351 } 352 catch (CheckstyleException ex) { 353 throw new BuildException("Unable to process files: " + files, ex); 354 } 355 final int numWarnings = warningCounter.getCount(); 356 final boolean okStatus = numErrs <= maxErrors && numWarnings <= maxWarnings; 357 358 // Handle the return status 359 if (!okStatus) { 360 final String failureMsg = 361 "Got " + numErrs + " errors and " + numWarnings 362 + " warnings."; 363 if (failureProperty != null) { 364 getProject().setProperty(failureProperty, failureMsg); 365 } 366 367 if (failOnViolation) { 368 throw new BuildException(failureMsg, getLocation()); 369 } 370 } 371 } 372 373 /** 374 * Creates new instance of the root module. 375 * @return new instance of the root module 376 */ 377 private RootModule createRootModule() { 378 final RootModule rootModule; 379 try { 380 final Properties props = createOverridingProperties(); 381 final ThreadModeSettings threadModeSettings = 382 ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE; 383 final ConfigurationLoader.IgnoredModulesOptions ignoredModulesOptions; 384 if (executeIgnoredModules) { 385 ignoredModulesOptions = ConfigurationLoader.IgnoredModulesOptions.EXECUTE; 386 } 387 else { 388 ignoredModulesOptions = ConfigurationLoader.IgnoredModulesOptions.OMIT; 389 } 390 391 final Configuration configuration = ConfigurationLoader.loadConfiguration(config, 392 new PropertiesExpander(props), ignoredModulesOptions, threadModeSettings); 393 394 final ClassLoader moduleClassLoader = 395 Checker.class.getClassLoader(); 396 397 final ModuleFactory factory = new PackageObjectFactory( 398 Checker.class.getPackage().getName() + ".", moduleClassLoader); 399 400 rootModule = (RootModule) factory.createModule(configuration.getName()); 401 rootModule.setModuleClassLoader(moduleClassLoader); 402 rootModule.configure(configuration); 403 } 404 catch (final CheckstyleException ex) { 405 throw new BuildException(String.format(Locale.ROOT, "Unable to create Root Module: " 406 + "config {%s}, classpath {%s}.", config, classpath), ex); 407 } 408 return rootModule; 409 } 410 411 /** 412 * Create the Properties object based on the arguments specified 413 * to the ANT task. 414 * @return the properties for property expansion expansion 415 */ 416 private Properties createOverridingProperties() { 417 final Properties returnValue = new Properties(); 418 419 // Load the properties file if specified 420 if (properties != null) { 421 try (InputStream inStream = Files.newInputStream(properties.toPath())) { 422 returnValue.load(inStream); 423 } 424 catch (final IOException ex) { 425 throw new BuildException("Error loading Properties file '" 426 + properties + "'", ex, getLocation()); 427 } 428 } 429 430 // override with Ant properties like ${basedir} 431 final Map<String, Object> antProps = getProject().getProperties(); 432 for (Map.Entry<String, Object> entry : antProps.entrySet()) { 433 final String value = String.valueOf(entry.getValue()); 434 returnValue.setProperty(entry.getKey(), value); 435 } 436 437 // override with properties specified in subelements 438 for (Property p : overrideProps) { 439 returnValue.setProperty(p.getKey(), p.getValue()); 440 } 441 442 return returnValue; 443 } 444 445 /** 446 * Return the list of listeners set in this task. 447 * @return the list of listeners. 448 */ 449 private AuditListener[] getListeners() { 450 final int formatterCount = Math.max(1, formatters.size()); 451 452 final AuditListener[] listeners = new AuditListener[formatterCount]; 453 454 // formatters 455 try { 456 if (formatters.isEmpty()) { 457 final OutputStream debug = new LogOutputStream(this, Project.MSG_DEBUG); 458 final OutputStream err = new LogOutputStream(this, Project.MSG_ERR); 459 listeners[0] = new DefaultLogger(debug, AutomaticBean.OutputStreamOptions.CLOSE, 460 err, AutomaticBean.OutputStreamOptions.CLOSE); 461 } 462 else { 463 for (int i = 0; i < formatterCount; i++) { 464 final Formatter formatter = formatters.get(i); 465 listeners[i] = formatter.createListener(this); 466 } 467 } 468 } 469 catch (IOException ex) { 470 throw new BuildException(String.format(Locale.ROOT, "Unable to create listeners: " 471 + "formatters {%s}.", formatters), ex); 472 } 473 return listeners; 474 } 475 476 /** 477 * Returns the list of files (full path name) to process. 478 * @return the list of files included via the fileName, filesets and paths. 479 */ 480 private List<File> getFilesToCheck() { 481 final List<File> allFiles = new ArrayList<>(); 482 if (fileName != null) { 483 // oops we've got an additional one to process, don't 484 // forget it. No sweat, it's fully resolved via the setter. 485 log("Adding standalone file for audit", Project.MSG_VERBOSE); 486 allFiles.add(new File(fileName)); 487 } 488 489 final List<File> filesFromFileSets = scanFileSets(); 490 allFiles.addAll(filesFromFileSets); 491 492 final List<File> filesFromPaths = scanPaths(); 493 allFiles.addAll(filesFromPaths); 494 495 return allFiles; 496 } 497 498 /** 499 * Retrieves all files from the defined paths. 500 * @return a list of files defined via paths. 501 */ 502 private List<File> scanPaths() { 503 final List<File> allFiles = new ArrayList<>(); 504 505 for (int i = 0; i < paths.size(); i++) { 506 final Path currentPath = paths.get(i); 507 final List<File> pathFiles = scanPath(currentPath, i + 1); 508 allFiles.addAll(pathFiles); 509 } 510 511 return allFiles; 512 } 513 514 /** 515 * Scans the given path and retrieves all files for the given path. 516 * 517 * @param path A path to scan. 518 * @param pathIndex The index of the given path. Used in log messages only. 519 * @return A list of files, extracted from the given path. 520 */ 521 private List<File> scanPath(Path path, int pathIndex) { 522 final String[] resources = path.list(); 523 log(pathIndex + ") Scanning path " + path, Project.MSG_VERBOSE); 524 final List<File> allFiles = new ArrayList<>(); 525 int concreteFilesCount = 0; 526 527 for (String resource : resources) { 528 final File file = new File(resource); 529 if (file.isFile()) { 530 concreteFilesCount++; 531 allFiles.add(file); 532 } 533 else { 534 final DirectoryScanner scanner = new DirectoryScanner(); 535 scanner.setBasedir(file); 536 scanner.scan(); 537 final List<File> scannedFiles = retrieveAllScannedFiles(scanner, pathIndex); 538 allFiles.addAll(scannedFiles); 539 } 540 } 541 542 if (concreteFilesCount > 0) { 543 log(String.format(Locale.ROOT, "%d) Adding %d files from path %s", 544 pathIndex, concreteFilesCount, path), Project.MSG_VERBOSE); 545 } 546 547 return allFiles; 548 } 549 550 /** 551 * Returns the list of files (full path name) to process. 552 * @return the list of files included via the filesets. 553 */ 554 protected List<File> scanFileSets() { 555 final List<File> allFiles = new ArrayList<>(); 556 557 for (int i = 0; i < fileSets.size(); i++) { 558 final FileSet fileSet = fileSets.get(i); 559 final DirectoryScanner scanner = fileSet.getDirectoryScanner(getProject()); 560 final List<File> scannedFiles = retrieveAllScannedFiles(scanner, i); 561 allFiles.addAll(scannedFiles); 562 } 563 564 return allFiles; 565 } 566 567 /** 568 * Retrieves all matched files from the given scanner. 569 * 570 * @param scanner A directory scanner. Note, that {@link DirectoryScanner#scan()} 571 * must be called before calling this method. 572 * @param logIndex A log entry index. Used only for log messages. 573 * @return A list of files, retrieved from the given scanner. 574 */ 575 private List<File> retrieveAllScannedFiles(DirectoryScanner scanner, int logIndex) { 576 final String[] fileNames = scanner.getIncludedFiles(); 577 log(String.format(Locale.ROOT, "%d) Adding %d files from directory %s", 578 logIndex, fileNames.length, scanner.getBasedir()), Project.MSG_VERBOSE); 579 580 return Arrays.stream(fileNames) 581 .map(name -> scanner.getBasedir() + File.separator + name) 582 .map(File::new) 583 .collect(Collectors.toList()); 584 } 585 586 /** 587 * Poor mans enumeration for the formatter types. 588 */ 589 public static class FormatterType extends EnumeratedAttribute { 590 591 /** My possible values. */ 592 private static final String[] VALUES = {E_XML, E_PLAIN}; 593 594 @Override 595 public String[] getValues() { 596 return VALUES.clone(); 597 } 598 599 } 600 601 /** 602 * Details about a formatter to be used. 603 */ 604 public static class Formatter { 605 606 /** The formatter type. */ 607 private FormatterType type; 608 /** The file to output to. */ 609 private File toFile; 610 /** Whether or not the write to the named file. */ 611 private boolean useFile = true; 612 613 /** 614 * Set the type of the formatter. 615 * @param type the type 616 */ 617 public void setType(FormatterType type) { 618 this.type = type; 619 } 620 621 /** 622 * Set the file to output to. 623 * @param destination destination the file to output to 624 */ 625 public void setTofile(File destination) { 626 toFile = destination; 627 } 628 629 /** 630 * Sets whether or not we write to a file if it is provided. 631 * @param use whether not not to use provided file. 632 */ 633 public void setUseFile(boolean use) { 634 useFile = use; 635 } 636 637 /** 638 * Creates a listener for the formatter. 639 * @param task the task running 640 * @return a listener 641 * @throws IOException if an error occurs 642 */ 643 public AuditListener createListener(Task task) throws IOException { 644 final AuditListener listener; 645 if (type != null 646 && E_XML.equals(type.getValue())) { 647 listener = createXmlLogger(task); 648 } 649 else { 650 listener = createDefaultLogger(task); 651 } 652 return listener; 653 } 654 655 /** 656 * Creates default logger. 657 * @param task the task to possibly log to 658 * @return a DefaultLogger instance 659 * @throws IOException if an error occurs 660 */ 661 private AuditListener createDefaultLogger(Task task) 662 throws IOException { 663 final AuditListener defaultLogger; 664 if (toFile == null || !useFile) { 665 defaultLogger = new DefaultLogger( 666 new LogOutputStream(task, Project.MSG_DEBUG), 667 AutomaticBean.OutputStreamOptions.CLOSE, 668 new LogOutputStream(task, Project.MSG_ERR), 669 AutomaticBean.OutputStreamOptions.CLOSE 670 ); 671 } 672 else { 673 final OutputStream infoStream = Files.newOutputStream(toFile.toPath()); 674 defaultLogger = 675 new DefaultLogger(infoStream, AutomaticBean.OutputStreamOptions.CLOSE, 676 infoStream, AutomaticBean.OutputStreamOptions.NONE); 677 } 678 return defaultLogger; 679 } 680 681 /** 682 * Creates XML logger. 683 * @param task the task to possibly log to 684 * @return an XMLLogger instance 685 * @throws IOException if an error occurs 686 */ 687 private AuditListener createXmlLogger(Task task) throws IOException { 688 final AuditListener xmlLogger; 689 if (toFile == null || !useFile) { 690 xmlLogger = new XMLLogger(new LogOutputStream(task, Project.MSG_INFO), 691 AutomaticBean.OutputStreamOptions.CLOSE); 692 } 693 else { 694 xmlLogger = new XMLLogger(Files.newOutputStream(toFile.toPath()), 695 AutomaticBean.OutputStreamOptions.CLOSE); 696 } 697 return xmlLogger; 698 } 699 700 } 701 702 /** 703 * Represents a property that consists of a key and value. 704 */ 705 public static class Property { 706 707 /** The property key. */ 708 private String key; 709 /** The property value. */ 710 private String value; 711 712 /** 713 * Gets key. 714 * @return the property key 715 */ 716 public String getKey() { 717 return key; 718 } 719 720 /** 721 * Sets key. 722 * @param key sets the property key 723 */ 724 public void setKey(String key) { 725 this.key = key; 726 } 727 728 /** 729 * Gets value. 730 * @return the property value 731 */ 732 public String getValue() { 733 return value; 734 } 735 736 /** 737 * Sets value. 738 * @param value set the property value 739 */ 740 public void setValue(String value) { 741 this.value = value; 742 } 743 744 /** 745 * Sets the property value from a File. 746 * @param file set the property value from a File 747 */ 748 public void setFile(File file) { 749 value = file.getAbsolutePath(); 750 } 751 752 } 753 754 /** Represents a custom listener. */ 755 public static class Listener { 756 757 /** Class name of the listener class. */ 758 private String className; 759 760 /** 761 * Gets class name. 762 * @return the class name 763 */ 764 public String getClassname() { 765 return className; 766 } 767 768 /** 769 * Sets class name. 770 * @param name set the class name 771 */ 772 public void setClassname(String name) { 773 className = name; 774 } 775 776 } 777 778}