commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Dmitri Blinov (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (JEXL-199) An Update to the Add Method
Date Tue, 07 Jun 2016 15:35:20 GMT

    [ https://issues.apache.org/jira/browse/JEXL-199?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15318691#comment-15318691
] 

Dmitri Blinov commented on JEXL-199:
------------------------------------

In Jexl 3.0 there is a possibility to subclass the JexlArithmetic class and implement a method
*add* for example

{code}

public class SpringJadeArithmetic extends JexlArithmetic {

    public CharSequence add(CharSequence a, Boolean b) {

        if (b == null) 
          return a;

        if (a == null) 
          a = "";

        return a + String.valueOf(b);
    }
{code}

By the way, you can overload this method with other types, for example numbers, and you can
overload other operators as well, except logical (and/or) ones.

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

Mime
View raw message