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 (C) Intalio, Inc. All Rights Reserved.
042: */package org.exolab.javasource;
043:
044: import java.util.Vector;
045:
046: /**
047: * A class for handling source code for a constructor of a JClass.
048: *
049: * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
050: * @version $Revision: 6676 $ $Date: 2005-05-08 05:24:54 -0600 (Sun, 08 May 2005) $
051: */
052: public final class JConstructor extends JAnnotatedElementHelper {
053: //--------------------------------------------------------------------------
054:
055: /** The set of modifiers for this JConstructor. */
056: private JModifiers _modifiers;
057:
058: /** List of parameters for this JConstructor. */
059: private JNamedMap _params;
060:
061: /** The Class in this JConstructor has been declared. */
062: private AbstractJClass _declaringClass;
063:
064: /** The source code for this constructor. */
065: private JSourceCode _sourceCode;
066:
067: /** The exceptions that this JConstructor throws. */
068: private Vector _exceptions;
069:
070: //--------------------------------------------------------------------------
071:
072: /**
073: * Creates a new JConstructor for the provided declaring class.
074: *
075: * @param declaringClass The class this constructor creates.
076: */
077: protected JConstructor(final AbstractJClass declaringClass) {
078: _declaringClass = declaringClass;
079: _modifiers = new JModifiers();
080: _params = new JNamedMap();
081: _sourceCode = new JSourceCode();
082: _exceptions = new Vector(1);
083: }
084:
085: //--------------------------------------------------------------------------
086:
087: /**
088: * Returns the exceptions that this JConstructor lists in its throws clause.
089: *
090: * @return The exceptions that this JConstructor lists in its throws clause.
091: */
092: public JClass[] getExceptions() {
093: JClass[] jclasses = new JClass[_exceptions.size()];
094: _exceptions.copyInto(jclasses);
095: return jclasses;
096: }
097:
098: /**
099: * Adds the given Exception to this JConstructor's throws clause.
100: *
101: * @param exp The JClass representing the Exception.
102: */
103: public void addException(final JClass exp) {
104: if (exp == null) {
105: return;
106: }
107:
108: //-- make sure exception is not already added
109: String expClassName = exp.getName();
110: for (int i = 0; i < _exceptions.size(); i++) {
111: JClass jClass = (JClass) _exceptions.elementAt(i);
112: if (expClassName.equals(jClass.getName())) {
113: return;
114: }
115: }
116: //-- add exception
117: _exceptions.addElement(exp);
118: }
119:
120: /**
121: * Returns an array of JParameters consisting of the parameters of this
122: * JConstructor in declared order.
123: *
124: * @return A JParameter array consisting of the parameters of this
125: * JConstructor in declared order.
126: */
127: public JParameter[] getParameters() {
128: JParameter[] jpArray = new JParameter[_params.size()];
129: for (int i = 0; i < jpArray.length; i++) {
130: jpArray[i] = (JParameter) _params.get(i);
131: }
132: return jpArray;
133: }
134:
135: /**
136: * Adds the given parameter to this JConstructor's list of parameters.
137: *
138: * @param parameter The parameter to add to the this JConstructor's list of
139: * parameters.
140: */
141: public void addParameter(final JParameter parameter) {
142: if (parameter == null) {
143: return;
144: }
145:
146: //-- check current params
147: if (_params.get(parameter.getName()) != null) {
148: StringBuffer err = new StringBuffer();
149: err
150: .append("A parameter already exists for the constructor, ");
151: err.append(this ._declaringClass.getName());
152: err.append(", with the name: ");
153: err.append(parameter.getName());
154: throw new IllegalArgumentException(err.toString());
155: }
156:
157: _params.put(parameter.getName(), parameter);
158:
159: //-- be considerate and add the class name to the
160: //-- declaring class's list of imports
161: if (_declaringClass != null) {
162: JType jType = parameter.getType();
163: if (!jType.isPrimitive()) {
164: _declaringClass.addImport(jType.getName());
165: }
166: }
167: }
168:
169: /**
170: * Returns the class in which this JConstructor has been declared.
171: *
172: * @return The class in which this JConstructor has been declared.
173: */
174: public AbstractJClass getDeclaringClass() {
175: return _declaringClass;
176: }
177:
178: /**
179: * Returns the modifiers for this JConstructor.
180: *
181: * @return The modifiers for this JConstructor.
182: */
183: public JModifiers getModifiers() {
184: return _modifiers;
185: }
186:
187: /**
188: * Sets the modifiers on this JConstructor.
189: *
190: * @param modifiers Modifiers to set on this constructor.
191: */
192: public void setModifiers(final JModifiers modifiers) {
193: _modifiers = modifiers.copy();
194: _modifiers.setFinal(false);
195: }
196:
197: /**
198: * Returns the source code for this JConstructor.
199: *
200: * @return The source code.
201: */
202: public JSourceCode getSourceCode() {
203: return _sourceCode;
204: }
205:
206: /**
207: * Sets the source code for this constructor.
208: *
209: * @param sourceCode Source code to apply to this constructor.
210: */
211: public void setSourceCode(final String sourceCode) {
212: _sourceCode = new JSourceCode(sourceCode);
213: }
214:
215: /**
216: * Sets the source code for this constructor.
217: *
218: * @param sourceCode Source code to apply to this constructor.
219: */
220: public void setSourceCode(final JSourceCode sourceCode) {
221: _sourceCode = sourceCode;
222: }
223:
224: //--------------------------------------------------------------------------
225:
226: /**
227: * Prints this JConstructor to the provided JSourceWriter.
228: *
229: * @param jsw The JSourceWriter to print the constructor to.
230: */
231: public void print(final JSourceWriter jsw) {
232: // -- print annotations
233: printAnnotations(jsw);
234:
235: if (_modifiers.isPrivate()) {
236: jsw.write("private");
237: } else if (_modifiers.isProtected()) {
238: jsw.write("protected");
239: } else {
240: jsw.write("public");
241: }
242: jsw.write(' ');
243: jsw.write(_declaringClass.getLocalName());
244: jsw.write('(');
245:
246: //-- any parameter annotations?
247: boolean parameterAnnotations = false;
248: for (int i = 0; i < _params.size(); i++) {
249: JParameter jParameter = (JParameter) _params.get(i);
250: if (jParameter.hasAnnotations()) {
251: parameterAnnotations = true;
252: break;
253: }
254: }
255:
256: //-- print parameters
257: if (parameterAnnotations) {
258: jsw.indent();
259: }
260: for (int i = 0; i < _params.size(); i++) {
261: if (i > 0) {
262: jsw.write(", ");
263: }
264: if (parameterAnnotations) {
265: jsw.writeln();
266: }
267: JParameter jParameter = (JParameter) _params.get(i);
268: jParameter.printAnnotations(jsw);
269: String typeAndName = jParameter.toString();
270: jsw.write(typeAndName);
271: }
272: if (parameterAnnotations) {
273: jsw.unindent();
274: }
275:
276: jsw.write(")");
277: if (_exceptions.size() > 0) {
278: jsw.writeln();
279: jsw.write("throws ");
280: for (int i = 0; i < _exceptions.size(); i++) {
281: if (i > 0) {
282: jsw.write(", ");
283: }
284: JClass jClass = (JClass) _exceptions.elementAt(i);
285: jsw.write(jClass.getName());
286: }
287: }
288: jsw.writeln(" {");
289:
290: _sourceCode.print(jsw);
291:
292: if (!jsw.isNewline()) {
293: jsw.writeln();
294: }
295: jsw.writeln("}");
296: }
297:
298: /**
299: * {@inheritDoc}
300: */
301: public String toString() {
302: StringBuffer sb = new StringBuffer();
303: sb.append(_declaringClass.getName());
304: sb.append('(');
305:
306: //-- print parameters
307: for (int i = 0; i < _params.size(); i++) {
308: JParameter jp = (JParameter) _params.get(i);
309: if (i > 0) {
310: sb.append(", ");
311: }
312: sb.append(jp.getType().getName());
313: }
314: sb.append(')');
315: return sb.toString();
316: }
317:
318: //--------------------------------------------------------------------------
319: }
|