libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / tools / gnu / classpath / tools / rmic / RmiMethodGenerator.java
blobb526bbb9f753fe655f201db25a0e3f1f7a0d8a36
1 /* MethodGenerator.java -- Generates methods for rmi compiler.
2 Copyright (C) 2006 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
28 package gnu.classpath.tools.rmic;
30 import gnu.classpath.tools.rmic.AbstractMethodGenerator;
31 import gnu.java.rmi.server.RMIHashes;
33 import java.lang.reflect.Method;
34 import java.util.Properties;
36 /**
37 * Keeps information about the single method and generates the code fragments,
38 * related to that method.
40 * @author Audrius Meskauskas, Lithuania (audriusa@Bioinformatics.org)
42 public class RmiMethodGenerator
43 implements AbstractMethodGenerator
45 /**
46 * The method being defined.
48 Method method;
50 /**
51 * The parent code generator.
53 SourceRmicCompiler rmic;
55 /**
56 * Create the new method generator for the given method.
58 * @param aMethod the related method.
59 * @param aRmic the Rmic generator instance, where more class - related
60 * information is defined.
62 public RmiMethodGenerator(Method aMethod, SourceRmicCompiler aRmic)
64 method = aMethod;
65 rmic = aRmic;
66 if (method.getParameterTypes().length == 0)
67 rmic.addZeroSizeObjecArray = true;
70 /**
71 * Get the method parameter declaration.
73 * @return the string - method parameter declaration.
75 public String getArgumentList()
77 StringBuilder b = new StringBuilder();
79 Class[] args = method.getParameterTypes();
81 for (int i = 0; i < args.length; i++)
83 b.append(rmic.name(args[i]));
84 b.append(" p" + i);
85 if (i < args.length - 1)
86 b.append(", ");
88 return b.toString();
91 /**
92 * Get the method parameter list only (no type declarations). This is used to
93 * generate the method invocations statement.
95 * @return the string - method parameter list.
97 public String getArgumentNames()
99 StringBuilder b = new StringBuilder();
101 Class[] args = method.getParameterTypes();
103 for (int i = 0; i < args.length; i++)
105 b.append(" p" + i);
106 if (i < args.length - 1)
107 b.append(", ");
109 return b.toString();
113 * Get the list of exceptions, thrown by this method.
115 * @return the list of exceptions.
117 public String getThrows()
119 StringBuilder b = new StringBuilder();
121 Class[] args = method.getExceptionTypes();
123 for (int i = 0; i < args.length; i++)
125 b.append(rmic.name(args[i]));
126 if (i < args.length - 1)
127 b.append(", ");
129 return b.toString();
133 * Generate this method for the Stub class.
135 * @return the method body for the stub class.
137 public String generateStubMethod()
139 String templateName;
141 Properties vars = new Properties(rmic.vars);
142 vars.put("#return_type", rmic.name(method.getReturnType()));
143 vars.put("#method_name", method.getName());
144 vars.put("#method_hash", getMethodHashCode());
145 vars.put("#argument_list", getArgumentList());
146 vars.put("#object_arg_list", getArgListAsObjectArray());
147 vars.put("#declaring_class", rmic.name(method.getDeclaringClass()));
148 vars.put("#class_arg_list", getArgListAsClassArray());
150 String thr = getThrows();
151 if (thr.length() > 0)
152 vars.put("#throws", "\n throws " + thr);
153 else
154 vars.put("#throws", "");
156 if (method.getReturnType().equals(void.class))
157 templateName = "Stub_12MethodVoid.jav";
158 else
160 templateName = "Stub_12Method.jav";
161 vars.put("#return_statement", getReturnStatement());
164 String template = rmic.getResource(templateName);
165 String generated = rmic.replaceAll(template, vars);
166 return generated;
170 * Generate sentences for Reading and Defining Arguments.
172 * @return the sequence of sentences for reading and defining arguments.
174 public String getStaticMethodDeclarations()
176 StringBuilder b = new StringBuilder();
177 Class[] args = method.getParameterTypes();
179 for (int i = 0; i < args.length; i++)
181 b.append(" ");
182 b.append(rmic.name(args[i]));
183 b.append(" ");
184 b.append("p" + i);
185 b.append(" = ");
186 if (i < args.length - 1)
187 b.append("\n");
189 return b.toString();
193 * Get the write statement for writing parameters inside the stub.
195 * @return the write statement.
197 public String getArgListAsObjectArray()
199 Class[] args = method.getParameterTypes();
201 if (args.length==0)
202 return "NO_ARGS";
204 StringBuilder b = new StringBuilder("new Object[] {");
206 for (int i = 0; i < args.length; i++)
208 if (!args[i].isPrimitive())
209 b.append("p"+i);
210 else
212 b.append("new "+rmic.name(WrapUnWrapper.getWrappingClass(args[i])));
213 b.append("(p"+i+")");
215 if (i<args.length-1)
216 b.append(", ");
218 b.append("}");
219 return b.toString();
223 * Get the return statement, assuming that the returned object is placed into
224 * the variable "result".
226 public String getReturnStatement()
228 Class r = method.getReturnType();
229 if (r.equals(void.class))
230 return "";
231 else
233 if (r.isPrimitive())
235 String wcd = rmic.name(WrapUnWrapper.getWrappingClass(r));
236 return "return ((" + wcd + ") result)."
237 + WrapUnWrapper.getUnwrappingMethod(r) + ";";
239 else
240 return "return (" + rmic.name(r) + ") result;";
245 * Get argument list as class array.
247 public String getArgListAsClassArray()
249 StringBuilder b = new StringBuilder();
250 Class[] args = method.getParameterTypes();
252 for (int i = 0; i < args.length; i++)
254 b.append(rmic.name(args[i]));
255 b.append(".class");
256 if (i < args.length - 1)
257 b.append(", ");
259 return b.toString();
263 * RMI ties (previously named Skeletons) are no longer used since v 1.2. This
264 * method should never be called.
266 public String generateTieMethod()
268 throw new InternalError();
272 * Get the method hash code.
274 public String getMethodHashCode()
276 return RMIHashes.getMethodHash(method)+"L";
280 * Additional processing of the stub name (nothing to do for JRMP stubs).
282 public String convertStubName(String name)
284 return name;