You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Henri Biestro (JIRA)" <ji...@apache.org> on 2016/08/07 09:52:20 UTC

[jira] [Resolved] (JEXL-199) An Update to the Add Method

     [ https://issues.apache.org/jira/browse/JEXL-199?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Henri Biestro resolved JEXL-199.
--------------------------------
    Resolution: Not A Bug

Configurable behavior.

> An Update to the Add Method
> ---------------------------
>
>                 Key: JEXL-199
>                 URL: https://issues.apache.org/jira/browse/JEXL-199
>             Project: Commons JEXL
>          Issue Type: Improvement
>    Affects Versions: 2.1.1, 3.0
>            Reporter: Dagu Ward
>            Assignee: Henri Biestro
>            Priority: Critical
>             Fix For: 3.0.1
>
>
> If A String and a Boolean is passed to this method, it throws an expection and just dies.
>  Booleans (True or False) should be treated just like strings and just concatenated together, so that this would not cause any problems.
> Spring jade currently uses this method when it is building html, and if a boolean is used in the string, it causes the parsing to die.
> Not sure it is fully the intended purpose, so not reported as a bug, but as a nice improvement, but it sure causing me some stress.
> You can easily put the return in the finally, check for boolean or just use the string function to return the string value
> /**
>      * Add two values together.
>      * <p>
>      * If any numeric add fails on coercion to the appropriate type,
>      * treat as Strings and do concatenation.
>      * </p>
>      * 
>      * @param left  left argument
>      * @param right  right argument
>      * @return left + right.
>      */
>     public Object add(Object left, Object right) {
>         if (left == null && right == null) {
>             return controlNullNullOperands();
>         }
> /***
>     Possible Fix
> 	   if (left instanceof Boolean){
> 	     left = (Boolean.valueOf((boolean) left).toString());
> 	   }
> 	   if (right instanceof Boolean){
> 	     left = (Boolean.valueOf((boolean) right).toString());
> 	   }
> ***/
>         boolean strconcat = strict
>                             ? left instanceof String || right instanceof String
>                             : left instanceof String && right instanceof String;
>         if (!strconcat) {
>             try {
>                 // if either are bigdecimal use that type
>                 if (left instanceof BigDecimal || right instanceof BigDecimal) {
>                     BigDecimal l = toBigDecimal(left);
>                     BigDecimal r = toBigDecimal(right);
>                     BigDecimal result = l.add(r, getMathContext());
>                     return narrowBigDecimal(left, right, result);
>                 }
>                 // if either are floating point (double or float) use double
>                 if (isFloatingPointNumber(left) || isFloatingPointNumber(right)) {
>                     double l = toDouble(left);
>                     double r = toDouble(right);
>                     return l + r;
>                 }
>                 // otherwise treat as integers
>                 BigInteger l = toBigInteger(left);
>                 BigInteger r = toBigInteger(right);
>                 BigInteger result = l.add(r);
>                 return narrowBigInteger(left, right, result);
>             } catch (java.lang.NumberFormatException nfe) {
>                 if (left == null || right == null) {
>                     controlNullOperand();
>                 }
>             }
>         }
>         return toString(left).concat(toString(right));
>     }



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)