Fixed bug 1531593 - UnnecessaryConversionTemporary no longer reports false positives when toString() is invoked inside the call to 'new Long/Integer/etc()'.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4480 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2006-08-03 14:48:57 +00:00
parent 656b7856ad
commit 5cb7088581
4 changed files with 28 additions and 15 deletions

View File

@ -14,6 +14,7 @@ Fixed bug 1522054 - BooleanInstantiation now detects instantiations inside metho
Fixed bug 1522056 - UseStringBufferForStringAppends now flags appends which occur in static initializers and constructors
Fixed bug 1526530 - SingularField now finds fields which are hidden at the method or static level
Fixed bug 1529805 - UnusedModifier no longer throws NPEs on JDK 1.5 enums.
Fixed bug 1531593 - UnnecessaryConversionTemporary no longer reports false positives when toString() is invoked inside the call to 'new Long/Integer/etc()'.
Fixed a bug in AvoidProtectedFieldInFinalClass - it no longer reports false positives for protected fields in inner classes.
Fixed a bug in the C++ grammar - the tokenizer now properly recognizes macro definitions which are followed by a multiline comment.
Implemented RFE 1501850 - UnusedFormalParameter now catches cases where a parameter is assigned to but not used.

View File

@ -19,18 +19,26 @@ public class UnnecessaryTemporariesTest extends SimpleAggregatorTst {
public void testAll() {
runTests(new TestDescriptor[]{
new TestDescriptor(TEST1, "all glommed together", 6, rule),
new TestDescriptor(TEST2, "called on String", 0, rule),
});
}
private static final String TEST1 =
" public class Foo {" + PMD.EOL +
" void method (int x) {" + PMD.EOL +
" new Integer(x).toString(); " + PMD.EOL +
" new Long(x).toString(); " + PMD.EOL +
" new Float(x).toString(); " + PMD.EOL +
" new Byte((byte)x).toString(); " + PMD.EOL +
" new Double(x).toString(); " + PMD.EOL +
" new Short((short)x).toString(); " + PMD.EOL +
" }" + PMD.EOL +
" }";
" public class Foo {" + PMD.EOL +
" void method (int x) {" + PMD.EOL +
" new Integer(x).toString(); " + PMD.EOL +
" new Long(x).toString(); " + PMD.EOL +
" new Float(x).toString(); " + PMD.EOL +
" new Byte((byte)x).toString(); " + PMD.EOL +
" new Double(x).toString(); " + PMD.EOL +
" new Short((short)x).toString(); " + PMD.EOL +
" }" + PMD.EOL +
" }";
private static final String TEST2 =
" public class Foo {" + PMD.EOL +
" Long method (Foo foo) {" + PMD.EOL +
" return new Long(foo.get().toString()); " + PMD.EOL +
" }" + PMD.EOL +
" }";
}

View File

@ -17,6 +17,7 @@ import java.util.Set;
public class UnnecessaryConversionTemporary extends AbstractRule implements Rule {
private boolean inPrimaryExpressionContext;
private ASTPrimaryExpression primary;
private boolean usingPrimitiveWrapperAllocation;
private Set primitiveWrappers = new HashSet();
@ -36,6 +37,7 @@ public class UnnecessaryConversionTemporary extends AbstractRule implements Rule
}
// TODO... hmmm... is this inPrimaryExpressionContext gibberish necessary?
inPrimaryExpressionContext = true;
primary = node;
super.visit(node, data);
inPrimaryExpressionContext = false;
usingPrimitiveWrapperAllocation = false;
@ -54,11 +56,12 @@ public class UnnecessaryConversionTemporary extends AbstractRule implements Rule
}
public Object visit(ASTPrimarySuffix node, Object data) {
if (!inPrimaryExpressionContext || !usingPrimitiveWrapperAllocation) {
return super.visit(node, data);
}
if (node.getImage() != null && node.getImage().equals("toString")) {
addViolation(data, node);
if (inPrimaryExpressionContext && usingPrimitiveWrapperAllocation) {
if (node.getImage() != null && node.getImage().equals("toString")) {
if (node.jjtGetParent() == primary) {
addViolation(data, node);
}
}
}
return super.visit(node, data);
}

View File

@ -48,6 +48,7 @@
</subsection>
<subsection name="Contributors">
<ul>
<li>Maarten Coene - bug report for UnnecessaryConversionTemporary</li>
<li>Jorn Stampehl - Reported bug in UnusedModifier, reported and fixed bugs in JUnitTestsShouldContainAsserts/CyclomaticComplexity/TooManyFields, noticed redundancy of ExplicitCallToFinalize, reported bug in AvoidCallingFinalize, reported bug in JUnitAssertionsShouldIncludeMessage, reported bug in bug report on JUnitTestsShouldContainAsserts</li>
<li>Brian Remedios - code cleanup of StringUtil and various rules, cleanup of rule designer, code cleanup of net.sourceforge.pmd.ant.Formatter.java, code improvements to Eclipse plugin, created AbstractPoorMethodCall and refactored UseIndexOfChar</li>
<li>Ulrich Kriegel - reported Ant task documentation bug</li>