001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.java.gen;
031:
032: import com.caucho.java.JavaWriter;
033: import com.caucho.util.L10N;
034:
035: import java.io.IOException;
036: import java.lang.reflect.*;
037:
038: /**
039: * Basic method generation.
040: */
041: public class BaseMethod extends ClassComponent {
042: private static final L10N L = new L10N(BaseMethod.class);
043:
044: private Method _method;
045: private String _methodName;
046:
047: private boolean _isStatic;
048: private String _visibility = "public";
049:
050: private Class _returnType;
051: private Class[] _parameterTypes;
052:
053: private Class[] _exceptionTypes;
054:
055: private CallChain _call;
056:
057: /**
058: * Creates the base method
059: */
060: public BaseMethod(String methodName, CallChain call) {
061: _methodName = methodName;
062:
063: setCall(call);
064: }
065:
066: /**
067: * Creates the base method
068: */
069: public BaseMethod(Method method, CallChain call) {
070: _method = method;
071: _exceptionTypes = method.getExceptionTypes();
072:
073: setCall(call);
074: }
075:
076: /**
077: * Creates the base method
078: */
079: public BaseMethod(Method method) {
080: _method = method;
081: _exceptionTypes = method.getExceptionTypes();
082: }
083:
084: /**
085: * Creates the base method
086: */
087: public BaseMethod(Method apiMethod, Method implMethod) {
088: this (apiMethod, new MethodCallChain(implMethod));
089:
090: _exceptionTypes = implMethod.getExceptionTypes();
091: }
092:
093: /**
094: * Returns the call.
095: */
096: public CallChain getCall() {
097: return _call;
098: }
099:
100: /**
101: * Sets the call.
102: */
103: public void setCall(CallChain call) {
104: if (call == null)
105: throw new NullPointerException();
106:
107: _call = call;
108: }
109:
110: /**
111: * Returns the method.
112: */
113: public Method getMethod() {
114: return _method;
115: }
116:
117: /**
118: * Returns the method name.
119: */
120: public String getMethodName() {
121: if (_methodName != null)
122: return _methodName;
123: else
124: return _method.getName();
125: }
126:
127: /**
128: * Returns the parameter types.
129: */
130: public Class[] getParameterTypes() {
131: // ejb/0f7a
132:
133: if (_parameterTypes != null)
134: return _parameterTypes;
135: else if (_method != null)
136: return _method.getParameterTypes();
137: else
138: return _call.getParameterTypes();
139: }
140:
141: /**
142: * Gets the return type.
143: */
144: public Class getReturnType() {
145: if (_method != null)
146: return _method.getReturnType();
147: else
148: return _call.getReturnType();
149: }
150:
151: /**
152: * Returns the exception types.
153: */
154: public Class[] getExceptionTypes() {
155: return _exceptionTypes;
156: }
157:
158: /**
159: * Generates the code for the class.
160: *
161: * @param out the writer to the output stream.
162: */
163: public void generate(JavaWriter out) throws IOException {
164: String[] args = generateMethodHeader(out);
165:
166: Class[] exceptionTypes = getExceptionTypes();
167: if (exceptionTypes != null && exceptionTypes.length > 0) {
168: out.print(" throws ");
169:
170: for (int i = 0; i < exceptionTypes.length; i++) {
171: if (i != 0)
172: out.print(", ");
173:
174: out.printClass(exceptionTypes[i]);
175: }
176: out.println();
177: }
178:
179: // XXX: exception
180: out.println("{");
181: out.pushDepth();
182:
183: generateCall(out, args);
184:
185: out.popDepth();
186: out.println("}");
187: }
188:
189: /**
190: * Generates the method header
191: *
192: * @param out the writer to the output stream.
193: *
194: * @return the method arguments
195: */
196: public String[] generateMethodHeader(JavaWriter out)
197: throws IOException {
198: if (_visibility != null && !_visibility.equals(""))
199: out.print(_visibility + " ");
200:
201: if (_isStatic)
202: out.print("static ");
203:
204: out.printClass(getReturnType());
205: out.print(" " + getMethodName() + "(");
206:
207: Class[] parameterTypes = getParameterTypes();
208: String[] args = new String[parameterTypes.length];
209:
210: for (int i = 0; i < args.length; i++) {
211: if (i != 0)
212: out.print(", ");
213:
214: // ejb/0f7a
215: if (i + 1 == args.length && getMethod().isVarArgs()) {
216: out.printClass(parameterTypes[i].getComponentType());
217: out.print("...");
218: } else
219: out.printClass(parameterTypes[i]);
220:
221: args[i] = "a" + i;
222: out.print(" " + args[i]);
223:
224: }
225: out.println(")");
226:
227: return args;
228: }
229:
230: /**
231: * Generates the code for the call.
232: *
233: * @param out the writer to the output stream.
234: * @param args the arguments
235: */
236: protected void generateCall(JavaWriter out, String[] args)
237: throws IOException {
238: _call.generateCall(out, null, null, args);
239: }
240: }
|