You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Thibault Kruse (JIRA)" <ji...@apache.org> on 2018/02/26 03:18:00 UTC

[jira] [Commented] (LANG-1381) ToStringBuilder Works Inconsistently With HashMap

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

Thibault Kruse commented on LANG-1381:
--------------------------------------

Different Handling seems to come from different result of isRegistered(value) in ToStringStyle.appendInternal().

isRegistered(value) checks for getRegistry().containsKey(value), which in case of empty HashMaps is true for any empty HashMap.

Consider also outputs for these cases:

 

    person.put({color:#008000}"a"{color}, {color:#008000}"b"{color});
    person.{color:#660e7a}job{color}.put({color:#008000}"a"{color}, {color:#008000}"b"{color});
    System.{color:#660e7a}out{color}.println(person); // job=Job@...
    person.clear();
    System.{color:#660e7a}out{color}.println(person); // job=Manager

 

At a glance, containsKey(value) might have to be replaced with identity check inside keySet()

> ToStringBuilder Works Inconsistently With HashMap
> -------------------------------------------------
>
>                 Key: LANG-1381
>                 URL: https://issues.apache.org/jira/browse/LANG-1381
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.builder.*
>    Affects Versions: 3.7
>            Reporter: Brian Schack
>            Priority: Major
>         Attachments: Test.java
>
>
> 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]}}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)