Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / tools / gnu / classpath / tools / giop / grmic / MethodGenerator.java
blob80148d51aff9439bfeb6fbcdaf52d7cd7dbde412
1 /* MethodGenerator.java -- Generates methods for GIOP rmic 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.
22 package gnu.classpath.tools.giop.grmic;
24 import gnu.classpath.tools.AbstractMethodGenerator;
26 import java.lang.reflect.Method;
27 import java.util.Properties;
29 /**
30 * Keeps information about the single method and generates the code fragments,
31 * related to that method.
33 * @author Audrius Meskauskas, Lithuania (audriusa@Bioinformatics.org)
35 public class MethodGenerator implements AbstractMethodGenerator
37 /**
38 * The method being defined.
40 Method method;
42 /**
43 * The parent code generator.
45 GiopRmicCompiler rmic;
47 /**
48 * The previous method in the list, null for the first element.
49 * Used to avoid repretetive inclusion of the same hash code label.
51 MethodGenerator previous = null;
53 /**
54 * The hash character position.
56 int hashCharPosition;
58 /**
59 * Create the new method generator for the given method.
61 * @param aMethod
62 * the related method.
63 * @param aRmic
64 * the Rmic generator instance, where more class - related
65 * information is defined.
67 public MethodGenerator(Method aMethod, GiopRmicCompiler aRmic)
69 method = aMethod;
70 rmic = aRmic;
73 /**
74 * Get the method name.
76 * @return the name of the method.
78 public String getGiopMethodName()
80 String m = method.getName();
81 if (m.startsWith("get"))
82 return "_get_J" + m.substring("get".length());
83 else if (m.startsWith("set"))
84 return "_set_J" + m.substring("set".length());
85 else
86 return m;
89 /**
90 * Get the method parameter declaration.
92 * @return the string - method parameter declaration.
94 public String getArgumentList()
96 StringBuffer b = new StringBuffer();
98 Class[] args = method.getParameterTypes();
100 for (int i = 0; i < args.length; i++)
102 b.append(rmic.name(args[i]));
103 b.append(" p" + i);
104 if (i < args.length - 1)
105 b.append(", ");
107 return b.toString();
111 * Get the method parameter list only (no type declarations). This is used to
112 * generate the method invocations statement.
114 * @return the string - method parameter list.
116 public String getArgumentNames()
118 StringBuffer b = new StringBuffer();
120 Class[] args = method.getParameterTypes();
122 for (int i = 0; i < args.length; i++)
124 b.append(" p" + i);
125 if (i < args.length - 1)
126 b.append(", ");
128 return b.toString();
132 * Get the list of exceptions, thrown by this method.
134 * @return the list of exceptions.
136 public String getThrows()
138 StringBuffer b = new StringBuffer();
140 Class[] args = method.getExceptionTypes();
142 for (int i = 0; i < args.length; i++)
144 b.append(rmic.name(args[i]));
145 if (i < args.length - 1)
146 b.append(", ");
148 return b.toString();
152 * Generate this method for the Stub class.
154 * @return the method body for the stub class.
156 public String generateStubMethod()
158 String templateName;
160 Properties vars = new Properties(rmic.vars);
161 vars.put("#return_type", rmic.name(method.getReturnType()));
162 vars.put("#method_name", method.getName());
163 vars.put("#giop_method_name", getGiopMethodName());
164 vars.put("#argument_list", getArgumentList());
165 vars.put("#argument_names", getArgumentNames());
167 vars.put("#argument_write", getStubParaWriteStatement());
169 if (method.getReturnType().equals(void.class))
170 vars.put("#read_return", "return;");
171 else
172 vars.put("#read_return",
173 "return "
174 + GiopIo.getReadStatement(method.getReturnType(), rmic));
175 String thr = getThrows();
176 if (thr.length() > 0)
177 vars.put("#throws", "\n throws " + thr);
178 else
179 vars.put("#throws", "");
181 if (method.getReturnType().equals(void.class))
182 templateName = "StubMethodVoid.jav";
183 else
185 vars.put("#write_result",
186 GiopIo.getWriteStatement(method.getReturnType(), "result",
187 rmic));
188 templateName = "StubMethod.jav";
191 String template = rmic.getResource(templateName);
192 String generated = rmic.replaceAll(template, vars);
193 return generated;
197 * Generate this method handling fragment for the Tie class.
199 * @return the fragment to handle this method for the Tie class.
201 public String generateTieMethod()
203 String templateName;
205 Properties vars = new Properties(rmic.vars);
206 vars.put("#return_type", rmic.name(method.getReturnType()));
207 vars.put("#method_name", method.getName());
208 vars.put("#giop_method_name", getGiopMethodName());
209 vars.put("#argument_list", getArgumentList());
210 vars.put("#argument_names", getArgumentNames());
212 vars.put("#argument_write", getStubParaWriteStatement());
214 if (previous == null || previous.getHashChar()!=getHashChar())
215 vars.put("#hashCodeLabel"," case '"+getHashChar()+"':");
216 else
217 vars.put("#hashCodeLabel"," // also '"+getHashChar()+"':");
219 if (method.getReturnType().equals(void.class))
220 templateName = "TieMethodVoid.jav";
221 else
223 vars.put("#write_result",
224 GiopIo.getWriteStatement(method.getReturnType(), "result",
225 rmic));
226 templateName = "TieMethod.jav";
228 vars.put("#read_and_define_args", getRda());
230 String template = rmic.getResource(templateName);
231 String generated = rmic.replaceAll(template, vars);
232 return generated;
236 * Generate sentences for Reading and Defining Arguments.
238 * @return the sequence of sentences for reading and defining arguments.
240 public String getRda()
242 StringBuffer b = new StringBuffer();
243 Class[] args = method.getParameterTypes();
245 for (int i = 0; i < args.length; i++)
247 b.append(" ");
248 b.append(rmic.name(args[i]));
249 b.append(" ");
250 b.append("p"+i);
251 b.append(" = ");
252 b.append(GiopIo.getReadStatement(args[i], rmic));
253 if (i<args.length-1)
254 b.append("\n");
256 return b.toString();
260 * Get the write statement for writing parameters inside the stub.
262 * @return the write statement.
264 public String getStubParaWriteStatement()
266 StringBuffer b = new StringBuffer();
267 Class[] args = method.getParameterTypes();
269 for (int i = 0; i < args.length; i++)
271 b.append(" ");
272 b.append(GiopIo.getWriteStatement(args[i], "p" + i, rmic));
273 b.append("\n");
275 return b.toString();
279 * Get the hash char.
281 public char getHashChar()
283 return getGiopMethodName().charAt(hashCharPosition);