コード例 #1
0
 public static List<DataSource> getApplicableFiles(
     PMDConfiguration configuration, Set<Language> languages) {
   long startFiles = System.nanoTime();
   LanguageFilenameFilter fileSelector = new LanguageFilenameFilter(languages);
   List<DataSource> files = FileUtil.collectFiles(configuration.getInputPaths(), fileSelector);
   long endFiles = System.nanoTime();
   Benchmarker.mark(Benchmark.CollectFiles, endFiles - startFiles, 0);
   return files;
 }
コード例 #2
0
 private static void sortFiles(
     final PMDConfiguration configuration, final List<DataSource> files) {
   if (configuration.isStressTest()) {
     // randomize processing order
     Collections.shuffle(files);
   } else {
     final boolean useShortNames = configuration.isReportShortNames();
     final String inputPaths = configuration.getInputPaths();
     Collections.sort(
         files,
         new Comparator<DataSource>() {
           public int compare(DataSource left, DataSource right) {
             String leftString = left.getNiceFileName(useShortNames, inputPaths);
             String rightString = right.getNiceFileName(useShortNames, inputPaths);
             return leftString.compareTo(rightString);
           }
         });
   }
 }
コード例 #3
0
  /**
   * @param languageVersion LanguageVersion
   * @param ruleSet RuleSet
   * @param dataSources List<DataSource>
   * @param results Set<RuleDuration>
   * @param debug boolean
   * @throws PMDException
   * @throws IOException
   */
  private static void stress(
      LanguageVersion languageVersion,
      RuleSet ruleSet,
      List<DataSource> dataSources,
      Set<RuleDuration> results,
      boolean debug)
      throws PMDException, IOException {

    for (Rule rule : ruleSet.getRules()) {
      if (debug) {
        System.out.println("Starting " + rule.getName());
      }

      RuleSet working = new RuleSet();
      working.addRule(rule);
      RuleSets ruleSets = new RuleSets(working);

      PMDConfiguration config = new PMDConfiguration();
      config.setDefaultLanguageVersion(languageVersion);

      RuleContext ctx = new RuleContext();
      long start = System.currentTimeMillis();
      Reader reader = null;
      for (DataSource dataSource : dataSources) {
        reader = new InputStreamReader(dataSource.getInputStream());
        ctx.setSourceCodeFilename(dataSource.getNiceFileName(false, null));
        new SourceCodeProcessor(config).processSourceCode(reader, ruleSets, ctx);
        IOUtil.closeQuietly(reader);
      }
      long end = System.currentTimeMillis();
      long elapsed = end - start;
      results.add(new RuleDuration(elapsed, rule));
      if (debug) {
        System.out.println("Done timing " + rule.getName() + "; elapsed time was " + elapsed);
      }
    }
  }
コード例 #4
0
  private static Set<Language> getApplicableLanguages(
      PMDConfiguration configuration, RuleSets ruleSets) {
    Set<Language> languages = new HashSet<Language>();
    LanguageVersionDiscoverer discoverer = configuration.getLanguageVersionDiscoverer();

    for (Rule rule : ruleSets.getAllRules()) {
      Language language = rule.getLanguage();
      if (languages.contains(language)) continue;
      LanguageVersion version = discoverer.getDefaultLanguageVersion(language);
      if (RuleSet.applies(rule, version)) {
        languages.add(language);
      }
    }
    return languages;
  }
コード例 #5
0
  public static void doPMD(PMDConfiguration configuration) {

    // Load the RuleSets
    long startLoadRules = System.nanoTime();
    RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.getRulesetFactory(configuration);

    RuleSets ruleSets =
        RulesetsFactoryUtils.getRuleSets(
            configuration.getRuleSets(), ruleSetFactory, startLoadRules);
    if (ruleSets == null) return;

    Set<Language> languages = getApplicableLanguages(configuration, ruleSets);
    List<DataSource> files = getApplicableFiles(configuration, languages);

    long reportStart = System.nanoTime();
    try {
      Renderer renderer = configuration.createRenderer();
      List<Renderer> renderers = new LinkedList<Renderer>();
      renderers.add(renderer);

      renderer.setWriter(IOUtil.createWriter(configuration.getReportFile()));
      renderer.start();

      Benchmarker.mark(Benchmark.Reporting, System.nanoTime() - reportStart, 0);

      RuleContext ctx = new RuleContext();

      processFiles(configuration, ruleSetFactory, files, ctx, renderers);

      reportStart = System.nanoTime();
      renderer.end();
      renderer.flush();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
コード例 #6
0
  public static void processFiles(
      final PMDConfiguration configuration,
      final RuleSetFactory ruleSetFactory,
      final List<DataSource> files,
      final RuleContext ctx,
      final List<Renderer> renderers) {

    sortFiles(configuration, files);

    /*
     * Check if multithreaded support is available. ExecutorService can also be
     * disabled if threadCount is not positive, e.g. using the "-threads 0"
     * command line option.
     */
    if (SystemUtils.MT_SUPPORTED && configuration.getThreads() > 0) {
      new MultiThreadProcessor(configuration).processFiles(ruleSetFactory, files, ctx, renderers);
    } else {
      new MonoThreadProcessor(configuration).processFiles(ruleSetFactory, files, ctx, renderers);
    }
  }
コード例 #7
0
 public static PMDConfiguration transformParametersIntoConfiguration(
     PMDParameters params, String path) {
   PMDConfiguration configuration = new PMDConfiguration();
   configuration.setInputPaths(path);
   configuration.setReportFormat("text");
   configuration.setBenchmark(params.isBenchmark());
   configuration.setDebug(params.isDebug());
   configuration.setMinimumPriority(params.getMinimumPriority());
   configuration.setReportFile(params.getReportfile());
   configuration.setReportProperties(params.getProperties());
   configuration.setReportShortNames(params.isShortnames());
   configuration.setRuleSets("java-stanly,java-basic,java-naming");
   configuration.setShowSuppressedViolations(params.isShowsuppressed());
   configuration.setSourceEncoding(params.getEncoding());
   configuration.setStressTest(params.isStress());
   configuration.setSuppressMarker(params.getSuppressmarker());
   configuration.setThreads(params.getThreads());
   for (LanguageVersion language :
       LanguageVersion.findVersionsForLanguageTerseName(params.getLanguage())) {
     configuration
         .getLanguageVersionDiscoverer()
         .setDefaultLanguageVersion(language.getLanguage().getVersion(params.getVersion()));
   }
   try {
     configuration.prependClasspath(params.getAuxclasspath());
   } catch (IOException e) {
     throw new IllegalArgumentException("Invalid auxiliary classpath: " + e.getMessage(), e);
   }
   return configuration;
 }