예제 #1
0
  private static boolean hasRealText(String line) {

    if (StringUtil.isEmpty(line)) {
      return false;
    }

    return !StringUtil.isAnyOf(line.trim(), "//", "/*", "/**", "*", "*/");
  }
  /* @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent) */
  public void selectionChanged(SelectionChangedEvent event) {

    RuleViolation violation = selectedViolationFrom(event);
    if (violation == null) return;

    String varName = violation.getVariableName();
    if (StringUtil.isEmpty(varName)) return;

    int beginLine = violation.getBeginLine();
    int endLine = violation.getEndLine();

    if (beginLine != 0 && endLine != 0) {
      try {
        int offset = getDocument().getLineOffset(violation.getBeginLine() - 1);
        int length = getDocument().getLineOffset(violation.getEndLine()) - offset;
        highlight(offset, length);
      } catch (BadLocationException ble) {
        logError(
            StringKeys.ERROR_RUNTIME_EXCEPTION + "Exception when selecting a line in the editor",
            ble);
      }

      // showMethodToMarker(marker);
      showMethodToViolation(violation);

      // then we calculate and color _a possible_ Path
      // for this Error in the Dataflow
      DataflowGraph graph = graphViewer.getGraph();
      if (!graphViewer.isDisposed() && graph != null) {
        graph.markPath(beginLine, endLine, varName);
      }
    }
  }
예제 #3
0
  /**
   * 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;
  }
  /**
   * @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);
    }
  }
예제 #5
0
  /** Generates a html table with violation information. */
  private String displayRuleViolation(RuleViolation vio) {

    StringBuilder sb = new StringBuilder(200);
    sb.append("<table border=\"0\">");
    renderViolationRow(sb, "Rule:", vio.getRule().getName());
    renderViolationRow(sb, "Description:", vio.getDescription());

    if (StringUtil.isNotEmpty(vio.getVariableName())) {
      renderViolationRow(sb, "Variable:", vio.getVariableName());
    }

    if (vio.getEndLine() > 0) {
      renderViolationRow(sb, "Line:", vio.getEndLine() + " and " + vio.getBeginLine());
    } else {
      renderViolationRow(sb, "Line:", Integer.toString(vio.getBeginLine()));
    }

    sb.append("</table>");
    return sb.toString();
  }
예제 #6
0
  private String withoutCommentMarkup(String text) {

    return StringUtil.withoutPrefixes(text.trim(), "//", "*", "/**");
  }
예제 #7
0
 private static boolean isSame(String s1, String s2) {
   return StringUtil.isSame(s1, s2, true, false, true);
 }