net.sourceforge.pmd.RuleSet Java Examples

The following examples show how to use net.sourceforge.pmd.RuleSet. 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: PmdMessages.java    From warnings-ng-plugin with MIT License 6 votes vote down vote up
/**
 * Initializes the rules.
 *
 * @return the number of rule sets
 */
public int initialize() {
    try {
        Iterator<RuleSet> ruleSets = RulesetsFactoryUtils.defaultFactory().getRegisteredRuleSets();
        while (ruleSets.hasNext()) {
            RuleSet ruleSet = ruleSets.next();
            rules.put(ruleSet.getName(), ruleSet);
        }
        if (rules.isEmpty()) {
            LOGGER.log(Level.SEVERE, ERROR_MESSAGE);
        }
        return rules.size();
    }
    catch (RuleSetNotFoundException exception) {
        LOGGER.log(Level.SEVERE, ERROR_MESSAGE, exception);
    }
    return 0;
}
 
Example #3
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 #4
Source File: PmdExecutor.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
private RuleSets createRulesets(String repositoryKey) {
  String rulesXml = pmdProfileExporter.exportProfile(repositoryKey, rulesProfile);
  File ruleSetFile = pmdConfiguration.dumpXmlRuleSet(repositoryKey, rulesXml);
  String ruleSetFilePath = ruleSetFile.getAbsolutePath();
  RuleSetFactory ruleSetFactory = new RuleSetFactory();
  try {
    RuleSet ruleSet = ruleSetFactory.createRuleSet(ruleSetFilePath);
    return new RuleSets(ruleSet);
  } catch (RuleSetNotFoundException e) {
    throw new IllegalStateException(e);
  }
}
 
Example #5
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 #6
Source File: XPathEvaluator.java    From pmd-designer with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Evaluates an XPath query on the compilation unit. Performs
 * no side effects.
 *
 * @param compilationUnit AST root
 * @param languageVersion language version
 * @param xpathVersion    XPath version
 * @param xpathQuery      XPath query
 * @param properties      Properties of the rule
 *
 * @throws XPathEvaluationException if there was an error during the evaluation. The cause is preserved
 */
public static List<Node> evaluateQuery(Node compilationUnit,
                                       LanguageVersion languageVersion,
                                       String xpathVersion,
                                       String xpathQuery,
                                       Map<String, String> propertyValues,
                                       List<PropertyDescriptorSpec> properties) throws XPathEvaluationException {

    if (StringUtils.isBlank(xpathQuery)) {
        return emptyList();
    }

    try {
        List<Node> results = new ArrayList<>();

        XPathRule xpathRule = new XPathRule() {
            @Override
            public void addViolation(Object data, Node node, String arg) {
                results.add(node);
            }
        };


        xpathRule.setMessage("");
        xpathRule.setLanguage(languageVersion.getLanguage());
        xpathRule.setXPath(xpathQuery);
        xpathRule.setVersion(xpathVersion);

        properties.stream()
                  .map(PropertyDescriptorSpec::build)
                  .forEach(xpathRule::definePropertyDescriptor);

        propertyValues.forEach((k, v) -> {
            PropertyDescriptor<?> d = xpathRule.getPropertyDescriptor(k);
            if (d != null) {
                setRulePropertyCapture(xpathRule, d, v);
            } else {
                throw new RuntimeException("Property '" + k + "' is not defined, available properties: "
                                               + properties.stream().map(PropertyDescriptorSpec::getName).collect(Collectors.toList()));
            }
        });

        final RuleSet ruleSet = new RuleSetFactory().createSingleRuleRuleSet(xpathRule);

        RuleSets ruleSets = new RuleSets(ruleSet);

        RuleContext ruleContext = new RuleContext();
        ruleContext.setLanguageVersion(languageVersion);
        ruleContext.setIgnoreExceptions(false);

        ruleSets.apply(singletonList(compilationUnit), ruleContext, xpathRule.getLanguage());

        return results;

    } catch (RuntimeException e) {
        throw new XPathEvaluationException(e);
    }
}