private void renderViolation(ViolationNode vnode) {

    vnode.getParent().addNumberOfViolation(1);
    RuleViolation vio = vnode.getRuleViolation();
    classBuf.append(
        "<tr>"
            + " <td>"
            + vio.getMethodName()
            + "</td>"
            + " <td>"
            + this.displayRuleViolation(vio)
            + "</td>"
            + "</tr>");
  }
 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());
 }
Exemple #3
0
  private MarkerInfo2 getMarkerInfo(RuleViolation violation, String type)
      throws PropertiesException {

    Rule rule = violation.getRule();

    MarkerInfo2 info = new MarkerInfo2(type, 7);

    info.add(IMarker.MESSAGE, violation.getDescription());
    info.add(IMarker.LINE_NUMBER, violation.getBeginLine());
    info.add(PMDRuntimeConstants.KEY_MARKERATT_LINE2, violation.getEndLine());
    info.add(PMDRuntimeConstants.KEY_MARKERATT_RULENAME, rule.getName());
    info.add(PMDRuntimeConstants.KEY_MARKERATT_PRIORITY, rule.getPriority().getPriority());

    switch (rule.getPriority().getPriority()) {
      case 1:
        info.add(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
        info.add(
            IMarker.SEVERITY,
            projectProperties.violationsAsErrors()
                ? IMarker.SEVERITY_ERROR
                : IMarker.SEVERITY_WARNING);
        break;
      case 2:
        if (projectProperties.violationsAsErrors()) {
          info.add(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
        } else {
          info.add(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
          info.add(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
        }
        break;

      case 5:
        info.add(IMarker.SEVERITY, IMarker.SEVERITY_INFO);
        break;

      case 3:
        info.add(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
      case 4:
      default:
        info.add(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
        break;
    }

    return info;
  }
  /* @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);
      }
    }
  }
  /** 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();
  }
Exemple #6
0
  public static String markerTypeFor(RuleViolation violation) {

    int priorityId = violation.getRule().getPriority().getPriority();

    switch (priorityId) {
      case 1:
        return PMDRuntimeConstants.PMD_MARKER_1;
      case 2:
        return PMDRuntimeConstants.PMD_MARKER_2;
      case 3:
        return PMDRuntimeConstants.PMD_MARKER_3;
      case 4:
        return PMDRuntimeConstants.PMD_MARKER_4;
      case 5:
        return PMDRuntimeConstants.PMD_MARKER_5;
      default:
        return PMDRuntimeConstants.PMD_MARKER;
    }
  }
Exemple #7
0
  private void updateMarkers(IFile file, RuleContext context, boolean fTask)
      throws CoreException, PropertiesException {

    Map<IFile, Set<MarkerInfo2>> accumulator = getAccumulator();
    Set<MarkerInfo2> markerSet = new HashSet<MarkerInfo2>();
    List<Review> reviewsList = findReviewedViolations(file);
    Review review = new Review();
    Iterator<RuleViolation> iter = context.getReport().iterator();
    //        final IPreferences preferences = PMDPlugin.getDefault().loadPreferences();
    //        final int maxViolationsPerFilePerRule = preferences.getMaxViolationsPerFilePerRule();
    Map<Rule, Integer> violationsByRule = new HashMap<Rule, Integer>();

    Rule rule = null;
    while (iter.hasNext()) {
      RuleViolation violation = iter.next();
      rule = violation.getRule();
      review.ruleName = rule.getName();
      review.lineNumber = violation.getBeginLine();

      if (reviewsList.contains(review)) {
        log.debug(
            "Ignoring violation of rule "
                + rule.getName()
                + " at line "
                + violation.getBeginLine()
                + " because of a review.");
        continue;
      }

      Integer count = violationsByRule.get(rule);
      if (count == null) {
        count = NumericConstants.ZERO;
        violationsByRule.put(rule, count);
      }

      int maxViolations = maxAllowableViolationsFor(rule);

      if (count.intValue() < maxViolations) {
        // Ryan Gustafson 02/16/2008 - Always use PMD_MARKER, as people get confused as to why PMD
        // problems don't always show up on Problems view like they do when you do build.
        // markerSet.add(getMarkerInfo(violation, fTask ? PMDRuntimeConstants.PMD_TASKMARKER :
        // PMDRuntimeConstants.PMD_MARKER));
        markerSet.add(getMarkerInfo(violation, markerTypeFor(violation)));
        /*
        if (isDfaEnabled && violation.getRule().usesDFA()) {
            markerSet.add(getMarkerInfo(violation, PMDRuntimeConstants.PMD_DFA_MARKER));
        } else {
             markerSet.add(getMarkerInfo(violation, fTask ? PMDRuntimeConstants.PMD_TASKMARKER : PMDRuntimeConstants.PMD_MARKER));
        }
         */
        violationsByRule.put(rule, Integer.valueOf(count.intValue() + 1));

        log.debug(
            "Adding a violation for rule "
                + rule.getName()
                + " at line "
                + violation.getBeginLine());
      } else {
        log.debug(
            "Ignoring violation of rule "
                + rule.getName()
                + " at line "
                + violation.getBeginLine()
                + " because maximum violations has been reached for file "
                + file.getName());
      }
    }

    if (accumulator != null) {
      log.debug("Adding markerSet to accumulator for file " + file.getName());
      accumulator.put(file, markerSet);
    }
  }