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