KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JavaConstructor.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-2004 Per Cederberg. All rights reserved.
32  */

33
34 package net.percederberg.grammatica.code.java;
35
36 import java.io.PrintWriter JavaDoc;
37 import java.util.Collections JavaDoc;
38 import java.util.LinkedList JavaDoc;
39
40 import net.percederberg.grammatica.code.CodeElement;
41 import net.percederberg.grammatica.code.CodeStyle;
42
43 /**
44  * A class generating a Java constructor declaration.
45  *
46  * @author Per Cederberg, <per at percederberg dot net>
47  * @version 1.5
48  */

49 public class JavaConstructor extends CodeElement {
50
51     /**
52      * The public access modifier constant.
53      */

54     public static final int PUBLIC = JavaModifier.PUBLIC;
55
56     /**
57      * The protected access modifier constant.
58      */

59     public static final int PROTECTED = JavaModifier.PROTECTED;
60
61     /**
62      * The package local access modifier constant (i.e. no modifier).
63      */

64     public static final int PACKAGE_LOCAL = JavaModifier.PACKAGE_LOCAL;
65
66     /**
67      * The private access modifier constant.
68      */

69     public static final int PRIVATE = JavaModifier.PRIVATE;
70
71     /**
72      * The modifier flags.
73      */

74     private int modifiers;
75
76     /**
77      * The class to construct.
78      */

79     private JavaClass cls;
80
81     /**
82      * The argument list.
83      */

84     private String JavaDoc args;
85
86     /**
87      * The exceptions declared to be thrown.
88      */

89     private LinkedList JavaDoc throwList;
90
91     /**
92      * The implementing code.
93      */

94     private LinkedList JavaDoc code;
95
96     /**
97      * The constructor comment.
98      */

99     private JavaComment comment;
100
101     /**
102      * Creates a new empty constructor.
103      */

104     public JavaConstructor() {
105         this("");
106     }
107
108     /**
109      * Creates a new constructor with the specified arguments.
110      *
111      * @param args the argument list, excluding parenthesis
112      */

113     public JavaConstructor(String JavaDoc args) {
114         this(PUBLIC, args);
115     }
116
117     /**
118      * Creates a new constructor with the specified arguments.
119      *
120      * @param modifiers the modifier flags
121      * @param args the argument list, excluding parenthesis
122      */

123     public JavaConstructor(int modifiers, String JavaDoc args) {
124         this.modifiers = modifiers;
125         this.cls = null;
126         this.args = args;
127         this.throwList = new LinkedList JavaDoc();
128         this.code = new LinkedList JavaDoc();
129         this.comment = null;
130     }
131
132     /**
133      * Returns the class for this constructor, or null.
134      *
135      * @return the class for this constructor, or
136      * null if none has been assigned
137      */

138     public JavaClass getJavaClass() {
139         return this.cls;
140     }
141
142     /**
143      * Sets the class for this constructor.
144      *
145      * @param cls the class to add the constructor to
146      */

147     void setJavaClass(JavaClass cls) {
148         this.cls = cls;
149     }
150
151     /**
152      * Adds a class to the list of exceptions thrown.
153      *
154      * @param className the name of the exception thrown
155      */

156     public void addThrows(String JavaDoc className) {
157         this.throwList.add(className);
158     }
159
160     /**
161      * Adds one or more lines of actual code.
162      *
163      * @param codeLines the lines of Java code to add
164      */

165     public void addCode(String JavaDoc codeLines) {
166         int pos;
167
168         pos = codeLines.indexOf('\n');
169         while (pos >= 0) {
170             this.code.add(codeLines.substring(0, pos));
171             codeLines = codeLines.substring(pos + 1);
172             pos = codeLines.indexOf('\n');
173         }
174         this.code.add(codeLines);
175     }
176
177     /**
178      * Sets a comment for this constructor.
179      *
180      * @param comment the new constructor comment
181      */

182     public void addComment(JavaComment comment) {
183         this.comment = comment;
184     }
185
186     /**
187      * Returns a numeric category number for the code element. A lower
188      * category number implies that the code element should be placed
189      * before code elements with a higher category number within a
190      * declaration.
191      *
192      * @return the category number
193      */

194     public int category() {
195         return 7;
196     }
197
198     /**
199      * Prints the code element to the specified output stream.
200      *
201      * @param out the output stream
202      * @param style the code style to use
203      * @param indent the indentation level
204      */

205     public void print(PrintWriter JavaDoc out, CodeStyle style, int indent) {
206         String JavaDoc indentStr = style.getIndent(indent);
207         String JavaDoc codeIndentStr = style.getIndent(indent + 1);
208         StringBuffer JavaDoc res = new StringBuffer JavaDoc();
209         String JavaDoc str;
210         boolean brokenThrows = false;
211
212         // Print comment
213
if (comment != null) {
214             comment.print(out, style, indent);
215         }
216
217         // Handle declaration
218
res.append(indentStr);
219         res.append(JavaModifier.createModifierDecl(modifiers));
220         res.append(cls.toString());
221         res.append("(");
222         res.append(args);
223         res.append(")");
224         str = getThrowDecl();
225         if (str.length() > 0) {
226             if (res.length() + str.length() < style.getMargin()) {
227                 res.append(" ");
228             } else {
229                 res.append("\n");
230                 res.append(codeIndentStr);
231                 brokenThrows = true;
232             }
233             res.append(str);
234         }
235         res.append(" {\n");
236
237         // Handle code
238
if (brokenThrows && code.size() > 0) {
239             res.append("\n");
240         }
241         for (int i = 0; i < code.size(); i++) {
242             if (code.get(i).toString().length() > 0) {
243                 res.append(codeIndentStr);
244                 res.append(code.get(i).toString());
245                 res.append("\n");
246             } else {
247                 res.append("\n");
248             }
249         }
250         res.append(indentStr);
251         res.append("}");
252
253         // Print method
254
out.println(res.toString());
255     }
256
257     /**
258      * Returns a 'throws' declaration. If there are no classes to throw,
259      * an empty string will be returned.
260      *
261      * @return a throw declaration, or
262      * an empty string if no exceptions are thrown
263      */

264     private String JavaDoc getThrowDecl() {
265         StringBuffer JavaDoc res = new StringBuffer JavaDoc("throws ");
266
267         if (throwList.size() == 0) {
268             return "";
269         }
270         Collections.sort(throwList);
271         for (int i = 0; i < throwList.size(); i++) {
272             res.append(throwList.get(i).toString());
273             if (i < throwList.size() - 1) {
274                 res.append(", ");
275             }
276         }
277         return res.toString();
278     }
279 }
280
Popular Tags