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