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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.java.gen;
030:
031: import com.caucho.java.JavaWriter;
032: import com.caucho.util.L10N;
033:
034: import java.io.IOException;
035:
036: import java.lang.reflect.*;
037:
038: /**
039: * Generates the skeleton for a method call.
040: */
041: public class MethodCallChain extends CallChain {
042: private static L10N L = new L10N(MethodCallChain.class);
043:
044: private Method _method;
045: private String _methodName;
046: private Class[] _parameterTypes;
047: private Class _returnType;
048:
049: /**
050: * Creates the chain.
051: */
052: public MethodCallChain() {
053: }
054:
055: /**
056: * Creates the chain with the method.
057: */
058: public MethodCallChain(Method method) {
059: _method = method;
060: _methodName = method.getName();
061: _parameterTypes = method.getParameterTypes();
062: _returnType = method.getReturnType();
063: }
064:
065: /**
066: * Creates the chain with the method.
067: */
068: public MethodCallChain(String methodName, Class[] params,
069: Class returnType) {
070: _methodName = methodName;
071: _parameterTypes = params;
072: _returnType = returnType;
073: }
074:
075: /**
076: * Returns the method.
077: */
078: public Method getMethod() {
079: return _method;
080: }
081:
082: /**
083: * Returns the method's parameter types.
084: */
085: public Class[] getParameterTypes() {
086: return _parameterTypes;
087: }
088:
089: /**
090: * Returns the method's return type.
091: */
092: public Class getReturnType() {
093: return _returnType;
094: }
095:
096: /**
097: * Generates the code for the method call.
098: *
099: * @param out the writer to the output stream.
100: * @param retVar the variable to hold the return value
101: * @param var the object to be called
102: * @param args the method arguments
103: */
104: public void generateCall(JavaWriter out, String retVar, String var,
105: String[] args) throws IOException {
106: if (getReturnType().getName().equals("void")) {
107: } else if (retVar != null)
108: out.print(retVar + " = ");
109: else
110: out.print("return ");
111:
112: if (var != null)
113: out.print(var + ".");
114: else
115: out.print("super.");
116:
117: out.print(_methodName + "(");
118:
119: for (int i = 0; i < args.length; i++) {
120: if (i != 0)
121: out.print(", ");
122:
123: out.print(args[i]);
124: }
125:
126: out.println(");");
127: }
128: }
|