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/] [RmiMethodGenerator.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 rmi 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
package gnu.classpath.tools.rmic;
39
 
40
import gnu.classpath.tools.rmic.AbstractMethodGenerator;
41
import gnu.java.rmi.server.RMIHashes;
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 RmiMethodGenerator
53
    implements AbstractMethodGenerator
54
{
55
  /**
56
   * The method being defined.
57
   */
58
  Method method;
59
 
60
  /**
61
   * The parent code generator.
62
   */
63
  SourceRmicCompiler rmic;
64
 
65
  /**
66
   * Create the new method generator for the given method.
67
   *
68
   * @param aMethod the related method.
69
   * @param aRmic the Rmic generator instance, where more class - related
70
   *          information is defined.
71
   */
72
  public RmiMethodGenerator(Method aMethod, SourceRmicCompiler aRmic)
73
  {
74
    method = aMethod;
75
    rmic = aRmic;
76
    if (method.getParameterTypes().length == 0)
77
      rmic.addZeroSizeObjecArray = true;
78
  }
79
 
80
  /**
81
   * Get the method parameter declaration.
82
   *
83
   * @return the string - method parameter declaration.
84
   */
85
  public String getArgumentList()
86
  {
87
    StringBuilder b = new StringBuilder();
88
 
89
    Class[] args = method.getParameterTypes();
90
 
91
    for (int i = 0; i < args.length; i++)
92
      {
93
        b.append(rmic.name(args[i]));
94
        b.append(" p" + i);
95
        if (i < args.length - 1)
96
          b.append(", ");
97
      }
98
    return b.toString();
99
  }
100
 
101
  /**
102
   * Get the method parameter list only (no type declarations). This is used to
103
   * generate the method invocations statement.
104
   *
105
   * @return the string - method parameter list.
106
   */
107
  public String getArgumentNames()
108
  {
109
    StringBuilder b = new StringBuilder();
110
 
111
    Class[] args = method.getParameterTypes();
112
 
113
    for (int i = 0; i < args.length; i++)
114
      {
115
        b.append(" p" + i);
116
        if (i < args.length - 1)
117
          b.append(", ");
118
      }
119
    return b.toString();
120
  }
121
 
122
  /**
123
   * Get the list of exceptions, thrown by this method.
124
   *
125
   * @return the list of exceptions.
126
   */
127
  public String getThrows()
128
  {
129
    StringBuilder b = new StringBuilder();
130
 
131
    Class[] args = method.getExceptionTypes();
132
 
133
    for (int i = 0; i < args.length; i++)
134
      {
135
        b.append(rmic.name(args[i]));
136
        if (i < args.length - 1)
137
          b.append(", ");
138
      }
139
    return b.toString();
140
  }
141
 
142
  /**
143
   * Generate this method for the Stub class.
144
   *
145
   * @return the method body for the stub class.
146
   */
147
  public String generateStubMethod()
148
  {
149
    String templateName;
150
 
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());
159
 
160
    String thr = getThrows();
161
    if (thr.length() > 0)
162
      vars.put("#throws", "\n    throws " + thr);
163
    else
164
      vars.put("#throws", "");
165
 
166
    if (method.getReturnType().equals(void.class))
167
      templateName = "Stub_12MethodVoid.jav";
168
    else
169
      {
170
        templateName = "Stub_12Method.jav";
171
        vars.put("#return_statement", getReturnStatement());
172
      }
173
 
174
    String template = rmic.getResource(templateName);
175
    String generated = rmic.replaceAll(template, vars);
176
    return generated;
177
  }
178
 
179
  /**
180
   * Generate sentences for Reading and Defining Arguments.
181
   *
182
   * @return the sequence of sentences for reading and defining arguments.
183
   */
184
  public String getStaticMethodDeclarations()
185
  {
186
    StringBuilder b = new StringBuilder();
187
    Class[] args = method.getParameterTypes();
188
 
189
    for (int i = 0; i < args.length; i++)
190
      {
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");
198
      }
199
    return b.toString();
200
  }
201
 
202
  /**
203
   * Get the write statement for writing parameters inside the stub.
204
   *
205
   * @return the write statement.
206
   */
207
  public String getArgListAsObjectArray()
208
  {
209
    Class[] args = method.getParameterTypes();
210
 
211
    if (args.length==0)
212
      return "NO_ARGS";
213
 
214
    StringBuilder b = new StringBuilder("new Object[] {");
215
 
216
    for (int i = 0; i < args.length; i++)
217
      {
218
        if (!args[i].isPrimitive())
219
          b.append("p"+i);
220
        else
221
          {
222
            b.append("new "+rmic.name(WrapUnWrapper.getWrappingClass(args[i])));
223
            b.append("(p"+i+")");
224
          }
225
        if (i<args.length-1)
226
          b.append(", ");
227
      }
228
    b.append("}");
229
    return b.toString();
230
  }
231
 
232
  /**
233
   * Get the return statement, assuming that the returned object is placed into
234
   * the variable "result".
235
   */
236
  public String getReturnStatement()
237
  {
238
    Class r = method.getReturnType();
239
    if (r.equals(void.class))
240
      return "";
241
    else
242
      {
243
        if (r.isPrimitive())
244
          {
245
            String wcd = rmic.name(WrapUnWrapper.getWrappingClass(r));
246
            return "return ((" + wcd + ") result)."
247
                   + WrapUnWrapper.getUnwrappingMethod(r) + ";";
248
          }
249
        else
250
          return "return (" + rmic.name(r) + ") result;";
251
      }
252
  }
253
 
254
  /**
255
   * Get argument list as class array.
256
   */
257
  public String getArgListAsClassArray()
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(rmic.name(args[i]));
265
        b.append(".class");
266
        if (i < args.length - 1)
267
          b.append(", ");
268
      }
269
    return b.toString();
270
  }
271
 
272
  /**
273
   * RMI ties (previously named Skeletons) are no longer used since v 1.2. This
274
   * method should never be called.
275
   */
276
  public String generateTieMethod()
277
  {
278
    throw new InternalError();
279
  }
280
 
281
  /**
282
   * Get the method hash code.
283
   */
284
  public String getMethodHashCode()
285
  {
286
    return RMIHashes.getMethodHash(method)+"L";
287
  }
288
 
289
  /**
290
   * Additional processing of the stub name (nothing to do for JRMP stubs).
291
   */
292
  public String convertStubName(String name)
293
  {
294
    return name;
295
  }
296
 
297
}

powered by: WebSVN 2.1.0

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