You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CheckstyleExecutor.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2009 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * Sonar is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * Sonar is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.plugins.checkstyle;
  21. import com.puppycrawl.tools.checkstyle.Checker;
  22. import com.puppycrawl.tools.checkstyle.PackageNamesLoader;
  23. import com.puppycrawl.tools.checkstyle.XMLLogger;
  24. import org.apache.commons.io.IOUtils;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import org.sonar.api.BatchExtension;
  28. import org.sonar.api.batch.ProjectClasspath;
  29. import org.sonar.api.utils.TimeProfiler;
  30. import java.io.File;
  31. import java.io.FileOutputStream;
  32. import java.io.OutputStream;
  33. import java.net.URLClassLoader;
  34. public class CheckstyleExecutor implements BatchExtension {
  35. private static Logger LOG = LoggerFactory.getLogger(CheckstyleExecutor.class);
  36. private CheckstyleConfiguration configuration;
  37. private URLClassLoader projectClassloader;
  38. private CheckstyleAuditListener listener;
  39. public CheckstyleExecutor(CheckstyleConfiguration configuration, CheckstyleAuditListener listener, ProjectClasspath classpath) {
  40. this.configuration = configuration;
  41. this.listener = listener;
  42. this.projectClassloader = classpath.getClassloader();
  43. }
  44. CheckstyleExecutor(CheckstyleConfiguration configuration, CheckstyleAuditListener listener, URLClassLoader projectClassloader) {
  45. this.configuration = configuration;
  46. this.listener = listener;
  47. this.projectClassloader = projectClassloader;
  48. }
  49. /**
  50. * Execute Checkstyle and return the generated XML report.
  51. */
  52. public void execute() {
  53. TimeProfiler profiler = new TimeProfiler().start("Execute Checkstyle " + CheckstyleVersion.getVersion());
  54. ClassLoader initialClassLoader = Thread.currentThread().getContextClassLoader();
  55. Thread.currentThread().setContextClassLoader(PackageNamesLoader.class.getClassLoader());
  56. Checker checker = null;
  57. OutputStream xmlOutput = null;
  58. try {
  59. checker = new Checker();
  60. checker.setClassloader(projectClassloader);
  61. checker.setModuleClassLoader(Thread.currentThread().getContextClassLoader());
  62. checker.addListener(listener);
  63. File xmlReport = configuration.getTargetXMLReport();
  64. if (xmlReport != null) {
  65. LOG.info("Checkstyle output report: " + xmlReport.getAbsolutePath());
  66. xmlOutput = new FileOutputStream(xmlReport);
  67. checker.addListener(new XMLLogger(xmlOutput, true));
  68. }
  69. checker.setCharset(configuration.getCharset().name());
  70. checker.configure(configuration.getCheckstyleConfiguration());
  71. checker.process(configuration.getSourceFiles());
  72. profiler.stop();
  73. } catch (Exception e) {
  74. throw new RuntimeException("Can not execute Checkstyle", e);
  75. } finally {
  76. if (checker != null) {
  77. checker.destroy();
  78. }
  79. IOUtils.closeQuietly(xmlOutput);
  80. Thread.currentThread().setContextClassLoader(initialClassLoader);
  81. }
  82. }
  83. }