Fixed bug 1351706 - CompareObjectsWithEquals now catches more cases.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@3972 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2005-11-09 16:34:53 +00:00
parent 3afa0a9335
commit c680594a25
4 changed files with 22 additions and 27 deletions

View File

@ -16,6 +16,7 @@ Fixed bug 1312723 - Added isSyntacticallyPublic() behavior to ASTFieldDeclaratio
Fixed bug 1313216 - Designer was not displaying 'final' attribute for ASTLocalVariableDeclaration nodes.
Fixed bug 1314086 - Added logging-jakarta-commons as a short name for rulesets/logging-jakarta-commons.xml to SimpleRuleSetNameMapper.
Fixed bug 1351498 - Improved UnnecessaryCaseChange warning message.
Fixed bug 1351706 - CompareObjectsWithEquals now catches more cases.
Implemented RFE 1311309 - Suppressed RuleViolation counts are now included in the reports.
Implemented RFE 1220371 - Rule violation suppression via annotations. Per the JLS, @SuppressWarnings can be placed before the following nodes: TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE.
Implemented RFE 1275547 - OverrideBothEqualsAndHashcode now skips Comparator implementations.

View File

@ -22,6 +22,7 @@ public class CompareObjectsWithEqualsTest extends SimpleAggregatorTst{
new TestDescriptor(TEST4, "missed hit - qualified names. that's ok, we can't resolve the types yet, so better to skip this for now", 0, rule),
new TestDescriptor(TEST5, "more qualified name skippage", 0, rule),
new TestDescriptor(TEST6, "locals", 1, rule),
new TestDescriptor(TEST7, "2 locals declared on one line", 1, rule),
});
}
@ -69,4 +70,14 @@ public class CompareObjectsWithEqualsTest extends SimpleAggregatorTst{
" }" + PMD.EOL +
"}";
private static final String TEST7 =
"public class Foo {" + PMD.EOL +
" void bar() {" + PMD.EOL +
" String a,b;" + PMD.EOL +
" a = \"foo\";" + PMD.EOL +
" b = \"bar\";" + PMD.EOL +
" if (a == b) {}" + PMD.EOL +
" }" + PMD.EOL +
"}";
}

View File

@ -6,14 +6,8 @@ import net.sourceforge.pmd.ast.ASTInitializer;
import net.sourceforge.pmd.ast.ASTName;
import net.sourceforge.pmd.ast.Node;
import net.sourceforge.pmd.ast.SimpleNode;
import net.sourceforge.pmd.symboltable.NameOccurrence;
import net.sourceforge.pmd.symboltable.Scope;
import net.sourceforge.pmd.symboltable.VariableNameDeclaration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class CompareObjectsWithEquals extends AbstractRule {
private boolean hasName(Node n) {
@ -37,28 +31,17 @@ public class CompareObjectsWithEquals extends AbstractRule {
return data;
}
check((Scope)node.getScope(), node, data);
check(node.getScope().getEnclosingMethodScope(), node, data);
return data;
}
ASTName n0 = (ASTName)node.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
ASTName n1 = (ASTName)node.jjtGetChild(1).jjtGetChild(0).jjtGetChild(0);
private void check(Scope scope, SimpleNode node, Object ctx) {
Map vars = scope.getVariableDeclarations();
for (Iterator i = vars.keySet().iterator(); i.hasNext();) {
VariableNameDeclaration key = (VariableNameDeclaration)i.next();
if (key.isPrimitiveType() || key.isArray()) {
continue;
}
List usages = (List)vars.get(key);
if (usages.isEmpty()) {
continue;
}
for (Iterator j = usages.iterator(); j.hasNext();) {
if (((NameOccurrence)j.next()).getLocation().jjtGetParent().jjtGetParent().jjtGetParent() == node) {
addViolation(ctx, node);
return;
}
if (n0.getNameDeclaration() instanceof VariableNameDeclaration && n1.getNameDeclaration() instanceof VariableNameDeclaration) {
VariableNameDeclaration nd0 = (VariableNameDeclaration)n0.getNameDeclaration();
VariableNameDeclaration nd1 = (VariableNameDeclaration)n1.getNameDeclaration();
if (nd0.isReferenceType() && nd1.isReferenceType()) {
addViolation(data, node);
}
}
return data;
}
}

View File

@ -45,6 +45,7 @@
</subsection>
<subsection name="Contributors">
<ul>
<li>Raja Rajan - 2 bug reports for CompareObjectswithEquals</li>
<li>Jeff Chamblee - suggested better message for UnnecessaryCaseChange, bug report for CompareObjectsWithEquals</li>
<li>Dave Brosius - suggested MisleadingVariableName rule, a couple of nice patches to clean up some string handling inefficiencies, non-static class usages, and unclosed streams/readers - found with Findbugs, I daresay :-)</li>
<li>Johan Stuyts - nice patch to clean up build environment</li>
@ -68,7 +69,6 @@
<li>Pieter Bloemendaal - reported JDK 1.3 parsing bug 1292609, command line docs bug report, bug report for UnusedPrivateMethod, found typo in ArrayIsStoredDirectly, bug report for AvoidReassigningParametersRule</li>
<li>shawn2005 - documentation bug report</li>
<li>Benoit Xhenseval - bug report for UnusedPrivateMethod, suggestion to add elapsed time to XML report, bug report for ImmutableField, many bug reports (with good failure cases!), Ant task patch and bug report, XSLT patch, suggestion for improving XML report</li>
<li>Raja Rajan - bug report for CompareObjectswithEquals</li>
<li>Andrew Taylor - bug report for StringInstantiation</li>
<li>Jeff Jensen - suggested description attribute in property element</li>
<li>Wim Deblauwe - suggested putting property names/values in generated docs, UselessOverridingMethod, reported bug in JUnitTestsShouldContainAsserts, front page and "how to make a ruleset" patches, noted problems with web site rule index, bug report for JUnitTestsShouldContainAsserts, Clover license coordination and implementation, UseCorrectExceptionLogging, coordinated and coded a much nicer asXML() implementation, suggested cleanup of UnusedFormalParameter, Javadoc patch, SystemPrintln bug report, helped get Ant task and CLI squared away with JDK 1.5 params, JDK 1.5-specific bug reports, suggested improvements for ExceptionSignatureDeclaration</li>