KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > code > java > JavaType


1 /*
2  * JavaType.java
3  *
4  * This work is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * This work is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * As a special exception, the copyright holders of this library give
20  * you permission to link this library with independent modules to
21  * produce an executable, regardless of the license terms of these
22  * independent modules, and to copy and distribute the resulting
23  * executable under terms of your choice, provided that you also meet,
24  * for each linked independent module, the terms and conditions of the
25  * license of that module. An independent module is a module which is
26  * not derived from or based on this library. If you modify this
27  * library, you may extend this exception to your version of the
28  * library, but you are not obligated to do so. If you do not wish to
29  * do so, delete this exception statement from your version.
30  *
31  * Copyright (c) 2003 Per Cederberg. All rights reserved.
32  */

33
34 package net.percederberg.grammatica.code.java;
35
36 import java.io.PrintWriter JavaDoc;
37
38 import net.percederberg.grammatica.code.CodeElement;
39 import net.percederberg.grammatica.code.CodeElementContainer;
40 import net.percederberg.grammatica.code.CodeStyle;
41
42 /**
43  * An abstract superclass for the Java class and interface code
44  * generators.
45  *
46  * @author Per Cederberg, <per at percederberg dot net>
47  * @version 1.0
48  */

49 abstract class JavaType extends CodeElementContainer {
50
51     /**
52      * The type modifier flags.
53      */

54     private int modifiers;
55
56     /**
57      * The type name.
58      */

59     private String JavaDoc name;
60
61     /**
62      * The name of the type that this type extends.
63      */

64     private String JavaDoc extendType;
65
66     /**
67      * The set of classes or interfaces that this type implements.
68      */

69     private String JavaDoc[] implementTypes = null;
70
71     /**
72      * The type comment.
73      */

74     private JavaComment comment = null;
75
76     /**
77      * Creates a new type code generator.
78      *
79      * @param modifiers the modifier constant flags
80      * @param name the type name
81      * @param extendsType the class or interface to extend
82      * @param implementType the class or interface to implement
83      */

84     protected JavaType(int modifiers,
85                        String JavaDoc name,
86                        String JavaDoc extendsType,
87                        String JavaDoc implementType) {
88
89         this.modifiers = modifiers;
90         this.name = name;
91         this.extendType = extendsType;
92         if (implementType == null || implementType.equals("")) {
93             this.implementTypes = new String JavaDoc[0];
94         } else {
95             this.implementTypes = new String JavaDoc[1];
96             this.implementTypes[0] = implementType;
97         }
98     }
99
100     /**
101      * Creates a new type code generator.
102      *
103      * @param modifiers the modifier constant flags
104      * @param name the type name
105      * @param extendsType the class or interface to extend
106      * @param implementTypes the classes or interfaces to implement
107      */

108     protected JavaType(int modifiers,
109                        String JavaDoc name,
110                        String JavaDoc extendsType,
111                        String JavaDoc[] implementTypes) {
112
113         this.modifiers = modifiers;
114         this.name = name;
115         this.extendType = extendsType;
116         this.implementTypes = implementTypes;
117     }
118
119     /**
120      * Returns the type name.
121      *
122      * @return the type name
123      */

124     public String JavaDoc toString() {
125         return name;
126     }
127
128     /**
129      * Adds a comment to this type.
130      *
131      * @param comment the new type comment
132      */

133     public void addComment(JavaComment comment) {
134         this.comment = comment;
135     }
136
137     /**
138      * Prints the type to the specified stream.
139      *
140      * @param out the output stream
141      * @param style the code style to use
142      * @param indent the indentation level
143      * @param type the type name (i.e. "class" or "interface")
144      */

145     protected void print(PrintWriter JavaDoc out,
146                          CodeStyle style,
147                          int indent,
148                          String JavaDoc type) {
149
150         String JavaDoc indentStr = style.getIndent(indent);
151         String JavaDoc codeIndentStr = style.getIndent(indent + 1);
152         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
153         String JavaDoc str;
154
155         // Print type comment
156
if (this.comment != null) {
157             this.comment.print(out, style, indent);
158         }
159
160         // Print class declaration
161
buf.append(indentStr);
162         buf.append(JavaModifier.createModifierDecl(modifiers));
163         buf.append(type);
164         buf.append(" ");
165         buf.append(name);
166         if (extendType != null && !extendType.equals("")) {
167             buf.append(" extends ");
168             buf.append(extendType);
169         }
170         str = createImplDecl();
171         if (str.length() > 0) {
172             if (buf.length() + str.length() > style.getMargin()) {
173                 buf.append("\n");
174                 buf.append(codeIndentStr);
175             } else {
176                 buf.append(" ");
177             }
178             buf.append(str);
179         }
180         buf.append(" {");
181         out.println(buf.toString());
182         out.println();
183
184         // Print class contents
185
printContents(out, style, indent + 1);
186
187         // Print end of class
188
out.println(indentStr + "}");
189     }
190
191     /**
192      * Prints the lines separating two elements.
193      *
194      * @param out the output stream
195      * @param style the code style to use
196      * @param prev the previous element, or null if first
197      * @param next the next element, or null if last
198      */

199     protected void printSeparator(PrintWriter JavaDoc out,
200                                   CodeStyle style,
201                                   CodeElement prev,
202                                   CodeElement next) {
203
204         if (prev == null || next == null) {
205             // Do nothing
206
} else {
207             out.println();
208         }
209     }
210
211     /**
212      * Creates a string with the implements declaration.
213      *
214      * @return a string with the implements declararation, or
215      * null for no implement declaration
216      */

217     private String JavaDoc createImplDecl() {
218         StringBuffer JavaDoc res = new StringBuffer JavaDoc("implements ");
219
220         if (implementTypes == null || implementTypes.length <= 0) {
221             return "";
222         }
223         for (int i = 0; i < implementTypes.length; i++) {
224             res.append(implementTypes[i]);
225             if (i + 1 < implementTypes.length) {
226                 res.append(", ");
227             }
228         }
229
230         return res.toString();
231     }
232 }
233
Popular Tags