You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by he...@apache.org on 2011/02/18 13:49:14 UTC

svn commit: r1071986 - in /commons/proper/jexl/trunk: ./ src/main/java/org/apache/commons/jexl2/ src/main/java/org/apache/commons/jexl2/parser/ src/test/java/org/apache/commons/jexl2/

Author: henrib
Date: Fri Feb 18 12:49:13 2011
New Revision: 1071986

URL: http://svn.apache.org/viewvc?rev=1071986&view=rev
Log:
JEXL-108

Added:
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTMapLiteral.java   (with props)
Modified:
    commons/proper/jexl/trunk/pom.xml
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Debugger.java
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTArrayLiteral.java
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/Parser.jjt
    commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/IssuesTest.java

Modified: commons/proper/jexl/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/pom.xml?rev=1071986&r1=1071985&r2=1071986&view=diff
==============================================================================
--- commons/proper/jexl/trunk/pom.xml (original)
+++ commons/proper/jexl/trunk/pom.xml Fri Feb 18 12:49:13 2011
@@ -19,7 +19,7 @@
     <parent>
         <groupId>org.apache.commons</groupId>
         <artifactId>commons-parent</artifactId>
-        <version>17</version>
+        <version>18</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <groupId>org.apache.commons</groupId>

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Debugger.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Debugger.java?rev=1071986&r1=1071985&r2=1071986&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Debugger.java (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Debugger.java Fri Feb 18 12:49:13 2011
@@ -297,10 +297,12 @@ final class Debugger implements ParserVi
     public Object visit(ASTArrayLiteral node, Object data) {
         int num = node.jjtGetNumChildren();
         builder.append("[ ");
-        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;
@@ -464,10 +466,14 @@ final class Debugger implements ParserVi
     public Object visit(ASTMapLiteral node, Object data) {
         int num = node.jjtGetNumChildren();
         builder.append("{ ");
-        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);
+            }
+        } else {
+            builder.append(':');
         }
         builder.append(" }");
         return data;

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTArrayLiteral.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTArrayLiteral.java?rev=1071986&r1=1071985&r2=1071986&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTArrayLiteral.java (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTArrayLiteral.java Fri Feb 18 12:49:13 2011
@@ -34,7 +34,12 @@ public final class ASTArrayLiteral exten
     /** {@inheritDoc} */
     @Override
     public void jjtClose() {
-        constant = isConstant();
+        if (children == null || children.length == 0) {
+            array = new Object[0];
+            constant = true;
+        } else {
+            constant = isConstant();
+        }
     }
 
     /**

Added: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTMapLiteral.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTMapLiteral.java?rev=1071986&view=auto
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTMapLiteral.java (added)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTMapLiteral.java Fri Feb 18 12:49:13 2011
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.jexl2.parser;
+
+import java.util.Collections;
+import java.util.Map;
+
+public final class ASTMapLiteral extends JexlNode implements JexlNode.Literal<Object> {
+    /** The type literal value. */
+    Map<?,?> map = null;
+    /** Whether this array is constant or not. */
+    boolean constant = false;
+
+    ASTMapLiteral(int id) {
+        super(id);
+    }
+
+    ASTMapLiteral(Parser p, int id) {
+        super(p, id);
+    }
+
+
+    /** {@inheritDoc} */
+    @Override
+    public void jjtClose() {
+        if (children == null || children.length == 0) {
+            map = Collections.EMPTY_MAP;
+            constant = true;
+        } else {
+            constant = isConstant();
+        }
+    }
+
+    /**
+     *  Gets the literal value.
+     * @return the array literal
+     */
+    public Object getLiteral() {
+        return map;
+    }
+
+    /**
+     * Sets the literal value only if the descendants of this node compose a constant
+     * @param literal the literal array value
+     * @throws IllegalArgumentException if literal is not an array or null
+     */
+    public void setLiteral(Object literal) {
+        if (constant) {
+            if (!(literal instanceof Map<?,?>)) {
+                throw new IllegalArgumentException(literal.getClass() + " is not an array");
+            }
+            this.map = (Map<?,?>) literal;
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Object jjtAccept(ParserVisitor visitor, Object data) {
+        return visitor.visit(this, data);
+    }
+}

Propchange: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/ASTMapLiteral.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/Parser.jjt
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/Parser.jjt?rev=1071986&r1=1071985&r2=1071986&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/Parser.jjt (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/Parser.jjt Fri Feb 18 12:49:13 2011
@@ -460,12 +460,17 @@ void StringLiteral() :
 
 void ArrayLiteral() : {}
 {
-  <LBRACKET> Expression() ( <COMMA> Expression() )* <RBRACKET>
+  <LBRACKET> (Expression() ( <COMMA> Expression() )*)? <RBRACKET>
 }
 
 void MapLiteral() : {}
 {
-    <LCURLY>  MapEntry() ( <COMMA> MapEntry() )* <RCURLY>
+    <LCURLY>
+    (
+        MapEntry() ( <COMMA> MapEntry() )*
+    |
+        <COLON>
+    ) <RCURLY>
 }
 
 void MapEntry() : {}
@@ -533,9 +538,9 @@ void PrimaryExpression() #void : {}
 |
   LOOKAHEAD( <NEW> <LPAREN> ) Constructor()
 |
-  LOOKAHEAD( <LCURLY> MapEntry() ) MapLiteral()
+  LOOKAHEAD( <LCURLY> (Expression())? <COLON> ) MapLiteral()
 |
-  LOOKAHEAD( <LBRACKET> Expression() ) ArrayLiteral()
+  LOOKAHEAD( <LBRACKET> (Expression() | <RBRACKET>) ) ArrayLiteral()
 |
   Literal()
 }

Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/IssuesTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/IssuesTest.java?rev=1071986&r1=1071985&r2=1071986&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/IssuesTest.java (original)
+++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/IssuesTest.java Fri Feb 18 12:49:13 2011
@@ -23,7 +23,6 @@ import org.apache.commons.jexl2.internal
 import java.util.HashMap;
 import java.util.Map;
 import org.apache.commons.jexl2.introspection.UberspectImpl;
-import org.apache.commons.jexl2.parser.JexlNode;
 
 /**
  * Test cases for reported issues
@@ -479,4 +478,46 @@ public class IssuesTest extends JexlTest
             assertEquals(expected, value);
         }
     }
+
+    public void test108() throws Exception {
+        Expression expr;
+        Object value;
+        JexlEngine jexl = new JexlEngine();
+        expr = jexl.createExpression("size([])");
+        value = expr.evaluate(null);
+        assertEquals(0, value);
+        expr = jexl.createExpression(expr.dump());
+        value = expr.evaluate(null);
+        assertEquals(0, value);
+
+        expr = jexl.createExpression("if (true) { [] } else { {:} }");
+        value = expr.evaluate(null);
+        assertTrue(value.getClass().isArray());
+        expr = jexl.createExpression(expr.dump());
+        value = expr.evaluate(null);
+        assertTrue(value.getClass().isArray());
+
+        expr = jexl.createExpression("size({:})");
+        value = expr.evaluate(null);
+        assertEquals(0, value);
+        expr = jexl.createExpression(expr.dump());
+        value = expr.evaluate(null);
+        assertEquals(0, value);
+
+        expr = jexl.createExpression("if (false) { [] } else { {:} }");
+        value = expr.evaluate(null);
+        assertTrue(value instanceof Map<?,?>);
+        expr = jexl.createExpression(expr.dump());
+        value = expr.evaluate(null);
+        assertTrue(value instanceof Map<?,?>);
+    }
+
+    public void test109() throws Exception {
+        JexlEngine jexl = new JexlEngine();
+        Object value;
+        JexlContext context = new MapContext();
+        context.set("foo.bar", 40);
+        value = jexl.createExpression("foo.bar + 2").evaluate(context);
+        assertEquals(42, value);
+    }
 }