OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [tools/] [gnu/] [classpath/] [tools/] [rmic/] [MethodGenerator.java] - Blame information for rev 779

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 779 jeremybenn
/* MethodGenerator.java -- Generates methods for GIOP rmic compiler.
2
   Copyright (C) 2006 Free Software Foundation
3
 
4
This file is part of GNU Classpath.
5
 
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.
10
 
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.
15
 
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.
20
 
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.
25
 
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. */
37
 
38
 
39
package gnu.classpath.tools.rmic;
40
 
41
import gnu.classpath.tools.rmic.AbstractMethodGenerator;
42
 
43
import java.lang.reflect.Method;
44
import java.util.Properties;
45
 
46
/**
47
 * Keeps information about the single method and generates the code fragments,
48
 * related to that method.
49
 *
50
 * @author Audrius Meskauskas, Lithuania (audriusa@Bioinformatics.org)
51
 */
52
public class MethodGenerator implements AbstractMethodGenerator
53
{
54
  /**
55
   * The method being defined.
56
   */
57
  Method method;
58
 
59
  /**
60
   * The parent code generator.
61
   */
62
  SourceGiopRmicCompiler rmic;
63
 
64
  /**
65
   * The previous method in the list, null for the first element.
66
   * Used to avoid repretetive inclusion of the same hash code label.
67
   */
68
  MethodGenerator previous = null;
69
 
70
  /**
71
   * The hash character position.
72
   */
73
  int hashCharPosition;
74
 
75
  /**
76
   * Create the new method generator for the given method.
77
   *
78
   * @param aMethod
79
   *          the related method.
80
   * @param aRmic
81
   *          the Rmic generator instance, where more class - related
82
   *          information is defined.
83
   */
84
  public MethodGenerator(Method aMethod, SourceGiopRmicCompiler aRmic)
85
  {
86
    method = aMethod;
87
    rmic = aRmic;
88
  }
89
 
90
  /**
91
   * Get the method name.
92
   *
93
   * @return the name of the method.
94
   */
95
  public String getGiopMethodName()
96
  {
97
    String m = method.getName();
98
    if (m.startsWith("get"))
99
      return "_get_J" + m.substring("get".length());
100
    else if (m.startsWith("set"))
101
      return "_set_J" + m.substring("set".length());
102
    else
103
      return m;
104
  }
105
 
106
  /**
107
   * Get the method parameter declaration.
108
   *
109
   * @return the string - method parameter declaration.
110
   */
111
  public String getArgumentList()
112
  {
113
    StringBuilder b = new StringBuilder();
114
 
115
    Class[] args = method.getParameterTypes();
116
 
117
    for (int i = 0; i < args.length; i++)
118
      {
119
        b.append(rmic.name(args[i]));
120
        b.append(" p" + i);
121
        if (i < args.length - 1)
122
          b.append(", ");
123
      }
124
    return b.toString();
125
  }
126
 
127
  /**
128
   * Get the method parameter list only (no type declarations). This is used to
129
   * generate the method invocations statement.
130
   *
131
   * @return the string - method parameter list.
132
   */
133
  public String getArgumentNames()
134
  {
135
    StringBuilder b = new StringBuilder();
136
 
137
    Class[] args = method.getParameterTypes();
138
 
139
    for (int i = 0; i < args.length; i++)
140
      {
141
        b.append(" p" + i);
142
        if (i < args.length - 1)
143
          b.append(", ");
144
      }
145
    return b.toString();
146
  }
147
 
148
  /**
149
   * Get the list of exceptions, thrown by this method.
150
   *
151
   * @return the list of exceptions.
152
   */
153
  public String getThrows()
154
  {
155
    StringBuilder b = new StringBuilder();
156
 
157
    Class[] args = method.getExceptionTypes();
158
 
159
    for (int i = 0; i < args.length; i++)
160
      {
161
        b.append(rmic.name(args[i]));
162
        if (i < args.length - 1)
163
          b.append(", ");
164
      }
165
    return b.toString();
166
  }
167
 
168
  /**
169
   * Generate this method for the Stub class.
170
   *
171
   * @return the method body for the stub class.
172
   */
173
  public String generateStubMethod()
174
  {
175
    String templateName;
176
 
177
    Properties vars = new Properties(rmic.vars);
178
    vars.put("#return_type", rmic.name(method.getReturnType()));
179
    vars.put("#method_name", method.getName());
180
    vars.put("#giop_method_name", getGiopMethodName());
181
    vars.put("#argument_list", getArgumentList());
182
    vars.put("#argument_names", getArgumentNames());
183
 
184
    vars.put("#argument_write", getStubParaWriteStatement());
185
 
186
    if (method.getReturnType().equals(void.class))
187
      vars.put("#read_return", "return;");
188
    else
189
      vars.put("#read_return",
190
               "return "
191
                   + GiopIo.getReadStatement(method.getReturnType(), rmic));
192
    String thr = getThrows();
193
    if (thr.length() > 0)
194
      vars.put("#throws", "\n    throws " + thr);
195
    else
196
      vars.put("#throws", "");
197
 
198
    if (method.getReturnType().equals(void.class))
199
      templateName = "StubMethodVoid.jav";
200
    else
201
      {
202
        vars.put("#write_result",
203
                 GiopIo.getWriteStatement(method.getReturnType(), "result",
204
                                          rmic));
205
        templateName = "StubMethod.jav";
206
      }
207
 
208
    String template = rmic.getResource(templateName);
209
    String generated = rmic.replaceAll(template, vars);
210
    return generated;
211
  }
212
 
213
  /**
214
   * Generate this method handling fragment for the Tie class.
215
   *
216
   * @return the fragment to handle this method for the Tie class.
217
   */
218
  public String generateTieMethod()
219
  {
220
    String templateName;
221
 
222
    Properties vars = new Properties(rmic.vars);
223
    vars.put("#return_type", rmic.name(method.getReturnType()));
224
    vars.put("#method_name", method.getName());
225
    vars.put("#giop_method_name", getGiopMethodName());
226
    vars.put("#argument_list", getArgumentList());
227
    vars.put("#argument_names", getArgumentNames());
228
 
229
    vars.put("#argument_write", getStubParaWriteStatement());
230
 
231
    if (previous == null || previous.getHashChar()!=getHashChar())
232
      vars.put("#hashCodeLabel","    case '"+getHashChar()+"':");
233
    else
234
      vars.put("#hashCodeLabel","    // also '"+getHashChar()+"':");
235
 
236
    if (method.getReturnType().equals(void.class))
237
      templateName = "TieMethodVoid.jav";
238
    else
239
      {
240
        vars.put("#write_result",
241
                 GiopIo.getWriteStatement(method.getReturnType(), "result",
242
                                          rmic));
243
        templateName = "TieMethod.jav";
244
      }
245
    vars.put("#read_and_define_args", getRda());
246
 
247
    String template = rmic.getResource(templateName);
248
    String generated = rmic.replaceAll(template, vars);
249
    return generated;
250
  }
251
 
252
  /**
253
   * Generate sentences for Reading and Defining Arguments.
254
   *
255
   * @return the sequence of sentences for reading and defining arguments.
256
   */
257
  public String getRda()
258
  {
259
    StringBuilder b = new StringBuilder();
260
    Class[] args = method.getParameterTypes();
261
 
262
    for (int i = 0; i < args.length; i++)
263
      {
264
        b.append("                ");
265
        b.append(rmic.name(args[i]));
266
        b.append(" ");
267
        b.append("p"+i);
268
        b.append(" = ");
269
        b.append(GiopIo.getReadStatement(args[i], rmic));
270
        if (i<args.length-1)
271
          b.append("\n");
272
      }
273
    return b.toString();
274
  }
275
 
276
  /**
277
   * Get the write statement for writing parameters inside the stub.
278
   *
279
   * @return the write statement.
280
   */
281
  public String getStubParaWriteStatement()
282
  {
283
    StringBuilder b = new StringBuilder();
284
    Class[] args = method.getParameterTypes();
285
 
286
    for (int i = 0; i < args.length; i++)
287
      {
288
        b.append("             ");
289
        b.append(GiopIo.getWriteStatement(args[i], "p" + i, rmic));
290
        b.append("\n");
291
      }
292
    return b.toString();
293
  }
294
 
295
  /**
296
   * Get the hash char.
297
   */
298
  public char getHashChar()
299
  {
300
    return getGiopMethodName().charAt(hashCharPosition);
301
  }
302
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.