|
1 /* |
|
2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 import com.sun.source.tree.BinaryTree; |
|
27 import com.sun.source.tree.ClassTree; |
|
28 import com.sun.source.tree.CompilationUnitTree; |
|
29 import com.sun.source.tree.IdentifierTree; |
|
30 import com.sun.source.tree.LiteralTree; |
|
31 import com.sun.source.tree.PrimitiveTypeTree; |
|
32 import com.sun.source.tree.Tree; |
|
33 import com.sun.source.tree.VariableTree; |
|
34 import com.sun.source.util.TreeScanner; |
|
35 |
|
36 import java.util.HashSet; |
|
37 import java.util.Locale; |
|
38 import java.util.Set; |
|
39 |
|
40 |
|
41 public class CodeBuilder { |
|
42 |
|
43 public static String buildCodeToGenerate(Tree t, String builder) { |
|
44 StringBuilder result = new StringBuilder(); |
|
45 new TreeScanner<Void, Void>() { |
|
46 private final Set<String> usedNames = new HashSet<>(); |
|
47 private String currentBuilder = builder; |
|
48 @Override |
|
49 public Void visitClass(ClassTree node, Void p) { |
|
50 result.append(currentBuilder() + "._class(\"" + node.getSimpleName() + "\", "); |
|
51 doScan("C", () -> super.visitClass(node, p)); |
|
52 result.append(")"); |
|
53 return null; |
|
54 } |
|
55 @Override |
|
56 public Void visitCompilationUnit(CompilationUnitTree node, Void p) { |
|
57 result.append(currentBuilder() + ".createCompilationUnitTree("); |
|
58 doScan("U", () -> super.visitCompilationUnit(node, p)); |
|
59 result.append(")"); |
|
60 return null; |
|
61 } |
|
62 @Override |
|
63 public Void visitVariable(VariableTree node, Void p) { |
|
64 result.append(currentBuilder() + ".field(\"" + node.getName() + "\", "); //XXX: field/vs local variable! |
|
65 doScan("T", node.getType()); |
|
66 result.append(", "); |
|
67 doScan("F", () -> { |
|
68 //TODO: modifiers.... |
|
69 if (node.getInitializer() != null) { |
|
70 result.append(currentBuilder() + ".init("); |
|
71 doScan("E", node.getInitializer()); |
|
72 result.append(")"); |
|
73 } |
|
74 }); |
|
75 result.append(")"); |
|
76 return null; |
|
77 } |
|
78 |
|
79 @Override |
|
80 public Void visitPrimitiveType(PrimitiveTypeTree node, Void p) { |
|
81 result.append(currentBuilder() + "._" + node.getPrimitiveTypeKind().name().toLowerCase(Locale.ROOT) + "()"); |
|
82 return null; |
|
83 } |
|
84 |
|
85 @Override |
|
86 public Void visitLiteral(LiteralTree node, Void p) { |
|
87 result.append(currentBuilder() + ".literal(" + node.getValue() + ")"); |
|
88 return null; |
|
89 } |
|
90 |
|
91 @Override |
|
92 public Void visitBinary(BinaryTree node, Void p) { |
|
93 switch (node.getKind()) { |
|
94 case PLUS: |
|
95 result.append(currentBuilder() + ".plus("); |
|
96 doScan("E", node.getLeftOperand()); |
|
97 result.append(", "); |
|
98 doScan("E", node.getRightOperand()); |
|
99 result.append(")"); |
|
100 break; |
|
101 default: throw new IllegalStateException("Not handled: " + node.getKind()); |
|
102 } |
|
103 return null; |
|
104 } |
|
105 |
|
106 @Override |
|
107 public Void visitIdentifier(IdentifierTree node, Void p) { |
|
108 result.append(currentBuilder() + ".ident(\"" + node.getName() + "\")"); |
|
109 return null; |
|
110 } |
|
111 |
|
112 private String currentBuilder() { |
|
113 if (currentBuilder != null) { |
|
114 String res = currentBuilder; |
|
115 currentBuilder = null; |
|
116 return res; |
|
117 } |
|
118 return ""; |
|
119 } |
|
120 private void doScan(String preferredBuilderName, Tree... subTreesToScan) { |
|
121 doScan(preferredBuilderName, () -> { |
|
122 for (Tree tree : subTreesToScan) { |
|
123 scan(tree, null); |
|
124 } |
|
125 }); |
|
126 } |
|
127 |
|
128 private void doScan(String preferredBuilderName, Runnable scan) { |
|
129 String prevBuilder = currentBuilder; |
|
130 String builder = inferBuilderName(preferredBuilderName); |
|
131 try { |
|
132 result.append(builder + " -> "); |
|
133 int len = result.length(); |
|
134 currentBuilder = builder; |
|
135 scan.run(); |
|
136 if (result.length() == len) { |
|
137 result.append("{}"); |
|
138 } |
|
139 } finally { |
|
140 currentBuilder = prevBuilder; |
|
141 usedNames.remove(builder); |
|
142 } |
|
143 } |
|
144 private String inferBuilderName(String preferredBuilderName) { |
|
145 Integer idx = null; |
|
146 while (true) { |
|
147 String thisBuilder = preferredBuilderName + (idx != null ? idx : ""); |
|
148 if (usedNames.add(thisBuilder)) { |
|
149 return thisBuilder; |
|
150 } |
|
151 idx = idx != null ? idx + 1 : 1; |
|
152 } |
|
153 } |
|
154 }.scan(t, null); |
|
155 |
|
156 return result.toString(); |
|
157 } |
|
158 |
|
159 } |