Debian Codegraph @

chunkio-processor/src/com/android/tools/chunkio/codegen/CodeGenerator.java

raw
1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.tools.chunkio.codegen;
18
19 import com.android.tools.chunkio.ChunkUtils;
20
21 import javax.lang.model.element.Modifier;
22 import java.io.IOException;
23 import java.util.Collections;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Locale;
27 import java.util.Set;
28 import java.util.TreeSet;
29
30 final class CodeGenerator {
31 private final Appendable out;
32
33 private final String indent;
34 private int indentLevel = 0;
35 private boolean needIndent = false;
36 private int multiline = -1;
37
38 private String currentPackage;
39
40 private final Set<String> importCandidates = new TreeSet<>();
41 private final Set<String> imports;
42
43 CodeGenerator(Appendable out, String indent) {
44 this.out = out;
45 this.indent = indent;
46 imports = Collections.emptySet();
47 }
48
49 CodeGenerator(Appendable out, String indent, Set<String> imports) {
50 this.out = out;
51 this.indent = indent;
52 this.imports = imports;
53 }
54
55 Set<String> getImportCandidates() {
56 return importCandidates;
57 }
58
59 Set<String> getImports() {
60 return imports;
61 }
62
63 CodeGenerator indent() {
64 indentLevel++;
65 return this;
66 }
67
68 private CodeGenerator indent(int level) {
69 indentLevel += level;
70 return this;
71 }
72
73 CodeGenerator unindent() {
74 if (indentLevel > 0) indentLevel--;
75 return this;
76 }
77
78 private CodeGenerator unindent(int level) {
79 if (indentLevel > level - 1) indentLevel -= level;
80 return this;
81 }
82
83 CodeGenerator emit(CodeSnippet snippet) throws IOException {
84 Iterator<Object> values = snippet.getValues().iterator();
85
86 for (String part : snippet.getParts()) {
87 if ("$L".equals(part)) {
88 emitLiteral(values.next());
89 } else if ("$N".equals(part)) {
90 emitIndented(String.valueOf(values.next()));
91 } else if ("$S".equals(part)) {
92 emitString(values.next());
93 } else if ("$T".equals(part)) {
94 ((TypeDef) values.next()).emit(this);
95 } else if ("$$".equals(part)) {
96 emitIndented("$");
97 } else if ("$>".equals(part)) {
98 indent();
99 } else if ("$<".equals(part)) {
100 unindent();
101 } else if ("$[".equals(part)) {
102 multiline = 0;
103 } else if ("$]".equals(part)) {
104 if (multiline > 0) unindent(2);
105 multiline = -1;
106 } else {
107 emitIndented(part);
108 }
109 }
110 return this;
111 }
112
113 private void emitString(Object value) throws IOException {
114 if (value == null) {
115 emitIndented("null");
116 } else {
117 emitIndented(escapeString(value.toString()));
118 }
119 }
120
121 private static String escapeString(String s) {
122 StringBuilder builder = new StringBuilder(s.length());
123 builder.append('"');
124 char c;
125 for (int i = 0; i < s.length(); i++) {
126 switch (c = s.charAt(i)) {
127 case '"':
128 builder.append("\\\"");
129 break;
130 case '\\':
131 builder.append("\\\\");
132 break;
133 case '\b':
134 builder.append("\\b");
135 break;
136 case '\f':
137 builder.append("\\f");
138 break;
139 case '\n':
140 builder.append("\\n");
141 break;
142 case '\r':
143 builder.append("\\r");
144 break;
145 case '\t':
146 builder.append("\\t");
147 break;
148 default:
149 builder.append(c);
150 }
151 }
152 builder.append('"');
153 return builder.toString();
154 }
155
156 CodeGenerator emit(String format, Object... args) throws IOException {
157 return emit(CodeSnippet.builder().add(format, args).build());
158 }
159
160 private void emitLiteral(Object value) throws IOException {
161 if (value instanceof CodeSnippet) {
162 emit((CodeSnippet) value);
163 } else {
164 emitIndented(String.valueOf(value));
165 }
166 }
167
168 void emitIndented(String s) throws IOException {
169 String[] lines = s.split("\n", -1);
170 boolean firstLine = true;
171 boolean lastLineWasStatement = false;
172
173 for (String line : lines) {
174 if (!firstLine) {
175 out.append('\n');
176 if (!lastLineWasStatement) {
177 if (multiline >= 0) {
178 if (multiline == 0) indent(2);
179 multiline++;
180 }
181 }
182 needIndent = true;
183 }
184 firstLine = false;
185
186 if (line.isEmpty()) continue;
187 if (needIndent) emitIndent();
188 needIndent = false;
189
190 out.append(line);
191 lastLineWasStatement = line.charAt(line.length() - 1) == ';';
192 }
193 }
194
195 private void emitIndent() throws IOException {
196 for (int i = 0; i < indentLevel; i++) {
197 out.append(indent);
198 }
199 }
200
201 void emitModifiers(Set<Modifier> modifiers) throws IOException {
202 for (Modifier modifier : modifiers) {
203 emitIndented(modifier.name().toLowerCase(Locale.ENGLISH) + " ");
204 }
205 }
206
207 void setPackage(String currentPackage) {
208 this.currentPackage = currentPackage;
209 }
210
211 void emitClassName(List<String> names) throws IOException {
212 String packageName = names.get(0);
213 if (!packageName.equals(currentPackage) && !"java.lang".equals(packageName)) {
214 String canonicalName = ChunkUtils.join(names, ".");
215 if (imports.contains(canonicalName)) {
216 emit(ChunkUtils.join(names.subList(1, names.size()), "."));
217 return;
218 }
219 importCandidates.add(canonicalName);
220 emitIndented(canonicalName);
221 return;
222 }
223 emitIndented(ChunkUtils.join(names.subList(1, names.size()), "."));
224 }
225 }