commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hen...@apache.org
Subject svn commit: r1051565 - in /commons/proper/jexl/trunk/src: main/java/org/apache/commons/jexl2/parser/ASTArrayLiteral.java main/java/org/apache/commons/jexl2/parser/JexlNode.java test/java/org/apache/commons/jexl2/JexlTestCase.java
Date Tue, 21 Dec 2010 17:18:08 GMT
Author: henrib
Date: Tue Dec 21 17:18:07 2010
New Revision: 1051565

URL: http://svn.apache.org/viewvc?rev=1051565&view=rev
Log:
JEXL-105 fix; added an "isConstant" check on JexlNode to allow detection of constant array
literals (could be generalized to maps and even to other expressions)

Modified:
    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/JexlNode.java
    commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/JexlTestCase.java

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=1051565&r1=1051564&r2=1051565&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
Tue Dec 21 17:18:07 2010
@@ -19,6 +19,8 @@ package org.apache.commons.jexl2.parser;
 public final class ASTArrayLiteral extends JexlNode implements JexlNode.Literal<Object>
{
     /** The type literal value. */
     Object array = null;
+    /** Whether this array is constant or not. */
+    boolean constant = false;
 
     ASTArrayLiteral(int id) {
         super(id);
@@ -28,6 +30,13 @@ public final class ASTArrayLiteral exten
         super(p, id);
     }
 
+
+    /** {@inheritDoc} */
+    @Override
+    public void jjtClose() {
+        constant = isConstant();
+    }
+
     /**
      *  Gets the literal value.
      * @return the array literal
@@ -37,15 +46,17 @@ public final class ASTArrayLiteral exten
     }
 
     /**
-     * Sets the literal value.
+     * 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 (literal != null && !literal.getClass().isArray()) {
-            throw new IllegalArgumentException(literal.getClass() + " is not an array");
+        if (constant) {
+            if (literal != null && !literal.getClass().isArray()) {
+                throw new IllegalArgumentException(literal.getClass() + " is not an array");
+            }
+            this.array = literal;
         }
-        this.array = literal;
     }
 
     /** {@inheritDoc} */

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/JexlNode.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/JexlNode.java?rev=1051565&r1=1051564&r2=1051565&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/JexlNode.java
(original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/parser/JexlNode.java
Tue Dec 21 17:18:07 2010
@@ -58,4 +58,23 @@ public abstract class JexlNode extends S
         JexlInfo info = getInfo();
         return info != null? info.debugString() : "";
     }
+
+    /**
+     * Whether this node is a constant node
+     * Its value can not change after the first evaluation and can be cached indefinitely.
+     * @return true if constant, false otherwise
+     */
+    public boolean isConstant() {
+        if (this instanceof JexlNode.Literal<?>) {
+            if (children != null) {
+                for(JexlNode child : children) {
+                    if (!child.isConstant()) {
+                        return false;
+                    }
+                }
+            }
+            return true;
+        }
+        return false;
+    }
 }

Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/JexlTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/JexlTestCase.java?rev=1051565&r1=1051564&r2=1051565&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/JexlTestCase.java (original)
+++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/JexlTestCase.java Tue
Dec 21 17:18:07 2010
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.commons.jexl2;
+package org.apache.commons.jexl2; 
 
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;



Mime
View raw message