1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.jdo.impl.enhancer.generator;
19
20 import java.lang.reflect.Modifier;
21
22 import java.util.List;
23
24 import java.io.Writer;
25 import java.io.FileWriter;
26 import java.io.IOException;
27
28
29 /***
30 *
31 */
32 final class CodeWriter
33 extends NameHelper
34 {
35 static private final String lineSeparator
36 = System.getProperty("line.separator");
37
38 static private final String indent = " ";
39
40 private Writer writer = null;
41
42 private int initialIndents = 0;
43
44 public final void setWriter(Writer writer)
45 {
46 this.writer = writer;
47 }
48
49 public final void setInitialIndents(int indents)
50 {
51 this.initialIndents = indents;
52 }
53
54 public void writePackage(final String packagename,/package-summary.html">ong> void writePackage(final String packagename,
55 final String[] comments)
56 throws IOException
57 {
58 writeComment(0, "Generated by " + Main.class);
59 writeComments(0, comments);
60 if (packagename != null && packagename.length() > 0) {
61 writeln();
62 writeln(0, "package " + normalizeClassName(packagename) + ';');
63 }
64 writeln();
65 }
66
67 public void writeImports(final List imports,
68 final String[] comments)
69 throws IOException
70 {
71 writeComments(0, comments);
72 final int n = (imports != null ? imports.size() : 0);
73 for (int i = 0; i < n; i++) {
74 String imp = (String)imports.get(i);
75 if (imp != null && imp.length() > 0) {
76 writeln(0, "import " + imp + ';');
77 } else {
78 writeln();
79 }
80 }
81 writeln();
82 }
83
84 public void writeClassHeader(final int modifiers,
85 String classname,
86 String superclass,
87 final String[] interfaces,
88 final String[] comments)
89 throws IOException
90 {
91 writeComments(0, comments);
92
93 classname = getClassName(classname);
94 superclass = normalizeClassName(superclass);
95
96
97 final String mod = Modifier.toString(modifiers);
98 writeln(0, mod +(mod.length() > 0 ? " " : "") + "class " + classname);
99
100
101 if (superclass != null) {
102 writeln(1, "extends " + superclass);
103 }
104
105
106 {
107 final int n = (interfaces != null ? interfaces.length : 0);
108 if (n > 0) {
109 write(1, "implements ");
110 for (int i = 0; i < n; i++) {
111 write((String)interfaces[i]);
112 if (i < n - 1) {
113 write(0, ", ");
114 }
115 }
116 writeln();
117 }
118 }
119
120 writeln(0, "{");
121 }
122
123 public void writeClassEnd()
124 throws IOException
125 {
126 writeln(0, "}");
127 writeln();
128 }
129
130 public void writeField(final String name,
131 final int modifiers,
132 String type,
133 final String init_value,
134 final String[] comments)
135 throws IOException
136 {
137 writeComments(1, comments);
138 type = normalizeClassName(type);
139 String s = Modifier.toString(modifiers) + ' ' + type + ' ' + name;
140 if (init_value != null) {
141 s += " = " + init_value;
142 }
143 writeln(1, s + ';');
144 writeln();
145 }
146
147 public void writeStaticInitializer(final List impl,
148 final String[] comments)
149 throws IOException
150 {
151 writeComments(1, comments);
152
153
154 writeln(1, "static");
155 writeln(1, "{");
156
157
158 final int n =(impl != null ? impl.size() : 0);
159 for (int i = 0; i < n; i++) {
160 writeln(2, (String)impl.get(i));
161 }
162
163
164 writeln(1, "}");
165 writeln();
166 }
167
168 public void writeMethod(final String name,
169 final int modifiers,
170 final String return_type,
171 final String[] param_names,
172 final String[] param_types,
173 final String[] exceptions,
174 final List impl,
175 final String[] comments)
176 throws IOException
177 {
178 writeComments(1, comments);
179
180
181 String sig = createMethodSignature(name, modifiers,
182 return_type,
183 param_names, param_types,
184 exceptions);
185
186 if (sig.length() > 0) {
187 writeln(1, sig);
188 }
189 writeln(1, "{");
190
191
192 final int n =(impl != null ? impl.size() : 0);
193 for (int i = 0; i < n; i++) {
194 writeln(2,(String) impl.get(i));
195 }
196
197
198 writeln(1, "}");
199 writeln();
200 }
201
202 public void writeConstructor(final String name,
203 final int modifiers,
204 final String[] param_names,
205 final String[] param_types,
206 final String[] exceptions,
207 final List impl,
208 final String[] comments)
209 throws IOException
210 {
211 writeMethod(name, modifiers, null,
212 param_names, param_types, exceptions, impl,
213 comments);
214 }
215
216 static private String createMethodSignature(final String name,
217 final int modifiers,
218 String return_type,
219 final String[] param_names,
220 final String[] param_types,
221 final String[] exceptions)
222 throws IOException
223 {
224 return_type = normalizeClassName(return_type);
225 String s = "";
226 if (modifiers != 0) {
227 s += Modifier.toString(modifiers) + ' ';
228 }
229 s += (return_type != null ? return_type + " " : "") + name;
230
231
232 {
233 s += "(";
234 final int n = (param_names != null ? param_names.length : 0);
235 for (int i = 0; i < n; i++) {
236 s += (normalizeClassName(param_types[i]) + ' '
237 + param_names[i]);
238 if (i < n - 1) {
239 s += ", ";
240 }
241 }
242 s += ')';
243 }
244
245
246 {
247 final int n = (exceptions != null ? exceptions.length : 0);
248 if (n > 0) {
249 s += " throws ";
250 for (int i = 0; i < n; i++) {
251 s += exceptions[i];
252 if (i < n - 1) {
253 s += ", ";
254 }
255 }
256 }
257 }
258
259 return s;
260 }
261
262 public void writeComment(final int indents,
263 final String comment)
264 throws IOException
265 {
266 if (comment != null) {
267 writeln(indents, "// " + comment);
268 }
269 }
270
271 public void writeComments(final int indents,
272 final String[] comments)
273 throws IOException
274 {
275 final int n = (comments != null ? comments.length : 0);
276 for (int i = 0; i < n; i++) {
277 final String s = comments[i];
278 writeln(indents, "// " + (s != null ? s : ""));
279 }
280 }
281
282 private void _write(final int indents,
283 final String s)
284 throws IOException
285 {
286 for (int i = 0; i < indents; i++) {
287 writer.write(indent);
288 }
289 writer.write(s);
290 }
291
292 private void write(final int indents,
293 final String s)
294 throws IOException
295 {
296 _write(indents + this.initialIndents, s);
297 }
298
299 private void write(final String s)
300 throws IOException
301 {
302 _write(0, s);
303 }
304
305 private void writeln(final int indents,
306 final String s)
307 throws IOException
308 {
309 if (this.initialIndents > 0) {
310 _write(this.initialIndents, "");
311 }
312 _write(indents, s + lineSeparator);
313 }
314
315 public void writeln()
316 throws IOException
317 {
318 writeln(0, "");
319 }
320 }