commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 16676] - StackOverflow due to ToStringBuilder
Date Fri, 14 Mar 2003 02:19:53 GMT
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16676>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16676

StackOverflow due to ToStringBuilder





------- Additional Comments From ggregory@seagullsw.com  2003-03-14 02:19 -------
I think you are correct since there is no object connection between 
ToStringBuilder and an arbitrary object's toString method which may or may not 
use ToStringBuilder. Would something like the nasty experiment below work in 
all cases?

package test;

import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * @author ggregory
 */
public class ToStringBuilderRTest extends TestCase {

    public void testMe() {
        System.out.println(new NodeFixture());
        System.out.println(new NodeFixture(new NodeFixture(new NodeFixture
())));
        NodeFixture n = new NodeFixture();
        n.node = n;
        System.out.println(n);
        System.out.println(new NodeFixture(new NodeFixture(n)));
        assertEquals("RefectionToStringBuilder.registry.size() > 0", 
RefectionToStringBuilder.registry.size(), 0);
    }
}

class NodeFixture {
    public NodeFixture node;
    public NodeFixture() {
    }
    public NodeFixture(NodeFixture node) {
        this.node = node;
    }
    public String toString() {
        return new RefectionToStringBuilder(this).toString();
    }
}

class RefectionToStringBuilder {
    
    static Map registry = new HashMap();
    Object o;
    
    public RefectionToStringBuilder(Object o) {
        this.o = o;
    }
    
    public String toString() {
        String s;
        try {
            if (registry.containsKey(o)) {
                return "<cycle detected: " + o.getClass().getName() + "@" + 
System.identityHashCode(o) + ">";
            }
            registry.put(o, null);
            s = ToStringBuilder.reflectionToString(o);
        } finally {
            registry.remove(o);
        }
        return s;
    }
    
}

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message