Author: sebb Date: Sat Aug 1 17:56:55 2009 New Revision: 799903 URL: http://svn.apache.org/viewvc?rev=799903&view=rev Log: JEXL-62 Avoid NPE in Interpreter when () omitted from method with no parameters (e.g. var.hashCode) Includes test case.
Thanks for patch by Henri Biestro <hbiestro at gmail dot com>. Modified: commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/Interpreter.java commons/proper/jexl/branches/2.0/src/test/org/apache/commons/jexl/IssuesTest.java Modified: commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/Interpreter.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/Interpreter.java?rev=799903&r1=799902&r2=799903&view=diff ============================================================================== --- commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/Interpreter.java (original) +++ commons/proper/jexl/branches/2.0/src/java/org/apache/commons/jexl/Interpreter.java Sat Aug 1 17:56:55 2009 @@ -279,6 +279,7 @@ JexlNode propertyNode = null; Object property = null; boolean isVariable = true; + int v = 0; StringBuilder variableName = null; // 1: follow children till penultimate int last = left.jjtGetNumChildren() - 1; @@ -293,9 +294,11 @@ // if we get null back as a result, check for an ant variable if (isVariable) { String name = ((ASTIdentifier) objectNode).image; - if (c == 0) { + if (v == 0) { variableName = new StringBuilder(name); - } else { + v = 1; + } + for(; v <= c; ++v) { variableName.append('.'); variableName.append(name); } @@ -958,6 +961,7 @@ StringBuilder variableName = null; Map<String, ?> vars = context.getVars(); boolean isVariable = true; + int v = 0; for (int i = 0; i < numChildren; i++) { JexlNode theNode = node.jjtGetChild(i); isVariable &= (theNode instanceof ASTIdentifier); @@ -965,9 +969,11 @@ // if we get null back a result, check for an ant variable if (result == null && isVariable) { String name = ((ASTIdentifier) theNode).image; - if (i == 0) { + if (v == 0) { variableName = new StringBuilder(name); - } else { + v = 1; + } + for(; v <= i; ++v) { variableName.append('.'); variableName.append(name); } Modified: commons/proper/jexl/branches/2.0/src/test/org/apache/commons/jexl/IssuesTest.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/test/org/apache/commons/jexl/IssuesTest.java?rev=799903&r1=799902&r2=799903&view=diff ============================================================================== --- commons/proper/jexl/branches/2.0/src/test/org/apache/commons/jexl/IssuesTest.java (original) +++ commons/proper/jexl/branches/2.0/src/test/org/apache/commons/jexl/IssuesTest.java Sat Aug 1 17:56:55 2009 @@ -183,4 +183,35 @@ } } + // JEXL-62 + public void test62() throws Exception { + JexlContext ctxt; + JexlEngine jexl = new JexlEngine(); + jexl.setSilent(true); // to avoid throwing JexlException on null method call + + Script jscript; + + ctxt = JexlHelper.createContext(); + jscript = jexl.createScript("dummy.hashCode()"); + assertEquals(jscript.getText(), null, jscript.execute(ctxt)); // OK + + ctxt.getVars().put("dummy", "abcd"); + assertEquals(jscript.getText(), Integer.valueOf("abcd".hashCode()), jscript.execute(ctxt)); // OK + + jscript = jexl.createScript("dummy.hashCode"); + assertEquals(jscript.getText(), null, jscript.execute(ctxt)); // OK + + Expression jexpr; + + ctxt = JexlHelper.createContext(); + jexpr = jexl.createExpression("dummy.hashCode()"); + assertEquals(jexpr.getExpression(), null, jexpr.evaluate(ctxt)); // OK + + ctxt.getVars().put("dummy", "abcd"); + assertEquals(jexpr.getExpression(), Integer.valueOf("abcd".hashCode()), jexpr.evaluate(ctxt)); // OK + + jexpr = jexl.createExpression("dummy.hashCode"); + assertEquals(jexpr.getExpression(), null, jexpr.evaluate(ctxt)); // OK + } + } \ No newline at end of file