1 5 package com.tc.object.walker; 6 7 import com.tc.util.ClassUtils; 8 9 import java.lang.reflect.Field ; 10 11 14 public class PrintVisitor implements Visitor { 15 private static final String INDENT = " "; 16 private final OutputSink out; 17 private final WalkTest walkTest; 18 private final ValueFormatter formatter; 19 20 public PrintVisitor(OutputSink sink, WalkTest walkTest, ValueFormatter formatter) { 21 this.out = sink; 22 this.walkTest = walkTest; 23 this.formatter = formatter; 24 } 25 26 public void visitRootObject(MemberValue value) { 27 outputLine(typeDisplay(value.getValueObject().getClass()) + " (id " + value.getId() + ")"); 28 } 29 30 private void outputLine(String line) { 31 out.output(line); 32 } 33 34 public void visitValue(MemberValue value, int depth) { 35 StringBuffer buf = new StringBuffer (); 36 boolean isLiteral = !walkTest.shouldTraverse(value); 37 38 indent(depth, buf); 39 40 if (value.isMapKey()) { 41 buf.append("key "); 42 } else if (value.isMapValue()) { 43 buf.append("value "); 44 } 45 46 Field field = value.getSourceField(); 47 if (field != null) { 48 buf.append(typeDisplay(field.getType()) + " "); 49 boolean shadowed = value.isShadowed(); 50 if (shadowed) { 51 buf.append(field.getDeclaringClass().getName() + "."); 52 } 53 54 buf.append(field.getName() + " "); 55 } 56 57 if (value.isElement()) { 58 buf.append("[" + value.getIndex() + "] "); 59 } 60 61 buf.append("= "); 62 63 Object o = value.getValueObject(); 64 65 if (isLiteral || o == null) { 66 buf.append(formatter.format(value.getValueObject())); 67 } else { 68 if (value.isRepeated()) { 69 buf.append("(ref id " + value.getId() + ")"); 70 } else { 71 if ((field != null) && (o.getClass().equals(field.getType()))) { 72 buf.append("(id " + value.getId() + ")"); 73 } else { 74 buf.append("(" + typeDisplay(o.getClass()) + ", id " + value.getId() + ")"); 75 } 76 } 77 } 78 79 String adorn = formatter.valueAdornment(value); 80 if (adorn != null) { 81 buf.append(adorn); 82 } 83 84 outputLine(buf.toString()); 85 } 86 87 private static final String JAVA_LANG = "java.lang."; 88 private static final int JAVA_LANG_LEN = JAVA_LANG.length(); 89 90 private static final String JAVA_UTIL = "java.util."; 91 private static final int JAVA_UTIL_LEN = JAVA_UTIL.length(); 92 93 private static String typeDisplay(Class c) { 94 String type = c.getName(); 95 int dim = 0; 96 if (c.isArray()) { 97 dim = ClassUtils.arrayDimensions(c); 98 type = ClassUtils.baseComponetType(c).getName(); 99 } 100 101 if (type.startsWith(JAVA_LANG) && type.lastIndexOf('.') + 1 == JAVA_LANG_LEN) { 102 type = type.substring(JAVA_LANG_LEN); 103 } else if (type.startsWith(JAVA_UTIL) && type.lastIndexOf('.') + 1 == JAVA_UTIL_LEN) { 104 type = type.substring(JAVA_UTIL_LEN); 105 } 106 107 for (int i = 0; i < dim; i++) { 108 type = type + "[]"; 109 } 110 111 return type; 112 } 113 114 public void visitMapEntry(int index, int depth) { 115 StringBuffer buf = new StringBuffer (); 116 indent(depth, buf); 117 buf.append("[entry ").append(index).append("]"); 118 outputLine(buf.toString()); 119 } 120 121 private static void indent(int currentDepth, StringBuffer buffer) { 122 while (currentDepth-- > 0) { 123 buffer.append(INDENT); 124 } 125 } 126 127 public interface OutputSink { 128 void output(String line); 129 } 130 131 public interface ValueFormatter { 132 String format(Object value); 133 134 String valueAdornment(MemberValue value); 135 } 136 137 } | Popular Tags |