View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 1999-2002 (C) Intalio, Inc. All Rights Reserved.
42   */
43  package org.exolab.javasource;
44  
45  import java.util.Vector;
46  
47  /**
48   * Describes the definition of a enum constant.
49   *
50   * @author <a href="mailto:andrew DOT fawcett AT coda DOT com">Andrew Fawcett</a>
51   * @version $Revision$ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
52   */
53  public final class JEnumConstant extends JAnnotatedElementHelper implements JMember {
54      //--------------------------------------------------------------------------
55  
56      /**  Name of this JEnumConstant. */
57      private String _name;
58      
59      /** Array of arguments provided to this JEnumConstant at initialization. May be null. */
60      private String[] _arguments;
61      
62      /** JavaDoc comment for this JEnumConstant. */
63      private JDocComment _comment;
64      
65      /** A list of methods attached to this JEnumConstant. */
66      private Vector<JMethod> _methods = null;
67  
68      //--------------------------------------------------------------------------
69  
70      /**
71       * Constructs a JEnumConstant with a given name and no initialization
72       * arguements.
73       *
74       * @param name Name of the constant.
75       */
76      public JEnumConstant(final String name) {
77          this(name, null);
78      }
79  
80      /**
81       * Constructs a JEnumConstant with a given name and initialization arguments.
82       *
83       * @param name Name of the constant.
84       * @param arguments The initialization arguments provided.
85       */
86      public JEnumConstant(final String name, final String[] arguments) {
87          setName(name);
88          
89          _methods = new Vector<JMethod>();
90          _comment = new JDocComment();
91          _comment.appendComment("Constant " + name);
92          _arguments = arguments;
93      }
94  
95      //--------------------------------------------------------------------------
96  
97      /**
98       * Returns the modifiers for this JEnumConstant.
99       *
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      * Returns the amount of arguments.
126      * 
127      * @return The amount of arguments.
128      */
129     public int getArgumentCount() {
130         return _arguments.length;
131     }
132 
133     /**
134      * Adds the given JMethod to this JEnumConstant.
135      *
136      * @param jMethod The JMethod to add.
137      */
138     public void addMethod(final JMethod jMethod) {
139          addMethod(jMethod, true);
140     }
141 
142     /**
143      * Adds the given JMethod to this JEnumConstant.
144      *
145      * @param jMethod The JMethod to add.
146      * @param importReturnType True if we add the importReturnType to the class
147      *        import lists. It could be useful to set it to false when all
148      *        types are fully qualified.
149      */
150     public void addMethod(final JMethod jMethod, final boolean importReturnType) {
151         if (jMethod == null) {
152             throw new IllegalArgumentException("Class methods cannot be null");
153         }
154 
155         //-- check method name and signatures *add later*
156 
157         //-- keep method list sorted for esthetics when printing
158         //-- START SORT :-)
159         boolean added = false;
160         JModifiers modifiers = jMethod.getModifiers();
161 
162         if (modifiers.isAbstract()) {
163             getModifiers().setAbstract(true);
164         }
165 
166         for (int i = 0; i < _methods.size(); i++) {
167             JMethod tmp = _methods.elementAt(i);
168             //-- first compare modifiers
169             if (tmp.getModifiers().isPrivate()) {
170                 if (!modifiers.isPrivate()) {
171                     _methods.insertElementAt(jMethod, i);
172                     added = true;
173                     break;
174                 }
175             }
176             //-- compare names
177             if (jMethod.getName().compareTo(tmp.getName()) < 0) {
178                     _methods.insertElementAt(jMethod, i);
179                     added = true;
180                     break;
181             }
182         }
183         //-- END SORT
184         if (!added) { _methods.addElement(jMethod); }
185     }
186 
187     /**
188      * Adds the given array of JMethods to this JEnumConstant.
189      *
190      * @param jMethods The array of JMethod to add.
191      */
192     public void addMethods(final JMethod[] jMethods) {
193         for (int i = 0; i < jMethods.length; i++) {
194             addMethod(jMethods[i]);
195         }
196     }
197 
198     /**
199      * Returns an array of all the JMethods of this JEnumConstant.
200      *
201      * @return An array of all the JMethods of this JEnumConstant.
202      */
203     public JMethod[] getMethods() {
204         int size = _methods.size();
205         JMethod[] marray = new JMethod[size];
206 
207         for (int i = 0; i < _methods.size(); i++) {
208             marray[i] = _methods.elementAt(i);
209         }
210         return marray;
211     }
212     
213     public int getMethodCount() {
214         return _methods.size();
215     }
216 
217     /**
218      * Returns the first occurance of the method with the given name, starting
219      * from the specified index.
220      *
221      * @param name The name of the method to look for.
222      * @param startIndex The starting index to begin the search.
223      * @return The method if found, otherwise null.
224      */
225     public JMethod getMethod(final String name, final int startIndex) {
226         for (int i = startIndex; i < _methods.size(); i++) {
227             JMethod jMethod = _methods.elementAt(i);
228             if (jMethod.getName().equals(name)) { return jMethod; }
229         }
230         return null;
231     }
232 
233     /**
234      * Returns the JMethod located at the specified index.
235      *
236      * @param index The index of the JMethod to return.
237      * @return The JMethod.
238      */
239     public JMethod getMethod(final int index) {
240         return _methods.elementAt(index);
241     }
242 
243     /**
244      * Sets the name of this JEnumConstant.
245      *
246      * @param name The name of this JEnumConstant.
247      */
248     public void setName(final String name) {
249         if (!JNaming.isValidJavaIdentifier(name)) {
250             String err = "'" + name + "' is ";
251             if (JNaming.isKeyword(name)) {
252                 err += "a reserved word and may not be used as "
253                     + " a field name.";
254             } else {
255                 err += "not a valid Java identifier.";
256             }
257             throw new IllegalArgumentException(err);
258         }
259         _name = name;
260     }
261 
262     /**
263      * Returns the name of this JEnumConstant.
264      *
265      * @return The name of this JEnumConstant.
266      */
267     public String getName() {
268         return _name;
269     }
270 
271     /**
272      * Sets the JavaDoc comment describing this JEnumConstant.
273      *
274      * @param comment The JavaDoc comment for this JEnumConstant.
275      */
276     public void setComment(final JDocComment comment) {
277         _comment = comment;
278     }
279 
280     /**
281      * Sets the JavaDoc comment describing this JEnumConstant.
282      *
283      * @param comment The JavaDoc comment for this JEnumConstant.
284      */
285     public void setComment(final String comment) {
286         if (_comment == null) {
287             _comment = new JDocComment();
288         }
289         _comment.setComment(comment);
290     }
291 
292     /**
293      * Returns the JavaDoc comment describing this JEnumConstant.
294      *
295      * @return The JavaDoc comment describing this JEnumConstant, or null if
296      *         none has been set.
297      */
298     public JDocComment getComment() {
299         return _comment;
300     }
301 
302     //--------------------------------------------------------------------------
303 
304     /**
305      * prints this enum constant.
306      *
307      * @param jsw The JSourceWriter to print to. Must not be null.
308      */
309     public void print(final JSourceWriter jsw) {
310         //-- print comments
311         if (_comment != null) { _comment.print(jsw); }
312         //-- print annotation
313         if (printAnnotations(jsw)) { jsw.writeln(); }
314         //-- print name
315         jsw.write(_name);
316         //-- print arguments
317         if (_arguments != null && _arguments.length > 0) {
318             jsw.write("(");
319             for (int a = 0; a < _arguments.length; a++) {
320                 jsw.write(_arguments[a]);
321                 if (a < _arguments.length - 1) { jsw.write(", "); }
322             }
323             jsw.write(")");
324         }
325         //-- print methods
326         if (_methods.size() > 0) {
327             jsw.write(" {");
328             jsw.writeln();
329             jsw.indent();
330             for (int i = 0; i < _methods.size(); i++) {
331                 JMethod jMethod = _methods.elementAt(i);
332                 jMethod.print(jsw);
333                 if (i < _methods.size() - 1) { jsw.writeln(); }
334             }
335             jsw.unindent();
336             jsw.write("}");
337         }
338     }
339 
340     //--------------------------------------------------------------------------
341 }