KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JavaVariable.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.LinkedList JavaDoc;
38
39 import net.percederberg.grammatica.code.CodeElement;
40 import net.percederberg.grammatica.code.CodeStyle;
41
42 /**
43  * A class generating a Java variable declaration. The variable
44  * declaration should be placed as a member in a class.
45  *
46  * @author Per Cederberg, <per at percederberg dot net>
47  * @version 1.5
48  */

49 public class JavaVariable 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 final modifier constant.
78      */

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

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

89     public static final int VOLATILE = JavaModifier.VOLATILE;
90
91     /**
92      * The variable modifiers.
93      */

94     private int modifiers;
95
96     /**
97      * The variable type.
98      */

99     private String JavaDoc type;
100
101     /**
102      * The variable name.
103      */

104     private String JavaDoc name;
105
106     /**
107      * The variable init value. This initialization value is only used
108      * if a single initialization value is added. Otherwise the vector
109      * initialization is used.
110      *
111      * @see #vectorInit
112      */

113     private String JavaDoc initValue;
114
115     /**
116      * The list of init values. These initialization values are only
117      * used for array initializations, not for single values.
118      *
119      * @see #initValue
120      */

121     private LinkedList JavaDoc initValueList;
122
123     /**
124      * The variable comment.
125      */

126     private JavaComment comment;
127
128     /**
129      * Creates a new variable with the specified type and name.
130      *
131      * @param type the variable type
132      * @param name the variable name
133      */

134     public JavaVariable(String JavaDoc type, String JavaDoc name) {
135         this(PUBLIC, type, name);
136     }
137
138     /**
139      * Creates a new variable with the specified modifiers, type and
140      * name.
141      *
142      * @param modifiers the modifier flags to use
143      * @param type the variable type
144      * @param name the variable name
145      */

146     public JavaVariable(int modifiers, String JavaDoc type, String JavaDoc name) {
147         this(modifiers, type, name, null);
148     }
149
150     /**
151      * Creates a new variable with the specified type, name and
152      * initializer.
153      *
154      * @param type the variable type
155      * @param name the variable name
156      * @param initValue the initialize value
157      */

158     public JavaVariable(String JavaDoc type, String JavaDoc name, String JavaDoc initValue) {
159         this(PUBLIC, type, name, initValue);
160     }
161
162     /**
163      * Creates a new variable with the specified modifiers, type, name
164      * and initializer.
165      *
166      * @param modifiers the modifier flags to use
167      * @param type the variable type
168      * @param name the variable name
169      * @param initValue the initialize value
170      */

171     public JavaVariable(int modifiers,
172                         String JavaDoc type,
173                         String JavaDoc name,
174                         String JavaDoc initValue) {
175
176         this.modifiers = modifiers;
177         this.type = type;
178         this.name = name;
179         this.initValue = initValue;
180         this.initValueList = new LinkedList JavaDoc();
181         this.comment = null;
182     }
183
184     /**
185      * Adds a comment to this variable.
186      *
187      * @param comment the comment to add
188      */

189     public void addComment(JavaComment comment) {
190         this.comment = comment;
191     }
192
193     /**
194      * Adds initialization code for an array element value. Each array
195      * element value added will be added last in the list of
196      * initialization values. If an init value has been specified with
197      * the constructor, it will be added first.
198      *
199      * @param elementValue the array element value
200      */

201     public void addArrayInit(String JavaDoc elementValue) {
202         if (this.initValue != null) {
203             this.initValueList.add(this.initValue);
204             this.initValue = null;
205         }
206         this.initValueList.add(elementValue);
207     }
208
209     /**
210      * Returns a numeric category number for the code element. A lower
211      * category number implies that the code element should be placed
212      * before code elements with a higher category number within a
213      * declaration.
214      *
215      * @return the category number
216      */

217     public int category() {
218         return ((modifiers & STATIC) > 0) ? 4 : 5;
219     }
220
221     /**
222      * Prints the code element to the specified output stream.
223      *
224      * @param out the output stream
225      * @param style the code style to use
226      * @param indent the indentation level
227      */

228     public void print(PrintWriter JavaDoc out, CodeStyle style, int indent) {
229         String JavaDoc indentStr = style.getIndent(indent);
230         String JavaDoc prefix = JavaModifier.createModifierDecl(modifiers);
231         String JavaDoc init;
232
233         if (comment != null) {
234             comment.print(out, style, indent);
235         }
236         init = getInitCode(style, indent);
237         if (init == null) {
238             out.println(indentStr + prefix + type + " " + name + ";");
239         } else {
240             out.println(indentStr + prefix + type + " " + name + " = " +
241                         init + ";");
242         }
243     }
244
245     /**
246      * Returns indented initialization code lines. If not init code is
247      * available, the method returns null.
248      *
249      * @param style the Java code style
250      * @param indent the indentation level
251      *
252      * @return the indented initialization code
253      */

254     private String JavaDoc getInitCode(CodeStyle style, int indent) {
255         String JavaDoc indentStr = style.getIndent(indent);
256         String JavaDoc codeIndentStr = style.getIndent(indent + 1);
257         StringBuffer JavaDoc res;
258
259         // Check for simple init values
260
if (initValueList.size() == 0 && initValue == null) {
261             return null;
262         } else if (initValue != null) {
263             return initValue;
264         }
265
266         // Create array of init values
267
res = new StringBuffer JavaDoc("{\n");
268         for (int i = 0; i < initValueList.size(); i++) {
269             res.append(codeIndentStr);
270             res.append(initValueList.get(i).toString());
271             if (i + 1 < initValueList.size()) {
272                 res.append(",\n");
273             } else {
274                 res.append("\n");
275             }
276         }
277         res.append(indentStr);
278         res.append("}");
279
280         return res.toString();
281     }
282 }
283
Popular Tags