Example #1
0
  public static void main(String[] args) {
    CommandLineOptions opts = new CommandLineOptions(args);

    List files;
    if (opts.containsCommaSeparatedFileList()) {
      files = collectFromCommaDelimitedString(opts.getInputFileName());
    } else {
      files = collectFilesFromOneName(opts.getInputFileName());
    }

    PMD pmd;
    if (opts.jdk13()) {
      pmd = new PMD(new TargetJDK1_3());
    } else {
      pmd = new PMD();
    }

    RuleContext ctx = new RuleContext();
    ctx.setReport(new Report());

    try {
      RuleSetFactory ruleSetFactory = new RuleSetFactory();
      RuleSet rules = ruleSetFactory.createRuleSet(opts.getRulesets());
      for (Iterator i = files.iterator(); i.hasNext(); ) {
        File file = (File) i.next();
        ctx.setSourceCodeFilename(
            glomName(opts.shortNamesEnabled(), opts.getInputFileName(), file));
        try {
          pmd.processFile(new FileInputStream(file), opts.getEncoding(), rules, ctx);
        } catch (PMDException pmde) {
          if (opts.debugEnabled()) {
            pmde.getReason().printStackTrace();
          }
          ctx.getReport()
              .addError(
                  new Report.ProcessingError(
                      pmde.getMessage(),
                      glomName(opts.shortNamesEnabled(), opts.getInputFileName(), file)));
        }
      }
    } catch (FileNotFoundException fnfe) {
      System.out.println(opts.usage());
      fnfe.printStackTrace();
    } catch (RuleSetNotFoundException rsnfe) {
      System.out.println(opts.usage());
      rsnfe.printStackTrace();
    }

    try {
      Renderer r = opts.createRenderer();
      System.out.println(r.render(ctx.getReport()));
    } catch (Exception e) {
      System.out.println(e.getMessage());
      System.out.println(opts.usage());
      if (opts.debugEnabled()) {
        e.printStackTrace();
      }
    }
  }
 public void testPluginname() throws Throwable {
   Rule rule = new XPathRule();
   rule.addProperty("xpath", "//VariableDeclaratorId[string-length(@Image) < 3]");
   rule.setMessage("{0}");
   rule.addProperty("pluginname", "true");
   PMD p = new PMD();
   RuleContext ctx = new RuleContext();
   Report report = new Report();
   ctx.setReport(report);
   ctx.setSourceCodeFilename("n/a");
   RuleSet rules = new RuleSet();
   rules.addRule(rule);
   p.processFile(new StringReader(TEST1), rules, ctx);
   RuleViolation rv = (RuleViolation) report.iterator().next();
   assertEquals("a", rv.getDescription());
 }
  /**
   * @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);
      }
    }
  }