public static String dump ( Object self ) { if ( self == null ) { return "null" ; } StringBuilder buffer = new StringBuilder ( "<" ) ; Class klass = self . getClass ( ) ; buffer . append ( klass . getName ( ) ) ; buffer . append ( "@" ) ; buffer . append ( Integer . toHexString ( self . hashCode ( ) ) ) ; boolean groovyObject = self instanceof GroovyObject ; /*jes this may be rewritten to use the new getProperties() stuff * but the original pulls out private variables, whereas getProperties() * does not. What's the real use of dump() here? */ while ( klass != null ) { for ( final Field field : klass . getDeclaredFields ( ) ) { if ( ( field . getModifiers ( ) & Modifier . STATIC ) == 0 ) { if ( groovyObject && field . getName ( ) . equals ( "metaClass" ) ) { continue ; } AccessController . doPrivileged ( new PrivilegedAction ( ) { public Object run ( ) { field . setAccessible ( true ) ; return null ; } } ) ; buffer . append ( " " ) ; buffer . append ( field . getName ( ) ) ; buffer . append ( "=" ) ; try { buffer . append ( InvokerHelper . toString ( field . get ( self ) ) ) ; } catch ( Exception e ) { buffer . append ( e ) ; } } } klass = klass . getSuperclass ( ) ; } /* here is a different implementation that uses getProperties(). I have left * it commented out because it returns a slightly different list of properties; * i.e. it does not return privates. I don't know what dump() really should be doing, * although IMO showing private fields is a no-no */ /* List props = getProperties(self); for(Iterator itr = props.keySet().iterator(); itr.hasNext(); ) { String propName = itr.next().toString(); // the original skipped this, so I will too if(pv.getName().equals("class")) continue; if(pv.getName().equals("metaClass")) continue; buffer.append(" "); buffer.append(propName); buffer.append("="); try { buffer.append(InvokerHelper.toString(props.get(propName))); } catch (Exception e) { buffer.append(e); } } */ buffer . append ( ">" ) ; return buffer . toString ( ) ; }
Generates a detailed dump string of an object showing its class hashCode and fields .