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