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)
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
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
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
;
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
46 * The method being defined.
51 * The parent code generator.
53 SourceRmicCompiler rmic
;
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
)
66 if (method
.getParameterTypes().length
== 0)
67 rmic
.addZeroSizeObjecArray
= true;
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
]));
85 if (i
< args
.length
- 1)
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
++)
106 if (i
< args
.length
- 1)
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)
133 * Generate this method for the Stub class.
135 * @return the method body for the stub class.
137 public String
generateStubMethod()
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
);
154 vars
.put("#throws", "");
156 if (method
.getReturnType().equals(void.class))
157 templateName
= "Stub_12MethodVoid.jav";
160 templateName
= "Stub_12Method.jav";
161 vars
.put("#return_statement", getReturnStatement());
164 String template
= rmic
.getResource(templateName
);
165 String generated
= rmic
.replaceAll(template
, vars
);
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
++)
182 b
.append(rmic
.name(args
[i
]));
186 if (i
< args
.length
- 1)
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();
204 StringBuilder b
= new StringBuilder("new Object[] {");
206 for (int i
= 0; i
< args
.length
; i
++)
208 if (!args
[i
].isPrimitive())
212 b
.append("new "+rmic
.name(WrapUnWrapper
.getWrappingClass(args
[i
])));
213 b
.append("(p"+i
+")");
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))
235 String wcd
= rmic
.name(WrapUnWrapper
.getWrappingClass(r
));
236 return "return ((" + wcd
+ ") result)."
237 + WrapUnWrapper
.getUnwrappingMethod(r
) + ";";
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
]));
256 if (i
< args
.length
- 1)
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
)