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 search through multiple 2D arrays and search the
Users of the website need to able to store images in their area ,
Need to be able to pull Magento products into an external template. Need to
I need to be able to access a list throughout my Rails project. For
I need to be able to create an id for a hyperlink. I have
I need to be able to strip out a condition in a string, here
I need to be able do the following on a TR: onmouseover highlight the
I need to be able to map out in coordinates real world cities and
I need to be able to change the filebrowserUploadUrl of CKEditor when I change
I need to be able to send a file to another website from server-side

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.