KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > idl > Method


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-2004 Gerald Brose.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package org.jacorb.idl;
22
23 /**
24  * @author Gerald Brose
25  * @version $Id: Method.java,v 1.19 2004/05/06 12:39:58 nicolas Exp $
26  *
27  * This class is used to represent accessor operations
28  */

29
30 import java.io.PrintWriter JavaDoc;
31
32 public class Method
33     implements Operation
34 {
35     public TypeSpec resultType;
36     public TypeSpec parameterType;
37
38     private String JavaDoc name;
39     private boolean pseudo;
40
41
42     public Method( TypeSpec res, TypeSpec params, String JavaDoc name, boolean pseudo )
43     {
44         resultType = res;
45         parameterType = params;
46         this.name = name;
47         this.pseudo = pseudo;
48     }
49
50     public boolean isGetter()
51     {
52         return resultType != null;
53     }
54
55     public String JavaDoc name()
56     {
57         return name;
58     }
59
60     public String JavaDoc opName()
61     {
62         if( isGetter() )
63             return "_get_" + name;
64         else
65             return "_set_" + name;
66     }
67
68     public String JavaDoc signature()
69     {
70         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
71         sb.append( name + "(" );
72         if( parameterType != null )
73         {
74             sb.append( parameterType.toString() );
75         }
76         sb.append( ")" );
77         return sb.toString();
78     }
79
80     public void printSignature( PrintWriter JavaDoc ps )
81     {
82         printSignature( ps, pseudo );
83     }
84
85     /**
86      * @param printModifiers whether "public abstract" should be added
87      */

88     public void printSignature( PrintWriter JavaDoc ps, boolean printModifiers )
89     {
90         ps.print( "\t" );
91         if( printModifiers )
92             ps.print( "public abstract " );
93
94         if( isGetter() )
95         {
96             ps.print( resultType.toString() );
97             ps.println( " " + name + "();" );
98         }
99         else
100         {
101             ps.print( "void " + name + "(" );
102             ps.print( parameterType.toString() );
103             ps.println( " arg);" );
104         }
105     }
106
107
108     public void printMethod( PrintWriter JavaDoc ps, String JavaDoc classname, boolean is_local, boolean is_abstract )
109     {
110         ps.print( "\tpublic " );
111
112         if( isGetter() )
113         {
114             // accessor method
115
ps.print( resultType.toString() );
116             ps.println( " " + name + "()" );
117             ps.println( "\t{" );
118             ps.println( "\t\twhile(true)" );
119             ps.println( "\t\t{" );
120
121             // remote part, not for locality constrained objects
122
//
123
if( !is_local )
124             {
125                 ps.println( "\t\tif(! this._is_local())" );
126                 ps.println( "\t\t{" );
127
128                 ps.println( "\t\t\torg.omg.CORBA.portable.InputStream _is = null;" );
129
130                 ps.println( "\t\t\ttry" );
131                 ps.println( "\t\t\t{" );
132                 ps.println( "\t\t\t\torg.omg.CORBA.portable.OutputStream _os = _request(\"_get_" + name + "\",true);" );
133                 ps.println( "\t\t\t\t_is = _invoke(_os);" );
134                 TypeSpec ts = resultType.typeSpec();
135                 ps.println( "\t\t\t\treturn " + ts.printReadExpression( "_is" ) + ";" );
136                 ps.println( "\t\t\t}" );
137                 ps.println( "\t\t\tcatch( org.omg.CORBA.portable.RemarshalException _rx ){}" );
138                 ps.println( "\t\t\tcatch( org.omg.CORBA.portable.ApplicationException _ax )" );
139                 ps.println( "\t\t\t{" );
140                 ps.println( "\t\t\t\tString _id = _ax.getId();" );
141                 ps.println( "\t\t\t\tthrow new RuntimeException(\"Unexpected exception \" + _id );" );
142                 ps.println( "\t\t\t}" );
143                 ps.println( "\t\t\tfinally" );
144                 ps.println( "\t\t\t{" );
145                 ps.println( "\t\t\t\tthis._releaseReply(_is);" );
146                 ps.println( "\t\t\t}" );
147                 ps.println( "\t\t}\n" );
148
149                 // local part
150
ps.println( "\t\telse" );
151                 ps.println( "\t\t{" );
152             }
153
154             ps.println( "\t\torg.omg.CORBA.portable.ServantObject _so = _servant_preinvoke( \"_get_" + name + "\", _opsClass);" );
155
156             ps.println( "\t\tif( _so == null )" );
157             ps.println( "\t\t\tthrow new org.omg.CORBA.UNKNOWN(\"local invocations not supported!\");" );
158             if( is_abstract )
159             {
160                 ps.println( "\t\t\t" + classname + " _localServant = (" +
161                             classname + ")_so.servant;" );
162             }
163             else
164             {
165                 ps.println( "\t\t\t" + classname + "Operations _localServant = (" +
166                             classname + "Operations)_so.servant;" );
167             }
168
169             ps.println( "\t\t\t" + resultType + " _result;" );
170
171             ps.println( "\t\ttry" );
172             ps.println( "\t\t{" );
173             ps.println( "\t\t\t_result = _localServant." + name + "();" );
174             ps.println( "\t\t}" );
175             ps.println( "\t\tfinally" );
176             ps.println( "\t\t{" );
177             ps.println( "\t\t\t_servant_postinvoke(_so);" );
178             ps.println( "\t\t}" );
179             ps.println( "\t\treturn _result;" );
180             ps.println( "\t\t}" );
181             if( !is_local ) ps.println( "\t\t}\n" );
182             ps.println( "\t}\n" );
183         }
184         else
185         {
186             /** modifier */
187
188             ps.print( "void " + name + "(" + parameterType.toString() );
189             ps.println( " a)" );
190             ps.println( "\t{" );
191             ps.println( "\t\twhile(true)" );
192             ps.println( "\t\t{" );
193             // remote part not for locality constrained objects
194
//
195
if( !is_local )
196             {
197                 ps.println( "\t\tif(! this._is_local())" );
198                 ps.println( "\t\t{" );
199                 ps.println( "\t\t\torg.omg.CORBA.portable.InputStream _is = null;" );
200
201                 ps.println( "\t\t\ttry" );
202                 ps.println( "\t\t\t{" );
203                 ps.println( "\t\t\t\torg.omg.CORBA.portable.OutputStream _os = _request(\"_set_" + name + "\",true);" );
204                 ps.println( "\t\t\t\t" + parameterType.typeSpec().printWriteStatement( "a", "_os" ) );
205                 ps.println( "\t\t\t\t_is = _invoke(_os);" );
206                 ps.println( "\t\t\t\treturn;" );
207                 ps.println( "\t\t\t}" );
208                 ps.println( "\t\t\tcatch( org.omg.CORBA.portable.RemarshalException _rx ){}" );
209                 ps.println( "\t\t\tcatch( org.omg.CORBA.portable.ApplicationException _ax )" );
210                 ps.println( "\t\t\t{" );
211                 ps.println( "\t\t\t\tString _id = _ax.getId();" );
212                 ps.println( "\t\t\t\tthrow new RuntimeException(\"Unexpected exception \" + _id );" );
213                 ps.println( "\t\t\t}" );
214                 ps.println( "\t\t\tfinally" );
215                 ps.println( "\t\t\t{" );
216                 ps.println( "\t\t\t\tthis._releaseReply(_is);" );
217                 ps.println( "\t\t\t}" );
218                 ps.println( "\t\t}\n" );
219
220                 // local part
221
ps.println( "\t\telse" );
222                 ps.println( "\t\t{" );
223
224             }
225             ps.println( "\t\t\torg.omg.CORBA.portable.ServantObject _so = _servant_preinvoke( \"_set_" + name + "\", _opsClass);" );
226
227             ps.println( "\t\t\tif( _so == null )" );
228             ps.println( "\t\t\t\tthrow new org.omg.CORBA.UNKNOWN(\"local invocations not supported!\");" );
229             ps.println( "\t\t\t" + classname + "Operations _localServant = (" + classname + "Operations)_so.servant;" );
230
231             ps.println( "\t\t\t\ttry" );
232             ps.println( "\t\t\t\t{" );
233             ps.println( "\t\t\t\t\t_localServant." + name + "(a);" );
234             ps.println( "\t\t\t\t}" );
235             ps.println( "\t\t\t\tfinally" );
236             ps.println( "\t\t\t\t{" );
237             ps.println( "\t\t\t\t\t_servant_postinvoke(_so);" );
238             ps.println( "\t\t\t\t}" );
239             ps.println( "\t\t\t\treturn;" );
240             ps.println( "\t\t\t}" );
241             if( !is_local ) ps.println( "\t\t}\n" );
242             ps.println( "\t}\n" );
243         }
244     }
245
246
247     public void print_sendc_Method( PrintWriter JavaDoc ps,
248                                     String JavaDoc classname )
249     {
250         ps.print( "\tpublic void sendc_" );
251
252         if( isGetter() )
253         {
254             // accessor method
255
ps.print ( "get_" + name );
256             ps.println( "(AMI_" + classname + "Handler ami_handler)" );
257             ps.println( "\t{" );
258             ps.println( "\t\twhile(true)" );
259             ps.println( "\t\t{" );
260
261             ps.println( "\t\t\ttry" );
262             ps.println( "\t\t\t{" );
263             ps.println( "\t\t\t\torg.omg.CORBA.portable.OutputStream _os = _request(\"_get_" + name + "\",true);" );
264             ps.println( "\t\t\t\t((org.jacorb.orb.Delegate)_get_delegate()).invoke(this, _os, ami_handler);" );
265             ps.println( "\t\t\t\treturn;" );
266             ps.println( "\t\t\t}" );
267             ps.println( "\t\t\tcatch( org.omg.CORBA.portable.RemarshalException _rx ){}" );
268             ps.println( "\t\t\tcatch( org.omg.CORBA.portable.ApplicationException _ax )" );
269             ps.println( "\t\t\t{" );
270             ps.println( "\t\t\t\tString _id = _ax.getId();" );
271             ps.println( "\t\t\t\tthrow new RuntimeException(\"Unexpected exception \" + _id );" );
272             ps.println( "\t\t\t}" );
273             ps.println( "\t\t}" );
274             ps.println( "\t}\n" );
275         }
276         else
277         {
278             // modifier
279
ps.print ( "set_" + name );
280             ps.print ( "(AMI_" + classname + "Handler ami_handler, " );
281             ps.println ( parameterType.toString() + " attr_" + name + ")");
282             ps.println( "\t{" );
283             ps.println( "\t\twhile(true)" );
284             ps.println( "\t\t{" );
285             ps.println( "\t\t\ttry" );
286             ps.println( "\t\t\t{" );
287             ps.println( "\t\t\t\torg.omg.CORBA.portable.OutputStream _os = _request(\"_set_" + name + "\",true);" );
288             ps.println( "\t\t\t\t" + parameterType.typeSpec().printWriteStatement( "attr_" + name, "_os" ) );
289             ps.println( "\t\t\t\t((org.jacorb.orb.Delegate)_get_delegate()).invoke(this, _os, ami_handler);" );
290             ps.println( "\t\t\t\treturn;" );
291             ps.println( "\t\t\t}" );
292             ps.println( "\t\t\tcatch( org.omg.CORBA.portable.RemarshalException _rx ){}" );
293             ps.println( "\t\t\tcatch( org.omg.CORBA.portable.ApplicationException _ax )" );
294             ps.println( "\t\t\t{" );
295             ps.println( "\t\t\t\tString _id = _ax.getId();" );
296             ps.println( "\t\t\t\tthrow new RuntimeException(\"Unexpected exception \" + _id );" );
297             ps.println( "\t\t\t}" );
298             ps.println( "\t\t}" );
299             ps.println( "\t}\n" );
300         }
301     }
302
303     public void printDelegatedMethod( PrintWriter JavaDoc ps )
304     {
305         ps.print( "\tpublic " );
306         if( isGetter() )
307         {
308             ps.print( resultType.toString() );
309             ps.println( " " + name + "()" );
310             ps.println( "\t{" );
311             ps.println( "\t\treturn _delegate." + name + "();" );
312             ps.println( "\t}\n" );
313         }
314         else
315         {
316             /** modifier */
317
318             ps.print( "void " + name + "(" + parameterType.toString() );
319             ps.println( " a)" );
320             ps.println( "\t{" );
321             ps.println( "\t\t_delegate." + name + "(a);" );
322             ps.println( "\t}\n" );
323         }
324     }
325
326     public void printInvocation( PrintWriter JavaDoc ps )
327     {
328
329         ps.println( "\t\t\t_out = handler.createReply();" );
330         ps.print( "\t\t\t" );
331
332         if( isGetter() )
333         {
334             ps.println( resultType.typeSpec().printWriteStatement( name + "()", "_out" ) );
335         }
336         else
337         {
338             ps.println( name + "(" + parameterType.printReadExpression( "_input" ) + ");" );
339         }
340     }
341
342     public void accept( IDLTreeVisitor visitor )
343     {
344         visitor.visitMethod( this );
345     }
346
347
348
349 }
350
Popular Tags