This is an automated email from the ASF dual-hosted git repository.

henrib pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jexl.git


The following commit(s) were added to refs/heads/master by this push:
     new 9dab666  JEXL-324: fixed grammar for new(arg, ...), made one arg 
mandatory - fixed debugger (belt and suspenders) Task #JEXL-324 - 
JexlEngine.createExpression("new()").getParsedText() throws NPE
9dab666 is described below

commit 9dab666d3ecb09fc9ab2e9acd9486ac066e888c8
Author: henrib <hen...@apache.org>
AuthorDate: Mon Jan 27 22:48:43 2020 +0100

    JEXL-324: fixed grammar for new(arg, ...), made one arg mandatory
    - fixed debugger (belt and suspenders)
    Task #JEXL-324 - JexlEngine.createExpression("new()").getParsedText() 
throws NPE
---
 .../apache/commons/jexl3/internal/Debugger.java    | 12 ++++---
 .../org/apache/commons/jexl3/parser/Parser.jjt     |  6 ++--
 .../org/apache/commons/jexl3/Issues300Test.java    | 40 +++++++++++++++++++++-
 3 files changed, 49 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/apache/commons/jexl3/internal/Debugger.java 
b/src/main/java/org/apache/commons/jexl3/internal/Debugger.java
index 0312c50..e3ffecc 100644
--- a/src/main/java/org/apache/commons/jexl3/internal/Debugger.java
+++ b/src/main/java/org/apache/commons/jexl3/internal/Debugger.java
@@ -801,10 +801,12 @@ public class Debugger extends ParserVisitor implements 
JexlInfo.Detail {
     protected Object visit(ASTConstructorNode node, Object data) {
         int num = node.jjtGetNumChildren();
         builder.append("new(");
-        accept(node.jjtGetChild(0), data);
-        for (int i = 1; i < num; ++i) {
-            builder.append(", ");
-            accept(node.jjtGetChild(i), data);
+        if (num > 0) {
+            accept(node.jjtGetChild(0), data);
+            for (int i = 1; i < num; ++i) {
+                builder.append(", ");
+                accept(node.jjtGetChild(i), data);
+            }
         }
         builder.append(")");
         return data;
@@ -818,7 +820,7 @@ public class Debugger extends ParserVisitor implements 
JexlInfo.Detail {
             builder.append(":");
             accept(node.jjtGetChild(1), data);
             accept(node.jjtGetChild(2), data);
-        } else {
+        } else if (num == 2) {
             accept(node.jjtGetChild(0), data);
             accept(node.jjtGetChild(1), data);
         }
diff --git a/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt 
b/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
index 89390dd..5cbb9a6 100644
--- a/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
+++ b/src/main/java/org/apache/commons/jexl3/parser/Parser.jjt
@@ -70,8 +70,8 @@ public final class Parser extends JexlParser
         } catch (TokenMgrError xtme) {
             throw new JexlException.Tokenization(info, xtme).clean();
         } catch (ParseException xparse) {
-            Token errortok = token.next;
-            throw new JexlException.Parsing(info.at(errortok.beginLine, 
errortok.beginColumn), token.image).clean();
+            Token errortok = jj_lastpos != null? jj_lastpos : jj_scanpos != 
null ? jj_scanpos : token;
+            throw new JexlException.Parsing(info.at(errortok.beginLine, 
errortok.beginColumn), errortok.image).clean();
         } finally {
             token_source.defaultLexState = DEFAULT;
             cleanup(previous);
@@ -811,7 +811,7 @@ void FunctionCall() #void : {}
 
 void Constructor() #ConstructorNode() : {}
 {
-  <NEW> <LPAREN> [ Expression() ( <COMMA> Expression() )* ] <RPAREN>
+  <NEW> <LPAREN> Expression() ( <COMMA> Expression() )* <RPAREN>
 }
 
 void Parameter() #void :
diff --git a/src/test/java/org/apache/commons/jexl3/Issues300Test.java 
b/src/test/java/org/apache/commons/jexl3/Issues300Test.java
index 6649f3a..a7b5904 100644
--- a/src/test/java/org/apache/commons/jexl3/Issues300Test.java
+++ b/src/test/java/org/apache/commons/jexl3/Issues300Test.java
@@ -25,6 +25,7 @@ import java.util.List;
 import java.util.Map;
 import org.junit.Assert;
 import static org.junit.Assert.assertEquals;
+import org.junit.Ignore;
 import org.junit.Test;
 
 /**
@@ -377,5 +378,42 @@ public class Issues300Test {
             Assert.assertEquals(ctls[i], output);
         }
     }
-
+    
+    @Ignore
+    public void test323() throws Exception {
+        // Create or retrieve an engine
+        JexlEngine jexl = new JexlBuilder().safe(false).create();
+        // on recent code: JexlEngine jexl = new 
JexlBuilder().safe(false).create();
+
+        // Populate to identical global variables
+        JexlContext jc = new MapContext();
+        jc.set("NormalVariable", null);
+        jc.set("ant.variable", null);
+
+        // Evaluate the value of the normal variable
+        JexlExpression expression1 = jexl.createExpression("NormalVariable");
+        Object o1 = expression1.evaluate(jc);
+        Assert.assertEquals(null, o1);
+
+        // Evaluate the value of the ant-style variable
+        JexlExpression expression2 = jexl.createExpression("ant.variable");
+        Object o2 = expression2.evaluate(jc); // <-- BUG: throws exception 
instead of returning null
+        Assert.assertEquals(null, o2);
+    }
+    
+    @Test
+    public void test324() throws Exception {
+        JexlEngine jexl = new JexlBuilder().create();
+        String src42 = "new('java.lang.Integer', 42)";
+        JexlExpression expr0 = jexl.createExpression(src42);
+        Assert.assertEquals(42, expr0.evaluate(null));
+        String parsed = expr0.getParsedText();
+        Assert.assertEquals(src42, parsed);
+        try {
+            JexlExpression expr = jexl.createExpression("new()");
+            Assert.fail("should not parse");
+        } catch (JexlException.Parsing xparse) {
+            Assert.assertTrue(xparse.toString().contains("new"));
+        }
+    }
 }

Reply via email to