001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 1999-2002 (C) Intalio, Inc. All Rights Reserved.
042: */package org.exolab.javasource;
043:
044: import java.util.Vector;
045:
046: /**
047: * Describes the definition of a enum constant.
048: *
049: * @author <a href="mailto:andrew DOT fawcett AT coda DOT com">Andrew Fawcett</a>
050: * @version $Revision: 6669 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
051: */
052: public final class JEnumConstant extends JAnnotatedElementHelper
053: implements JMember {
054: //--------------------------------------------------------------------------
055:
056: /** Name of this JEnumConstant. */
057: private String _name;
058:
059: /** Array of arguments provided to this JEnumConstant at initialization. May be null. */
060: private String[] _arguments;
061:
062: /** JavaDoc comment for this JEnumConstant. */
063: private JDocComment _comment;
064:
065: /** A list of methods attached to this JEnumConstant. */
066: private Vector _methods = null;
067:
068: //--------------------------------------------------------------------------
069:
070: /**
071: * Constructs a JEnumConstant with a given name and no initialization
072: * arguements.
073: *
074: * @param name Name of the constant.
075: */
076: public JEnumConstant(final String name) {
077: this (name, null);
078: }
079:
080: /**
081: * Constructs a JEnumConstant with a given name and initialization arguments.
082: *
083: * @param name Name of the constant.
084: * @param arguments The initialization arguments provided.
085: */
086: public JEnumConstant(final String name, final String[] arguments) {
087: setName(name);
088:
089: _methods = new Vector();
090: _comment = new JDocComment();
091: _comment.appendComment("Constant " + name);
092: _arguments = arguments;
093: }
094:
095: //--------------------------------------------------------------------------
096:
097: /**
098: * Returns the modifiers for this JEnumConstant.
099: *
100: * @return The modifiers for this JEnumConstant.
101: */
102: public JModifiers getModifiers() {
103: throw new RuntimeException("Not implemented.");
104: }
105:
106: /**
107: * Sets the arguments specified by this constant.
108: *
109: * @param args Initialization arguments for this constant.
110: */
111: public void setArguments(final String[] args) {
112: _arguments = args;
113: }
114:
115: /**
116: * Returns the arguments used by this constant.
117: *
118: * @return The arguments used by this constant.
119: */
120: public String[] getArguments() {
121: return _arguments;
122: }
123:
124: /**
125: * Adds the given JMethod to this JEnumConstant.
126: *
127: * @param jMethod The JMethod to add.
128: */
129: public void addMethod(final JMethod jMethod) {
130: addMethod(jMethod, true);
131: }
132:
133: /**
134: * Adds the given JMethod to this JEnumConstant.
135: *
136: * @param jMethod The JMethod to add.
137: * @param importReturnType True if we add the importReturnType to the class
138: * import lists. It could be useful to set it to false when all
139: * types are fully qualified.
140: */
141: public void addMethod(final JMethod jMethod,
142: final boolean importReturnType) {
143: if (jMethod == null) {
144: throw new IllegalArgumentException(
145: "Class methods cannot be null");
146: }
147:
148: //-- check method name and signatures *add later*
149:
150: //-- keep method list sorted for esthetics when printing
151: //-- START SORT :-)
152: boolean added = false;
153: JModifiers modifiers = jMethod.getModifiers();
154:
155: if (modifiers.isAbstract()) {
156: getModifiers().setAbstract(true);
157: }
158:
159: for (int i = 0; i < _methods.size(); i++) {
160: JMethod tmp = (JMethod) _methods.elementAt(i);
161: //-- first compare modifiers
162: if (tmp.getModifiers().isPrivate()) {
163: if (!modifiers.isPrivate()) {
164: _methods.insertElementAt(jMethod, i);
165: added = true;
166: break;
167: }
168: }
169: //-- compare names
170: if (jMethod.getName().compareTo(tmp.getName()) < 0) {
171: _methods.insertElementAt(jMethod, i);
172: added = true;
173: break;
174: }
175: }
176: //-- END SORT
177: if (!added) {
178: _methods.addElement(jMethod);
179: }
180: }
181:
182: /**
183: * Adds the given array of JMethods to this JEnumConstant.
184: *
185: * @param jMethods The array of JMethod to add.
186: */
187: public void addMethods(final JMethod[] jMethods) {
188: for (int i = 0; i < jMethods.length; i++) {
189: addMethod(jMethods[i]);
190: }
191: }
192:
193: /**
194: * Returns an array of all the JMethods of this JEnumConstant.
195: *
196: * @return An array of all the JMethods of this JEnumConstant.
197: */
198: public JMethod[] getMethods() {
199: int size = _methods.size();
200: JMethod[] marray = new JMethod[size];
201:
202: for (int i = 0; i < _methods.size(); i++) {
203: marray[i] = (JMethod) _methods.elementAt(i);
204: }
205: return marray;
206: }
207:
208: /**
209: * Returns the first occurance of the method with the given name, starting
210: * from the specified index.
211: *
212: * @param name The name of the method to look for.
213: * @param startIndex The starting index to begin the search.
214: * @return The method if found, otherwise null.
215: */
216: public JMethod getMethod(final String name, final int startIndex) {
217: for (int i = startIndex; i < _methods.size(); i++) {
218: JMethod jMethod = (JMethod) _methods.elementAt(i);
219: if (jMethod.getName().equals(name)) {
220: return jMethod;
221: }
222: }
223: return null;
224: }
225:
226: /**
227: * Returns the JMethod located at the specified index.
228: *
229: * @param index The index of the JMethod to return.
230: * @return The JMethod.
231: */
232: public JMethod getMethod(final int index) {
233: return (JMethod) _methods.elementAt(index);
234: }
235:
236: /**
237: * Sets the name of this JEnumConstant.
238: *
239: * @param name The name of this JEnumConstant.
240: */
241: public void setName(final String name) {
242: if (!JNaming.isValidJavaIdentifier(name)) {
243: String err = "'" + name + "' is ";
244: if (JNaming.isKeyword(name)) {
245: err += "a reserved word and may not be used as "
246: + " a field name.";
247: } else {
248: err += "not a valid Java identifier.";
249: }
250: throw new IllegalArgumentException(err);
251: }
252: _name = name;
253: }
254:
255: /**
256: * Returns the name of this JEnumConstant.
257: *
258: * @return The name of this JEnumConstant.
259: */
260: public String getName() {
261: return _name;
262: }
263:
264: /**
265: * Sets the JavaDoc comment describing this JEnumConstant.
266: *
267: * @param comment The JavaDoc comment for this JEnumConstant.
268: */
269: public void setComment(final JDocComment comment) {
270: _comment = comment;
271: }
272:
273: /**
274: * Sets the JavaDoc comment describing this JEnumConstant.
275: *
276: * @param comment The JavaDoc comment for this JEnumConstant.
277: */
278: public void setComment(final String comment) {
279: if (_comment == null) {
280: _comment = new JDocComment();
281: }
282: _comment.setComment(comment);
283: }
284:
285: /**
286: * Returns the JavaDoc comment describing this JEnumConstant.
287: *
288: * @return The JavaDoc comment describing this JEnumConstant, or null if
289: * none has been set.
290: */
291: public JDocComment getComment() {
292: return _comment;
293: }
294:
295: //--------------------------------------------------------------------------
296:
297: /**
298: * prints this enum constant.
299: *
300: * @param jsw The JSourceWriter to print to. Must not be null.
301: */
302: public void print(final JSourceWriter jsw) {
303: //-- print comments
304: if (_comment != null) {
305: _comment.print(jsw);
306: }
307: //-- print annotation
308: if (printAnnotations(jsw)) {
309: jsw.writeln();
310: }
311: //-- print name
312: jsw.write(_name);
313: //-- print arguments
314: if (_arguments != null && _arguments.length > 0) {
315: jsw.write("(");
316: for (int a = 0; a < _arguments.length; a++) {
317: jsw.write(_arguments[a]);
318: if (a < _arguments.length - 1) {
319: jsw.write(", ");
320: }
321: }
322: jsw.write(")");
323: }
324: //-- print methods
325: if (_methods.size() > 0) {
326: jsw.write(" {");
327: jsw.writeln();
328: jsw.indent();
329: for (int i = 0; i < _methods.size(); i++) {
330: JMethod jMethod = (JMethod) _methods.elementAt(i);
331: jMethod.print(jsw);
332: if (i < _methods.size() - 1) {
333: jsw.writeln();
334: }
335: }
336: jsw.unindent();
337: jsw.write("}");
338: }
339: }
340:
341: //--------------------------------------------------------------------------
342: }
|