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

ToStringBuilder Works Inconsistently With HashMap

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 3.7
    • None
    • lang.builder.*
    • None

    Description

      In the following code, why do the two lines containing `System.out.println(person);` yield different outputs? The second line indirectly calls the method `Job.toString` yielding the string `"Manager"`, but the first line mysteriously does not yielding `Job@28f67ac7`. The line in between `person.put("a", "b");` doesn't seem to me like it should make any difference.

      Code:

      {{import java.util.*;
      import org.apache.commons.lang3.builder.*;

      class Job extends HashMap<String, String> {
      @Override public String toString()

      { return "Manager"; }

      }

      class Person extends HashMap<String, String> {
      Job job;

      Person()

      { this.job = new Job(); }

      @Override public String toString()

      { return ToStringBuilder.reflectionToString(this); }

      }

      class Test {
      public static void main(String[] args)

      { Person person = new Person(); System.out.println(person); person.put("a", "b"); System.out.println(person); }

      }}}

      Console:

      {{Person@2b80d80f[job=Job@28f67ac7,threshold=0,loadFactor=0.75]
      Person@2b80d80f[job=Manager,threshold=12,loadFactor=0.75]}}

      Attachments

        1. Test.java
          0.5 kB
          Brian Schack

        Activity

          People

            Unassigned Unassigned
            schackbrian Brian Schack
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: