pmd: fix #1118 ClassCastException in pmd.lang.ecmascript.ast.ASTElementGet

This commit is contained in:
Andreas Dangel committed 2013-08-03 14:17:11 +02:00
1 parent 66e8cfc622
commit 9255e26c70
4 files changed
+25 -8

No files matched your search

+1
View File
@@ -25,6 +25,7 @@ CPD Command Line Changes:
Fixed bug 991: AvoidSynchronizedAtMethodLevel for static methods
Fixed bug 1114: CPD - Tokenizer not initialized with requested properties
Fixed bug 1118: ClassCastException in pmd.lang.ecmascript.ast.ASTElementGet
May 1, 2013 - 5.0.4:
@@ -19,10 +19,10 @@ public class ASTElementGet extends AbstractEcmascriptNode<ElementGet> {
}
public EcmascriptNode getTarget() {
return (EcmascriptNode) node.getTarget();
return EcmascriptTreeBuilder.createNodeAdapter(node.getTarget());
}
public EcmascriptNode getElement() {
return (EcmascriptNode) node.getElement();
return EcmascriptTreeBuilder.createNodeAdapter(node.getElement());
}
}
@@ -146,7 +146,7 @@ public final class EcmascriptTreeBuilder implements NodeVisitor {
this.parseProblems = parseProblems;
}
private <T extends AstNode> EcmascriptNode<T> createNodeAdapter(T node) {
static <T extends AstNode> EcmascriptNode<T> createNodeAdapter(T node) {
try {
@SuppressWarnings("unchecked") // the register function makes sure only EcmascriptNode<T> can be added,
// where T is "T extends AstNode".
@@ -7,6 +7,7 @@ import static org.junit.Assert.assertEquals;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.lang.ast.Node;
@@ -17,13 +18,15 @@ import org.mozilla.javascript.ast.AstRoot;
public class EcmascriptParserTest {
private EcmascriptNode<AstRoot> parse(String code) {
EcmascriptParser parser = new EcmascriptParser(new EcmascriptParserOptions());
Reader sourceCode = new StringReader(code);
return parser.parse(sourceCode);
}
@Test
public void testLineNumbers() {
EcmascriptParser parser = new EcmascriptParser(new EcmascriptParserOptions());
Reader sourceCode = new StringReader(SOURCE_CODE);
EcmascriptNode<AstRoot> node = parser.parse(sourceCode);
EcmascriptNode<AstRoot> node = parse(SOURCE_CODE);
assertEquals(1, node.getBeginLine());
assertEquals(1, node.getBeginColumn());
assertEquals(3, node.getEndLine());
@@ -42,6 +45,19 @@ public class EcmascriptParserTest {
assertEquals(16, child.getEndColumn());
}
/**
* Test bug https://sourceforge.net/p/pmd/bugs/1118/
*/
@Test
public void testArrayAccess() {
EcmascriptNode<AstRoot> node = parse("function a() { b['a'] = 1; c[1] = 2; }");
List<ASTElementGet> arrays = node.findDescendantsOfType(ASTElementGet.class);
assertEquals("b", arrays.get(0).getTarget().getImage());
assertEquals("a", arrays.get(0).getElement().getImage());
assertEquals("c", arrays.get(1).getTarget().getImage());
assertEquals("1", arrays.get(1).getElement().getImage());
}
private static final String SOURCE_CODE =
"function a() {" + PMD.EOL
+ " alert('hello');" + PMD.EOL