KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > java > gen > BaseMethod


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.java.gen;
31
32 import com.caucho.bytecode.JClass;
33 import com.caucho.bytecode.JMethod;
34 import com.caucho.java.JavaWriter;
35 import com.caucho.util.L10N;
36
37 import java.io.IOException JavaDoc;
38
39 /**
40  * Basic method generation.
41  */

42 public class BaseMethod extends ClassComponent {
43   private static final L10N L = new L10N(BaseMethod.class);
44
45   private JMethod _method;
46   private String JavaDoc _methodName;
47
48   private boolean _isStatic;
49   private String JavaDoc _visibility = "public";
50
51   private JClass _returnType;
52   private JClass []_parameterTypes;
53   
54   private JClass []_exceptionTypes;
55
56   private CallChain _call;
57
58   /**
59    * Creates the base method
60    */

61   public BaseMethod(String JavaDoc methodName, CallChain call)
62   {
63     _methodName = methodName;
64
65     setCall(call);
66   }
67
68   /**
69    * Creates the base method
70    */

71   public BaseMethod(JMethod method, CallChain call)
72   {
73     _method = method;
74     _exceptionTypes = method.getExceptionTypes();
75
76     setCall(call);
77   }
78
79   /**
80    * Creates the base method
81    */

82   public BaseMethod(JMethod method)
83   {
84     _method = method;
85     _exceptionTypes = method.getExceptionTypes();
86   }
87
88   /**
89    * Creates the base method
90    */

91   public BaseMethod(JMethod apiMethod, JMethod implMethod)
92   {
93     this(apiMethod, new MethodCallChain(implMethod));
94   }
95
96   /**
97    * Returns the call.
98    */

99   public CallChain getCall()
100   {
101     return _call;
102   }
103
104   /**
105    * Sets the call.
106    */

107   public void setCall(CallChain call)
108   {
109     if (call == null)
110       throw new NullPointerException JavaDoc();
111     
112     _call = call;
113   }
114
115   /**
116    * Returns the method.
117    */

118   public JMethod getMethod()
119   {
120     return _method;
121   }
122   
123   /**
124    * Returns the method name.
125    */

126   public String JavaDoc getMethodName()
127   {
128     if (_methodName != null)
129       return _methodName;
130     else
131       return _method.getName();
132   }
133
134   /**
135    * Returns the parameter types.
136    */

137   public JClass []getParameterTypes()
138   {
139     if (_parameterTypes != null)
140       return _parameterTypes;
141     else if (_method != null)
142       return _method.getParameterTypes();
143     else
144       return _call.getParameterTypes();
145   }
146
147   /**
148    * Gets the return type.
149    */

150   public JClass getReturnType()
151   {
152     if (_method != null)
153       return _method.getReturnType();
154     else
155       return _call.getReturnType();
156   }
157
158   /**
159    * Returns the exception types.
160    */

161   public JClass []getExceptionTypes()
162   {
163     return _exceptionTypes;
164   }
165   
166   /**
167    * Generates the code for the class.
168    *
169    * @param out the writer to the output stream.
170    */

171   public void generate(JavaWriter out)
172     throws IOException JavaDoc
173   {
174     String JavaDoc []args = generateMethodHeader(out);
175
176     JClass []exceptionTypes = getExceptionTypes();
177     if (exceptionTypes != null && exceptionTypes.length > 0) {
178       out.print(" throws ");
179
180       for (int i = 0; i < exceptionTypes.length; i++) {
181     if (i != 0)
182       out.print(", ");
183
184     out.print(exceptionTypes[i].getPrintName());
185       }
186       out.println();
187     }
188
189     // XXX: exception
190
out.println("{");
191     out.pushDepth();
192
193     generateCall(out, args);
194     
195     out.popDepth();
196     out.println("}");
197   }
198   
199   /**
200    * Generates the method header
201    *
202    * @param out the writer to the output stream.
203    *
204    * @return the method arguments
205    */

206   public String JavaDoc []generateMethodHeader(JavaWriter out)
207     throws IOException JavaDoc
208   {
209     if (_visibility != null && ! _visibility.equals(""))
210       out.print(_visibility + " ");
211
212     if (_isStatic)
213       out.print("static ");
214
215     out.print(getReturnType().getPrintName());
216     out.print(" " + getMethodName() + "(");
217
218     JClass []parameterTypes = getParameterTypes();
219     String JavaDoc []args = new String JavaDoc[parameterTypes.length];
220     
221     for (int i = 0; i < args.length; i++) {
222       if (i != 0)
223     out.print(", ");
224       
225       out.print(parameterTypes[i].getPrintName());
226       
227       args[i] = "a" + i;
228       out.print(" " + args[i]);
229
230     }
231     out.println(")");
232
233     return args;
234   }
235   
236   /**
237    * Generates the code for the call.
238    *
239    * @param out the writer to the output stream.
240    * @param args the arguments
241    */

242   protected void generateCall(JavaWriter out, String JavaDoc []args)
243     throws IOException JavaDoc
244   {
245     _call.generateCall(out, null, null, args);
246   }
247 }
248
Popular Tags