Fixed annotation bug: ClassCastException when a formal parameter had multiple annotations

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@5626 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Xavier Le Vourch
2007-11-03 20:04:40 +00:00
parent 741aca9fa8
commit abb7c6a9ab
3 changed files with 23 additions and 4 deletions

View File

@ -1,3 +1,7 @@
????, 2007 - 4.1:
Fixed annotation bug: ClassCastException when a formal parameter had multiple annotations
November 01, 2007 - 4.1rc1:
New rules:
Basic ruleset: AvoidUsingHardCodedIP,CheckResultSet

View File

@ -37,11 +37,25 @@ public class ASTVariableDeclaratorIdTest extends ParserTst {
assertEquals("String", name.getImage());
}
@Test
public void testAnnotations() throws Throwable {
ASTCompilationUnit acu = super.getNodes(ASTCompilationUnit.class, TEST_ANNOTATIONS).iterator().next();
ASTVariableDeclaratorId id = acu.findChildrenOfType(ASTVariableDeclaratorId.class).get(0);
ASTClassOrInterfaceType name = (ASTClassOrInterfaceType) id.getTypeNameNode().jjtGetChild(0);
assertEquals("String", name.getImage());
}
private static final String TYPE_NAME_NODE =
"public class Test {" + PMD.EOL +
" private String bar;" + PMD.EOL +
"}";
private static final String TEST_ANNOTATIONS =
"public class Foo {" + PMD.EOL +
" public void bar(@A1 @A2 String s) {}" + PMD.EOL +
"}";
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(ASTVariableDeclaratorIdTest.class);
}

View File

@ -75,11 +75,12 @@ public class ASTVariableDeclaratorId extends SimpleJavaNode implements TypeNode
}
private SimpleNode findTypeNameNode(Node node) {
if (node.jjtGetChild(0) instanceof ASTAnnotation) {
ASTType typeNode = (ASTType) node.jjtGetChild(1);
return (SimpleNode) typeNode.jjtGetChild(0);
int i = 0;
while (node.jjtGetChild(i) instanceof ASTAnnotation) {
// skip annotations
i++;
}
ASTType typeNode = (ASTType) node.jjtGetChild(0);
ASTType typeNode = (ASTType) node.jjtGetChild(i);
return (SimpleNode) typeNode.jjtGetChild(0);
}