pmd: fix #1118 ClassCastException in pmd.lang.ecmascript.ast.ASTElementGet
This commit is contained in:
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
Fixed bug 991: AvoidSynchronizedAtMethodLevel for static methods
|
Fixed bug 991: AvoidSynchronizedAtMethodLevel for static methods
|
||||||
Fixed bug 1114: CPD - Tokenizer not initialized with requested properties
|
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:
|
May 1, 2013 - 5.0.4:
|
||||||
|
@ -19,10 +19,10 @@ public class ASTElementGet extends AbstractEcmascriptNode<ElementGet> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public EcmascriptNode getTarget() {
|
public EcmascriptNode getTarget() {
|
||||||
return (EcmascriptNode) node.getTarget();
|
return EcmascriptTreeBuilder.createNodeAdapter(node.getTarget());
|
||||||
}
|
}
|
||||||
|
|
||||||
public EcmascriptNode getElement() {
|
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;
|
this.parseProblems = parseProblems;
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T extends AstNode> EcmascriptNode<T> createNodeAdapter(T node) {
|
static <T extends AstNode> EcmascriptNode<T> createNodeAdapter(T node) {
|
||||||
try {
|
try {
|
||||||
@SuppressWarnings("unchecked") // the register function makes sure only EcmascriptNode<T> can be added,
|
@SuppressWarnings("unchecked") // the register function makes sure only EcmascriptNode<T> can be added,
|
||||||
// where T is "T extends AstNode".
|
// where T is "T extends AstNode".
|
||||||
|
@ -7,6 +7,7 @@ import static org.junit.Assert.assertEquals;
|
|||||||
|
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import net.sourceforge.pmd.PMD;
|
import net.sourceforge.pmd.PMD;
|
||||||
import net.sourceforge.pmd.lang.ast.Node;
|
import net.sourceforge.pmd.lang.ast.Node;
|
||||||
@ -17,13 +18,15 @@ import org.mozilla.javascript.ast.AstRoot;
|
|||||||
|
|
||||||
public class EcmascriptParserTest {
|
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
|
@Test
|
||||||
public void testLineNumbers() {
|
public void testLineNumbers() {
|
||||||
|
EcmascriptNode<AstRoot> node = parse(SOURCE_CODE);
|
||||||
EcmascriptParser parser = new EcmascriptParser(new EcmascriptParserOptions());
|
|
||||||
Reader sourceCode = new StringReader(SOURCE_CODE);
|
|
||||||
EcmascriptNode<AstRoot> node = parser.parse(sourceCode);
|
|
||||||
|
|
||||||
assertEquals(1, node.getBeginLine());
|
assertEquals(1, node.getBeginLine());
|
||||||
assertEquals(1, node.getBeginColumn());
|
assertEquals(1, node.getBeginColumn());
|
||||||
assertEquals(3, node.getEndLine());
|
assertEquals(3, node.getEndLine());
|
||||||
@ -42,6 +45,19 @@ public class EcmascriptParserTest {
|
|||||||
assertEquals(16, child.getEndColumn());
|
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 =
|
private static final String SOURCE_CODE =
|
||||||
"function a() {" + PMD.EOL
|
"function a() {" + PMD.EOL
|
||||||
+ " alert('hello');" + PMD.EOL
|
+ " alert('hello');" + PMD.EOL
|
||||||
|
Reference in New Issue
Block a user