KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > code > visualbasic > VisualBasicMethod


1 /*
2  * VisualBasicMethod.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) 2004 Adrian Moore. All rights reserved.
32  * Copyright (c) 2004 Per Cederberg. All rights reserved.
33  */

34
35 package net.percederberg.grammatica.code.visualbasic;
36
37 import java.io.PrintWriter 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 Visual Basic method declaration.
45  *
46  * @author Adrian Moore, <adrianrob at hotmail dot com>
47  * @author Per Cederberg, <per at percederberg dot net>
48  * @version 1.5
49  * @since 1.5
50  */

51 public class VisualBasicMethod extends CodeElement {
52
53     /**
54      * The public access modifier constant.
55      */

56     public static final int PUBLIC = VisualBasicModifier.PUBLIC;
57
58     /**
59      * The protected friend access modifier constant.
60      */

61     public static final int PROTECTED_FRIEND =
62         VisualBasicModifier.PROTECTED_FRIEND;
63
64     /**
65      * The protected access modifier constant.
66      */

67     public static final int PROTECTED = VisualBasicModifier.PROTECTED;
68
69     /**
70      * The friend access modifier constant.
71      */

72     public static final int FRIEND = VisualBasicModifier.FRIEND;
73
74     /**
75      * The private access modifier constant. Cannot be combined with
76      * virtual, overrides, or must override.
77      */

78     public static final int PRIVATE = VisualBasicModifier.PRIVATE;
79
80     /**
81      * The shared modifier constant. Cannot be combined with overridable,
82      * overrides, or must override.
83      */

84     public static final int SHARED = VisualBasicModifier.SHARED;
85
86     /**
87      * The shadows modifier constant. Cannot be combined with overrides.
88      */

89     public static final int SHADOWS = VisualBasicModifier.SHADOWS;
90
91     /**
92      * The overridable modifier constant. Cannot be combined with
93      * private, shared, overrides, or must override.
94      */

95     public static final int OVERRIDABLE = VisualBasicModifier.OVERRIDABLE;
96
97     /**
98      * The not overridable modifier constant. Cannot be combined with
99      * must override.
100      */

101     public static final int NOT_OVERRIDABLE =
102         VisualBasicModifier.NOT_OVERRIDABLE;
103
104     /**
105      * The overrides modifier constant. Cannot be combined with
106      * private, shared, overridable, or shadows.
107      */

108     public static final int OVERRIDES = VisualBasicModifier.OVERRIDES;
109
110     /**
111      * The must override modifier constant. Cannot be combined with
112      * private, shared, overridable, not overridable, or extern.
113      */

114     public static final int MUST_OVERRIDE =
115         VisualBasicModifier.MUST_OVERRIDE;
116
117     /**
118      * The overloads modifier constant.
119      */

120     public static final int OVERLOADS = VisualBasicModifier.OVERLOADS;
121
122     /**
123      * The method modifier flags.
124      */

125     private int modifiers;
126
127     /**
128      * The method name.
129      */

130     private String JavaDoc name;
131
132     /**
133      * The argument list.
134      */

135     private String JavaDoc args;
136
137     /**
138      * The return type.
139      */

140     private String JavaDoc returnType;
141
142     /**
143      * The implementing code.
144      */

145     private LinkedList JavaDoc code;
146
147     /**
148      * The method comment.
149      */

150     private VisualBasicComment comment;
151
152     /**
153      * The print code flag.
154      */

155     private boolean printCode;
156
157     /**
158      * Creates a new method with the specified name. The method will
159      * not take any arguments and will return void.
160      *
161      * @param name the method name
162      */

163     public VisualBasicMethod(String JavaDoc name) {
164         this(name, "");
165     }
166
167     /**
168      * Creates a new method with the specified name and arguments. The
169      * method will return void.
170      *
171      * @param name the method name
172      * @param args the argument list, excluding parenthesis
173      */

174     public VisualBasicMethod(String JavaDoc name, String JavaDoc args) {
175         this(name, args, "");
176     }
177
178     /**
179      * Creates a new method with the specified arguments.
180      *
181      * @param name the method name
182      * @param args the argument list, excluding parenthesis
183      * @param returnType the return type
184      */

185     public VisualBasicMethod(String JavaDoc name, String JavaDoc args, String JavaDoc returnType) {
186         this(PUBLIC, name, args, returnType);
187     }
188
189     /**
190      * Creates a new method with the specified arguments.
191      *
192      * @param modifiers the modifier flags to use
193      * @param name the method name
194      * @param args the argument list, excluding parenthesis
195      * @param returnType the return type
196      */

197     public VisualBasicMethod(int modifiers,
198                              String JavaDoc name,
199                              String JavaDoc args,
200                              String JavaDoc returnType) {
201
202         this.modifiers = modifiers;
203         this.name = name;
204         this.args = args;
205         this.returnType = returnType;
206         this.code = new LinkedList JavaDoc();
207         this.comment = null;
208         this.printCode = true;
209     }
210
211     /**
212      * Adds one or more lines of actual code.
213      *
214      * @param codeLines the lines of Java code to add
215      */

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

233     public void addComment(VisualBasicComment comment) {
234         this.comment = comment;
235     }
236
237     /**
238      * Returns a numeric category number for the code element. A lower
239      * category number implies that the code element should be placed
240      * before code elements with a higher category number within a
241      * declaration.
242      *
243      * @return the category number
244      */

245     public int category() {
246         return ((modifiers & SHARED) > 0) ? 6 : 8;
247     }
248
249     /**
250      * Checks if the method source code can the printed. This method
251      * will return false if the method is must override or if the print
252      * code flag is set to false.
253      *
254      * @return true if method source code can be printed, or
255      * false otherwise
256      */

257     public boolean canPrintCode() {
258         return printCode && (modifiers & MUST_OVERRIDE) == 0;
259     }
260
261     /**
262      * Sets the print code flag.
263      *
264      * @param value the new print code flag value
265      */

266     public void setPrintCode(boolean value) {
267         this.printCode = value;
268     }
269
270     /**
271      * Prints the code element to the specified output stream.
272      *
273      * @param out the output stream
274      * @param style the code style to use
275      * @param indent the indentation level
276      */

277     public void print(PrintWriter JavaDoc out, CodeStyle style, int indent) {
278         String JavaDoc indentStr = style.getIndent(indent);
279         String JavaDoc codeIndentStr = style.getIndent(indent + 1);
280         StringBuffer JavaDoc res = new StringBuffer JavaDoc();
281
282         // Print comment
283
if (comment != null) {
284             comment.print(out, style, indent);
285         }
286
287         // Handle declaration
288
res.append(indentStr);
289         res.append(VisualBasicModifier.createModifierDecl(modifiers));
290         if (returnType.equals("")) {
291             res.append("Sub");
292         } else {
293             res.append("Function");
294         }
295         res.append(" ");
296         res.append(name);
297         res.append("(");
298         res.append(args);
299         if (returnType.equals("")) {
300             res.append(")");
301         } else {
302             res.append(") As ");
303             res.append(returnType);
304         }
305
306         // Handle code
307
if (canPrintCode()) {
308             res.append("\n");
309             for (int i = 0; i < code.size(); i++) {
310                 if (code.get(i).toString().length() > 0) {
311                     res.append(codeIndentStr);
312                     res.append(code.get(i).toString());
313                     res.append("\n");
314                 } else {
315                     res.append("\n");
316                 }
317             }
318             res.append(indentStr);
319             if (returnType.equals("")) {
320                 res.append("End Sub");
321             } else {
322                 res.append("End Function");
323             }
324         }
325
326         // Print method
327
out.println(res.toString());
328     }
329 }
330
Popular Tags