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@codehaus.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.codehaus.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   * $Id$
44   */
45  package org.codehaus.modello.plugin.java.javasource;
46  
47  /*
48   * Copyright (c) 2004, Codehaus.org
49   *
50   * Permission is hereby granted, free of charge, to any person obtaining a copy of
51   * this software and associated documentation files (the "Software"), to deal in
52   * the Software without restriction, including without limitation the rights to
53   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
54   * of the Software, and to permit persons to whom the Software is furnished to do
55   * so, subject to the following conditions:
56   *
57   * The above copyright notice and this permission notice shall be included in all
58   * copies or substantial portions of the Software.
59   *
60   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
61   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
62   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
63   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
64   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
65   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
66   * SOFTWARE.
67   */
68  
69  import java.util.ArrayList;
70  import java.util.List;
71  
72  /**
73   * A class which holds information about the methods of
74   * a JClass.
75   * Modelled closely after the Java Reflection API.
76   * This class is part of package which is used to
77   * create source code.
78   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
79   * @version $Revision$ $Date$
80   **/
81  public class JMethod implements JMember {
82  
83      /**
84       * The set of classes that contain this JMethod.
85       **/
86      private List<JClass> _classes = null;
87  
88      /**
89       * The JavaDoc comment for this JMethod. This
90       * will overwrite the JavaDoc for the
91       * JMethodSignature.
92       **/
93      private JDocComment jdc = null;
94  
95      /**
96       * The source code for this method
97       **/
98      private JSourceCode source = null;
99  
100     /**
101      * The signature for this method.
102      **/
103     private JMethodSignature _signature = null;
104 
105     /**
106      * The annotation(s) for this method.
107      */
108     private JAnnotations annotations = null;
109 
110     /**
111      * Creates a new JMethod with the given name and "void" return type.
112      *
113      * @param name, the method name. Must not be null.
114      **/
115     public JMethod(String name) {
116         this(name, null, null);
117     } // -- JMethod
118 
119     /**
120      * Creates a new JMethod with the given name and returnType.
121      * For "void" return types, simply pass in null as the returnType.
122      *
123      * @param name, the method name. Must not be null.
124      * @param returnType the return type of the method. May be null.
125      * @deprecated removed in future version of javasource
126      **/
127     public JMethod(JType returnType, String name) {
128         this(name, returnType, null);
129     } // -- JMethod
130 
131     /**
132      * Creates a new JMethod with the given name and returnType.
133      * For "void" return types, simply pass in null as the returnType.
134      *
135      * @param name, the method name. Must not be null.
136      * @param returnType the return type of the method. May be null.
137      * @param returnDoc Javadoc comment for the &#064;return annotation. If
138      *            null, a default (and mostly useless) javadoc comment will be
139      *            generated.
140      **/
141     public JMethod(final String name, final JType returnType, final String returnDoc) {
142         if ((name == null) || (name.length() == 0)) {
143             String err = "The method name must not be null or zero-length";
144             throw new IllegalArgumentException(err);
145         }
146 
147         _classes = new ArrayList<JClass>(1);
148         this.source = new JSourceCode();
149         _signature = new JMethodSignature(name, returnType);
150         this.jdc = _signature.getJDocComment();
151         jdc.appendComment("Method " + name + ".");
152 
153         // -- create comment
154         if (returnType != null) {
155             if (returnDoc != null && returnDoc.length() > 0) {
156                 jdc.addDescriptor(JDocDescriptor.createReturnDesc(returnDoc));
157             } else {
158                 jdc.addDescriptor(JDocDescriptor.createReturnDesc(returnType.getLocalName()));
159             }
160         }
161     }
162 
163     /**
164      * Adds the given Exception to this Method's throws clause.
165      *
166      * @param exp the JClass representing the Exception
167      **/
168     public void addException(JClass exp) {
169         _signature.addException(exp);
170     } // -- addException
171 
172     /**
173      * Adds the given parameter to this JMethod's list of parameters.
174      *
175      * @param parameter the parameter to add to the this Methods
176      * list of parameters.
177      * @throws java.lang.IllegalArgumentException when a parameter already
178      * exists for this Method with the same name as the new parameter
179      **/
180     public void addParameter(JParameter parameter) throws IllegalArgumentException {
181         _signature.addParameter(parameter);
182     } // -- addParameter
183 
184     /**
185      * Returns the JDocComment describing this member.
186      * @return the JDocComment describing this member.
187      **/
188     public JDocComment getJDocComment() {
189         return this.jdc;
190     } // -- getJDocComment
191 
192     /**
193      * Returns the class in which this JMember has been declared
194      * @return the class in which this JMember has been declared
195      **
196      * public JClass getDeclaringClass() {
197      * return _declaringClass;
198      * } //-- getDeclaringClass
199      */
200 
201     /**
202      * Returns the exceptions that this JMember throws.
203      *
204      * @return the exceptions that this JMember throws.
205      **/
206     public JClass[] getExceptions() {
207         return _signature.getExceptions();
208     } // -- getExceptions
209 
210     /**
211      * Returns the modifiers for this JMember.
212      *
213      * @return the modifiers for this JMember.
214      **/
215     public JModifiers getModifiers() {
216         return _signature.getModifiers();
217     } // -- getModifiers
218 
219     /**
220      * Returns the name of this JMember.
221      *
222      * @return the name of this JMember.
223      **/
224     public String getName() {
225         return _signature.getName();
226     } // -- getName
227 
228     /**
229      * Returns the JParameter at the given index.
230      *
231      * @param index the index of the JParameter to return.
232      * @return the JParameter at the given index.
233      **/
234     public JParameter getParameter(int index) {
235         return _signature.getParameter(index);
236     } // -- getParameter
237 
238     /**
239      * Returns the set of JParameters for this JMethod.
240      * <BR>
241      * <B>Note:</B> the array is a copy, the params in the array
242      * are the actual references.
243      *
244      * @return the set of JParameters for this JMethod
245      **/
246     public JParameter[] getParameters() {
247         return _signature.getParameters();
248     } // -- getParameters
249 
250     /**
251      * Returns the JType that represents the return type of the method.
252      *
253      * @return the JType that represents the return type of the method.
254      **/
255     public JType getReturnType() {
256         return _signature.getReturnType();
257     } // -- getReturnType
258 
259     /**
260      * Returns the JMethodSignature for this JMethod.
261      *
262      * @return the JMethodSignature for this JMethod.
263      **/
264     public JMethodSignature getSignature() {
265         return _signature;
266     } // -- getSignature
267 
268     /**
269      * Returns the JSourceCode for the method body.
270      *
271      * @return the JSourceCode for the method body.
272      **/
273     public JSourceCode getSourceCode() {
274         return this.source;
275     } // -- getSourceCode
276 
277     /**
278      * Sets the comment describing this member. The comment
279      * will be printed when this member is printed with the
280      * Class Printer.
281      *
282      * @param comment the comment for this member
283      * @see #getJDocComment
284      **/
285     public void setComment(String comment) {
286         jdc.setComment(comment);
287     } // -- setComment
288 
289     /**
290      * Sets the JModifiers for this JMethod. This
291      * JMethod will use only a copy of the JModifiers.
292      * <B>Note:</B> The JModifiers will be set in the
293      * containing JMethodSignature. If the JMethodSignature
294      * is used by other methods, keep in mind that it will be
295      * changed.
296      *
297      * @param modifiers the JModifiers to set.
298      **/
299     public void setModifiers(JModifiers modifiers) {
300         _signature.setModifiers(modifiers);
301     } // -- setModifiers
302 
303     /**
304      * Sets the given string as the source code (method body)
305      * for this JMethod.
306      *
307      * @param source the String that represents the method body.
308      **/
309     public void setSourceCode(String source) {
310         this.source = new JSourceCode(source);
311     } // -- setSource
312 
313     /**
314      * Sets the given JSourceCode as the source code (method body)
315      * for this JMethod.
316      *
317      * @param source the JSourceCode that represents the method body.
318      **/
319     public void setSourceCode(JSourceCode source) {
320         this.source = source;
321     } // -- setSource;
322 
323     /**
324      * Prints this JMethod to the given JSourceWriter.
325      *
326      * @param jsw the JSourceWriter to print to.
327      **/
328     public void print(JSourceWriter jsw) {
329 
330         // ------------/
331         // - Java Doc -/
332         // ------------/
333 
334         jdc.print(jsw);
335 
336         // --------------------/
337         // - Annotations     -/
338         // --------------------/
339 
340         JAnnotations annotations = getAnnotations();
341         if (annotations != null) annotations.print(jsw);
342 
343         // --------------------/
344         // - Method Signature -/
345         // --------------------/
346 
347         _signature.print(jsw, false);
348 
349         if (_signature.getModifiers().isAbstract()) {
350             jsw.writeln(";");
351         } else {
352             jsw.writeln();
353             jsw.writeln("{");
354             source.print(jsw);
355             jsw.write("} //-- ");
356             jsw.writeln(toString());
357         }
358     } // -- print
359 
360     /**
361      * Returns the String representation of this JMethod,
362      * which is the method prototype.
363      * @return the String representation of this JMethod, which
364      * is simply the method prototype
365      **/
366     public String toString() {
367         return _signature.toString();
368     } // -- toString
369 
370     // ---------------------/
371     // - PROTECTED METHODS -/
372     // ---------------------/
373 
374     /**
375      * Adds the given JClass to the set of classes that
376      * contain this method.
377      *
378      * @param jClass the JClass to add as one of
379      * the JClasses that contain this method.
380      **/
381     protected void addDeclaringClass(JClass jClass) {
382         _classes.add(jClass);
383     } // -- addDeclaringClass
384 
385     /**
386      * Removes the given JClass from the set of classes that
387      * contain this method.
388      *
389      * @param jClass the JClass to add as one of
390      * the JClasses that contain this method.
391      **/
392     protected void removeDeclaringClass(JClass jClass) {
393         _classes.remove(jClass);
394     } // -- removeDeclaringClass
395 
396     protected String[] getParameterClassNames() {
397         return _signature.getParameterClassNames();
398     } // -- getParameterClassNames
399 
400     /**
401      * @return the annotations
402      */
403     public JAnnotations getAnnotations() {
404         return annotations;
405     }
406 
407     /**
408      * @param annotation the annotation to append
409      */
410     public void appendAnnotation(String annotation) {
411         if (annotations == null) {
412             annotations = new JAnnotations();
413         }
414         annotations.appendAnnotation(annotation);
415     }
416 
417     /**
418      * @param annotations the annotations to set
419      */
420     public void setAnnotations(JAnnotations annotations) {
421         this.annotations = annotations;
422     }
423 } // -- JMember