You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2009/08/05 21:28:16 UTC

svn commit: r801374 - in /commons/proper/jexl/branches/2.0/src: main/java/org/apache/commons/jexl/Interpreter.java test/java/org/apache/commons/jexl/MapLiteralTest.java

Author: sebb
Date: Wed Aug  5 19:28:15 2009
New Revision: 801374

URL: http://svn.apache.org/viewvc?rev=801374&view=rev
Log:
JEXL-74 Maps do not handle integer keys correctly 

Modified:
    commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Interpreter.java
    commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/MapLiteralTest.java

Modified: commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Interpreter.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Interpreter.java?rev=801374&r1=801373&r2=801374&view=diff
==============================================================================
--- commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Interpreter.java (original)
+++ commons/proper/jexl/branches/2.0/src/main/java/org/apache/commons/jexl/Interpreter.java Wed Aug  5 19:28:15 2009
@@ -325,6 +325,20 @@
                 context.getVars().put(String.valueOf(property), right);
                 return right;
             }
+        } else if (propertyNode instanceof ASTIntegerLiteral) {
+            property = visit((ASTIntegerLiteral) propertyNode, null);
+            // deal with ant variable
+            if (isVariable && object == null) {
+                if (variableName != null) {
+                    if (last > 0) {
+                        variableName.append('.');
+                    }
+                    variableName.append(property);
+                    property = variableName.toString();
+                }
+                context.getVars().put(String.valueOf(property), right);
+                return right;
+            }
         } else if (propertyNode instanceof ASTArrayAccess) {
             // first objectNode is the identifier
             objectNode = propertyNode;
@@ -1168,7 +1182,7 @@
                 }
             }
         }
-        VelPropertyGet vg = uberspect.getPropertyGet(object, attribute.toString(), node);
+        VelPropertyGet vg = uberspect.getPropertyGet(object, attribute, node);
         if (vg != null) {
             try {
                 Object value = vg.invoke(object);
@@ -1211,7 +1225,6 @@
      * @param node the node that evaluated as the object
      */
     protected void setAttribute(Object object, Object attribute, Object value, JexlNode node) {
-        String s = attribute.toString();
         // attempt to reuse last executor cached in volatile JexlNode.value
         if (node != null && cache) {
             Object cached = node.jjtGetValue();
@@ -1223,7 +1236,7 @@
                 }
             }
         }
-        VelPropertySet vs = uberspect.getPropertySet(object, s, value, node);
+        VelPropertySet vs = uberspect.getPropertySet(object, attribute, value, node);
         if (vs != null) {
             try {
                 // cache executor in volatile JexlNode.value
@@ -1260,4 +1273,4 @@
     public Object visit(SimpleNode node, Object data) {
         throw new UnsupportedOperationException("Not supported yet.");
     }
-}
\ No newline at end of file
+}

Modified: commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/MapLiteralTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/MapLiteralTest.java?rev=801374&r1=801373&r2=801374&view=diff
==============================================================================
--- commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/MapLiteralTest.java (original)
+++ commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/MapLiteralTest.java Wed Aug  5 19:28:15 2009
@@ -54,6 +54,36 @@
 
         Object o = e.evaluate( jc );
         assertEquals( Collections.singletonMap( new Integer( 5 ), new Integer( 10 ) ), o );
+
+        e = JEXL.createExpression("m = [ 3 => 30, 4 => 40, 5 => 'fifty', '7' => 'seven', 7 => 'SEVEN' ]");
+        e.evaluate(jc);
+
+        e = JEXL.createExpression("m.3");
+        o = e.evaluate(jc);
+        assertEquals(new Integer(30), o);
+
+        e = JEXL.createExpression("m[4]");
+        o = e.evaluate(jc);
+        assertEquals(new Integer(40), o);
+
+        jc.getVars().put("i", Integer.valueOf(5));
+        e = JEXL.createExpression("m[i]");
+        o = e.evaluate(jc);
+        assertEquals("fifty", o);
+
+        e = JEXL.createExpression("m.3 = 'thirty'");
+        e.evaluate(jc);
+        e = JEXL.createExpression("m.3");
+        o = e.evaluate(jc);
+        assertEquals("thirty", o);
+
+        e = JEXL.createExpression("m['7']");
+        o = e.evaluate(jc);
+        assertEquals("seven", o);
+
+        e = JEXL.createExpression("m.7");
+        o = e.evaluate(jc);
+        assertEquals("SEVEN", o);
     }
 
     public void testSizeOfSimpleMapLiteral() throws Exception {
@@ -80,4 +110,16 @@
         assertFalse( ( (Boolean) o ).booleanValue() );
     }
 
+    public void testMapMapLiteral() throws Exception {
+        Expression e = JEXL.createExpression( "['foo' => [ 'inner' => 'bar' ]]" );
+        JexlContext jc = JexlHelper.createContext();
+        Object o = e.evaluate( jc );
+        assertNotNull(o);
+
+        jc.getVars().put("outer", o);
+        e = JEXL.createExpression("outer.foo.inner");
+        o = e.evaluate( jc );
+        assertEquals( "bar", o );
+    }
+    
 }