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

embedded objects are not toString-ed like top-level objects

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.5, 3.0.1
    • 3.2
    • lang.builder.*
    • None
    • Linux Ubuntu
      java version "1.6.0_24"
      Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
      Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02, mixed mode)

    Description

      I have a simple class 'A' defined as follows:

      ======================================
      public class A {
      int p1;
      String p2;
      B b;
      }
      ======================================

      While I execute the following instructions:
      ToStringBuilder builder = new ReflectionToStringBuilder(a);
      System.out.println(builder.toString());

      The output is:
      A@3ea981ca[p1=0,p2=<null>,b=B@1ee7b241]
      that's normal, without recursion

      So, I defined my own style, for recursive toString-ing display:

      ======================================
      class MyStyle extends ToStringStyle {
      private final static ToStringStyle instance = new MyStyle();

      public MyStyle()

      { setArrayContentDetail(true); setUseShortClassName(true); setUseClassName(true); setUseIdentityHashCode(true); setFieldSeparator(", "); }

      public static ToStringStyle getInstance()

      { return instance; }

      ;

      @Override
      public void appendDetail(final StringBuffer buffer, final String fieldName, final Object value) {
      if (!value.getClass().getName().startsWith("java"))

      { buffer.append(ReflectionToStringBuilder.toString(value, instance)); }

      else

      { super.appendDetail(buffer, fieldName, value); }

      }

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

      { appendDetail(buffer, fieldName, value.toArray()); }

      }
      ======================================

      When I use my custom MyStyle:
      String s = ReflectionToStringBuilder.toString(a, MyStyle.getInstance());
      System.out.println(s);

      The output is:
      A@3ea981ca[p1=0, p2=<null>, b=byte@1ee7b241[p4=234]]

      So, the name of the class 'B' is not displayed.
      I expected something like: b=B@1ee7b241[p4=234]

      Instead, the name of the class 'B' is replaced with 'byte'.
      I don't know why.

      Attachments

        1. LANG-754.patch
          0.9 kB
          Thomas Neidhart

        Activity

          People

            Unassigned Unassigned
            dmdevito Dominique De Vito
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 24h
                24h
                Remaining:
                Remaining Estimate - 24h
                24h
                Logged:
                Time Spent - Not Specified
                Not Specified