Hello,
In some circumstances call to node.getLeft() or node.getRight() generated a NullPointerException.
function test(){
a(); // OK
b.c(); // OK
d[0](); // OK
e[0].f(); // OK
y.z[0](); // FAIL ==> java.lang.NullPointerException
}
package com.home.cwcodecheck.pmd;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ecmascript.ast.ASTElementGet;
import net.sourceforge.pmd.lang.ecmascript.ast.ASTFunctionCall;
import net.sourceforge.pmd.lang.ecmascript.ast.ASTName;
import net.sourceforge.pmd.lang.ecmascript.ast.ASTPropertyGet;
import net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule;
public class DebugPMD extends AbstractEcmascriptRule {
public Object visit(final ASTFunctionCall node, final Object data) {
System.out.println("call: " + getName(node.getTarget()) + "()");
return super.visit(node, data);
}
protected String getName(final Node node){
if( node instanceof ASTName ){
return ((ASTName)node).getIdentifier();
}
if( node instanceof ASTPropertyGet ){
final ASTPropertyGet pgNode = (ASTPropertyGet)node;
final String leftName = getName(pgNode.**getLeft()**);
final String rightName = getName(pgNode.**getRight()**);
return leftName + "." + rightName;
}
if( node instanceof ASTElementGet ){
return getName(((ASTElementGet)node).getTarget()) + "[]";
}
return "????";
}
}
call: a()
call: b.c()
call: d[]()
call: e[].f()
java.lang.NullPointerException
at net.sourceforge.pmd.lang.ast.AbstractNode.jjtGetChild(AbstractNode.java:77)
at net.sourceforge.pmd.lang.ecmascript.ast.AbstractInfixEcmascriptNode.getLeft(AbstractInfixEcmascriptNode.java:23)
at com.home.cwcodecheck.pmd.DebugPMD.getName(DebugPMD.java:31)
at com.home.cwcodecheck.pmd.DebugPMD.getName(DebugPMD.java:38)
at com.home.cwcodecheck.pmd.DebugPMD.visit(DebugPMD.java:17)
at net.sourceforge.pmd.lang.ecmascript.ast.ASTFunctionCall.jjtAccept(ASTFunctionCall.java:17)
at net.sourceforge.pmd.lang.ecmascript.ast.AbstractEcmascriptNode.childrenAccept(AbstractEcmascriptNode.java:48)
at net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule.visit(AbstractEcmascriptRule.java:105)
at net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule.visit(AbstractEcmascriptRule.java:166)
at net.sourceforge.pmd.lang.ecmascript.ast.ASTExpressionStatement.jjtAccept(ASTExpressionStatement.java:19)
at net.sourceforge.pmd.lang.ecmascript.ast.AbstractEcmascriptNode.childrenAccept(AbstractEcmascriptNode.java:48)
at net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule.visit(AbstractEcmascriptRule.java:105)
at net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule.visit(AbstractEcmascriptRule.java:130)
at net.sourceforge.pmd.lang.ecmascript.ast.ASTBlock.jjtAccept(ASTBlock.java:18)
at net.sourceforge.pmd.lang.ecmascript.ast.AbstractEcmascriptNode.childrenAccept(AbstractEcmascriptNode.java:48)
at net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule.visit(AbstractEcmascriptRule.java:105)
at net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule.visit(AbstractEcmascriptRule.java:182)
at net.sourceforge.pmd.lang.ecmascript.ast.ASTFunctionNode.jjtAccept(ASTFunctionNode.java:18)
In place of the java stack dump, the expected result is
call: y.z[]();
NOTE: The problem appears during the getLeft() because it is called before getRight(). But there is the same problem if getRight() is called first.
Regards
Hi,
In fact the problem is not in getLeft() ot getRight() but in pmd.lang.ecmascript.ASTElementGet.getTarget() and getElement(). (These functions have been changed to fix bug [#1118])
In some circumstances (or always?) these functions return an ASTPropertyGet node with no children.
If you try to use the children of this invalid node, for example by calling getLeft() or getRight(), you get the NullPointerException because the children array is null.
It is obvious that the problem is in the function EcmascriptTreeBuilder.createNodeAdapter(), but my understanding of the PMD internals stops here.
Regards
Related
Issues:
#1118Last edit: Jimmy Profit 2013-10-09
You're right - basically the fix for [#1118] was not complete.
Commit: https://github.com/pmd/pmd/commit/64e35caa50196270939c334588f9ef7c8faa2567
Related
Issues:
#1118