#1305 variable declaration inside switch causes ClassCastException

This commit is contained in:
Andreas Dangel
2015-02-20 20:28:39 +01:00
parent 96b425be46
commit 9b8496ab16
3 changed files with 34 additions and 13 deletions

View File

@ -48,21 +48,16 @@ public class PrematureDeclarationRule extends AbstractJavaRule {
AbstractJavaNode grandparent = (AbstractJavaNode)node.jjtGetParent().jjtGetParent();
List<Node> nextBlocks = blocksAfter(grandparent, node);
List<ASTBlockStatement> nextBlocks = blocksAfter(grandparent, node);
ASTBlockStatement statement;
for (Node block : nextBlocks) {
for (ASTBlockStatement block : nextBlocks) {
if (hasReferencesIn(block, varName)) break;
statement = (ASTBlockStatement)block;
if (hasReferencesIn(statement, varName)) break;
if (hasExit(statement)) {
if (hasExit(block)) {
addViolation(data, node, varName);
break;
}
}
}
return visit((AbstractJavaNode) node, data);
}
@ -184,15 +179,18 @@ public class PrematureDeclarationRule extends AbstractJavaRule {
* @param node SimpleNode
* @return List
*/
private static List<Node> blocksAfter(AbstractJavaNode block, AbstractJavaNode node) {
private static List<ASTBlockStatement> blocksAfter(AbstractJavaNode block, AbstractJavaNode node) {
int count = block.jjtGetNumChildren();
int start = indexOf(block, node.jjtGetParent()) + 1;
List<Node> nextBlocks = new ArrayList<Node>(count);
List<ASTBlockStatement> nextBlocks = new ArrayList<ASTBlockStatement>(count);
for (int i=start; i<count; i++) {
nextBlocks.add(block.jjtGetChild(i));
Node maybeBlock = block.jjtGetChild(i);
if (maybeBlock instanceof ASTBlockStatement) {
nextBlocks.add((ASTBlockStatement)maybeBlock);
}
}
return nextBlocks;

View File

@ -65,4 +65,26 @@ public class Bar {
}
]]></code>
</test-code>
<test-code>
<description>#1305 variable declaration inside switch causes ClassCastException</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class PrematureDeclarationClassCastException {
public void bar() {
int switchvar = 0;
switch (switchvar) {
case 1:
boolean localvar;
break;
case 2:
localvar = false;
if (localvar) {
//
}
}
}
}
]]></code>
</test-code>
</test-data>