Search in sources :

Example 1 with RuleFactory

use of net.sourceforge.pmd.rules.RuleFactory in project pmd by pmd.

the class RuleSetFactory method parseSingleRuleNode.

/**
 * Parse a rule node as a single Rule. The Rule has been fully defined
 * within the context of the current RuleSet.
 *
 * @param ruleSetReferenceId
 *            The RuleSetReferenceId of the RuleSet being parsed.
 * @param ruleSetBuilder
 *            The RuleSet being constructed.
 * @param ruleNode
 *            Must be a rule element node.
 */
private void parseSingleRuleNode(RuleSetReferenceId ruleSetReferenceId, RuleSetBuilder ruleSetBuilder, Node ruleNode) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    Element ruleElement = (Element) ruleNode;
    // it.
    if (StringUtils.isNotBlank(ruleSetReferenceId.getRuleName()) && !isRuleName(ruleElement, ruleSetReferenceId.getRuleName())) {
        return;
    }
    Rule rule = new RuleFactory().buildRule(ruleElement);
    rule.setRuleSetName(ruleSetBuilder.getName());
    ruleSetBuilder.addRule(rule);
}
Also used : RuleFactory(net.sourceforge.pmd.rules.RuleFactory) Element(org.w3c.dom.Element) XPathRule(net.sourceforge.pmd.lang.rule.XPathRule) MockRule(net.sourceforge.pmd.lang.rule.MockRule)

Example 2 with RuleFactory

use of net.sourceforge.pmd.rules.RuleFactory in project pmd by pmd.

the class RuleSetFactory method parseRuleReferenceNode.

/**
 * Parse a rule node as a RuleReference. A RuleReference is a single Rule
 * which comes from another RuleSet with some of it's attributes potentially
 * overridden.
 *
 * @param ruleSetReferenceId
 *            The RuleSetReferenceId of the RuleSet being parsed.
 * @param ruleSetBuilder
 *            The RuleSet being constructed.
 * @param ruleNode
 *            Must be a rule element node.
 * @param ref
 *            A reference to a Rule.
 * @param withDeprecatedRuleReferences
 *            whether rule references that are deprecated should be ignored
 *            or not
 */
private void parseRuleReferenceNode(RuleSetReferenceId ruleSetReferenceId, RuleSetBuilder ruleSetBuilder, Node ruleNode, String ref, boolean withDeprecatedRuleReferences) throws RuleSetNotFoundException {
    Element ruleElement = (Element) ruleNode;
    // it.
    if (StringUtils.isNotBlank(ruleSetReferenceId.getRuleName()) && !isRuleName(ruleElement, ruleSetReferenceId.getRuleName())) {
        return;
    }
    // load the ruleset with minimum priority low, so that we get all rules, to be able to exclude any rule
    // minimum priority will be applied again, before constructing the final ruleset
    RuleSetFactory ruleSetFactory = new RuleSetFactory(resourceLoader, RulePriority.LOW, warnDeprecated, this.compatibilityFilter != null);
    boolean isSameRuleSet = false;
    RuleSetReferenceId otherRuleSetReferenceId = RuleSetReferenceId.parse(ref).get(0);
    if (!otherRuleSetReferenceId.isExternal() && containsRule(ruleSetReferenceId, otherRuleSetReferenceId.getRuleName())) {
        otherRuleSetReferenceId = new RuleSetReferenceId(ref, ruleSetReferenceId);
        isSameRuleSet = true;
    }
    // do not ignore deprecated rule references
    Rule referencedRule = ruleSetFactory.createRule(otherRuleSetReferenceId, true);
    if (referencedRule == null) {
        throw new IllegalArgumentException("Unable to find referenced rule " + otherRuleSetReferenceId.getRuleName() + "; perhaps the rule name is mispelled?");
    }
    if (warnDeprecated && referencedRule.isDeprecated()) {
        if (referencedRule instanceof RuleReference) {
            RuleReference ruleReference = (RuleReference) referencedRule;
            if (LOG.isLoggable(Level.WARNING)) {
                LOG.warning("Use Rule name " + ruleReference.getRuleSetReference().getRuleSetFileName() + '/' + ruleReference.getOriginalName() + " instead of the deprecated Rule name " + otherRuleSetReferenceId + ". PMD " + PMDVersion.getNextMajorRelease() + " will remove support for this deprecated Rule name usage.");
            }
        } else if (referencedRule instanceof MockRule) {
            if (LOG.isLoggable(Level.WARNING)) {
                LOG.warning("Discontinue using Rule name " + otherRuleSetReferenceId + " as it has been removed from PMD and no longer functions." + " PMD " + PMDVersion.getNextMajorRelease() + " will remove support for this Rule.");
            }
        } else {
            if (LOG.isLoggable(Level.WARNING)) {
                LOG.warning("Discontinue using Rule name " + otherRuleSetReferenceId + " as it is scheduled for removal from PMD." + " PMD " + PMDVersion.getNextMajorRelease() + " will remove support for this Rule.");
            }
        }
    }
    RuleSetReference ruleSetReference = new RuleSetReference(otherRuleSetReferenceId.getRuleSetFileName(), false);
    RuleReference ruleReference = new RuleFactory().decorateRule(referencedRule, ruleSetReference, ruleElement);
    if (warnDeprecated && ruleReference.isDeprecated()) {
        if (LOG.isLoggable(Level.WARNING)) {
            LOG.warning("Use Rule name " + ruleReference.getRuleSetReference().getRuleSetFileName() + '/' + ruleReference.getOriginalName() + " instead of the deprecated Rule name " + ruleSetReferenceId.getRuleSetFileName() + '/' + ruleReference.getName() + ". PMD " + PMDVersion.getNextMajorRelease() + " will remove support for this deprecated Rule name usage.");
        }
    }
    if (withDeprecatedRuleReferences || !isSameRuleSet || !ruleReference.isDeprecated()) {
        ruleSetBuilder.addRuleReplaceIfExists(ruleReference);
    }
}
Also used : RuleReference(net.sourceforge.pmd.lang.rule.RuleReference) RuleFactory(net.sourceforge.pmd.rules.RuleFactory) Element(org.w3c.dom.Element) XPathRule(net.sourceforge.pmd.lang.rule.XPathRule) MockRule(net.sourceforge.pmd.lang.rule.MockRule) MockRule(net.sourceforge.pmd.lang.rule.MockRule)

Aggregations

MockRule (net.sourceforge.pmd.lang.rule.MockRule)2 XPathRule (net.sourceforge.pmd.lang.rule.XPathRule)2 RuleFactory (net.sourceforge.pmd.rules.RuleFactory)2 Element (org.w3c.dom.Element)2 RuleReference (net.sourceforge.pmd.lang.rule.RuleReference)1