[pmd-devel] SF.net SVN: pmd:[7631] trunk/pmd/src/main/java/net/sourceforge/pmd/lang
A source code analyzer
Brought to you by:
adangel,
juansotuyo
From: <hoo...@us...> - 2012-02-24 19:10:40
|
Revision: 7631 http://pmd.svn.sourceforge.net/pmd/?rev=7631&view=rev Author: hooperbloob Date: 2012-02-24 19:10:33 +0000 (Fri, 24 Feb 2012) Log Message: ----------- Various cleanups & refactorings. I would suggest using the name of ParametricRuleViolation instead. Modified Paths: -------------- trunk/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/JavaRuleViolation.java trunk/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/controversial/DaaRuleViolation.java trunk/pmd/src/main/java/net/sourceforge/pmd/lang/rule/ParametizedRuleViolation.java Modified: trunk/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/JavaRuleViolation.java =================================================================== --- trunk/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/JavaRuleViolation.java 2012-02-24 19:05:58 UTC (rev 7630) +++ trunk/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/JavaRuleViolation.java 2012-02-24 19:10:33 UTC (rev 7631) @@ -49,18 +49,8 @@ packageName = sourceFileScope.getPackageName() == null ? "" : sourceFileScope.getPackageName(); // Class name is built from enclosing ClassScopes - String qualifiedName = null; - for (ASTClassOrInterfaceDeclaration parent : node.getParentsOfType(ASTClassOrInterfaceDeclaration.class)) { - if (qualifiedName == null) { - qualifiedName = parent.getScope().getEnclosingClassScope().getClassName(); - } else { - qualifiedName = parent.getScope().getEnclosingClassScope().getClassName() - + "$" + qualifiedName; - } - } - if (qualifiedName != null) { - className = qualifiedName; - } + setClassNameFrom(node); + // Method name comes from 1st enclosing MethodScope if (node.getFirstParentOfType(ASTMethodDeclaration.class) != null) { methodName = scope.getEnclosingMethodScope().getName(); @@ -88,6 +78,22 @@ } } + private void setClassNameFrom(JavaNode node) { + + String qualifiedName = null; + for (ASTClassOrInterfaceDeclaration parent : node.getParentsOfType(ASTClassOrInterfaceDeclaration.class)) { + String clsName = parent.getScope().getEnclosingClassScope().getClassName(); + if (qualifiedName == null) { + qualifiedName = clsName; + } else { + qualifiedName = clsName + '$' + qualifiedName; + } + } + if (qualifiedName != null) { + className = qualifiedName; + } + } + private boolean suppresses(final Node node) { return node instanceof CanSuppressWarnings && ((CanSuppressWarnings) node).hasSuppressWarningsAnnotationFor(getRule()); Modified: trunk/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/controversial/DaaRuleViolation.java =================================================================== --- trunk/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/controversial/DaaRuleViolation.java 2012-02-24 19:05:58 UTC (rev 7630) +++ trunk/pmd/src/main/java/net/sourceforge/pmd/lang/java/rule/controversial/DaaRuleViolation.java 2012-02-24 19:10:33 UTC (rev 7631) @@ -14,33 +14,23 @@ * is required for showing what variable produces the UR DD or DU anomaly. * * @author Sven Jacob - * + * @author Brian Remedios */ public class DaaRuleViolation extends JavaRuleViolation { - private String variableName; - private int beginLine; - private int endLine; - private String type; + + private final String variableName; + private final String type; public DaaRuleViolation(Rule rule, RuleContext ctx, Node node, String type, String msg, String var, int beginLine, int endLine) { super(rule, ctx, (JavaNode)node, msg); this.variableName = var; - this.beginLine = beginLine; - this.endLine = endLine; + setLines(beginLine, endLine); this.type = type; } public String getVariableName() { return variableName; } - - public int getBeginLine() { - return beginLine; - } - - public int getEndLine() { - return endLine; - } public String getType() { return type; Modified: trunk/pmd/src/main/java/net/sourceforge/pmd/lang/rule/ParametizedRuleViolation.java =================================================================== --- trunk/pmd/src/main/java/net/sourceforge/pmd/lang/rule/ParametizedRuleViolation.java 2012-02-24 19:05:58 UTC (rev 7630) +++ trunk/pmd/src/main/java/net/sourceforge/pmd/lang/rule/ParametizedRuleViolation.java 2012-02-24 19:10:33 UTC (rev 7631) @@ -10,11 +10,12 @@ import net.sourceforge.pmd.RuleContext; import net.sourceforge.pmd.RuleViolation; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.util.StringUtil; public class ParametizedRuleViolation<T extends Node> implements RuleViolation { - protected Rule rule; - protected String description; + protected final Rule rule; + protected final String description; protected boolean suppressed; protected String filename; @@ -24,10 +25,10 @@ protected int endLine; protected int endColumn; - protected String packageName; - protected String className; - protected String methodName; - protected String variableName; + protected String packageName = ""; + protected String className = ""; + protected String methodName = ""; + protected String variableName = ""; // FUTURE Fix to understand when a violation _must_ have a Node, and when it must not (to prevent erroneous Rules silently logging w/o a Node). Modify RuleViolationFactory to support identifying without a Node, and update Rule base classes too. public ParametizedRuleViolation(Rule rule, RuleContext ctx, T node, String message) { @@ -38,34 +39,32 @@ this.filename = ""; } if (node != null) { - this.beginLine = node.getBeginLine(); - this.beginColumn = node.getBeginColumn(); - this.endLine = node.getEndLine(); - this.endColumn = node.getEndColumn(); + beginLine = node.getBeginLine(); + beginColumn = node.getBeginColumn(); + endLine = node.getEndLine(); + endColumn = node.getEndColumn(); } - this.packageName = ""; - this.className = ""; - this.methodName = ""; - this.variableName = ""; // Apply Rule specific suppressions - if (node != null && rule != null) { - // Regex - String regex = rule.getProperty(Rule.VIOLATION_SUPPRESS_REGEX_DESCRIPTOR); - if (regex != null && description != null) { - if (Pattern.matches(regex, description)) { - suppressed = true; - } - } + if (node != null && rule != null) + setSuppression(rule, node); + } - // XPath - if (!suppressed) { - String xpath = rule.getProperty(Rule.VIOLATION_SUPPRESS_XPATH_DESCRIPTOR); - if (xpath != null) { - suppressed = node.hasDescendantMatchingXPath(xpath); - } - } - } + private void setSuppression(Rule rule, T node) { + + String regex = rule.getProperty(Rule.VIOLATION_SUPPRESS_REGEX_DESCRIPTOR); // Regex + if (regex != null && description != null) { + if (Pattern.matches(regex, description)) { + suppressed = true; + } + } + + if (!suppressed) { // XPath + String xpath = rule.getProperty(Rule.VIOLATION_SUPPRESS_XPATH_DESCRIPTOR); + if (xpath != null) { + suppressed = node.hasDescendantMatchingXPath(xpath); + } + } } protected String expandVariables(String message) { @@ -88,8 +87,9 @@ } protected boolean isVariable(String name) { - return "variableName".equals(name) || "methodName".equals(name) || "className".equals(name) - || "packageName".equals(name) || rule.getPropertyDescriptor(name) != null; + return + StringUtil.isAnyOf(name, "variableName", "methodName", "className", "packageName") || + rule.getPropertyDescriptor(name) != null; } protected String getVariableValue(String name) { @@ -116,7 +116,7 @@ } public boolean isSuppressed() { - return this.suppressed; + return suppressed; } public String getFilename() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |