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;
 }
Beispiel #2
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);
    }
  }