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