Merge branch 'release/multiple-code-improvements-fix-1' of https://github.com/DevFactory/pmd into DevFactory-release/multiple-code-improvements-fix-1

This commit is contained in:
Andreas Dangel
2016-05-20 19:11:14 +02:00
5 changed files with 43 additions and 39 deletions

View File

@ -81,7 +81,7 @@ public abstract class AbstractNcssCountRule extends AbstractStatisticalApexRule
* @return count of the number of children of the node, plus one * @return count of the number of children of the node, plus one
*/ */
protected Integer countNodeChildren(Node node, Object data) { protected Integer countNodeChildren(Node node, Object data) {
Integer nodeCount = null; Integer nodeCount;
int lineCount = 0; int lineCount = 0;
for (int i = 0; i < node.jjtGetNumChildren(); i++) { for (int i = 0; i < node.jjtGetNumChildren(); i++) {
nodeCount = (Integer) ((ApexNode<?>) node.jjtGetChild(i)).jjtAccept(this, data); nodeCount = (Integer) ((ApexNode<?>) node.jjtGetChild(i)).jjtAccept(this, data);

View File

@ -53,9 +53,9 @@ public class TooManyFieldsRule extends AbstractApexRule {
bumpCounterFor(clazz); bumpCounterFor(clazz);
} }
} }
for (String k : stats.keySet()) { for (Map.Entry<String, Integer> entry : stats.entrySet()) {
int val = stats.get(k); int val = entry.getValue();
Node n = nodes.get(k); Node n = nodes.get(entry.getKey());
if (val > maxFields) { if (val > maxFields) {
addViolation(data, n); addViolation(data, n);
} }

View File

@ -36,4 +36,6 @@ public class PropertyDescriptorFields {
public static final String MAX = "max"; public static final String MAX = "max";
/** To limit the range of valid values, e.g. to Enums */ /** To limit the range of valid values, e.g. to Enums */
public static final String LEGAL_PACKAGES = "legalPackages"; public static final String LEGAL_PACKAGES = "legalPackages";
private PropertyDescriptorFields() {}
} }

View File

@ -34,6 +34,7 @@ import net.sourceforge.pmd.util.filter.Filters;
public class RuleSet { public class RuleSet {
private static final Logger LOG = Logger.getLogger(RuleSet.class.getName()); private static final Logger LOG = Logger.getLogger(RuleSet.class.getName());
private static final String MISSING_RULE = "Missing rule";
private List<Rule> rules = new ArrayList<>(); private List<Rule> rules = new ArrayList<>();
private String fileName; private String fileName;
@ -80,7 +81,7 @@ public class RuleSet {
*/ */
public void addRule(Rule rule) { public void addRule(Rule rule) {
if (rule == null) { if (rule == null) {
throw new IllegalArgumentException("Missing rule"); throw new IllegalArgumentException(MISSING_RULE);
} }
rules.add(rule); rules.add(rule);
} }
@ -96,7 +97,7 @@ public class RuleSet {
*/ */
public boolean addRuleReplaceIfExists(Rule rule) { public boolean addRuleReplaceIfExists(Rule rule) {
if (rule == null) { if (rule == null) {
throw new IllegalArgumentException("Missing rule"); throw new IllegalArgumentException(MISSING_RULE);
} }
boolean replaced = false; boolean replaced = false;
@ -122,7 +123,7 @@ public class RuleSet {
*/ */
public boolean addRuleIfNotExists(Rule rule) { public boolean addRuleIfNotExists(Rule rule) {
if (rule == null) { if (rule == null) {
throw new IllegalArgumentException("Missing rule"); throw new IllegalArgumentException(MISSING_RULE);
} }
boolean exists = false; boolean exists = false;
@ -182,10 +183,8 @@ public class RuleSet {
*/ */
public boolean usesDFA(Language language) { public boolean usesDFA(Language language) {
for (Rule r : rules) { for (Rule r : rules) {
if (r.getLanguage().equals(language)) { if (r.getLanguage().equals(language) && r.usesDFA()) {
if (r.usesDFA()) { return true;
return true;
}
} }
} }
return false; return false;
@ -509,10 +508,8 @@ public class RuleSet {
*/ */
public boolean usesTypeResolution(Language language) { public boolean usesTypeResolution(Language language) {
for (Rule r : rules) { for (Rule r : rules) {
if (r.getLanguage().equals(language)) { if (r.getLanguage().equals(language) && r.usesTypeResolution()) {
if (r.usesTypeResolution()) { return true;
return true;
}
} }
} }
return false; return false;

View File

@ -48,6 +48,13 @@ public class RuleSetFactory {
private static final Logger LOG = Logger.getLogger(RuleSetFactory.class.getName()); private static final Logger LOG = Logger.getLogger(RuleSetFactory.class.getName());
private static final String DESCRIPTION = "description";
private static final String UNEXPECTED_ELEMENT = "Unexpected element <";
private static final String PRIORITY = "priority";
private static final String FOR_RULE = "' for Rule ";
private static final String MESSAGE = "message";
private static final String EXTERNAL_INFO_URL = "externalInfoUrl";
private ClassLoader classLoader = RuleSetFactory.class.getClassLoader(); private ClassLoader classLoader = RuleSetFactory.class.getClassLoader();
private RulePriority minimumPriority = RulePriority.LOW; private RulePriority minimumPriority = RulePriority.LOW;
private boolean warnDeprecated = false; private boolean warnDeprecated = false;
@ -257,7 +264,7 @@ public class RuleSetFactory {
Node node = nodeList.item(i); Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeType() == Node.ELEMENT_NODE) {
String nodeName = node.getNodeName(); String nodeName = node.getNodeName();
if ("description".equals(nodeName)) { if (DESCRIPTION.equals(nodeName)) {
ruleSet.setDescription(parseTextNode(node)); ruleSet.setDescription(parseTextNode(node));
} else if ("include-pattern".equals(nodeName)) { } else if ("include-pattern".equals(nodeName)) {
ruleSet.addIncludePattern(parseTextNode(node)); ruleSet.addIncludePattern(parseTextNode(node));
@ -266,7 +273,7 @@ public class RuleSetFactory {
} else if ("rule".equals(nodeName)) { } else if ("rule".equals(nodeName)) {
parseRuleNode(ruleSetReferenceId, ruleSet, node, withDeprecatedRuleReferences); parseRuleNode(ruleSetReferenceId, ruleSet, node, withDeprecatedRuleReferences);
} else { } else {
throw new IllegalArgumentException("Unexpected element <" + node.getNodeName() throw new IllegalArgumentException(UNEXPECTED_ELEMENT + node.getNodeName()
+ "> encountered as child of <ruleset> element."); + "> encountered as child of <ruleset> element.");
} }
} }
@ -346,7 +353,7 @@ public class RuleSetFactory {
String excludedRuleName = excludeElement.getAttribute("name"); String excludedRuleName = excludeElement.getAttribute("name");
ruleSetReference.addExclude(excludedRuleName); ruleSetReference.addExclude(excludedRuleName);
excludedRulesCheck.add(excludedRuleName); excludedRulesCheck.add(excludedRuleName);
} else if (isElementNode(child, "priority")) { } else if (isElementNode(child, PRIORITY)) {
priority = parseTextNode(child).trim(); priority = parseTextNode(child).trim();
} }
} }
@ -406,7 +413,7 @@ public class RuleSetFactory {
String languageName = ruleElement.getAttribute("language"); String languageName = ruleElement.getAttribute("language");
Language language = LanguageRegistry.findLanguageByTerseName(languageName); Language language = LanguageRegistry.findLanguageByTerseName(languageName);
if (language == null) { if (language == null) {
throw new IllegalArgumentException("Unknown Language '" + languageName + "' for Rule " + rule.getName() throw new IllegalArgumentException("Unknown Language '" + languageName + FOR_RULE + rule.getName()
+ ", supported Languages are " + ", supported Languages are "
+ LanguageRegistry.commaSeparatedTerseNamesForLanguage(LanguageRegistry.findWithRuleSupport())); + LanguageRegistry.commaSeparatedTerseNamesForLanguage(LanguageRegistry.findWithRuleSupport()));
} }
@ -424,7 +431,7 @@ public class RuleSetFactory {
LanguageVersion minimumLanguageVersion = language.getVersion(minimumLanguageVersionName); LanguageVersion minimumLanguageVersion = language.getVersion(minimumLanguageVersionName);
if (minimumLanguageVersion == null) { if (minimumLanguageVersion == null) {
throw new IllegalArgumentException("Unknown minimum Language Version '" + minimumLanguageVersionName throw new IllegalArgumentException("Unknown minimum Language Version '" + minimumLanguageVersionName
+ "' for Language '" + language.getTerseName() + "' for Rule " + rule.getName() + "' for Language '" + language.getTerseName() + FOR_RULE + rule.getName()
+ "; supported Language Versions are: " + "; supported Language Versions are: "
+ LanguageRegistry.commaSeparatedTerseNamesForLanguageVersion(language.getVersions())); + LanguageRegistry.commaSeparatedTerseNamesForLanguageVersion(language.getVersions()));
} }
@ -436,7 +443,7 @@ public class RuleSetFactory {
LanguageVersion maximumLanguageVersion = language.getVersion(maximumLanguageVersionName); LanguageVersion maximumLanguageVersion = language.getVersion(maximumLanguageVersionName);
if (maximumLanguageVersion == null) { if (maximumLanguageVersion == null) {
throw new IllegalArgumentException("Unknown maximum Language Version '" + maximumLanguageVersionName throw new IllegalArgumentException("Unknown maximum Language Version '" + maximumLanguageVersionName
+ "' for Language '" + language.getTerseName() + "' for Rule " + rule.getName() + "' for Language '" + language.getTerseName() + FOR_RULE + rule.getName()
+ "; supported Language Versions are: " + "; supported Language Versions are: "
+ LanguageRegistry.commaSeparatedTerseNamesForLanguageVersion(language.getVersions())); + LanguageRegistry.commaSeparatedTerseNamesForLanguageVersion(language.getVersions()));
} }
@ -447,7 +454,7 @@ public class RuleSetFactory {
throw new IllegalArgumentException("The minimum Language Version '" throw new IllegalArgumentException("The minimum Language Version '"
+ rule.getMinimumLanguageVersion().getTerseName() + rule.getMinimumLanguageVersion().getTerseName()
+ "' must be prior to the maximum Language Version '" + "' must be prior to the maximum Language Version '"
+ rule.getMaximumLanguageVersion().getTerseName() + "' for Rule " + rule.getName() + rule.getMaximumLanguageVersion().getTerseName() + FOR_RULE + rule.getName()
+ "; perhaps swap them around?"); + "; perhaps swap them around?");
} }
@ -455,9 +462,9 @@ public class RuleSetFactory {
if (StringUtil.isNotEmpty(since)) { if (StringUtil.isNotEmpty(since)) {
rule.setSince(since); rule.setSince(since);
} }
rule.setMessage(ruleElement.getAttribute("message")); rule.setMessage(ruleElement.getAttribute(MESSAGE));
rule.setRuleSetName(ruleSet.getName()); rule.setRuleSetName(ruleSet.getName());
rule.setExternalInfoUrl(ruleElement.getAttribute("externalInfoUrl")); rule.setExternalInfoUrl(ruleElement.getAttribute(EXTERNAL_INFO_URL));
if (hasAttributeSetTrue(ruleElement, "dfa")) { if (hasAttributeSetTrue(ruleElement, "dfa")) {
rule.setUsesDFA(); rule.setUsesDFA();
@ -474,16 +481,16 @@ public class RuleSetFactory {
continue; continue;
} }
String nodeName = node.getNodeName(); String nodeName = node.getNodeName();
if (nodeName.equals("description")) { if (nodeName.equals(DESCRIPTION)) {
rule.setDescription(parseTextNode(node)); rule.setDescription(parseTextNode(node));
} else if (nodeName.equals("example")) { } else if (nodeName.equals("example")) {
rule.addExample(parseTextNode(node)); rule.addExample(parseTextNode(node));
} else if (nodeName.equals("priority")) { } else if (nodeName.equals(PRIORITY)) {
rule.setPriority(RulePriority.valueOf(Integer.parseInt(parseTextNode(node).trim()))); rule.setPriority(RulePriority.valueOf(Integer.parseInt(parseTextNode(node).trim())));
} else if (nodeName.equals("properties")) { } else if (nodeName.equals("properties")) {
parsePropertiesNode(rule, node); parsePropertiesNode(rule, node);
} else { } else {
throw new IllegalArgumentException("Unexpected element <" + nodeName throw new IllegalArgumentException(UNEXPECTED_ELEMENT + nodeName
+ "> encountered as child of <rule> element for Rule " + rule.getName()); + "> encountered as child of <rule> element for Rule " + rule.getName());
} }
@ -581,25 +588,25 @@ public class RuleSetFactory {
if (ruleElement.hasAttribute("name")) { if (ruleElement.hasAttribute("name")) {
ruleReference.setName(ruleElement.getAttribute("name")); ruleReference.setName(ruleElement.getAttribute("name"));
} }
if (ruleElement.hasAttribute("message")) { if (ruleElement.hasAttribute(MESSAGE)) {
ruleReference.setMessage(ruleElement.getAttribute("message")); ruleReference.setMessage(ruleElement.getAttribute(MESSAGE));
} }
if (ruleElement.hasAttribute("externalInfoUrl")) { if (ruleElement.hasAttribute(EXTERNAL_INFO_URL)) {
ruleReference.setExternalInfoUrl(ruleElement.getAttribute("externalInfoUrl")); ruleReference.setExternalInfoUrl(ruleElement.getAttribute(EXTERNAL_INFO_URL));
} }
for (int i = 0; i < ruleElement.getChildNodes().getLength(); i++) { for (int i = 0; i < ruleElement.getChildNodes().getLength(); i++) {
Node node = ruleElement.getChildNodes().item(i); Node node = ruleElement.getChildNodes().item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeType() == Node.ELEMENT_NODE) {
if (node.getNodeName().equals("description")) { if (node.getNodeName().equals(DESCRIPTION)) {
ruleReference.setDescription(parseTextNode(node)); ruleReference.setDescription(parseTextNode(node));
} else if (node.getNodeName().equals("example")) { } else if (node.getNodeName().equals("example")) {
ruleReference.addExample(parseTextNode(node)); ruleReference.addExample(parseTextNode(node));
} else if (node.getNodeName().equals("priority")) { } else if (node.getNodeName().equals(PRIORITY)) {
ruleReference.setPriority(RulePriority.valueOf(Integer.parseInt(parseTextNode(node)))); ruleReference.setPriority(RulePriority.valueOf(Integer.parseInt(parseTextNode(node))));
} else if (node.getNodeName().equals("properties")) { } else if (node.getNodeName().equals("properties")) {
parsePropertiesNode(ruleReference, node); parsePropertiesNode(ruleReference, node);
} else { } else {
throw new IllegalArgumentException("Unexpected element <" + node.getNodeName() throw new IllegalArgumentException(UNEXPECTED_ELEMENT + node.getNodeName()
+ "> encountered as child of <rule> element for Rule " + ruleReference.getName()); + "> encountered as child of <rule> element for Rule " + ruleReference.getName());
} }
} }
@ -630,11 +637,9 @@ public class RuleSetFactory {
NodeList rules = ruleSetElement.getElementsByTagName("rule"); NodeList rules = ruleSetElement.getElementsByTagName("rule");
for (int i = 0; i < rules.getLength(); i++) { for (int i = 0; i < rules.getLength(); i++) {
Element rule = (Element) rules.item(i); Element rule = (Element) rules.item(i);
if (rule.hasAttribute("name")) { if (rule.hasAttribute("name") && rule.getAttribute("name").equals(ruleName)) {
if (rule.getAttribute("name").equals(ruleName)) { found = true;
found = true; break;
break;
}
} }
} }
} catch (Exception e) { } catch (Exception e) {