KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JavaMethod.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 method declaration.
45  *
46  * @author Per Cederberg, <per at percederberg dot net>
47  * @version 1.5
48  */

49 public class JavaMethod 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 static modifier constant.
73      */

74     public static final int STATIC = JavaModifier.STATIC;
75
76     /**
77      * The abstract modifier constant.
78      */

79     public static final int ABSTRACT = JavaModifier.ABSTRACT;
80
81     /**
82      * The final modifier constant.
83      */

84     public static final int FINAL = JavaModifier.FINAL;
85
86     /**
87      * The synchronized modifier constant.
88      */

89     public static final int SYNCHRONIZED = JavaModifier.SYNCHRONIZED;
90
91     /**
92      * The abstract modifier constant.
93      */

94     public static final int NATIVE = JavaModifier.NATIVE;
95
96     /**
97      * The strictfp modifier constant.
98      */

99     public static final int STRICTFP = JavaModifier.STRICTFP;
100
101     /**
102      * The method modifier flags.
103      */

104     private int modifiers;
105
106     /**
107      * The method name.
108      */

109     private String JavaDoc name;
110
111     /**
112      * The argument list.
113      */

114     private String JavaDoc args;
115
116     /**
117      * The return type.
118      */

119     private String JavaDoc returnType;
120
121     /**
122      * The exceptions declared to be thrown.
123      */

124     private LinkedList JavaDoc throwList;
125
126     /**
127      * The implementing code.
128      */

129     private LinkedList JavaDoc code;
130
131     /**
132      * The method comment.
133      */

134     private JavaComment comment;
135
136     /**
137      * The print code flag.
138      */

139     private boolean printCode;
140
141     /**
142      * Creates a new method with the specified name. The method will
143      * not take any arguments and will return void.
144      *
145      * @param name the method name
146      */

147     public JavaMethod(String JavaDoc name) {
148         this(name, "");
149     }
150
151     /**
152      * Creates a new method with the specified name and arguments. The
153      * method will return void.
154      *
155      * @param name the method name
156      * @param args the argument list, excluding parenthesis
157      */

158     public JavaMethod(String JavaDoc name, String JavaDoc args) {
159         this(name, args, "void");
160     }
161
162     /**
163      * Creates a new method with the specified arguments.
164      *
165      * @param name the method name
166      * @param args the argument list, excluding parenthesis
167      * @param returnType the return type
168      */

169     public JavaMethod(String JavaDoc name, String JavaDoc args, String JavaDoc returnType) {
170         this(PUBLIC, name, args, returnType);
171     }
172
173     /**
174      * Creates a new method with the specified arguments.
175      *
176      * @param modifiers the modifier flags to use
177      * @param name the method name
178      * @param args the argument list, excluding parenthesis
179      * @param returnType the return type
180      */

181     public JavaMethod(int modifiers,
182                       String JavaDoc name,
183                       String JavaDoc args,
184                       String JavaDoc returnType) {
185
186         this.modifiers = modifiers;
187         this.name = name;
188         this.args = args;
189         this.returnType = returnType;
190         this.throwList = new LinkedList JavaDoc();
191         this.code = new LinkedList JavaDoc();
192         this.comment = null;
193         this.printCode = true;
194     }
195
196     /**
197      * Adds a class to the list of exceptions thrown.
198      *
199      * @param className the name of the exception thrown
200      */

201     public void addThrows(String JavaDoc className) {
202         this.throwList.add(className);
203     }
204
205     /**
206      * Adds one or more lines of actual code.
207      *
208      * @param codeLines the lines of Java code to add
209      */

210     public void addCode(String JavaDoc codeLines) {
211         int pos;
212
213         pos = codeLines.indexOf('\n');
214         while (pos >= 0) {
215             code.add(codeLines.substring(0, pos));
216             codeLines = codeLines.substring(pos + 1);
217             pos = codeLines.indexOf('\n');
218         }
219         code.add(codeLines);
220     }
221
222     /**
223      * Sets a comment for this method.
224      *
225      * @param comment the new method comment
226      */

227     public void addComment(JavaComment comment) {
228         this.comment = comment;
229     }
230
231     /**
232      * Returns a numeric category number for the code element. A lower
233      * category number implies that the code element should be placed
234      * before code elements with a higher category number within a
235      * declaration.
236      *
237      * @return the category number
238      */

239     public int category() {
240         return ((modifiers & STATIC) > 0) ? 6 : 8;
241     }
242
243     /**
244      * Checks if the method source code can the printed. This method
245      * will return false if the method is abstract or if the print
246      * code flag is set to false.
247      *
248      * @return true if method source code can be printed, or
249      * false otherwise
250      */

251     public boolean canPrintCode() {
252         return printCode && (modifiers & ABSTRACT) == 0;
253     }
254
255     /**
256      * Sets the print code flag.
257      *
258      * @param value the new print code flag value
259      */

260     public void setPrintCode(boolean value) {
261         this.printCode = value;
262     }
263
264     /**
265      * Prints the code element to the specified output stream.
266      *
267      * @param out the output stream
268      * @param style the code style to use
269      * @param indent the indentation level
270      */

271     public void print(PrintWriter JavaDoc out, CodeStyle style, int indent) {
272         String JavaDoc indentStr = style.getIndent(indent);
273         String JavaDoc codeIndentStr = style.getIndent(indent + 1);
274         StringBuffer JavaDoc res = new StringBuffer JavaDoc();
275         String JavaDoc str;
276         boolean brokenThrows = false;
277
278         // Print comment
279
if (comment != null) {
280             comment.print(out, style, indent);
281         }
282
283         // Handle declaration
284
res.append(indentStr);
285         res.append(JavaModifier.createModifierDecl(modifiers));
286         res.append(returnType);
287         res.append(" ");
288         res.append(name);
289         res.append("(");
290         res.append(args);
291         res.append(")");
292         str = getThrowDecl();
293         if (str.length() > 0) {
294             if (res.length() + str.length() < style.getMargin()) {
295                 res.append(" ");
296             } else {
297                 res.append("\n");
298                 res.append(codeIndentStr);
299                 brokenThrows = true;
300             }
301             res.append(str);
302         }
303
304         // Handle code
305
if (canPrintCode()) {
306             res.append(" {\n");
307             if (brokenThrows && code.size() > 0) {
308                 res.append("\n");
309             }
310             for (int i = 0; i < code.size(); i++) {
311                 if (code.get(i).toString().length() > 0) {
312                     res.append(codeIndentStr);
313                     res.append(code.get(i).toString());
314                     res.append("\n");
315                 } else {
316                     res.append("\n");
317                 }
318             }
319             res.append(indentStr);
320             res.append("}");
321         } else {
322             res.append(";");
323         }
324
325         // Print method
326
out.println(res.toString());
327     }
328
329     /**
330      * Returns a 'throws' declaration. If there are no classes to throw,
331      * an empty string will be returned.
332      *
333      * @return a throw declaration, or
334      * an empty string if no exceptions are thrown
335      */

336     private String JavaDoc getThrowDecl() {
337         StringBuffer JavaDoc res = new StringBuffer JavaDoc("throws ");
338
339         if (throwList.size() == 0) {
340             return "";
341         }
342         Collections.sort(throwList);
343         for (int i = 0; i < throwList.size(); i++) {
344             res.append(throwList.get(i).toString());
345             if (i < throwList.size() - 1) {
346                 res.append(", ");
347             }
348         }
349         return res.toString();
350     }
351 }
352
Popular Tags