package com.tutego.insel.meta;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.util.ArrayList;

public class ToStringHelper
{
  public static String toString( Object o )
  {
    ArrayList<String> list = new ArrayList<String>();

	  toString( o, o.getClass(), list );

	  return o.getClass().getName().concat( list.toString() );
 }

 private static void toString( Object o, Class<?> clazz, ArrayList<String> list )
 {
	Field[] fields = clazz.getDeclaredFields();
	AccessibleObject.setAccessible( fields, true );

	for ( Field f : fields ) {
	 try {
		list.add( f.getName() + "=" + f.get(o) );
	 }
	 catch ( IllegalAccessException e ) { e.printStackTrace(); }
	}

	if ( clazz.getSuperclass().getSuperclass() != null )
		toString( o, clazz.getSuperclass(), list );
 }
}
