# HG changeset patch # User Christian Humer # Date 1367586176 -7200 # Node ID e6fe35d64b71b3873a09ce0ba25da355e2edb3b6 # Parent e2965e5cd47480aaea4fcc72184b666eee38bcbe Implemented a method to produce a compact string representation of the truffle tree which just shows the nodes and their children but no data fields. diff -r e2965e5cd474 -r e6fe35d64b71 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java --- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java Fri May 03 14:59:46 2013 +0200 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java Fri May 03 15:02:56 2013 +0200 @@ -473,8 +473,16 @@ } public static String printTreeToString(Node node) { + return printTreeToString(node, false); + } + + private static String printTreeToString(Node node, boolean compact) { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); - printTree(new PrintStream(byteOut), node); + if (compact) { + printCompactTree(new PrintStream(byteOut), node); + } else { + printTree(new PrintStream(byteOut), node); + } try { byteOut.flush(); } catch (IOException e) { @@ -493,6 +501,61 @@ printTree(p, node, new NodeTreeResolver()); } + public static String printCompactTreeToString(Node node) { + return printTreeToString(node, true); + } + + public static void printCompactTree(PrintStream p, Node node) { + printCompactTree(p, null, node, 1); + } + + private static void printCompactTree(PrintStream p, Node parent, Node node, int level) { + if (node == null) { + return; + } + for (int i = 0; i < level; i++) { + p.print(" "); + } + if (parent == null) { + p.println(node.getClass().getSimpleName()); + } else { + String fieldName = null; + Field[] fields = NodeUtil.getAllFields(parent.getClass()); + try { + for (Field field : fields) { + field.setAccessible(true); + Object value = field.get(parent); + if (value == node) { + fieldName = field.getName(); + break; + } else if (value instanceof Node[]) { + int index = 0; + for (Node arrayNode : (Node[]) value) { + if (arrayNode == node) { + fieldName = field.getName() + "[" + index + "]"; + break; + } + index++; + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + if (fieldName == null) { + fieldName = "unknownField"; + } + p.print(fieldName); + p.print(" = "); + p.println(node.getClass().getSimpleName()); + } + + for (Node child : node.getChildren()) { + printCompactTree(p, node, child, level + 1); + } + } + /** * Prints a human readable form of a tree to the given {@link PrintStream}. The * {@link TreeResolver} interface needs to be implemented to specify how the method can read the