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;
 }
  /**
   * @param args String[]
   * @throws RuleSetNotFoundException
   * @throws IOException
   * @throws PMDException
   */
  public static void main(String[] args)
      throws RuleSetNotFoundException, IOException, PMDException {

    String targetjdk = findOptionalStringValue(args, "--targetjdk", "1.4");
    Language language = Language.JAVA;
    LanguageVersion languageVersion = language.getVersion(targetjdk);
    if (languageVersion == null) {
      languageVersion = language.getDefaultVersion();
    }

    String srcDir =
        findOptionalStringValue(args, "--source-directory", "/usr/local/java/src/java/lang/");
    List<DataSource> dataSources =
        FileUtil.collectFiles(srcDir, new LanguageFilenameFilter(language));

    boolean debug = findBooleanSwitch(args, "--debug");
    boolean parseOnly = findBooleanSwitch(args, "--parse-only");

    if (debug) {
      System.out.println("Using " + language.getName() + " " + languageVersion.getVersion());
    }
    if (parseOnly) {
      Parser parser = PMD.parserFor(languageVersion, null);
      parseStress(parser, dataSources, debug);
    } else {
      String ruleset = findOptionalStringValue(args, "--ruleset", "");
      if (debug) {
        System.out.println("Checking directory " + srcDir);
      }
      Set<RuleDuration> results = new TreeSet<RuleDuration>();
      RuleSetFactory factory = new RuleSetFactory();
      if (StringUtil.isNotEmpty(ruleset)) {
        stress(languageVersion, factory.createRuleSet(ruleset), dataSources, results, debug);
      } else {
        Iterator<RuleSet> i = factory.getRegisteredRuleSets();
        while (i.hasNext()) {
          stress(languageVersion, i.next(), dataSources, results, debug);
        }
      }

      TextReport report = new TextReport();
      report.generate(results, System.err);
    }
  }
Exemple #3
0
  /**
   * Run PMD against a resource
   *
   * @param resource the resource to process
   */
  protected final void reviewResource(IResource resource) {

    IFile file = (IFile) resource.getAdapter(IFile.class);
    if (file == null || file.getFileExtension() == null) return;

    Reader input = null;
    try {
      boolean included = isIncluded(file);
      log.debug("Derived files included: " + projectProperties.isIncludeDerivedFiles());
      log.debug("file " + file.getName() + " is derived: " + file.isDerived());
      log.debug("file checked: " + included);

      prepareMarkerAccumulator(file);

      LanguageVersionDiscoverer languageDiscoverer = new LanguageVersionDiscoverer();
      LanguageVersion languageVersion =
          languageDiscoverer.getDefaultLanguageVersionForFile(file.getName());
      // in case it is java, select the correct java version
      if (languageVersion != null && languageVersion.getLanguage() == Language.JAVA) {
        languageVersion = PMDPlugin.javaVersionFor(file.getProject());
      }
      if (languageVersion != null) {
        configuration().setDefaultLanguageVersion(languageVersion);
      }
      log.debug("discovered language: " + languageVersion);

      final File sourceCodeFile = file.getRawLocation().toFile();
      if (included
          && getRuleSet().applies(sourceCodeFile)
          && isFileInWorkingSet(file)
          && languageVersion != null) {
        subTask("PMD checking: " + file.getName());

        Timer timer = new Timer();

        RuleContext context = PMD.newRuleContext(file.getName(), sourceCodeFile);
        context.setLanguageVersion(languageVersion);

        input = new InputStreamReader(file.getContents(), file.getCharset());
        //                    getPmdEngine().processFile(input, getRuleSet(), context);
        //                    getPmdEngine().processFile(sourceCodeFile, getRuleSet(), context);

        RuleSets rSets = new RuleSets(getRuleSet());
        new SourceCodeProcessor(configuration()).processSourceCode(input, rSets, context);

        timer.stop();
        pmdDuration += timer.getDuration();

        updateMarkers(file, context, isUseTaskMarker());

        worked(1);
        fileCount++;
      } else {
        log.debug("The file " + file.getName() + " is not in the working set");
      }

    } catch (CoreException e) {
      log.error("Core exception visiting " + file.getName(), e); // TODO:		// complete message
    } catch (PMDException e) {
      log.error("PMD exception visiting " + file.getName(), e); // TODO: 		// complete message
    } catch (IOException e) {
      log.error("IO exception visiting " + file.getName(), e); // TODO: 		// complete message
    } catch (PropertiesException e) {
      log.error("Properties exception visiting " + file.getName(), e); // TODO:	// complete message
    } finally {
      IOUtil.closeQuietly(input);
    }
  }