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 /**
38 * A class which holds information about the methods of a JClass. Modelled closely after the Java
39 * Reflection API. This class is part of package which is used to create source code.
40 *
41 * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
42 * @version $Revision$ $Date: 2004-12-03 11:57:33 -0700 (Fri, 03 Dec 2004) $
43 */
44 public final class JMethod implements JMember, JAnnotatedElement {
45 // --------------------------------------------------------------------------
46
47 /** The set of classes that contain this JMethod. */
48 private final Vector<JClass> _classes = new Vector<>(1);
49
50 /**
51 * The JavaDoc comment for this JMethod. This will overwrite the JavaDoc for the JMethodSignature.
52 */
53 private JDocComment _jdc = null;
54
55 /** The source code for this method. */
56 private JSourceCode _source = null;
57
58 /** The signature for this method. */
59 private JMethodSignature _signature = null;
60
61 // --------------------------------------------------------------------------
62
63 /**
64 * Creates a new JMethod with the given name and "void" return type.
65 *
66 * @param name The method name. Must not be null.
67 */
68 public JMethod(final String name) {
69 if (name == null || name.length() == 0) {
70 String err = "The method name must not be null or zero-length";
71 throw new IllegalArgumentException(err);
72 }
73 _source = new JSourceCode();
74 _signature = new JMethodSignature(name);
75 _jdc = _signature.getJDocComment();
76 }
77
78 /**
79 * Creates a new JMethod with the given name and returnType. The return type must not be empty or
80 * null. For "void" return types, use {@link #JMethod(String)} instead of this constructor.
81 *
82 *
83 * @param name The method name. Must not be null.
84 * @param returnType The return type of the method. Must not be null.
85 * @param returnDoc Javadoc comment for the @return annotation. If null, a default (and
86 * mostly useless) javadoc comment will be generated.
87 */
88 public JMethod(final String name, final JType returnType, final String returnDoc) {
89 this(name);
90
91 _signature = new JMethodSignature(name, returnType);
92 _jdc = _signature.getJDocComment();
93 _jdc.appendComment("Method " + name + ".");
94 if (returnDoc != null && returnDoc.length() > 0) {
95 _jdc.addDescriptor(JDocDescriptor.createReturnDesc(returnDoc));
96 } else {
97 _jdc.addDescriptor(JDocDescriptor.createReturnDesc(returnType.getLocalName()));
98 }
99 }
100
101 // --------------------------------------------------------------------------
102
103 /**
104 * Adds the given Exception to this JMethod's throws clause.
105 *
106 * @param exp The JClass representing the Exception.
107 * @param description JavaDoc comment explaining when this exception is thrown.
108 */
109 public void addException(final JClass exp, final String description) {
110 _signature.addException(exp);
111 _jdc.addDescriptor(JDocDescriptor.createExceptionDesc(exp.getName(), description));
112 }
113
114 /**
115 * Adds the given parameter to this JMethod's list of parameters.
116 *
117 * @param parameter The parameter to add to the this JMethod's list of parameters.
118 */
119 public void addParameter(final JParameter parameter) {
120 _signature.addParameter(parameter);
121
122 // -- be considerate and add the class name to the
123 // -- each declaring class' list of imports
124 JType jType = parameter.getType();
125 while (jType.isArray() || jType instanceof JCollectionType) {
126 if (jType.isArray()) {
127 jType = ((JArrayType) jType).getComponentType();
128 } else {
129 jType = ((JCollectionType) jType).getComponentType();
130 }
131 }
132 if (!jType.isPrimitive()) {
133 JClass jClass = (JClass) jType;
134 for (JClass iClass : _classes) {
135 iClass.addImport(jClass.getName());
136 }
137 }
138 }
139
140 /**
141 * Returns the JavaDoc comment describing this JMethod.
142 *
143 * @return The JavaDoc comment describing this JMethod.
144 */
145 public JDocComment getJDocComment() {
146 return _jdc;
147 }
148
149 /**
150 * Returns the exceptions that this JMethod throws.
151 *
152 * @return The exceptions that this JMethod throws.
153 */
154 public JClass[] getExceptions() {
155 return _signature.getExceptions();
156 }
157
158 /**
159 * Returns the amount of exceptions thrown.
160 *
161 * @return The amount of exceptions thrown.
162 */
163 public int getExceptionCount() {
164 return _signature.getExceptions().length;
165 }
166
167 /**
168 * Returns the modifiers for this JMethod.
169 *
170 * @return The modifiers for this JMethod.
171 */
172 public JModifiers getModifiers() {
173 return _signature.getModifiers();
174 }
175
176 /**
177 * Returns the name of this JMethod.
178 *
179 * @return The name of this JMethod.
180 */
181 public String getName() {
182 return _signature.getName();
183 }
184
185 /**
186 * Returns the JParameter at the given index.
187 *
188 * @param index The index of the JParameter to return.
189 * @return The JParameter at the given index.
190 */
191 public JParameter getParameter(final int index) {
192 return _signature.getParameter(index);
193 }
194
195 /**
196 * Returns the set of JParameters for this JMethod. <br/>
197 * <B>Note:</B> the array is a copy, the parameters in the array are the actual references.
198 *
199 * @return The set of JParameters for this JMethod.
200 */
201 public JParameter[] getParameters() {
202 return _signature.getParameters();
203 }
204
205 /**
206 * Returns the amount of parameters.
207 *
208 * @return The amount of parameters.
209 */
210 public int getParameterCount() {
211 return _signature.getParameters().length;
212 }
213
214 /**
215 * Returns the JType that represents the return type of the JMethod.
216 *
217 * @return The JType that represents the return type of the JMethod.
218 */
219 public JType getReturnType() {
220 return _signature.getReturnType();
221 }
222
223 /**
224 * Returns the JMethodSignature for this JMethod.
225 *
226 * @return The JMethodSignature for this JMethod.
227 */
228 public JMethodSignature getSignature() {
229 return _signature;
230 }
231
232 /**
233 * Returns the JSourceCode for the method body.
234 *
235 * @return The JSourceCode for the method body.
236 */
237 public JSourceCode getSourceCode() {
238 return this._source;
239 }
240
241 /**
242 * Sets the name of this JMethod.
243 *
244 * @param name The name of this method.
245 */
246 public void setName(final String name) {
247 _signature.setName(name);
248 }
249
250 /**
251 * Sets the comment describing this JMethod. The comment will be printed when this JMethod is
252 * printed.
253 *
254 * @param comment The comment for this member.
255 */
256 public void setComment(final String comment) {
257 _jdc.setComment(comment);
258 }
259
260 /**
261 * Sets the JModifiers for this JMethod. This JMethod will use only a copy of the JModifiers.
262 * <br/>
263 * <B>Note:</B> The JModifiers will be set in the containing JMethodSignature. If the
264 * JMethodSignature is used by other methods, keep in mind that it will be changed.
265 *
266 * @param modifiers The JModifiers to set.
267 */
268 public void setModifiers(final JModifiers modifiers) {
269 _signature.setModifiers(modifiers);
270 }
271
272 /**
273 * Sets the given string as the source code (method body) for this JMethod.
274 *
275 * @param source The String that represents the method body.
276 */
277 public void setSourceCode(final String source) {
278 setSourceCode(new JSourceCode(source));
279 }
280
281 /**
282 * Sets the given JSourceCode as the source code (method body) for this JMethod.
283 *
284 * @param source The JSourceCode that represents the method body.
285 */
286 public void setSourceCode(final JSourceCode source) {
287 _source = source;
288 }
289
290 /**
291 * {@inheritDoc}
292 */
293 public String toString() {
294 return _signature.toString();
295 }
296
297 /**
298 * {@inheritDoc}
299 */
300 public JAnnotation getAnnotation(final JAnnotationType annotationType) {
301 return _signature.getAnnotation(annotationType);
302 }
303
304 /**
305 * {@inheritDoc}
306 */
307 public JAnnotation[] getAnnotations() {
308 return _signature.getAnnotations();
309 }
310
311 /**
312 * {@inheritDoc}
313 */
314 public boolean isAnnotationPresent(final JAnnotationType annotationType) {
315 return _signature.isAnnotationPresent(annotationType);
316 }
317
318 /**
319 * {@inheritDoc}
320 */
321 public void addAnnotation(final JAnnotation annotation) {
322 _signature.addAnnotation(annotation);
323 }
324
325 /**
326 * {@inheritDoc}
327 */
328 public JAnnotation removeAnnotation(final JAnnotationType annotationType) {
329 return _signature.removeAnnotation(annotationType);
330 }
331
332 /**
333 * {@inheritDoc}
334 */
335 public boolean hasAnnotations() {
336 return _signature.hasAnnotations();
337 }
338
339 // --------------------------------------------------------------------------
340
341 /**
342 * Prints this JMethod to the given JSourceWriter.
343 *
344 * @param jsw The JSourceWriter to print to.
345 */
346 public void print(final JSourceWriter jsw) {
347 // ------------/
348 // - Java Doc -/
349 // ------------/
350
351 _jdc.print(jsw);
352
353 // --------------------/
354 // - Method Signature -/
355 // --------------------/
356
357 _signature.print(jsw, false);
358
359 if (_signature.getModifiers().isAbstract()) {
360 jsw.writeln(";");
361 } else {
362 jsw.writeln(" {");
363 _source.print(jsw);
364 jsw.writeln("}");
365 }
366 }
367
368 // --------------------------------------------------------------------------
369 }