commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hen...@apache.org
Subject svn commit: r1051561 - /commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Interpreter.java
Date Tue, 21 Dec 2010 17:10:29 GMT
Author: henrib
Date: Tue Dec 21 17:10:28 2010
New Revision: 1051561

URL: http://svn.apache.org/viewvc?rev=1051561&view=rev
Log:
Better control of ternary operator behavior confronted with null

Modified:
    commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Interpreter.java

Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Interpreter.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Interpreter.java?rev=1051561&r1=1051560&r2=1051561&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Interpreter.java (original)
+++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl2/Interpreter.java Tue
Dec 21 17:10:28 2010
@@ -257,7 +257,7 @@ public class Interpreter implements Pars
         if (namespace == null) {
             throw new JexlException(node, "no such function namespace " + prefix);
         }
-        // allow namespace to be instantiated as functor with context
+        // allow namespace to be instantiated as functor with context if possible, not an
error otherwise
         if (namespace instanceof Class<?>) {
             Object[] args = new Object[]{context};
             Constructor<?> ctor = uberspect.getConstructor(namespace,args, node);
@@ -1058,9 +1058,7 @@ public class Interpreter implements Pars
             }
         }
         if (result == null) {
-            if (isVariable
-                    && !(node.jjtGetParent() instanceof ASTTernaryNode)
-                    && !context.has(variableName.toString())) {
+            if (isVariable && !context.has(variableName.toString()) && !isTernaryProtected(node))
{
                 JexlException xjexl = new JexlException(node, "undefined variable " + variableName.toString());
                 return unknownVariable(xjexl);
             }
@@ -1068,6 +1066,25 @@ public class Interpreter implements Pars
         return result;
     }
 
+    /**
+     * Check if a null evaluated expression is protected by a ternary expression.
+     * The rationale is that the ternary / elvis expressions are meant for the user to explictly
take
+     * control over the error generation; ie, ternaries can return null even if the engine
in strict mode
+     * would normally throw an exception.
+     * @param node the expression node
+     * @return true if nullable variable, false otherwise
+     */
+    private boolean isTernaryProtected(JexlNode node) {
+        for(JexlNode walk = node.jjtGetParent(); walk != null; walk = walk.jjtGetParent())
{
+            if (walk instanceof ASTTernaryNode) {
+                return true;
+            } else if (!(walk instanceof ASTReference || walk instanceof ASTArrayAccess))
{
+                break;
+            }
+        }
+        return false;
+    }
+
     /** {@inheritDoc} */
     public Object visit(ASTSizeFunction node, Object data) {
         Object val = node.jjtGetChild(0).jjtAccept(this, data);



Mime
View raw message