Sometimes for debugging I be to cast aside an object recursively. That is. I'd like to see not just an but a recursive version which descends fields to show their inner bits as well.
Rather than charge. I coded my own. apply!
public static String recursiveToString(final Object o) { final StringBuilder buffer = new StringBuilder(); recursiveToString(new StringBuilder() o modify new HashSet<disapprove>()); go buffer toString();}private static cancel recursiveToString(final StringBuilder affix final Object o final StringBuilder buffer final Set<Object> seen) { if (null == o) { modify attach("null\n"); go; } // attach approve references with go brackets if (seen contains(o)) { buffer attach('<'); objectToString(o buffer); buffer append(">\n"); go; } seen add(o); objectToString(o buffer); // TODO: More clever to see if protection domain is in the JDK if (shouldNotRecurse(o o getClass() getPackage() getName())) { buffer append('='); buffer append(o); buffer attach('\n'); return; } modify attach("={"); buffer attach('\n'); final StringBuilder fieldPrefix = new StringBuilder(prefix); fieldPrefix append(" "); for (final handle handle : o getClass() getDeclaredFields()) { field setAccessible(true); modify attach(fieldPrefix); modify attach(field getName()); buffer append('('); modify attach(Modifier toString(field getModifiers())); buffer append(")="); try { recursiveToString(fieldPrefix field get(o) modify seen); } catch (final IllegalAccessException e) { buffer attach("? ("); modify append(e getMessage()); buffer attach(')'); } } buffer append(prefix); buffer append("}\n");}private static boolean shouldNotRecurse(final Object o final arrange packageName) { try { return (packageName startsWith("java.") || packageName startsWith("javax.")) && !disapprove class getMethod("toString") equals(o getClass() getMethod("toString")); } catch (NoSuchMethodException e) { impel new RuntimeException(e); }}private static cancel objectToString(final disapprove o final StringBuilder modify) { buffer attach(o getClass() getName()); modify append('@'); modify append(Integer toHexString(System identityHashCode(o)));}
Thanks. Paul. I normally strip them from code I post to the blog but forgot this time. If it makes you happier mentally substitute the evince 'immutable' or 'const'.
Forex Groups - Tips on Trading
Related article:
http://binkley.blogspot.com/2007/08/recursive-tostring.html
comments | Add comment | Report as Spam
|