net.sourceforge.pmd.Rule Java Examples

The following examples show how to use net.sourceforge.pmd.Rule. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Main.java    From diff-check with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static Set<Language> getApplicableLanguages(PMDConfiguration configuration, RuleSets ruleSets) {
    Set<Language> languages = new HashSet<>();
    LanguageVersionDiscoverer discoverer = configuration.getLanguageVersionDiscoverer();

    for (Rule rule : ruleSets.getAllRules()) {
        Language language = rule.getLanguage();
        if (languages.contains(language)) {
            continue;
        }
        LanguageVersion version = discoverer.getDefaultLanguageVersion(language);
        if (RuleSet.applies(rule, version)) {
            languages.add(language);
            if (LOG.isLoggable(Level.FINE)) {
                LOG.fine("Using " + language.getShortName() + " version: " + version.getShortName());
            }
        }
    }
    return languages;
}
 
Example #2
Source File: PmdProcessor.java    From sputnik with Apache License 2.0 6 votes vote down vote up
/**
 * Paste from PMD
 */
private static Set<Language> getApplicableLanguages(PMDConfiguration configuration, RuleSets ruleSets) {
    Set<Language> languages = new HashSet<>();
    LanguageVersionDiscoverer discoverer = configuration.getLanguageVersionDiscoverer();

    for (Rule rule : ruleSets.getAllRules()) {
        Language language = rule.getLanguage();
        if (languages.contains(language))
            continue;
        LanguageVersion version = discoverer.getDefaultLanguageVersion(language);
        if (RuleSet.applies(rule, version)) {
            languages.add(language);
            log.debug("Using {} version: {}", language.getShortName(), version.getShortName());
        }
    }
    return languages;
}
 
Example #3
Source File: PmdViolationRecorderTest.java    From sonar-p3c-pmd with MIT License 6 votes vote down vote up
@Test
public void should_convert_pmd_violation_to_sonar_violation() {
  org.sonar.api.rules.Rule sonarRule = createRuleInRuleFinder("RULE");
  File file1 = new File("src/source.java");
  DefaultInputFile inputFile1 = addToFileSystem(file1);
  Issuable issuable = createIssuable(inputFile1);
  RuleViolation pmdViolation = createPmdViolation(file1, 42, "Description", "RULE");

  Issue issue = mock(Issue.class);
  IssueBuilder issueBuilder = mock(IssueBuilder.class);
  when(issuable.newIssueBuilder()).thenReturn(issueBuilder);
  when(issueBuilder.ruleKey(sonarRule.ruleKey())).thenReturn(issueBuilder);
  when(issueBuilder.message("Description")).thenReturn(issueBuilder);
  when(issueBuilder.line(42)).thenReturn(issueBuilder);
  when(issueBuilder.build()).thenReturn(issue);

  pmdViolationRecorder.saveViolation(pmdViolation);
  verify(issuable).addIssue(issue);
  verify(issueBuilder).ruleKey(sonarRule.ruleKey());
  verify(issueBuilder).message("Description");
  verify(issueBuilder).line(42);
  verify(sonarRule, atLeastOnce()).ruleKey();
}
 
Example #4
Source File: PmdViolationRecorderTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
@Test
public void should_ignore_violation_on_non_issuable_resource() {
  org.sonar.api.rules.Rule sonarRule = createRuleInRuleFinder("RULE");
  File file1 = new File("test/source.java");
  addToFileSystem(file1);
  RuleViolation pmdViolation = createPmdViolation(file1, 42, "Description", "RULE");

  pmdViolationRecorder.saveViolation(pmdViolation);
  verifyZeroInteractions(sonarRule);
}
 
Example #5
Source File: CollectorRendererTest.java    From sputnik with Apache License 2.0 5 votes vote down vote up
@NotNull
private RuleViolation createRuleViolation(@NotNull Rule rule, String violationDescription, @NotNull Configuration config) {
    renderer = new CollectorRenderer(config);
    RuleViolation violation = mock(RuleViolation.class);
    when(violation.getRule()).thenReturn(rule);
    when(violation.getDescription()).thenReturn(violationDescription);

    return violation;
}
 
Example #6
Source File: CollectorRendererTest.java    From sputnik with Apache License 2.0 5 votes vote down vote up
@NotNull
private Rule createRule(String ruleDescription, String externalInfoUrl, RulePriority priority, @NotNull Configuration config) {
    renderer = new CollectorRenderer(config);
    Rule rule = mock(Rule.class);
    when(rule.getDescription()).thenReturn(ruleDescription);
    when(rule.getExternalInfoUrl()).thenReturn(externalInfoUrl);
    when(rule.getPriority()).thenReturn(priority);

    return rule;
}
 
Example #7
Source File: CollectorRendererTest.java    From sputnik with Apache License 2.0 5 votes vote down vote up
@Test
void shouldReportViolationWithoutDetails() throws IOException {
    Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of(GeneralOption.PMD_SHOW_VIOLATION_DETAILS.getKey(), "false"));
    Rule rule = createRule(RULE_DESCRIPTION, EXTERNAL_INFO_URL, RulePriority.MEDIUM, config);
    Report report = createReportWithVolation(createRuleViolation(rule, VIOLATION_DESCRIPTION, config), config);

    renderer.renderFileReport(report);

    Violation violation = renderer.getReviewResult().getViolations().get(0);
    assertThat(violation.getMessage()).isEqualTo(VIOLATION_DESCRIPTION);
}
 
Example #8
Source File: CollectorRendererTest.java    From sputnik with Apache License 2.0 5 votes vote down vote up
@Test
void shouldReportViolationWithDetails() throws IOException {
    Configuration config = new ConfigurationSetup().setUp(ImmutableMap.of(GeneralOption.PMD_SHOW_VIOLATION_DETAILS.getKey(), "true"));
    Rule rule = createRule(RULE_DESCRIPTION, EXTERNAL_INFO_URL, RulePriority.HIGH, config);
    Report report = createReportWithVolation(createRuleViolation(rule, VIOLATION_DESCRIPTION, config), config);

    renderer.renderFileReport(report);

    Violation violation = renderer.getReviewResult().getViolations().get(0);
    assertThat(violation.getMessage()).isEqualTo(DESCRIPTION_WITH_DETAILS);
    assertThat(violation.getSeverity()).isEqualTo(Severity.ERROR);
}
 
Example #9
Source File: PmdMessages.java    From warnings-ng-plugin with MIT License 5 votes vote down vote up
/**
 * Creates the message string to be shown for the specified rule.
 *
 * @param rule
 *         the rule
 *
 * @return the message string to be shown for the specified rule
 */
private String createMessage(final Rule rule) {
    StringBuilder message = new StringBuilder(rule.getDescription());
    List<String> examples = rule.getExamples();
    if (!examples.isEmpty()) {
        message.append(pre().with(code(examples.get(0))).renderFormatted());
    }
    if (StringUtils.isNotBlank(rule.getExternalInfoUrl())) {
        message.append(a().withHref(rule.getExternalInfoUrl()).withText("See PMD documentation.").renderFormatted());
    }
    return message.toString();
}
 
Example #10
Source File: PmdMessages.java    From warnings-ng-plugin with MIT License 5 votes vote down vote up
/**
 * Returns the message for the specified PMD rule.
 *
 * @param ruleSetName
 *         PMD rule set
 * @param ruleName
 *         PMD rule ID
 *
 * @return the message
 */
public String getMessage(final String ruleSetName, final String ruleName) {
    if (rules.containsKey(ruleSetName)) {
        RuleSet ruleSet = rules.get(ruleSetName);
        Rule rule = ruleSet.getRuleByName(ruleName);
        if (rule != null) {
            return createMessage(rule);
        }
    }
    return StringUtils.EMPTY;
}
 
Example #11
Source File: PmdViolationRecorderTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
private RuleViolation createPmdViolation(File file, int line, String description, String ruleName) {
  Rule rule = mock(Rule.class);
  when(rule.getName()).thenReturn(ruleName);
  RuleViolation pmdViolation = mock(RuleViolation.class);
  when(pmdViolation.getFilename()).thenReturn(file.getAbsolutePath());
  when(pmdViolation.getBeginLine()).thenReturn(line);
  when(pmdViolation.getDescription()).thenReturn(description);
  when(pmdViolation.getRule()).thenReturn(rule);
  return pmdViolation;
}
 
Example #12
Source File: Main.java    From diff-check with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Create a report, filter out any defective rules, and keep a record of
 * them.
 *
 * @param rs the rules
 * @param ctx the rule context
 * @param fileName the filename of the source file, which should appear in the report
 * @return the Report
 */
public static Report setupReport(RuleSets rs, RuleContext ctx, String fileName) {

    Set<Rule> brokenRules = removeBrokenRules(rs);
    Report report = Report.createReport(ctx, fileName);

    for (Rule rule : brokenRules) {
        report.addConfigError(new Report.RuleConfigurationError(rule, rule.dysfunctionReason()));
    }

    return report;
}
 
Example #13
Source File: PmdViolationRecorderTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
@Test
public void should_ignore_violation_on_unknown_resource() {
  org.sonar.api.rules.Rule sonarRule = createRuleInRuleFinder("RULE");
  File unknownFile = new File("src/UNKNOWN.java");
  RuleViolation pmdViolation = createPmdViolation(unknownFile, 42, "Description", "RULE");

  pmdViolationRecorder.saveViolation(pmdViolation);
  verifyZeroInteractions(sonarRule);
}
 
Example #14
Source File: Main.java    From diff-check with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Remove and return the misconfigured rules from the rulesets and log them
 * for good measure.
 *
 * @param ruleSets
 *            RuleSets
 * @return Set<Rule>
 */
private static Set<Rule> removeBrokenRules(RuleSets ruleSets) {

    Set<Rule> brokenRules = new HashSet<>();
    ruleSets.removeDysfunctionalRules(brokenRules);

    for (Rule rule : brokenRules) {
        if (LOG.isLoggable(Level.WARNING)) {
            LOG.log(Level.WARNING,
                "Removed misconfigured rule: " + rule.getName() + "  cause: " + rule.dysfunctionReason());
        }
    }

    return brokenRules;
}
 
Example #15
Source File: PmdViolationRecorderTest.java    From sonar-p3c-pmd with MIT License 4 votes vote down vote up
private org.sonar.api.rules.Rule createRuleInRuleFinder(String ruleName) {
  org.sonar.api.rules.Rule sonarRule = mock(org.sonar.api.rules.Rule.class);
  when(ruleFinder.findByKey("pmd", ruleName)).thenReturn(sonarRule);
  when(sonarRule.ruleKey()).thenReturn(RuleKey.of("pmd", ruleName));
  return sonarRule;
}
 
Example #16
Source File: PlainTextLanguage.java    From pmd-designer with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
protected void visit(Rule rule, Node node, RuleContext ctx) {
    rule.apply(Collections.singletonList(node), ctx);
}
 
Example #17
Source File: PlainTextLanguage.java    From pmd-designer with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String s, int i, int i1) {
    return new ParametricRuleViolation<>(rule, ruleContext, node, s);
}
 
Example #18
Source File: PlainTextLanguage.java    From pmd-designer with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String s) {
    return new ParametricRuleViolation<>(rule, ruleContext, node, s);
}
 
Example #19
Source File: XPathEvaluator.java    From pmd-designer with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static <T> void setRulePropertyCapture(Rule rule, PropertyDescriptor<T> descriptor, String value) {
    rule.setProperty(descriptor, descriptor.valueFrom(value));
}