commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject svn commit: r377890 - /jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTMethod.java
Date Tue, 14 Feb 2006 23:48:30 GMT
Author: dion
Date: Tue Feb 14 15:48:29 2006
New Revision: 377890

URL: http://svn.apache.org/viewcvs?rev=377890&view=rev
Log:
Remove 'unwiden'ing of numeric values which was introduced to 
fix Bug 32829.

A better solution is to fix the ASTMethod class to narrow the parameters,
rather than change JEXL's public 'contract' of using Long's for integer values.

Modified:
    jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTMethod.java

Modified: jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTMethod.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTMethod.java?rev=377890&r1=377889&r2=377890&view=diff
==============================================================================
--- jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTMethod.java
(original)
+++ jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/parser/ASTMethod.java
Tue Feb 14 15:48:29 2006
@@ -54,9 +54,24 @@
             }
 
             VelMethod vm = Introspector.getUberspect().getMethod(obj, methodName, params,
DUMMY);
-
+            /*
+             * DG: If we can't find an exact match, narrow the parameters and try again!

+             */
             if (vm == null)
-                return null;
+            {
+            	
+            	// replace all numbers with the smallest type that will fit
+            	for (int i = 0; i < params.length; i++)
+            	{
+            		Object param = params[i];
+            		if (param instanceof Number)
+            		{
+            			params[i] = narrow((Number)param);
+            		}
+            	}
+            	vm = Introspector.getUberspect().getMethod(obj, methodName, params, DUMMY);
+                if (vm == null) return null;
+            }
 
             return vm.invoke(obj, params);
         }
@@ -72,4 +87,53 @@
             throw e;
         }
     }
+
+    /**
+     * Given a Number, return back the value using 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.
+     * @since 1.0.1
+     */
+    private Number narrow(Number original)
+    {
+    	if (original == null) return null;
+    	Number result = null;
+    	if (original instanceof Double || original instanceof Float)
+    	{
+    		double value = result.doubleValue();
+    		if (value <= Float.MAX_VALUE && value >= Float.MIN_VALUE)
+    		{
+    			result = new Float(result.floatValue());
+    		}
+    		else
+    		{
+    			// it was a double
+    			result = original;
+    		}
+    	}
+    	else
+    	{
+    		long value = original.longValue();
+	        if (value <= Byte.MAX_VALUE && value >= Byte.MIN_VALUE)
+	        {
+	            // it will fit in a byte
+	            result = new Byte((byte)value);
+	        }
+	        else if (value <= Short.MAX_VALUE && value >= Short.MIN_VALUE)
+	        {
+	        	result = new Short((short)value);
+	        }
+	        else if (value <= Integer.MAX_VALUE && value >= Integer.MIN_VALUE)
+	        {
+	        	result = new Integer((int)value);
+	        }
+	        else
+	        {
+	        	result = original;
+	        }
+    	}
+        return result;
+    }
+
 }



---------------------------------------------------------------------
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