Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1005997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:23:13+00:00 2026-05-16T08:23:13+00:00

I need to be able to traverse through my entire object graph and log

  • 0

I need to be able to traverse through my entire object graph and log all contents of all member fields.

For example: Object A has a collection of Object B’s which has a collection of Object C’s and A, B, C have additional fields on them, etc.

Apache Commons ToStringBuilder is not sufficient since it won’t traverse down an object graph or output contents of a collection.

Does anyone know of another library that will do this or have a code snippet that does this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:23:14+00:00Added an answer on May 16, 2026 at 8:23 am

    You can traverse the whole tree using org.apache.commons.lang.builder.ReflectionToStringBuilder. The trick is that in ToStringStyle you need to traverse into the value. ToStringStyle will take care of values, already processed, and will not allow recursion. Here we go:

    System.out.println(ReflectionToStringBuilder.toString(schema, new RecursiveToStringStyle(5)));
    
    private static class RecursiveToStringStyle extends ToStringStyle {
    
        private static final int    INFINITE_DEPTH  = -1;
    
        /**
         * Setting {@link #maxDepth} to 0 will have the same effect as using original {@link #ToStringStyle}: it will
         * print all 1st level values without traversing into them. Setting to 1 will traverse up to 2nd level and so
         * on.
         */
        private int                 maxDepth;
    
        private int                 depth;
    
        public RecursiveToStringStyle() {
            this(INFINITE_DEPTH);
        }
    
        public RecursiveToStringStyle(int maxDepth) {
            setUseShortClassName(true);
            setUseIdentityHashCode(false);
    
            this.maxDepth = maxDepth;
        }
    
        @Override
        protected void appendDetail(StringBuffer buffer, String fieldName, Object value) {
            if (value.getClass().getName().startsWith("java.lang.")
                        || (maxDepth != INFINITE_DEPTH && depth >= maxDepth)) {
                buffer.append(value);
            }
            else {
                depth++;
                buffer.append(ReflectionToStringBuilder.toString(value, this));
                depth--;
            }
        }
    
        // another helpful method
        @Override
        protected void appendDetail(StringBuffer buffer, String fieldName, Collection<?> coll) {
             depth++;
             buffer.append(ReflectionToStringBuilder.toString(coll.toArray(), this, true, true));
             depth--;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to be able to get the primary key of an nserted row
I need to be able to plot events on a vertical timeline and I
i need to be able to able to detect when any character is entered
I need to be able to hide the soft keyboard in response to clicking
I need to be able to set a MySQL user variable that is used
I have an issue where I need to find the top level parent account
To insert/delete a node with a particular value in DLL (doubly linked list) entire
HTML <tr id=rowId><td><textarea class=inputTextarea></textarea></td><td><textarea class=inputTextarea></textarea></td></tr> <tr id=rowId2><td><textarea class=inputTextarea></textarea></td><td><textarea class=inputTextarea></textarea></td></tr> <tr id=rowId3><td><textarea class=inputTextarea></textarea></td><td><textarea class=inputTextarea></textarea></td></tr> Provided
I am looking for an algorithm that will allow me to visually separate any
C# / VS2010 What is the easiest way to obtain a build number? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.