Import GNU Classpath (20121202).
[official-gcc.git] / libjava / classpath / tools / gnu / classpath / tools / rmic / RmiMethodGenerator.java
blobd533f1220556aaa1fdf934237a634a2f512cc187
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.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.classpath.tools.rmic;
40 import gnu.classpath.tools.rmic.AbstractMethodGenerator;
41 import gnu.java.rmi.server.RMIHashes;
43 import java.lang.reflect.Method;
44 import java.util.Properties;
46 /**
47 * Keeps information about the single method and generates the code fragments,
48 * related to that method.
50 * @author Audrius Meskauskas, Lithuania (audriusa@Bioinformatics.org)
52 public class RmiMethodGenerator
53 implements AbstractMethodGenerator
55 /**
56 * The method being defined.
58 Method method;
60 /**
61 * The parent code generator.
63 SourceRmicCompiler rmic;
65 /**
66 * Create the new method generator for the given method.
68 * @param aMethod the related method.
69 * @param aRmic the Rmic generator instance, where more class - related
70 * information is defined.
72 public RmiMethodGenerator(Method aMethod, SourceRmicCompiler aRmic)
74 method = aMethod;
75 rmic = aRmic;
76 if (method.getParameterTypes().length == 0)
77 rmic.addZeroSizeObjecArray = true;
80 /**
81 * Get the method parameter declaration.
83 * @return the string - method parameter declaration.
85 public String getArgumentList()
87 StringBuilder b = new StringBuilder();
89 Class<?>[] args = method.getParameterTypes();
91 for (int i = 0; i < args.length; i++)
93 b.append(rmic.name(args[i]));
94 b.append(" p" + i);
95 if (i < args.length - 1)
96 b.append(", ");
98 return b.toString();
102 * Get the method parameter list only (no type declarations). This is used to
103 * generate the method invocations statement.
105 * @return the string - method parameter list.
107 public String getArgumentNames()
109 StringBuilder b = new StringBuilder();
111 Class<?>[] args = method.getParameterTypes();
113 for (int i = 0; i < args.length; i++)
115 b.append(" p" + i);
116 if (i < args.length - 1)
117 b.append(", ");
119 return b.toString();
123 * Get the list of exceptions, thrown by this method.
125 * @return the list of exceptions.
127 public String getThrows()
129 StringBuilder b = new StringBuilder();
131 Class<?>[] args = method.getExceptionTypes();
133 for (int i = 0; i < args.length; i++)
135 b.append(rmic.name(args[i]));
136 if (i < args.length - 1)
137 b.append(", ");
139 return b.toString();
143 * Generate this method for the Stub class.
145 * @return the method body for the stub class.
147 public String generateStubMethod()
149 String templateName;
151 Properties vars = new Properties(rmic.vars);
152 vars.put("#return_type", rmic.name(method.getReturnType()));
153 vars.put("#method_name", method.getName());
154 vars.put("#method_hash", getMethodHashCode());
155 vars.put("#argument_list", getArgumentList());
156 vars.put("#object_arg_list", getArgListAsObjectArray());
157 vars.put("#declaring_class", rmic.name(method.getDeclaringClass()));
158 vars.put("#class_arg_list", getArgListAsClassArray());
160 String thr = getThrows();
161 if (thr.length() > 0)
162 vars.put("#throws", "\n throws " + thr);
163 else
164 vars.put("#throws", "");
166 if (method.getReturnType().equals(void.class))
167 templateName = "Stub_12MethodVoid.jav";
168 else
170 templateName = "Stub_12Method.jav";
171 vars.put("#return_statement", getReturnStatement());
174 String template = rmic.getResource(templateName);
175 String generated = rmic.replaceAll(template, vars);
176 return generated;
180 * Generate sentences for Reading and Defining Arguments.
182 * @return the sequence of sentences for reading and defining arguments.
184 public String getStaticMethodDeclarations()
186 StringBuilder b = new StringBuilder();
187 Class<?>[] args = method.getParameterTypes();
189 for (int i = 0; i < args.length; i++)
191 b.append(" ");
192 b.append(rmic.name(args[i]));
193 b.append(" ");
194 b.append("p" + i);
195 b.append(" = ");
196 if (i < args.length - 1)
197 b.append("\n");
199 return b.toString();
203 * Get the write statement for writing parameters inside the stub.
205 * @return the write statement.
207 public String getArgListAsObjectArray()
209 Class[] args = method.getParameterTypes();
211 if (args.length==0)
212 return "NO_ARGS";
214 StringBuilder b = new StringBuilder("new Object[] {");
216 for (int i = 0; i < args.length; i++)
218 if (!args[i].isPrimitive())
219 b.append("p"+i);
220 else
222 b.append("new "+rmic.name(WrapUnWrapper.getWrappingClass(args[i])));
223 b.append("(p"+i+")");
225 if (i<args.length-1)
226 b.append(", ");
228 b.append("}");
229 return b.toString();
233 * Get the return statement, assuming that the returned object is placed into
234 * the variable "result".
236 public String getReturnStatement()
238 Class r = method.getReturnType();
239 if (r.equals(void.class))
240 return "";
241 else
243 if (r.isPrimitive())
245 String wcd = rmic.name(WrapUnWrapper.getWrappingClass(r));
246 return "return ((" + wcd + ") result)."
247 + WrapUnWrapper.getUnwrappingMethod(r) + ";";
249 else
250 return "return (" + rmic.name(r) + ") result;";
255 * Get argument list as class array.
257 public String getArgListAsClassArray()
259 StringBuilder b = new StringBuilder();
260 Class[] args = method.getParameterTypes();
262 for (int i = 0; i < args.length; i++)
264 b.append(rmic.name(args[i]));
265 b.append(".class");
266 if (i < args.length - 1)
267 b.append(", ");
269 return b.toString();
273 * RMI ties (previously named Skeletons) are no longer used since v 1.2. This
274 * method should never be called.
276 public String generateTieMethod()
278 throw new InternalError();
282 * Get the method hash code.
284 public String getMethodHashCode()
286 return RMIHashes.getMethodHash(method)+"L";
290 * Additional processing of the stub name (nothing to do for JRMP stubs).
292 public String convertStubName(String name)
294 return name;