KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > codegen > JavaMethod


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: JavaMethod.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.codegen;
25
26
27 //FIXME: string arrays could be string buffers, since printer handles newlines.
28

29 /**
30  * Object that is used to assemble the Java source for a method.
31  */

32 public final class JavaMethod {
33     /** The method name, or null for class constructor */
34     private String JavaDoc fName;
35
36     /** The return type */
37     private String JavaDoc fReturnType;
38
39     /** Modifiers of the method */
40     protected int fModifiers;
41
42     /** Parameters */
43     private JavaParameter[] fParameters;
44
45     /** Documentation */
46     private String JavaDoc[] fDoc;
47
48     /** Code body */
49     private JavaCode fCode = new JavaCode();
50
51     /**
52      * Adjust modifiers based on other modifiers.
53      */

54     private static int adjustModifiers(String JavaDoc returnType,
55                                        int modifiers) {
56         // Only public methods are include in interfaces.
57
if ((modifiers & (JavaModifiers.PROTECTED|JavaModifiers.PRIVATE)) != 0) {
58             modifiers |= JavaModifiers.OMIT_INTERFACE;
59         }
60         // Only static methods are omitted from interfaces.
61
if ((modifiers & JavaModifiers.STATIC) != 0) {
62             modifiers |= JavaModifiers.OMIT_INTERFACE;
63         }
64         // Constructors and static initializers are omitted from the interface
65
if (returnType == null) {
66             modifiers |= JavaModifiers.OMIT_INTERFACE;
67         }
68         return modifiers;
69     }
70
71     /**
72      * Constructor.
73      *
74      * @param returnType The type the method returns. Specify null for a
75      * constructor.
76      * @param name The name of the method. If name is null, then this
77      * creates a static initializer.
78      * @param parameters A array describing the parameters; null or empty if
79      * no parameters.
80      * @param modifiers The method modifier bit set.
81      * @param doc The documentation to include. The parameter documentation
82      * will be appended to this, so should not be repeated.
83      * Each row of the array is a line of documentation.
84      */

85     //FIXME: reorder arguments to be like java method def order.
86
public JavaMethod(String JavaDoc name,
87                       String JavaDoc returnType,
88                       int modifiers,
89                       JavaParameter[] parameters,
90                       String JavaDoc[] doc) {
91         fName = name;
92         fReturnType = returnType;
93         fModifiers = adjustModifiers(returnType, modifiers);
94         if (parameters != null) {
95             fParameters = (JavaParameter[])parameters.clone();
96         }
97         fDoc = (String JavaDoc[])doc.clone();
98     }
99
100     /**
101      * Get the code object associate with the method body.
102      */

103     public JavaCode getCode() {
104         return fCode;
105     }
106
107     /**
108      * Get the name.
109      */

110     public String JavaDoc getName() {
111         return fName;
112     }
113
114     /**
115      * Get the return type.
116      */

117     public String JavaDoc getReturnType() {
118         return fReturnType;
119     }
120
121     /**
122      * Get the modifiers.
123      */

124     public int getModifiers() {
125         return fModifiers;
126     }
127
128     /**
129      * Print the method signature. This is public, since it used in
130      * generating method listings as well as code.
131      *
132      * @param out Write to this stream.
133      * @param beginBody If true, start a method body, otherwise end with
134      * a semicolon.
135      */

136     public void printMethodSignature(IndentWriter out,
137                                      boolean beginBody) {
138         int numParams = (fParameters == null) ? 0 : fParameters.length;
139
140         // Assemble upto `(' so length can be determined for indentation
141
StringBuffer JavaDoc start = new StringBuffer JavaDoc(JavaModifiers.toDecl(fModifiers));
142         if (fReturnType != null) {
143             start.append(fReturnType);
144             start.append(' ');
145         }
146         start.append(fName);
147         start.append('(');
148         out.print(start);
149         
150         // Construct indentation string for subsequent parameters if needed.
151
String JavaDoc indent = (numParams == 0) ? "" : JavaLang.makeIndent(start.length());
152
153         // Output parameters
154
for (int idx = 0; idx < numParams; idx++) {
155             if (idx > 0) {
156                 out.print(indent);
157             }
158             fParameters[idx].printDefinition(out);
159             if (idx < (numParams - 1)) {
160                 out.println(',');
161             }
162         }
163         if (beginBody) {
164             out.println(") {");
165         } else {
166             out.println(");");
167         }
168     }
169
170     /**
171      * Print the method.
172      */

173     public void print(IndentWriter out,
174                       boolean printBody) {
175         if (fDoc != null) {
176             out.println("/**");
177             for (int idx = 0; idx < fDoc.length; idx++) {
178                 out.print(" * ");
179                 out.println(fDoc[idx]);
180             }
181             out.println(" */");
182         }
183         if (fName != null) {
184             printMethodSignature(out, printBody);
185         } else {
186             out.println("static {");
187         }
188         if (printBody) {
189             out.enter();
190             fCode.print(out);
191             out.leave();
192             out.println("}");
193         }
194         out.println();
195     }
196 }
197
Popular Tags