Fixed bug 1188371 - AvoidInstantiatingObjectsInLoops no longer fires on instantiations in loops when the 'new' keyword is preceded by a 'return' or a 'throw'.
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@3458 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -8,6 +8,7 @@ Fixed bug 1183032 - The XMLRenderer no longer throws a SimpleDateFormat exceptio
|
||||
Fixed bug 1188372 - AtLeastOneConstructor no longer fires on interfaces.
|
||||
Fixed bug 1190508 - UnnecessaryBooleanAssertion no longer fires on nested boolean literals.
|
||||
Fixed bug 1190461 - UnusedLocal no longer misses usages which are on the RHS of a right bit shift operator.
|
||||
Fixed bug 1188371 - AvoidInstantiatingObjectsInLoops no longer fires on instantiations in loops when the 'new' keyword is preceded by a 'return' or a 'throw'.
|
||||
Implemented RFE 1171095 - LabeledStatement nodes now contain the image of the label.
|
||||
Modified command line parameters; removed -jdk15 and -jdk13 parameters and added a -'targetjdk [1.3|1.4|1.5]' parameter.
|
||||
Modified CSVRenderer to include more columns.
|
||||
|
@ -30,6 +30,8 @@ public class AvoidInstantiatingObjectsInLoopsTest extends SimpleAggregatorTst {
|
||||
new TestDescriptor(TEST2, "TEST2", 1, rule),
|
||||
new TestDescriptor(TEST3, "TEST3", 1, rule),
|
||||
new TestDescriptor(TEST4, "TEST4", 2, rule),
|
||||
new TestDescriptor(TEST5, "throw new is OK", 0, rule),
|
||||
new TestDescriptor(TEST6, "return new in loop is OK", 0, rule),
|
||||
//new TestDescriptor(BUG_1114051, "BUG [ 1114051 ] Semi-false positive for instantiating new object in loop", 0, rule), //FIXME
|
||||
});
|
||||
}
|
||||
@ -71,6 +73,24 @@ public class AvoidInstantiatingObjectsInLoopsTest extends SimpleAggregatorTst {
|
||||
" }" + PMD.EOL +
|
||||
"}";
|
||||
|
||||
private static final String TEST5 =
|
||||
"public class Foo {" + PMD.EOL +
|
||||
" public void test1() {" + PMD.EOL +
|
||||
" for(;;) {" + PMD.EOL +
|
||||
" throw new Exception();" + PMD.EOL +
|
||||
" }" + PMD.EOL +
|
||||
" }" + PMD.EOL +
|
||||
"}";
|
||||
|
||||
private static final String TEST6 =
|
||||
"public class Foo {" + PMD.EOL +
|
||||
" public String test1() {" + PMD.EOL +
|
||||
" for(;;) {" + PMD.EOL +
|
||||
" return new String();" + PMD.EOL +
|
||||
" }" + PMD.EOL +
|
||||
" }" + PMD.EOL +
|
||||
"}";
|
||||
|
||||
private static final String BUG_1114051 =
|
||||
"public class Foo {" + PMD.EOL +
|
||||
" public void test1() {" + PMD.EOL +
|
||||
|
@ -9,7 +9,8 @@ import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
|
||||
import net.sourceforge.pmd.ast.ASTDoStatement;
|
||||
import net.sourceforge.pmd.ast.ASTForStatement;
|
||||
import net.sourceforge.pmd.ast.ASTWhileStatement;
|
||||
import net.sourceforge.pmd.ast.Node;
|
||||
import net.sourceforge.pmd.ast.ASTThrowStatement;
|
||||
import net.sourceforge.pmd.ast.ASTReturnStatement;
|
||||
|
||||
public class AvoidInstantiatingObjectsInLoops extends AbstractOptimizationRule {
|
||||
|
||||
@ -22,22 +23,30 @@ public class AvoidInstantiatingObjectsInLoops extends AbstractOptimizationRule {
|
||||
|
||||
|
||||
public Object visit(ASTAllocationExpression node, Object data) {
|
||||
if (insideLoop(node)) {
|
||||
if (insideLoop(node) && fourthParentNotThrow(node) && fourthParentNotReturn(node)) {
|
||||
addViolation((RuleContext) data, node);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
private boolean fourthParentNotThrow(ASTAllocationExpression node) {
|
||||
return !(node.jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent() instanceof ASTThrowStatement);
|
||||
}
|
||||
|
||||
private boolean fourthParentNotReturn(ASTAllocationExpression node) {
|
||||
return !(node.jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent() instanceof ASTReturnStatement);
|
||||
}
|
||||
|
||||
private boolean insideLoop(ASTAllocationExpression node) {
|
||||
Node doNode = node.getFirstParentOfType(ASTDoStatement.class);
|
||||
Node whileNode = node.getFirstParentOfType(ASTWhileStatement.class);
|
||||
Node forNode = node.getFirstParentOfType(ASTForStatement.class);
|
||||
if (doNode!=null)
|
||||
if (node.getFirstParentOfType(ASTDoStatement.class)!=null) {
|
||||
return true;
|
||||
if (whileNode!=null)
|
||||
}
|
||||
if (node.getFirstParentOfType(ASTWhileStatement.class)!=null) {
|
||||
return true;
|
||||
if (forNode!=null)
|
||||
}
|
||||
if (node.getFirstParentOfType(ASTForStatement.class)!=null) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -43,9 +43,9 @@
|
||||
</subsection>
|
||||
<subsection name="Contributors">
|
||||
<ul>
|
||||
<li>Tom Parker - AvoidInstantiatingObjectsInLoops bug report, AtLeastOneConstructor bug report</li>
|
||||
<li>Fred Hartman - Fixed bug in UnnecessaryBooleanAssertion</li>
|
||||
<li>Payal Subhash - Tweaks to CSVRenderer</li>
|
||||
<li>Tom Parker - AtLeastOneConstructor bug report</li>
|
||||
<li>Wouter Zelle - initial implementation of SimplifyConditional</li>
|
||||
<li>Eric Olander - SimplifyConditional fix, UseStringBufferForStringAppends, CollapsibleIfStatements, AvoidInstanceofChecksInCatchClause, AssignmentToNonFinalStatic rule, nice patch for DFAPanel cleanup, AvoidProtectedFieldInFinalClass, ImmutableFieldRule, noticed missing image in Postfix nodes</li>
|
||||
<li>Christophe Mourette - Reported JDK 1.3 problem with XMLRenderer</li>
|
||||
|
Reference in New Issue
Block a user