use of net.sourceforge.pmd.lang.rule.RuleReference in project pmd by pmd.
the class RuleSetTest method testAddRuleSetByReferenceAllRule.
@Test
public void testAddRuleSetByReferenceAllRule() {
RuleSet set2 = createRuleSetBuilder("ruleset2").withFileName("foo").addRule(new MockRule("name", "desc", "msg", "rulesetname")).addRule(new MockRule("name2", "desc", "msg", "rulesetname")).build();
RuleSet set1 = createRuleSetBuilder("ruleset1").addRuleSetByReference(set2, true).build();
assertEquals("wrong rule size", 2, set1.getRules().size());
for (Rule rule : set1.getRules()) {
assertTrue("not a rule reference", rule instanceof RuleReference);
RuleReference ruleReference = (RuleReference) rule;
assertEquals("wrong ruleset file name", "foo", ruleReference.getRuleSetReference().getRuleSetFileName());
assertTrue("not all rule reference", ruleReference.getRuleSetReference().isAllRules());
}
}
use of net.sourceforge.pmd.lang.rule.RuleReference in project pmd by pmd.
the class RuleSetTest method testAddRuleSetByReferenceSingleRule.
@Test
public void testAddRuleSetByReferenceSingleRule() {
RuleSet set2 = createRuleSetBuilder("ruleset2").withFileName("foo").addRule(new MockRule("name", "desc", "msg", "rulesetname")).addRule(new MockRule("name2", "desc", "msg", "rulesetname")).build();
RuleSet set1 = createRuleSetBuilder("ruleset1").addRuleSetByReference(set2, false).build();
assertEquals("wrong rule size", 2, set1.getRules().size());
for (Rule rule : set1.getRules()) {
assertTrue("not a rule reference", rule instanceof RuleReference);
RuleReference ruleReference = (RuleReference) rule;
assertEquals("wrong ruleset file name", "foo", ruleReference.getRuleSetReference().getRuleSetFileName());
assertFalse("should not be all rule reference", ruleReference.getRuleSetReference().isAllRules());
}
}
use of net.sourceforge.pmd.lang.rule.RuleReference in project pmd by pmd.
the class RuleSetWriterTest method testRuleReferenceOverriddenName.
/**
* Unit test for #1312 see https://sourceforge.net/p/pmd/bugs/1312/
*
* @throws Exception
* any error
*/
@Test
public void testRuleReferenceOverriddenName() throws Exception {
RuleSetFactory ruleSetFactory = new RuleSetFactory();
RuleSet rs = ruleSetFactory.createRuleSet("dummy-basic");
RuleSetReference ruleSetReference = new RuleSetReference("rulesets/dummy/basic.xml");
RuleReference ruleRef = new RuleReference();
ruleRef.setRule(rs.getRuleByName("DummyBasicMockRule"));
ruleRef.setRuleSetReference(ruleSetReference);
// override the name
ruleRef.setName("Foo");
RuleSet ruleSet = ruleSetFactory.createSingleRuleRuleSet(ruleRef);
writer.write(ruleSet);
String written = out.toString("UTF-8");
Assert.assertTrue(written.contains("ref=\"rulesets/dummy/basic.xml/DummyBasicMockRule\""));
}
use of net.sourceforge.pmd.lang.rule.RuleReference in project pmd by pmd.
the class RuleDocGenerator method generateLanguageIndex.
/**
* Writes for each language an index file, which lists the rulesets, the rules
* and links to the ruleset pages.
* @param rulesets all registered/built-in rulesets
* @param sortedAdditionalRulesets additional rulesets
* @throws IOException
*/
private void generateLanguageIndex(Map<Language, List<RuleSet>> rulesets, Map<Language, List<RuleSet>> sortedAdditionalRulesets) throws IOException {
for (Map.Entry<Language, List<RuleSet>> entry : rulesets.entrySet()) {
String languageTersename = entry.getKey().getTerseName();
String filename = LANGUAGE_INDEX_FILENAME_PATTERN.replace("${language.tersename}", languageTersename);
Path path = getAbsoluteOutputPath(filename);
List<String> lines = new LinkedList<>();
lines.add("---");
lines.add("title: " + entry.getKey().getName() + " Rules");
lines.add("permalink: " + LANGUAGE_INDEX_PERMALINK_PATTERN.replace("${language.tersename}", languageTersename));
lines.add("folder: pmd/rules");
lines.add("---");
for (RuleSet ruleset : entry.getValue()) {
lines.add("## " + ruleset.getName());
lines.add("");
lines.add("{% include callout.html content=\"" + getRuleSetDescriptionSingleLine(ruleset).replaceAll("\"", "'") + "\" %}");
lines.add("");
for (Rule rule : getSortedRules(ruleset)) {
String link = RULESET_INDEX_PERMALINK_PATTERN.replace("${language.tersename}", languageTersename).replace("${ruleset.name}", RuleSetUtils.getRuleSetFilename(ruleset));
if (rule instanceof RuleReference) {
RuleReference ref = (RuleReference) rule;
if (ruleset.getFileName().equals(ref.getRuleSetReference().getRuleSetFileName())) {
// rule renamed within same ruleset
lines.add("* [" + rule.getName() + "](" + link + "#" + rule.getName().toLowerCase(Locale.ROOT) + "): " + DEPRECATION_LABEL_SMALL + "The rule has been renamed. Use instead " + "[" + ref.getRule().getName() + "](" + link + "#" + ref.getRule().getName().toLowerCase(Locale.ROOT) + ").");
} else {
// rule moved to another ruleset...
String otherLink = RULESET_INDEX_PERMALINK_PATTERN.replace("${language.tersename}", languageTersename).replace("${ruleset.name}", RuleSetUtils.getRuleSetFilename(ref.getRuleSetReference().getRuleSetFileName()));
lines.add("* [" + rule.getName() + "](" + link + "#" + rule.getName().toLowerCase(Locale.ROOT) + "): " + DEPRECATION_LABEL_SMALL + "The rule has been moved to another ruleset. Use instead " + "[" + ref.getRule().getName() + "](" + otherLink + "#" + ref.getRule().getName().toLowerCase(Locale.ROOT) + ").");
}
} else {
link += "#" + rule.getName().toLowerCase(Locale.ROOT);
lines.add("* [" + rule.getName() + "](" + link + "): " + (rule.isDeprecated() ? DEPRECATION_LABEL_SMALL : "") + getShortRuleDescription(rule));
}
}
lines.add("");
}
List<RuleSet> additionalRulesetsForLanguage = sortedAdditionalRulesets.get(entry.getKey());
if (additionalRulesetsForLanguage != null) {
lines.add("## Additional rulesets");
lines.add("");
for (RuleSet ruleset : additionalRulesetsForLanguage) {
boolean deprecated = RuleSetUtils.isRuleSetDeprecated(ruleset);
String rulesetName = ruleset.getName() + " (`" + RuleSetUtils.getRuleSetClasspath(ruleset) + "`)";
if (!deprecated) {
lines.add("* " + rulesetName + ":");
lines.add("");
lines.add(" " + getRuleSetDescriptionSingleLine(ruleset));
lines.add("");
} else {
lines.add("* " + rulesetName + ":");
lines.add("");
lines.add(" " + DEPRECATION_LABEL_SMALL + " This ruleset is for backwards compatibility.");
lines.add("");
}
lines.add(" It contains the following rules:");
lines.add("");
StringBuilder rules = new StringBuilder();
for (Rule rule : getSortedRules(ruleset)) {
if (rules.length() == 0) {
rules.append(" ");
} else {
rules.append(", ");
}
Rule resolvedRule = RuleSetUtils.resolveRuleReferences(rule);
if (resolvedRule instanceof RuleReference) {
// Note: deprecated rulesets contain by definition only rule references
RuleReference ref = (RuleReference) resolvedRule;
String otherLink = RULESET_INDEX_PERMALINK_PATTERN.replace("${language.tersename}", languageTersename).replace("${ruleset.name}", RuleSetUtils.getRuleSetFilename(ref.getRuleSetReference().getRuleSetFileName()));
rules.append("[").append(ref.getName()).append("](");
rules.append(otherLink).append("#").append(ref.getRule().getName().toLowerCase(Locale.ROOT)).append(")");
} else {
rules.append(rule.getName());
}
}
lines.add(rules.toString());
lines.add("");
}
lines.add("");
}
System.out.println("Generated " + path);
writer.write(path, lines);
}
}
use of net.sourceforge.pmd.lang.rule.RuleReference in project pmd by pmd.
the class RuleSetUtils method resolveRuleReferences.
/**
* Recursively resolves rule references until the last reference.
* The last reference is returned.
* If the given rule not a reference, the rule is returned.
*
* @param rule
* @return
*/
public static Rule resolveRuleReferences(Rule rule) {
Rule result = rule;
Rule ref = rule;
while (ref instanceof RuleReference) {
// remember the last reference
result = ref;
ref = ((RuleReference) ref).getRule();
}
return result;
}
Aggregations