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 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * $Id$
44   */
45  
46  package org.codehaus.modello.plugin.java.javasource;
47  
48  /*
49   * Copyright (c) 2004, Codehaus.org
50   *
51   * Permission is hereby granted, free of charge, to any person obtaining a copy of
52   * this software and associated documentation files (the "Software"), to deal in
53   * the Software without restriction, including without limitation the rights to
54   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
55   * of the Software, and to permit persons to whom the Software is furnished to do
56   * so, subject to the following conditions:
57   *
58   * The above copyright notice and this permission notice shall be included in all
59   * copies or substantial portions of the Software.
60   *
61   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
62   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
63   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
64   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
65   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
66   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
67   * SOFTWARE.
68   */
69  
70  /**
71   * A class for handling source code for a constructor of a JClass
72   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
73   * @version $Revision$ $Date$
74   **/
75  public class JConstructor
76  {
77  
78      /**
79       * The set of modifiers for this JMethod
80       **/
81      private JModifiers modifiers = null;
82  
83      /**
84       * List of parameters for this Constructor
85       **/
86      private JNamedMap params = null;
87  
88      /**
89       * The Class in this JMember has been declared
90       **/
91      private JClass declaringClass = null;
92  
93      private JSourceCode sourceCode = null;
94  
95      private JAnnotations annotations = null;
96  
97      /**
98       * Creates a new method with the given name and returnType.
99       * For "void" return types, simply pass in null as the returnType
100      **/
101     public JConstructor( JClass declaringClass )
102     {
103         this.declaringClass = declaringClass;
104         this.modifiers = new JModifiers();
105         this.params = new JNamedMap();
106         this.sourceCode = new JSourceCode();
107     }
108 
109     /**
110      * Adds the given parameter to this Methods list of parameters
111      * @param parameter the parameter to add to the this Methods
112      * list of parameters.
113      * @exception java.lang.IllegalArgumentException when a parameter already
114      * exists for this Method with the same name as the new parameter
115      **/
116     public void addParameter( JParameter parameter )
117         throws IllegalArgumentException
118     {
119         if ( parameter == null ) return;
120         //-- check current params
121         if ( params.get( parameter.getName() ) != null )
122         {
123             StringBuffer err = new StringBuffer();
124             err.append( "A parameter already exists for the constructor, " );
125             err.append( this.declaringClass.getName() );
126             err.append( ", with the name: " );
127             err.append( parameter.getName() );
128             throw new IllegalArgumentException( err.toString() );
129         }
130 
131         params.put( parameter.getName(), parameter );
132     } //-- addParameter
133 
134     /**
135      * Returns the class in which this JMember has been declared
136      * @return the class in which this JMember has been declared
137      **/
138     public JClass getDeclaringClass()
139     {
140         return this.declaringClass;
141     } //-- getDeclaringClass
142 
143     /**
144      * Returns the modifiers for this JConstructor
145      * @return the modifiers for this JConstructor
146      **/
147     public JModifiers getModifiers()
148     {
149         return this.modifiers;
150     } //-- getModifiers
151 
152 
153     /**
154      * Returns an array of JParameters consisting of the parameters
155      * of this Method in declared order
156      * @return a JParameter array consisting of the parameters
157      * of this Method in declared order
158      **/
159     public JParameter[] getParameters()
160     {
161         JParameter[] jpArray = new JParameter[params.size()];
162         for ( int i = 0; i < jpArray.length; i++ )
163         {
164             jpArray[i] = (JParameter) params.get( i );
165         }
166         return jpArray;
167 
168     } //-- getParameters
169 
170     public JSourceCode getSourceCode()
171     {
172         return this.sourceCode;
173     } //-- getSourceCode
174 
175     public void print( JSourceWriter jsw )
176     {
177         JAnnotations annotations = getAnnotations();
178         if ( annotations != null ) annotations.print( jsw );
179 
180         if ( modifiers.isPrivate() )
181             jsw.write( "private" );
182         else if ( modifiers.isProtected() )
183             jsw.write( "protected" );
184         else
185             jsw.write( "public" );
186         jsw.write( ' ' );
187         jsw.write( declaringClass.getLocalName() );
188         jsw.write( '(' );
189 
190         //-- print parameters
191         for ( int i = 0; i < params.size(); i++ )
192         {
193             if ( i > 0 ) jsw.write( ", " );
194             jsw.write( params.get( i ) );
195         }
196         jsw.writeln( ')' );
197         jsw.writeln( '{' );
198         //jsw.indent();
199         sourceCode.print( jsw );
200         //jsw.unindent();
201         if ( !jsw.isNewline() ) jsw.writeln();
202         jsw.write( "} //-- " );
203         jsw.writeln( toString() );
204     } //-- print
205 
206     public void setModifiers( JModifiers modifiers )
207     {
208         this.modifiers = modifiers.copy();
209         this.modifiers.setFinal( false );
210     } //-- setModifiers
211 
212     public void setSourceCode( String sourceCode )
213     {
214         this.sourceCode = new JSourceCode( sourceCode );
215     } //-- setSourceCode
216 
217     public void setSourceCode( JSourceCode sourceCode )
218     {
219         this.sourceCode = sourceCode;
220     } //-- setSourceCode
221 
222     public String toString()
223     {
224         StringBuffer sb = new StringBuffer();
225         sb.append( declaringClass.getName() );
226         sb.append( '(' );
227 
228         //-- print parameters
229         for ( int i = 0; i < params.size(); i++ )
230         {
231             JParameter jp = (JParameter) params.get( i );
232             if ( i > 0 ) sb.append( ", " );
233             sb.append( jp.getType().getName() );
234         }
235         sb.append( ')' );
236         return sb.toString();
237     } //-- toString
238 
239     /**
240      * @return the annotations
241      */
242     public JAnnotations getAnnotations()
243     {
244         return annotations;
245     }
246 
247     /**
248      * @param annotation the annotation to append
249      */
250     public void appendAnnotation( String annotation )
251     {
252         if ( annotations == null )
253         {
254             annotations = new JAnnotations();
255         }
256         annotations.appendAnnotation( annotation );
257     }
258 
259     /**
260      * @param annotations the annotations to set
261      */
262     public void setAnnotations( JAnnotations annotations )
263     {
264         this.annotations = annotations;
265     }
266 
267 } //-- JConstructor