commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henri Biestro (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (JEXL-199) An Update to the Add Method
Date Fri, 10 Jun 2016 04:50:20 GMT

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

Henri Biestro commented on JEXL-199:
------------------------------------

An even simpler method is to instantiate a lenient JexlArithmetic as in:
{code}
    @Test
    public void test199() throws Exception {
        JexlContext jc = new MapContext();
        JexlEngine jexl = new JexlBuilder().arithmetic(new JexlArithmetic(false)).create();

        JexlScript e = jexl.createScript("(x, y)->{ x + y }");
        Object r = e.execute(jc, true, "EURT");
        Assert.assertEquals("trueEURT", r);
        r = e.execute(jc, "ELSAF", false);
        Assert.assertEquals("ELSAFfalse", r);
    }
{code}

> 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