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)
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
22 package gnu
.classpath
.tools
.giop
.grmic
;
24 import gnu
.classpath
.tools
.AbstractMethodGenerator
;
26 import java
.lang
.reflect
.Method
;
27 import java
.util
.Properties
;
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
38 * The method being defined.
43 * The parent code generator.
45 GiopRmicCompiler rmic
;
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;
54 * The hash character position.
59 * Create the new method generator for the given method.
64 * the Rmic generator instance, where more class - related
65 * information is defined.
67 public MethodGenerator(Method aMethod
, GiopRmicCompiler aRmic
)
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());
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
]));
104 if (i
< args
.length
- 1)
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
++)
125 if (i
< args
.length
- 1)
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)
152 * Generate this method for the Stub class.
154 * @return the method body for the stub class.
156 public String
generateStubMethod()
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;");
172 vars
.put("#read_return",
174 + GiopIo
.getReadStatement(method
.getReturnType(), rmic
));
175 String thr
= getThrows();
176 if (thr
.length() > 0)
177 vars
.put("#throws", "\n throws " + thr
);
179 vars
.put("#throws", "");
181 if (method
.getReturnType().equals(void.class))
182 templateName
= "StubMethodVoid.jav";
185 vars
.put("#write_result",
186 GiopIo
.getWriteStatement(method
.getReturnType(), "result",
188 templateName
= "StubMethod.jav";
191 String template
= rmic
.getResource(templateName
);
192 String generated
= rmic
.replaceAll(template
, vars
);
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()
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()+"':");
217 vars
.put("#hashCodeLabel"," // also '"+getHashChar()+"':");
219 if (method
.getReturnType().equals(void.class))
220 templateName
= "TieMethodVoid.jav";
223 vars
.put("#write_result",
224 GiopIo
.getWriteStatement(method
.getReturnType(), "result",
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
);
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
++)
248 b
.append(rmic
.name(args
[i
]));
252 b
.append(GiopIo
.getReadStatement(args
[i
], rmic
));
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
++)
272 b
.append(GiopIo
.getWriteStatement(args
[i
], "p" + i
, rmic
));
281 public char getHashChar()
283 return getGiopMethodName().charAt(hashCharPosition
);