/* * Java data pretty printer * * Copyright (C) 2001 by Sam Steingold * GNU General Public License v2 (GPL2) is applicable: * You may use, modify and redistribute this code under the same conditions. * You must read the full text of the license * at * * $Id: Jpp.java,v 1.2 2001/02/15 22:41:28 sds Exp $ * $Source: /usr/local/cvs/sds/java/Jpp.java,v $ */ import java.lang.reflect.*; /** * Java Pretty Printer * * This prints your Java data (objects and arrays) in a nice way, * even if you did not bother to implements the proper * toString() methods. * * We use the toString() method for those classes that * define it themselves, so you can mix this with your own printers. * * Note that Lisp has a pretty printer built in, * so you can see your data right away! */ public class Jpp { /** return the string of spaces of the given length */ private static String pad (int width) { char[] buf = new char[width]; java.util.Arrays.fill(buf,' '); return new String(buf); } /** pretty-print an array into a string as #(...) */ private static String list_vec (Object obj,int offset) { if (offset>=0) offset++; Class type = obj.getClass(); if (!type.isArray()) return "#"; String ret = "("; for (int ii=0; ii" : (elt.getClass().isArray() ? list_vec(elt,offset) : print_object(elt,offset))); if (ii==0) ret += elt_str; else if ((elt_str.indexOf('\n') > 0) || ((elt_str.length() + ret.length() - ret.lastIndexOf('\n') > 78) && (offset >= 0))) ret += "\n"+pad(offset)+elt_str; else ret += " "+elt_str; } return ret+")"; } /** pretty-print the class object type to a string */ private static String print_class (Class type) { return print_class(type,null,null); } /** pretty-print the class object type to a string, obj is an instance of type (needed only when printing array types) */ private static String print_class (Class type,Object obj) { return print_class(type,obj,null); } /** pretty-print the class object type to a string, obj is an instance of type (needed only when printing array types); dims is the other dimentions already collected */ private static String print_class (Class type,Object obj,String dims) { if (!type.isArray()) return type.getName(); Class comp = type.getComponentType(); int len = (obj == null ? -1 : Array.getLength(obj)); dims = (dims==null?"":dims) + (len < 0 ? "[]" : "["+len+"]"); if (comp.isArray()) return print_class(comp,len>0?Array.get(obj,0):null,dims); else return comp.getName()+dims; } /** does this class define its own toString() method? */ private static boolean printed_natively_p (Class type) { try { type.getDeclaredMethod("toString",null); return true; } catch (Exception e) { return false; } } /** print the object nicely */ public static String print_object (Object obj) { return print_object(obj,0); } /** print the object nicely; with the given offset */ public static String print_object (Object obj,int offset) { offset += 2; if (obj == null) return "#"; Class type = obj.getClass(); if (obj instanceof String) return "\""+obj+"\""; else if (printed_natively_p(type)) return obj.toString(); else if (type.isArray()) return "#"+list_vec(obj,offset-1); else { Field[] slots = type.getFields(); String ret = "#<["+type.getName()+"]"; for (int ii=0; ii"; } } }