コード例 #1
0
ファイル: BaseVisitor.java プロジェクト: robinst/pmd
  /**
   * Search for reviewed violations in that file
   *
   * @param file
   */
  private List<Review> findReviewedViolations(final IFile file) {
    final List<Review> reviews = new ArrayList<Review>();
    BufferedReader reader = null;
    try {
      int lineNumber = 0;
      boolean findLine = false;
      boolean comment = false;
      final Stack<String> pendingReviews = new Stack<String>();
      reader = new BufferedReader(new InputStreamReader(file.getContents()));
      while (reader.ready()) {
        String line = reader.readLine();
        if (line != null) {
          line = line.trim();
          lineNumber++;
          if (line.startsWith("/*")) {
            comment = line.indexOf("*/") == -1;
          } else if (comment && line.indexOf("*/") != -1) {
            comment = false;
          } else if (!comment && line.startsWith(PMDRuntimeConstants.PLUGIN_STYLE_REVIEW_COMMENT)) {
            final String tail =
                line.substring(PMDRuntimeConstants.PLUGIN_STYLE_REVIEW_COMMENT.length());
            final String ruleName = tail.substring(0, tail.indexOf(':'));
            pendingReviews.push(ruleName);
            findLine = true;
          } else if (!comment
              && findLine
              && StringUtil.isNotEmpty(line)
              && !line.startsWith("//")) {
            findLine = false;
            while (!pendingReviews.empty()) {
              // @PMD:REVIEWED:AvoidInstantiatingObjectsInLoops:
              // by Herlin on 01/05/05 18:36
              final Review review = new Review();
              review.ruleName = pendingReviews.pop();
              review.lineNumber = lineNumber;
              reviews.add(review);
            }
          }
        }
      }

      // if (log.isDebugEnabled()) {
      // for (int i = 0; i < reviewsList.size(); i++) {
      // final Review review = (Review) reviewsList.get(i);
      // log.debug("Review : rule " + review.ruleName + ", line " +
      // review.lineNumber);
      // }
      // }

    } catch (CoreException e) {
      PMDPlugin.getDefault().logError("Core Exception when searching reviewed violations", e);
    } catch (IOException e) {
      PMDPlugin.getDefault().logError("IO Exception when searching reviewed violations", e);
    } finally {
      IOUtil.closeQuietly(reader);
    }

    return reviews;
  }
コード例 #2
0
  /**
   * Reads a Resource's File and return the Code as String.
   *
   * @param resource a resource to read ; the resource must be accessible.
   * @return a String which is the Files Content
   */
  protected String resourceToString(IResource resource) {
    final StringBuilder fileContents = new StringBuilder();
    BufferedReader bReader = null;
    try {
      // we create a FileReader
      bReader = new BufferedReader(new FileReader(resource.getRawLocation().toFile()));

      // ... and read the File line by line
      while (bReader.ready()) {
        fileContents.append(bReader.readLine()).append('\n');
      }
    } catch (FileNotFoundException fnfe) {
      PMDPlugin.getDefault()
          .logError(
              StringKeys.ERROR_FILE_NOT_FOUND + resource.toString() + " in " + this.toString(),
              fnfe);
    } catch (IOException ioe) {
      PMDPlugin.getDefault().logError(StringKeys.ERROR_IO_EXCEPTION + this.toString(), ioe);
    } finally {
      IOUtil.closeQuietly(bReader);
    }

    return fileContents.toString();
  }
コード例 #3
0
ファイル: BaseVisitor.java プロジェクト: robinst/pmd
  /**
   * 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);
    }
  }