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      * @param declaringClass the declaring class for this constructor
102      **/
103     public JConstructor( JClass declaringClass )
104     {
105         this.declaringClass = declaringClass;
106         this.modifiers = new JModifiers();
107         this.params = new JNamedMap();
108         this.sourceCode = new JSourceCode();
109     }
110 
111     /**
112      * Adds the given parameter to this Methods list of parameters
113      * @param parameter the parameter to add to the this Methods
114      * list of parameters.
115      * @exception java.lang.IllegalArgumentException when a parameter already
116      * exists for this Method with the same name as the new parameter
117      **/
118     public void addParameter( JParameter parameter )
119         throws IllegalArgumentException
120     {
121         if ( parameter == null ) return;
122         //-- check current params
123         if ( params.get( parameter.getName() ) != null )
124         {
125             StringBuffer err = new StringBuffer();
126             err.append( "A parameter already exists for the constructor, " );
127             err.append( this.declaringClass.getName() );
128             err.append( ", with the name: " );
129             err.append( parameter.getName() );
130             throw new IllegalArgumentException( err.toString() );
131         }
132 
133         params.put( parameter.getName(), parameter );
134     } //-- addParameter
135 
136     /**
137      * Returns the class in which this JMember has been declared
138      * @return the class in which this JMember has been declared
139      **/
140     public JClass getDeclaringClass()
141     {
142         return this.declaringClass;
143     } //-- getDeclaringClass
144 
145     /**
146      * Returns the modifiers for this JConstructor
147      * @return the modifiers for this JConstructor
148      **/
149     public JModifiers getModifiers()
150     {
151         return this.modifiers;
152     } //-- getModifiers
153 
154 
155     /**
156      * Returns an array of JParameters consisting of the parameters
157      * of this Method in declared order
158      * @return a JParameter array consisting of the parameters
159      * of this Method in declared order
160      **/
161     public JParameter[] getParameters()
162     {
163         JParameter[] jpArray = new JParameter[params.size()];
164         for ( int i = 0; i < jpArray.length; i++ )
165         {
166             jpArray[i] = (JParameter) params.get( i );
167         }
168         return jpArray;
169 
170     } //-- getParameters
171 
172     public JSourceCode getSourceCode()
173     {
174         return this.sourceCode;
175     } //-- getSourceCode
176 
177     public void print( JSourceWriter jsw )
178     {
179         JAnnotations annotations = getAnnotations();
180         if ( annotations != null ) annotations.print( jsw );
181 
182         if ( modifiers.isPrivate() )
183             jsw.write( "private" );
184         else if ( modifiers.isProtected() )
185             jsw.write( "protected" );
186         else
187             jsw.write( "public" );
188         jsw.write( ' ' );
189         jsw.write( declaringClass.getLocalName() );
190         jsw.write( '(' );
191 
192         //-- print parameters
193         for ( int i = 0; i < params.size(); i++ )
194         {
195             if ( i > 0 ) jsw.write( ", " );
196             jsw.write( params.get( i ) );
197         }
198         jsw.writeln( ')' );
199         jsw.writeln( '{' );
200         //jsw.indent();
201         sourceCode.print( jsw );
202         //jsw.unindent();
203         if ( !jsw.isNewline() ) jsw.writeln();
204         jsw.write( "} //-- " );
205         jsw.writeln( toString() );
206     } //-- print
207 
208     public void setModifiers( JModifiers modifiers )
209     {
210         this.modifiers = modifiers.copy();
211         this.modifiers.setFinal( false );
212     } //-- setModifiers
213 
214     public void setSourceCode( String sourceCode )
215     {
216         this.sourceCode = new JSourceCode( sourceCode );
217     } //-- setSourceCode
218 
219     public void setSourceCode( JSourceCode sourceCode )
220     {
221         this.sourceCode = sourceCode;
222     } //-- setSourceCode
223 
224     public String toString()
225     {
226         StringBuffer sb = new StringBuffer();
227         sb.append( declaringClass.getName() );
228         sb.append( '(' );
229 
230         //-- print parameters
231         for ( int i = 0; i < params.size(); i++ )
232         {
233             JParameter jp = (JParameter) params.get( i );
234             if ( i > 0 ) sb.append( ", " );
235             sb.append( jp.getType().getName() );
236         }
237         sb.append( ')' );
238         return sb.toString();
239     } //-- toString
240 
241     /**
242      * @return the annotations
243      */
244     public JAnnotations getAnnotations()
245     {
246         return annotations;
247     }
248 
249     /**
250      * @param annotation the annotation to append
251      */
252     public void appendAnnotation( String annotation )
253     {
254         if ( annotations == null )
255         {
256             annotations = new JAnnotations();
257         }
258         annotations.appendAnnotation( annotation );
259     }
260 
261     /**
262      * @param annotations the annotations to set
263      */
264     public void setAnnotations( JAnnotations annotations )
265     {
266         this.annotations = annotations;
267     }
268 
269 } //-- JConstructor