Visualizes a tree structure. Walks the tree, printing most fields of most objects. Excludes fields from the Object class, objects that would cause a cycle, and those specifically requested to ignored. Prints scalar-like values directly, else expands objects and array-like objects (including collecti
| 47 | * the object's fields. |
| 48 | */ |
| 49 | public class Visualizer { |
| 50 | |
| 51 | /** |
| 52 | * Performs actual visualization of the tree based on |
| 53 | * a set of events. |
| 54 | */ |
| 55 | public interface TreeVisualizer { |
| 56 | void startObj(String name, Object obj); |
| 57 | void startArray(String name); |
| 58 | void field(String name, Object value); |
| 59 | void elide(String name, Object value, String reason); |
| 60 | void emptyArray(String name); |
| 61 | void endArray(); |
| 62 | void endObj(); |
| 63 | } |
| 64 | |
| 65 | private static final Class<?> STD_SCALARS[] = { |
| 66 | Byte.class, |
| 67 | Integer.class, |
| 68 | Character.class, |
| 69 | Long.class, |
| 70 | Float.class, |
| 71 | Double.class, |
| 72 | String.class, |
| 73 | Boolean.class, |
| 74 | Enum.class, |
| 75 | AtomicLong.class, |
| 76 | BigDecimal.class, |
| 77 | BigInteger.class |
| 78 | }; |
| 79 | |
| 80 | private TreeVisualizer treeVis_; |
| 81 | private Set<Class<?>> scalarTypes_ = new HashSet<>(); |
| 82 | private Set<Class<?>> ignoreTypes_ = new HashSet<>(); |
| 83 | // Infinite recursion preventer. |
| 84 | // Since there is no IdentityHashMap. Values ignored. |
| 85 | private Map<Object, Object> parents_ = new IdentityHashMap<>(); |
| 86 | private int depthLimit_ = Integer.MAX_VALUE; |
| 87 | |
| 88 | public Visualizer(TreeVisualizer treeVis) { |
| 89 | this.treeVis_ = treeVis; |
| 90 | for (Class<?> c : STD_SCALARS) { |
| 91 | scalarTypes_.add(c); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Specify a class to ignore. Objects of this type are |
| 97 | * skipped during visualization, with a special message |
| 98 | * for that field in place of object expansion. |
| 99 | * |
| 100 | * @param cls the class to skip |
| 101 | */ |
| 102 | public void ignore(Class<?> cls) { |
| 103 | ignoreTypes_.add(cls); |
| 104 | } |
| 105 | |
| 106 | /** |
nothing calls this directly
no outgoing calls
no test coverage detected