DumpTree.jrag 1.67 KB
Newer Older
Niklas Fors's avatar
Niklas Fors committed
1
aspect DumpTree {
Johannes Mey's avatar
Johannes Mey committed
2
  private static final String ASTNode.DUMP_TREE_INDENT = "  ";
Niklas Fors's avatar
Niklas Fors committed
3

Johannes Mey's avatar
Johannes Mey committed
4
  public String ASTNode.dumpTree() {
5
6
    java.io.ByteArrayOutputStream bytes = new java.io.ByteArrayOutputStream();
    dumpTree(new java.io.PrintStream(bytes));
Johannes Mey's avatar
Johannes Mey committed
7
8
    return bytes.toString();
  }
Niklas Fors's avatar
Niklas Fors committed
9

10
  public void ASTNode.dumpTree(java.io.PrintStream out) {
Johannes Mey's avatar
Johannes Mey committed
11
12
13
    dumpTree(out, "");
    out.flush();
  }
Niklas Fors's avatar
Niklas Fors committed
14

15
  public void ASTNode.dumpTree(java.io.PrintStream out, String indent) {
Johannes Mey's avatar
Johannes Mey committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    out.print(indent + getClass().getSimpleName());
    out.print(getTokens());
    String extra = extraDumpInfo();
    if (!extra.isEmpty()) {
      out.print(" " + extra);
    }
    out.println();
    String childIndent = indent + DUMP_TREE_INDENT;
    for (ASTNode child : astChildren()) {
      if (child == null) {
        out.println(childIndent + "null");
      } else {
        child.dumpTree(out, childIndent);
      }
    }
  }
Niklas Fors's avatar
Niklas Fors committed
32

Johannes Mey's avatar
Johannes Mey committed
33
  public String ASTNode.extraDumpInfo() { return ""; }
Niklas Fors's avatar
Niklas Fors committed
34

Johannes Mey's avatar
Johannes Mey committed
35
  public String ASTNode.getTokens() {
36
    java.util.TreeSet<java.lang.reflect.Method> methods = new java.util.TreeSet<>(Comparator.comparing(java.lang.reflect.Method::getName));
Niklas Fors's avatar
Niklas Fors committed
37

Johannes Mey's avatar
Johannes Mey committed
38
    methods.addAll(java.util.Arrays.asList(getClass().getMethods()));
Niklas Fors's avatar
Niklas Fors committed
39

40
    StringBuilder result = new StringBuilder();
Johannes Mey's avatar
Johannes Mey committed
41
42
43
44
    for (java.lang.reflect.Method method : methods) {
      ASTNodeAnnotation.Token token = method.getAnnotation(ASTNodeAnnotation.Token.class);
      if (token != null) {
        try {
45
          result.append(String.format(" %s=\"%s\"", token.name(), method.invoke(this)));
46
47
        } catch (IllegalAccessException  ignored) {
        } catch (java.lang.reflect.InvocationTargetException ignored) {
Johannes Mey's avatar
Johannes Mey committed
48
49
50
        }
      }
    }
51
    return result.toString();
Johannes Mey's avatar
Johannes Mey committed
52
  }
53
}