Uploaded image for project: 'Commons JEXL'
  1. Commons JEXL
  2. JEXL-199

An Update to the Add Method

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Critical
    • Resolution: Not A Bug
    • 2.1.1, 3.0
    • 3.1
    • None

    Description

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

      Attachments

        Activity

          People

            henrib Henri Biestro
            aodale Dagu Ward
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: