commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r202229 - in /jakarta/commons/proper/jexl/trunk/src: java/org/apache/commons/jexl/parser/ test/org/apache/commons/jexl/
Date Tue, 28 Jun 2005 15:43:03 GMT
Author: dion
Date: Tue Jun 28 08:43:00 2005
New Revision: 202229

URL: http://svn.apache.org/viewcvs?rev=202229&view=rev
Log:
Bugzilla 32829:
 'abc'.substring(0,1+1) is empty
 'abc'.substring(0,2) gives ab as expected.

Modified:
    jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTAddNode.java
    jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTDivNode.java
    jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTModNode.java
    jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTMulNode.java
    jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTSubtractNode.java
    jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/JexlTest.java

Modified: jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTAddNode.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTAddNode.java?rev=202229&r1=202228&r2=202229&view=diff
==============================================================================
--- jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTAddNode.java
(original)
+++ jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTAddNode.java
Tue Jun 28 08:43:00 2005
@@ -54,7 +54,7 @@
          *  the spec says 'and'
          */
         if (left == null && right == null)
-            return new Long(0);
+            return new Byte((byte)0);
 
         /*
          *  if anything is float, double or string with ( "." | "E" | "e")
@@ -103,8 +103,8 @@
         {
             Long l = left == null ? new Long(0) : Coercion.coerceLong(left);
             Long r = right == null ? new Long(0) : Coercion.coerceLong(right);
-
-            return new Long(l.longValue() + r.longValue());
+            Long result = new Long(l.longValue() + r.longValue());
+            return unwiden(result);
         }
         catch( java.lang.NumberFormatException nfe )
         {
@@ -113,5 +113,29 @@
              */
             return left.toString().concat(right.toString());
         }
+    }
+    
+    /**
+     * Given a long, return back the smallest type the result will fit into.
+     * This works hand in hand with parameter 'widening' in java method calls,
+     * e.g. a call to substring(int,int) with an int and a long will fail, but
+     * a call to substring(int,int) with an int and a short will succeed.
+     */
+    public static Number unwiden(Long result)
+    {
+        if (result.longValue() <= Byte.MAX_VALUE && result.longValue() >= Byte.MIN_VALUE)
+        {
+            // it will fit in a byte
+            return new Byte((byte)result.longValue());
+        }
+        else if (result.longValue() <= Short.MAX_VALUE && result.longValue() >=
Short.MIN_VALUE)
+        {
+            return new Short((short)result.longValue());
+        }
+        else if (result.longValue() <= Integer.MAX_VALUE && result.longValue()
>= Integer.MIN_VALUE)
+        {
+            return new Integer((int)result.longValue());
+        }
+        return result;
     }
 }

Modified: jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTDivNode.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTDivNode.java?rev=202229&r1=202228&r2=202229&view=diff
==============================================================================
--- jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTDivNode.java
(original)
+++ jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTDivNode.java
Tue Jun 28 08:43:00 2005
@@ -53,7 +53,7 @@
          *  the spec says 'and', I think 'or'
          */
         if (left == null && right == null)
-            return new Integer(0);
+            return new Byte((byte)0);
 
         Double l = Coercion.coerceDouble(left);
         Double r = Coercion.coerceDouble(right);

Modified: jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTModNode.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTModNode.java?rev=202229&r1=202228&r2=202229&view=diff
==============================================================================
--- jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTModNode.java
(original)
+++ jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTModNode.java
Tue Jun 28 08:43:00 2005
@@ -54,7 +54,7 @@
          *  the spec says 'and', I think 'or'
          */
         if (left == null && right == null)
-            return new Integer(0);
+            return new Byte((byte)0);
 
         /*
          *  if anything is float, double or string with ( "." | "E" | "e")
@@ -99,7 +99,7 @@
         if (r.longValue() == 0)
             return new Long(0);
 
-        return new Long(l.longValue() % r.longValue());
+        return ASTAddNode.unwiden(new Long(l.longValue() % r.longValue()));
     }
 
 }

Modified: jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTMulNode.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTMulNode.java?rev=202229&r1=202228&r2=202229&view=diff
==============================================================================
--- jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTMulNode.java
(original)
+++ jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTMulNode.java
Tue Jun 28 08:43:00 2005
@@ -54,7 +54,7 @@
          *  the spec says 'and', I think 'or'
          */
         if (left == null && right == null)
-            return new Integer(0);
+            return new Byte((byte)0);
 
         /*
          *  if anything is float, double or string with ( "." | "E" | "e")
@@ -87,7 +87,7 @@
         Long l = Coercion.coerceLong(left);
         Long r = Coercion.coerceLong(right);
 
-        return new Long(l.longValue() * r.longValue());
+        return ASTAddNode.unwiden(new Long(l.longValue() * r.longValue()));
     }
 
 }

Modified: jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTSubtractNode.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTSubtractNode.java?rev=202229&r1=202228&r2=202229&view=diff
==============================================================================
--- jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTSubtractNode.java
(original)
+++ jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTSubtractNode.java
Tue Jun 28 08:43:00 2005
@@ -48,7 +48,7 @@
          *  the spec says 'and', I think 'or'
          */
         if (left == null && right == null)
-            return new Integer(0);
+            return new Byte((byte)0);
 
         /*
          *  if anything is float, double or string with ( "." | "E" | "e")
@@ -91,12 +91,8 @@
          * 
          */
         long v = l.longValue() - r.longValue();
+        return ASTAddNode.unwiden(new Long(v));
 
-        if ( left instanceof Integer && right instanceof Integer )
-        {
-            return new Integer((int) v);
-        }
-        return new Long(v);
      }
     /** Accept the visitor. **/
     public Object jjtAccept(ParserVisitor visitor, Object data)

Modified: jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/JexlTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/JexlTest.java?rev=202229&r1=202228&r2=202229&view=diff
==============================================================================
--- jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/JexlTest.java (original)
+++ jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/JexlTest.java Tue Jun
28 08:43:00 2005
@@ -319,6 +319,7 @@
         assertExpression(jc, "foo.substring(0,(size(foo)-3))", "abc");
         assertExpression(jc, "foo.substring(0,size(foo)-3)", "abc");
         assertExpression(jc, "foo.substring(0,foo.length()-3)", "abc");
+        assertExpression(jc, "foo.substring(0, 1+1)", "ab");
     }
 
 
@@ -333,12 +334,12 @@
 
         jc.getVars().put("foo", new Integer(2) );
 
-        assertExpression(jc, "foo + 2", new Long(4));
-        assertExpression(jc, "3 + 3", new Long(6));
-        assertExpression(jc, "3 + 3 + foo", new Long(8));
-        assertExpression(jc, "3 * 3", new Long(9));
-        assertExpression(jc, "3 * 3 + foo", new Long(11));
-        assertExpression(jc, "3 * 3 - foo", new Long(7));
+        assertExpression(jc, "foo + 2", new Byte((byte)4));
+        assertExpression(jc, "3 + 3", new Byte((byte)6));
+        assertExpression(jc, "3 + 3 + foo", new Byte((byte)8));
+        assertExpression(jc, "3 * 3", new Byte((byte)9));
+        assertExpression(jc, "3 * 3 + foo", new Byte((byte)11));
+        assertExpression(jc, "3 * 3 - foo", new Byte((byte)7));
 
         /*
          * test some floaty stuff
@@ -353,7 +354,7 @@
         assertExpression(jc, "6.4 / 3", new Double(6.4 / 3));
         assertExpression(jc, "0 / 3", new Double(0 / 3));
         assertExpression(jc, "3 / 0", new Double(0));
-        assertExpression(jc, "4 % 3", new Long(1));
+        assertExpression(jc, "4 % 3", new Byte((byte)1));
         assertExpression(jc, "4.8 % 3", new Double(4.8 % 3));
 
         /*
@@ -366,8 +367,8 @@
          * test new null coersion
          */
         jc.getVars().put("imanull", null );
-        assertExpression(jc, "imanull + 2", new Long(2));
-        assertExpression(jc, "imanull + imanull", new Long(0));
+        assertExpression(jc, "imanull + 2", new Byte((byte)2));
+        assertExpression(jc, "imanull + imanull", new Byte((byte)0));
         
         /* test for bugzilla 31577 */
         jc.getVars().put("n", new Integer(0));
@@ -854,7 +855,7 @@
      */
     public void testComment() throws Exception
     {
-        assertExpression(JexlHelper.createContext(), "## double or nothing\n 1 + 1", Long.valueOf("2"));
+        assertExpression(JexlHelper.createContext(), "## double or nothing\n 1 + 1", Byte.valueOf("2"));
     }
     
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message