KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > utility > generator > io > IOJavaClassWriter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * IOJavaClassWriter.java
26  *
27  * Created on November 12, 2001, 4:59 PM
28  */

29
30 package com.sun.jdo.spi.persistence.utility.generator.io;
31
32 import java.util.*;
33 import java.lang.reflect.Modifier JavaDoc;
34
35 import com.sun.jdo.spi.persistence.utility.StringHelper;
36 import com.sun.jdo.spi.persistence.utility.generator.JavaClassWriter;
37
38 /**
39  * This implementation of the {@link JavaClassWriter} interface is based on
40  * simple {@link java.lang.StringBuffer} "println" type statements.
41  * <p>
42  * The order of the generated code in this implementation depends on the
43  * initialization. The default order is to accept fields and methods in any
44  * order and generate all fields together and all methods together (by member
45  * category). Specifying the ordered parameter in the constructor will lead
46  * to a slightly different order: generation of fields and methods
47  * interspersed among each other in exactly the order they were added (no
48  * member categories).
49  *
50  * @author raccah
51  */

52 public final class IOJavaClassWriter implements JavaClassWriter
53 {
54     private static final String JavaDoc FIELD = "FIELD"; // NOI18N
55
private static final String JavaDoc INITIALIZER = "INITIALIZER"; // NOI18N
56
private static final String JavaDoc CONSTRUCTOR = "CONSTRUCTOR"; // NOI18N
57
private static final String JavaDoc METHOD = "METHOD"; // NOI18N
58
private static final String JavaDoc INNER_CLASS = "INNER_CLASS"; // NOI18N
59
private static final String JavaDoc MIXED = "MIXED"; // NOI18N
60
private static final String JavaDoc COMMA_SEPARATOR = ", "; // NOI18N
61

62     private boolean _maintainCategories;
63     private String JavaDoc _superclass;
64     private String JavaDoc _classDeclarationBlock;
65     private List _interfaces = new ArrayList();
66     private Map _members = new HashMap();
67
68     /** Creates a new instance of IOJavaClassWriter which maintains the
69      * order of member input but groups them by category.
70      * @see #IOJavaClassWriter(boolean)
71      */

72     public IOJavaClassWriter ()
73     {
74         this(true);
75     }
76
77     /** Creates a new instance of IOJavaClassWriter in which the order of the
78      * generated code depends on the specified flag.
79      * @param maintainCategories If <code>true</code>, the order of members is
80      * preserved within category groups. If <code>false</code>, the
81      * generation of fields and methods will be interspersed among each other
82      * in exactly the order they were added with no member categories.
83      */

84     public IOJavaClassWriter (boolean maintainCategories)
85     {
86         _maintainCategories = maintainCategories;
87     }
88
89     /** Sets the information for the class declaration including modifiers,
90      * name, and comments. Note that the name must not be fully qualified.
91      * @param modifiers The modifier flags for this class.
92      * @param className The (non-qualified) name of this class.
93      * @param comments The comments shown just above the class declaration.
94      * The comments are passed as an array so the line separators can be added
95      * by the implementation. Note that not all implementations will choose
96      * to make use of this comment.
97      * @see java.lang.reflect.Modifier
98      */

99     public void setClassDeclaration (final int modifiers,
100         final String JavaDoc className, final String JavaDoc[] comments)
101     {
102         final FormattedWriter writerHelper = new FormattedWriter();
103         final String JavaDoc modifierString = Modifier.toString(modifiers);
104
105         writerHelper.writeComments(comments);
106
107         writerHelper.writeln(modifierString + ((modifierString.length() > 0)
108             ? " " : "") + "class " + className); // NOI18N
109

110         _classDeclarationBlock = writerHelper.toString();
111     }
112
113     /** Sets the superclass of this class. Note that the name format must
114      * be package style (that is - it can contain . but not / or $).
115      * @param name The name of the superclass.
116      */

117     public void setSuperclass (final String JavaDoc name)
118     {
119         _superclass = name;
120     }
121
122     /** Adds an interface to the list of those implemented by this class.
123      * @param name The name of the interface.
124      */

125     public void addInterface (final String JavaDoc name)
126     {
127         if (!StringHelper.isEmpty(name))
128             _interfaces.add(name);
129     }
130
131     /** Adds a field to the list of those declared by this class. Note
132      * that the type format must be package style (that is - it can contain
133      * . but not / or $).
134      * @param name The name of the field.
135      * @param modifiers The modifier flags for this field.
136      * @param type A string representing the type of this field.
137      * @param initialValue A string representing the initial value of
138      * this field.
139      * @param comments The comments shown just above the declaration of
140      * this field. The comments are passed as an array so the line
141      * separators can be added by the implementation. Note that not all
142      * implementations will choose to make use of this comment.
143      * @see java.lang.reflect.Modifier
144      */

145     public void addField (final String JavaDoc name, final int modifiers, String JavaDoc type,
146         final String JavaDoc initialValue, final String JavaDoc[] comments)
147     {
148         final FormattedWriter writerHelper = new FormattedWriter();
149         final String JavaDoc fieldString =
150             Modifier.toString(modifiers) + ' ' + type + ' ' + name;
151
152         writerHelper.writeComments(comments);
153         writerHelper.writeln(fieldString + ((initialValue != null) ?
154             (" = " + initialValue) : "") + ';'); // NOI18N
155

156         getMemberList(FIELD).add(writerHelper.toString());
157     }
158
159     /** Adds an initializer to this class.
160      * @param isStatic True if this is a static initializer, false otherwise.
161      * @param body The implementation block of the initializer. The body of
162      * the implementation is passed as an array so the line separators can
163      * be added by the implementation.
164      * @param comments The comments shown just above the initializer block.
165      * The comments are passed as an array so the line separators can be added
166      * by the implementation. Note that not all implementations will choose
167      * to make use of this comment.
168      */

169     public void addInitializer (boolean isStatic, String JavaDoc[] body,
170         String JavaDoc[] comments)
171     {
172         final FormattedWriter writerHelper = new FormattedWriter();
173         final int n = (body != null ? body.length : 0);
174
175         writerHelper.writeComments(comments);
176
177         // header
178
writerHelper.writeln(isStatic ? "static" : ""); // NOI18N
179
writerHelper.writeln("{"); // NOI18N
180

181         // implementation
182
for (int i = 0; i < n; i++)
183             writerHelper.writeln(1, body[i]);
184
185         // end
186
writerHelper.writeln("}"); // NOI18N
187

188         getMemberList(INITIALIZER).add(writerHelper.toString());
189     }
190
191     /** Adds a constructor to this class. Note that the type format in the
192      * parameter type strings must be package style (that is - it can contain
193      * . but not / or $).
194      * @param name The name of the constructor - should be the same as the
195      * name of the class.
196      * @param modifiers The modifier flags for this constructor.
197      * @param parameterNames A list of parameter names.
198      * @param parameterTypes A list of parameter types.
199      * @param exceptions A list of exceptions.
200      * @param body The implementation block of the constructor. The body of
201      * the implementation is passed as an array so the line separators can
202      * be added by the implementation.
203      * @param comments The comments shown just above the constructor. The
204      * comments are passed as an array so the line separators can be added
205      * by the implementation. Note that not all implementations will choose
206      * to make use of this comment.
207      * @see java.lang.reflect.Modifier
208      */

209     public void addConstructor (final String JavaDoc name, final int modifiers,
210         final String JavaDoc[] parameterNames, final String JavaDoc[] parameterTypes,
211         final String JavaDoc[] exceptions, final String JavaDoc[] body, final String JavaDoc[] comments)
212     {
213         addMethod(name, modifiers, null, parameterNames, parameterTypes,
214             exceptions, body, comments, getMemberList(CONSTRUCTOR));
215     }
216
217     /** Adds a method to this class. Note that the type format in the
218      * return type and parameter type strings must be package style
219      * (that is - it can contain . but not / or $).
220      * @param name The name of the method.
221      * @param modifiers The modifier flags for this method.
222      * @param returnType A string representing the return type of this method.
223      * @param parameterNames A list of parameter names.
224      * @param parameterTypes A list of parameter types.
225      * @param exceptions A list of exceptions.
226      * @param body The implementation block of the method. The body of
227      * the implementation is passed as an array so the line separators can
228      * be added by the implementation.
229      * @param comments The comments shown just above the method. The
230      * comments are passed as an array so the line separators can be added
231      * by the implementation. Note that not all implementations will choose
232      * to make use of this comment.
233      * @see java.lang.reflect.Modifier
234      */

235     public void addMethod (final String JavaDoc name, final int modifiers,
236         final String JavaDoc returnType, final String JavaDoc[] parameterNames,
237         final String JavaDoc[] parameterTypes, final String JavaDoc[] exceptions,
238         final String JavaDoc[] body, final String JavaDoc[] comments)
239     {
240         addMethod(name, modifiers, returnType, parameterNames, parameterTypes,
241             exceptions, body, comments, getMemberList(METHOD));
242     }
243
244     /** Adds an inner class to this class.
245      * @param classWriter The definition of the inner class.
246      */

247     public void addClass (final JavaClassWriter classWriter)
248     {
249         if (classWriter != null)
250             getMemberList(INNER_CLASS).add(classWriter);
251     }
252
253     /** Returns a string representation of this object.
254      * @return The string representation of the generated class.
255      */

256     public String JavaDoc toString ()
257     {
258         final FormattedWriter writerHelper = new FormattedWriter();
259
260         writeClassDeclaration(writerHelper); // class declaration
261
writeMembers(writerHelper); // members
262
writerHelper.writeln("}"); // NOI18N // closing
263
writerHelper.writeln();
264
265         return writerHelper.toString();
266     }
267
268     private void addMethod (final String JavaDoc name, final int modifiers,
269         final String JavaDoc returnType, final String JavaDoc[] parameterNames,
270         final String JavaDoc[] parameterTypes, final String JavaDoc[] exceptions,
271         final String JavaDoc[] body, final String JavaDoc[] comments, List methodList)
272     {
273         final String JavaDoc signature = createMethodSignature(name, modifiers,
274             returnType, parameterNames, parameterTypes, exceptions);
275         final FormattedWriter writerHelper = new FormattedWriter();
276         final int n = (body != null ? body.length : 0);
277
278         writerHelper.writeComments(comments);
279
280         // signature==null if we have an instance initializer
281
if (signature.length() > 0)
282             writerHelper.writeln(signature);
283
284         writerHelper.writeln("{"); // NOI18N
285

286         // implementation
287
for (int i = 0; i < n; i++)
288             writerHelper.writeln(1, body[i]);
289
290         // end
291
writerHelper.writeln("}"); // NOI18N
292

293         methodList.add(writerHelper.toString());
294     }
295
296     static private String JavaDoc createMethodSignature (final String JavaDoc name,
297         final int modifiers, String JavaDoc returnType, final String JavaDoc[] parameterNames,
298         final String JavaDoc[] parameterTypes, final String JavaDoc[] exceptions)
299     {
300         int i, count = (parameterNames != null ? parameterNames.length : 0);
301         final List parameterPairs = new ArrayList();
302         final FormattedWriter writerHelper = new FormattedWriter();
303
304         if (modifiers != 0)
305             writerHelper.write(Modifier.toString(modifiers) + ' ');
306
307         writerHelper.write(((returnType != null) ?
308             returnType + " " : "") + name); // NOI18N
309

310         // parameters
311
writerHelper.write(" ("); // NOI18N
312

313         for (i = 0; i < count; i++)
314         {
315             writeListElement(i, count, parameterTypes[i] + ' ' +
316                 parameterNames[i], writerHelper);
317         }
318         writerHelper.write(")"); // NOI18N
319

320         // exceptions
321
count = (exceptions != null ? exceptions.length : 0);
322         if (count > 0)
323         {
324             writerHelper.writeln();
325             writerHelper.write(1, "throws "); // NOI18N
326

327             for (i = 0; i < count; i++)
328                 writeListElement(i, count, exceptions[i], writerHelper);
329         }
330
331         return writerHelper.toString();
332     }
333
334     static private void writeListElement (int i, int count, String JavaDoc string,
335         FormattedWriter writerHelper)
336     {
337         int indent = ((i == 0) ? 0 : 1);
338
339         if (i == (count - 1))
340             writerHelper.write(indent, string);
341         else
342             writerHelper.writeln(indent, string + COMMA_SEPARATOR);
343     }
344
345     private List getMemberList (String JavaDoc memberType)
346     {
347         Object JavaDoc memberList = null;
348
349         if (!_maintainCategories)
350             memberType = MIXED;
351
352         memberList = _members.get(memberType);
353         if (memberList == null)
354         {
355             memberList = new ArrayList();
356
357             _members.put(memberType, memberList);
358         }
359
360         return (List)memberList;
361     }
362
363     private void writeClassDeclaration (FormattedWriter writerHelper)
364     {
365         // class declaration block
366
writerHelper.write(_classDeclarationBlock);
367
368         // extends
369
if (_superclass != null)
370             writerHelper.writeln(1, "extends " + _superclass); // NOI18N
371

372         // implements
373
if ((_interfaces != null) && (_interfaces.size() > 0))
374         {
375             writerHelper.write(1, "implements "); // NOI18N
376
writerHelper.write(StringHelper.arrayToSeparatedList(
377                 _interfaces, COMMA_SEPARATOR));
378             writerHelper.writeln();
379         }
380
381         writerHelper.writeln("{"); // NOI18N
382
}
383
384     private void writeMembers (FormattedWriter writerHelper)
385     {
386         if (_maintainCategories)
387         {
388             writerHelper.writeList(1, getMemberList(FIELD));
389             writerHelper.writeList(1, getMemberList(INITIALIZER));
390             writerHelper.writeList(1, getMemberList(CONSTRUCTOR));
391             writerHelper.writeList(1, getMemberList(METHOD));
392             writerHelper.writeList(1, getMemberList(INNER_CLASS));
393         }
394         else
395             writerHelper.writeList(1, getMemberList(MIXED), true);
396     }
397 }
398
Popular Tags