Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-1452

MultilineRecursiveToStringStyle produces wrong output for BigDecimal

    XMLWordPrintableJSON

Details

    Description

      Class has 2 BigDecimal fields:

      BigDecimal biddecimal123 = new BigDecimal("123");
      BigDecimal biddecimal456 = new BigDecimal("456");
      

      MultilineRecursiveToStringStyle produces this output:

      BDToString@36baf30c[
      biddecimal123=java.math.BigDecimal@7adf9f5f[
      intVal=<null>,
      scale=0
      ],
      biddecimal456=java.math.BigDecimal@63961c42[
      intVal=<null>,
      scale=0
      ]
      ]

       

      ToStringStyle.MULTI_LINE_STYLE doesn't have this problem. It was introduced with method

      @Override
      public void appendDetail(final StringBuffer buffer, final String fieldName, final Object value)

      of MultilineRecursiveToStringStyle. It doesnt'n treat BigDecimal as a primitive type. So I had to do the following to solve my issue:

       

      class FixedMultilineRecursiveToStringStyle extends MultilineRecursiveToStringStyle{
          @Override
          public void appendDetail(StringBuffer buffer, String fieldName, Object value) {
              if (!BigDecimal.class.equals(value.getClass())){
                  super.appendDetail(buffer, fieldName, value);
              } else {
                  buffer.append(value);
              }
          }
      }
      

       

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            novozhilov.a.russia Aleksey Novozhilov
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: