KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > src > ConstructorElement


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.src;
21
22 import java.io.*;
23 import java.lang.reflect.Modifier JavaDoc;
24 import java.util.Arrays JavaDoc;
25
26 /** Describes the constructor of a class.
27 *
28 * @author Petr Hamernik
29 */

30 public class ConstructorElement extends MemberElement {
31     /** Format for the header - used in code generator */
32     private static final ElementFormat HEADER_FORMAT =
33         new ElementFormat("{m,,\" \"}{n}({a}){e,\" throws \",}"); // NOI18N
34

35     static final long serialVersionUID =4794977239847390179L;
36     /** Create a constructor with an in-memory implementation. */
37     public ConstructorElement() {
38         this(new Memory(), null);
39     }
40
41     /** Create a constructor.
42     * @param impl implementation of functionality
43     * @param clazz declaring class, or <code>null</code>
44     */

45     public ConstructorElement(ConstructorElement.Impl impl, ClassElement clazz) {
46         super(impl, clazz);
47     }
48
49     /** Clone the constructor.
50     * @return a new constructor that has same values as the original,
51     * but is represented in memory
52     */

53     public Object JavaDoc clone () {
54         return new ConstructorElement (new Memory (this), null);
55     }
56
57     final ConstructorElement.Impl getConstructorImpl() {
58         return (ConstructorElement.Impl)impl;
59     }
60
61     /* Get the modifiers for this constructor.
62      * @return the mask of modifers
63      * @see Modifier
64      */

65     public int getModifiersMask() {
66         return Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
67     }
68
69     /** Get the method parameters.
70     * @return the parameters
71     */

72     public final MethodParameter[] getParameters() {
73         return getConstructorImpl().getParameters();
74     }
75
76     /** Set the method parameters.
77     * @param params the new parameters
78     * @throws SourceException if impossible
79     */

80     public final void setParameters(MethodParameter[] params) throws SourceException {
81         getConstructorImpl().setParameters(params);
82     }
83
84     /** Get the thrown exceptions.
85      * @return the exceptions, by name
86     */

87     public final Identifier[] getExceptions() {
88         return getConstructorImpl().getExceptions();
89     }
90
91     /** Set the array of thrown exceptions.
92     * @param exceptions the new exceptions to throw, by name
93     * @throws SourceException if impossible
94     */

95     public final void setExceptions(Identifier[] exceptions) throws SourceException {
96         getConstructorImpl().setExceptions(exceptions);
97     }
98
99     /** Set the body of the constructor.
100     * @param s the new body (may be <code>null</code> for methods)
101     * @throws SourceException if impossible
102     * @see #getBody
103     */

104     public final void setBody (String JavaDoc s) throws SourceException {
105         getConstructorImpl ().setBody (s);
106     }
107
108     /** Get the body of the constructor.
109     * If <code>this</code> is actually a {@link MethodElement}, the body
110     * may be <code>null</code> when the method is abstract.
111     * A body consisting of an empty string, however, is just a concrete
112     * but empty body.
113     * @return the body (maye be <code>null</code> for methods)
114     */

115     public final String JavaDoc getBody () {
116         return getConstructorImpl ().getBody ();
117     }
118
119     /** Get this constructor's documentation.
120     * @return the JavaDoc
121     */

122     public final JavaDoc.Method getJavaDoc () {
123         return getConstructorImpl ().getJavaDoc ();
124     }
125
126     /* Print this element into the element printer.
127     * @param printer The element printer where to print to
128     * @exception ElementPrinterInterruptException if printer cancel the printing
129     */

130     public void print(ElementPrinter printer) throws ElementPrinterInterruptException {
131         printerMark(printer, printer.ELEMENT_BEGIN);
132
133         JavaDoc doc = getJavaDoc();
134         if ((doc != null) && !doc.isEmpty()) {
135             printerMark(printer, printer.JAVADOC_BEGIN); // JAVADOC begin
136
printJavaDoc(doc, printer);
137             printerMark(printer, printer.JAVADOC_END); // JAVADOC end
138
printer.println(""); // NOI18N
139
}
140
141         printerMark(printer, printer.HEADER_BEGIN); // HEADER begin
142
printer.print(getFormat().format(this));
143         printerMark(printer, printer.HEADER_END); // HEADER end
144

145         String JavaDoc body = getBody();
146         ClassElement declClass = getDeclaringClass();
147
148         if (((declClass != null) && declClass.isInterface()) || // in interface
149
(Modifier.isAbstract(getModifiers())) || // or abstract
150
(body == null)) { // body is null
151
printer.print(";"); // NOI18N
152
}
153         else {
154             printer.print(" {"); // NOI18N
155
printerMark(printer, printer.BODY_BEGIN); // BODY begin
156
printer.print(body);
157             printerMark(printer, printer.BODY_END); // BODY end
158
printer.print("}"); // NOI18N
159
}
160
161         printerMark(printer, printer.ELEMENT_END);
162     }
163
164     /** Marks the notable point in the writer.
165     * This method calls markConstructor and must be overriden
166     * in MethodElement to call markMethod.
167     * @exception ElementPrinterInterruptException if printer cancel the printing
168     */

169     void printerMark(ElementPrinter printer, int what) throws ElementPrinterInterruptException {
170         printer.markConstructor(this, what);
171     }
172
173     /** Get the printing format.
174     * May be overridden in subclasses.
175     */

176     ElementFormat getFormat() {
177         return HEADER_FORMAT;
178     }
179
180     /** Implementation of constructors.
181     * @see ConstructorElement
182     */

183     public interface Impl extends MemberElement.Impl {
184         /** @deprecated Only public by accident. */
185         /* public static final */ long serialVersionUID = -8757076629808175158L;
186         /** Get the method parameters.
187         * @return the parameters
188         */

189         public MethodParameter[] getParameters();
190
191         /** Set the method parameters.
192         * @param params the new parameters
193         * @throws SourceException if impossible
194         */

195         public void setParameters(MethodParameter[] params) throws SourceException;
196
197         /** Get the thrown exceptions.
198         * @return the exceptions, by name
199         */

200         public Identifier[] getExceptions();
201
202         /** Set the thrown exceptions.
203         * @param exceptions the new exceptions to be thrown, by name
204         * @throws SourceException if impossible
205         */

206         public void setExceptions(Identifier[] exceptions) throws SourceException;
207
208         /** Set the body.
209         * @param s the new body (may be <code>null</code> for methods)
210         * @throws SourceException if impossible
211         * @see ConstructorElement#getBody
212         */

213         public void setBody (String JavaDoc s) throws SourceException;
214
215         /** Get the body.
216         * @return the body (may be <code>null</code> for methods)
217         * @see ConstructorElement#getBody
218         */

219         public String JavaDoc getBody ();
220
221         /** Get the JavaDoc.
222         * @return the JavaDoc
223         */

224         public JavaDoc.Method getJavaDoc ();
225     }
226
227     /** Serves as a key for constructor elements.
228     * Enables them to be used in hashtables, etc.
229     * @see ConstructorElement
230     */

231     public static class Key extends Object JavaDoc {
232         /** Parameter types */
233         private Type[] params;
234
235         /** Construct a key by parameter types.
236         * @param params the parameter types
237         */

238         public Key (final Type[] params) {
239             this.params = params;
240         }
241
242         /** Construct a key for a constructor.
243         * Does not keep a reference.
244         * @param ce the constructor
245         */

246         public Key (final ConstructorElement ce) {
247             MethodParameter[] mp = ce.getParameters();
248             params = new Type[mp.length];
249             for (int i = 0; i < mp.length; i++) {
250                 params[i] = mp[i].getType();
251             }
252         }
253
254         /* Returns true if parameters are the same */
255         public boolean equals (Object JavaDoc obj) {
256             if (!(obj instanceof Key)) return false;
257             return Arrays.equals(params, ((Key)obj).params);
258         }
259
260         /* Computes hashcode as exclusive or of first and
261         * last parameter's names
262         * (or only from the first or return some constant
263         * for special cases) */

264         public int hashCode () {
265             if (params == null) return 0;
266             int length = params.length;
267             if (length == 0) return 0;
268             if (length == 1) return params[0].getFullString().hashCode();
269             return params[0].getFullString().hashCode() ^
270                    params[length - 1].getFullString().hashCode();
271         }
272
273     } // end of Key inner class
274

275     static class Memory extends MemberElement.Memory implements Impl {
276         /** arguments of the constructor or method */
277         private MethodParameter[] parameters;
278
279         /** exceptions throwed by the constructor or method */
280         private Identifier[] exceptions;
281
282         /** body */
283         private String JavaDoc body;
284
285         /** Java Doc */
286         private JavaDoc.Method javaDoc;
287
288         static final long serialVersionUID =-4826478874004410760L;
289         Memory() {
290             exceptions = new Identifier[0];
291             parameters = new MethodParameter[0];
292             body = ""; // NOI18N
293
javaDoc = JavaDocSupport.createMethodJavaDoc( null );
294         }
295
296         /** Copy constructor */
297         Memory (ConstructorElement el) {
298             super (el);
299             exceptions = el.getExceptions ();
300             parameters = el.getParameters ();
301             body = el.getBody ();
302             javaDoc = el.getJavaDoc().isEmpty() ?
303                       JavaDocSupport.createMethodJavaDoc( null ) :
304                       JavaDocSupport.createMethodJavaDoc( el.getJavaDoc().getRawText() );
305         }
306
307         /** @return the parameters
308         */

309         public MethodParameter[] getParameters() {
310             return parameters;
311         }
312
313         /** sets the method parameters
314         */

315         public void setParameters(MethodParameter[] params) {
316             MethodParameter[] m = parameters;
317             parameters = params;
318             firePropertyChange (PROP_PARAMETERS, m, params);
319         }
320
321         /** @return the array of the exceptions throwed by the method.
322         */

323         public Identifier[] getExceptions() {
324             return exceptions;
325         }
326
327         /** Sets the array of the exceptions throwed by the method.
328         */

329         public void setExceptions(Identifier[] exceptions) {
330             Identifier[] old = this.exceptions;
331             this.exceptions = exceptions;
332             firePropertyChange (PROP_EXCEPTIONS, old, exceptions);
333         }
334
335         /** Sets body of the element.
336         * @param s the body
337         */

338         public void setBody (String JavaDoc s) throws SourceException {
339             String JavaDoc old = body;
340             body = s;
341             firePropertyChange (PROP_BODY, old, body);
342         }
343
344         /** Getter for the body of element.
345         * @return the string representing the body
346         */

347         public String JavaDoc getBody () {
348             return body;
349         }
350
351         /** Provides access to constructor java doc.
352         * @return constructor java doc
353         */

354         public JavaDoc.Method getJavaDoc () {
355             return javaDoc;
356         }
357
358         public Object JavaDoc readResolve() {
359             return new ConstructorElement(this, null);
360         }
361     }
362 }
363
Popular Tags