KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > code > csharp > CSharpMethod


1 /*
2  * CSharpMethod.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.csharp;
35
36 import java.io.PrintWriter JavaDoc;
37 import java.util.LinkedList JavaDoc;
38
39 import net.percederberg.grammatica.code.CodeElement;
40 import net.percederberg.grammatica.code.CodeStyle;
41
42 /**
43  * A class generating a C# method declaration.
44  *
45  * @author Per Cederberg, <per at percederberg dot net>
46  * @version 1.5
47  */

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

53     public static final int PUBLIC = CSharpModifier.PUBLIC;
54
55     /**
56      * The protected internal access modifier constant.
57      */

58     public static final int PROTECTED_INTERNAL =
59         CSharpModifier.PROTECTED_INTERNAL;
60
61     /**
62      * The protected access modifier constant.
63      */

64     public static final int PROTECTED = CSharpModifier.PROTECTED;
65
66     /**
67      * The internal access modifier constant.
68      */

69     public static final int INTERNAL = CSharpModifier.INTERNAL;
70
71     /**
72      * The private access modifier constant. Cannot be combined with
73      * virtual, override, or abstract.
74      */

75     public static final int PRIVATE = CSharpModifier.PRIVATE;
76
77     /**
78      * The static modifier constant. Cannot be combined with virtual,
79      * override, or abstract.
80      */

81     public static final int STATIC = CSharpModifier.STATIC;
82
83     /**
84      * The new modifier constant. Cannot be combined with override.
85      */

86     public static final int NEW = CSharpModifier.NEW;
87
88     /**
89      * The virtual modifier constant. Cannot be combined with private,
90      * static, override, or abstract.
91      */

92     public static final int VIRTUAL = CSharpModifier.VIRTUAL;
93
94     /**
95      * The sealed modifier constant. Cannot be combined with abstract.
96      */

97     public static final int SEALED = CSharpModifier.SEALED;
98
99     /**
100      * The override modifier constant. Cannot be combined with private,
101      * static, virtual, or new.
102      */

103     public static final int OVERRIDE = CSharpModifier.OVERRIDE;
104
105     /**
106      * The abstract modifier constant. Cannot be combined with private,
107      * static, virtual, sealed, or extern.
108      */

109     public static final int ABSTRACT = CSharpModifier.ABSTRACT;
110
111     /**
112      * The extern modifier constant. Cannot be combined with abstract.
113      */

114     public static final int EXTERN = CSharpModifier.EXTERN;
115
116     /**
117      * The method modifier flags.
118      */

119     private int modifiers;
120
121     /**
122      * The method name.
123      */

124     private String JavaDoc name;
125
126     /**
127      * The argument list.
128      */

129     private String JavaDoc args;
130
131     /**
132      * The return type.
133      */

134     private String JavaDoc returnType;
135
136     /**
137      * The implementing code.
138      */

139     private LinkedList JavaDoc code;
140
141     /**
142      * The method comment.
143      */

144     private CSharpComment comment;
145
146     /**
147      * The print code flag.
148      */

149     private boolean printCode;
150
151     /**
152      * Creates a new method with the specified name. The method will
153      * not take any arguments and will return void.
154      *
155      * @param name the method name
156      */

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

168     public CSharpMethod(String JavaDoc name, String JavaDoc args) {
169         this(name, args, "void");
170     }
171
172     /**
173      * Creates a new method with the specified arguments.
174      *
175      * @param name the method name
176      * @param args the argument list, excluding parenthesis
177      * @param returnType the return type
178      */

179     public CSharpMethod(String JavaDoc name, String JavaDoc args, String JavaDoc returnType) {
180         this(PUBLIC, name, args, returnType);
181     }
182
183     /**
184      * Creates a new method with the specified arguments.
185      *
186      * @param modifiers the modifier flags to use
187      * @param name the method name
188      * @param args the argument list, excluding parenthesis
189      * @param returnType the return type
190      */

191     public CSharpMethod(int modifiers,
192                         String JavaDoc name,
193                         String JavaDoc args,
194                         String JavaDoc returnType) {
195
196         this.modifiers = modifiers;
197         this.name = name;
198         this.args = args;
199         this.returnType = returnType;
200         this.code = new LinkedList JavaDoc();
201         this.comment = null;
202         this.printCode = true;
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(CSharpComment 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
276         // Print comment
277
if (comment != null) {
278             comment.print(out, style, indent);
279         }
280
281         // Handle declaration
282
res.append(indentStr);
283         res.append(CSharpModifier.createModifierDecl(modifiers));
284         res.append(returnType);
285         res.append(" ");
286         res.append(name);
287         res.append("(");
288         res.append(args);
289         res.append(")");
290
291         // Handle code
292
if (canPrintCode()) {
293             res.append(" {\n");
294             for (int i = 0; i < code.size(); i++) {
295                 if (code.get(i).toString().length() > 0) {
296                     res.append(codeIndentStr);
297                     res.append(code.get(i).toString());
298                     res.append("\n");
299                 } else {
300                     res.append("\n");
301                 }
302             }
303             res.append(indentStr);
304             res.append("}");
305         } else {
306             res.append(";");
307         }
308
309         // Print method
310
out.println(res.toString());
311     }
312 }
313
Popular Tags