comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java @ 9538:e6fe35d64b71

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.
author Christian Humer <christian.humer@gmail.com>
date Fri, 03 May 2013 15:02:56 +0200
parents e2965e5cd474
children 98b004bf3985
comparison
equal deleted inserted replaced
9537:e2965e5cd474 9538:e6fe35d64b71
471 }); 471 });
472 return nodeList; 472 return nodeList;
473 } 473 }
474 474
475 public static String printTreeToString(Node node) { 475 public static String printTreeToString(Node node) {
476 return printTreeToString(node, false);
477 }
478
479 private static String printTreeToString(Node node, boolean compact) {
476 ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); 480 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
477 printTree(new PrintStream(byteOut), node); 481 if (compact) {
482 printCompactTree(new PrintStream(byteOut), node);
483 } else {
484 printTree(new PrintStream(byteOut), node);
485 }
478 try { 486 try {
479 byteOut.flush(); 487 byteOut.flush();
480 } catch (IOException e) { 488 } catch (IOException e) {
481 } 489 }
482 return new String(byteOut.toByteArray()); 490 return new String(byteOut.toByteArray());
489 * @param p the {@link PrintStream} to print to. 497 * @param p the {@link PrintStream} to print to.
490 * @param node the root node to write 498 * @param node the root node to write
491 */ 499 */
492 public static void printTree(PrintStream p, Node node) { 500 public static void printTree(PrintStream p, Node node) {
493 printTree(p, node, new NodeTreeResolver()); 501 printTree(p, node, new NodeTreeResolver());
502 }
503
504 public static String printCompactTreeToString(Node node) {
505 return printTreeToString(node, true);
506 }
507
508 public static void printCompactTree(PrintStream p, Node node) {
509 printCompactTree(p, null, node, 1);
510 }
511
512 private static void printCompactTree(PrintStream p, Node parent, Node node, int level) {
513 if (node == null) {
514 return;
515 }
516 for (int i = 0; i < level; i++) {
517 p.print(" ");
518 }
519 if (parent == null) {
520 p.println(node.getClass().getSimpleName());
521 } else {
522 String fieldName = null;
523 Field[] fields = NodeUtil.getAllFields(parent.getClass());
524 try {
525 for (Field field : fields) {
526 field.setAccessible(true);
527 Object value = field.get(parent);
528 if (value == node) {
529 fieldName = field.getName();
530 break;
531 } else if (value instanceof Node[]) {
532 int index = 0;
533 for (Node arrayNode : (Node[]) value) {
534 if (arrayNode == node) {
535 fieldName = field.getName() + "[" + index + "]";
536 break;
537 }
538 index++;
539 }
540 }
541 }
542 } catch (Exception e) {
543 e.printStackTrace();
544 }
545
546 if (fieldName == null) {
547 fieldName = "unknownField";
548 }
549 p.print(fieldName);
550 p.print(" = ");
551 p.println(node.getClass().getSimpleName());
552 }
553
554 for (Node child : node.getChildren()) {
555 printCompactTree(p, node, child, level + 1);
556 }
494 } 557 }
495 558
496 /** 559 /**
497 * Prints a human readable form of a tree to the given {@link PrintStream}. The 560 * Prints a human readable form of a tree to the given {@link PrintStream}. The
498 * {@link TreeResolver} interface needs to be implemented to specify how the method can read the 561 * {@link TreeResolver} interface needs to be implemented to specify how the method can read the