KickJava   Java API By Example, From Geeks To Geeks.

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


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

22
23 import java.io.File JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25
26 /**
27  * @author Gerald Brose
28  * @version $Id: AliasTypeSpec.java,v 1.47 2004/05/06 12:39:58 nicolas Exp $
29  */

30
31 public class AliasTypeSpec
32     extends TypeSpec
33 {
34     /** the type for which this is an alias */
35     public TypeSpec originalType;
36     private boolean written;
37     private boolean originalTypeWasScopedName = false;
38
39     /**
40      * Class constructor,
41      * @param ts - the TypeSpec for which to create a new alias
42      */

43
44     public AliasTypeSpec(TypeSpec ts )
45     {
46         super(IdlSymbol.new_num());
47         originalType = ts;
48     }
49
50     public Object JavaDoc clone()
51     {
52         AliasTypeSpec alias =
53             new AliasTypeSpec((TypeSpec)type_spec.clone());
54         alias.name = name;
55         alias.pack_name = pack_name;
56         return alias;
57     }
58
59     public String JavaDoc full_name()
60     {
61         if (pack_name.length() > 0)
62         {
63             String JavaDoc s =
64                 ScopedName.unPseudoName(pack_name + "." + name);
65
66             if (!s.startsWith("org.omg"))
67             {
68                 return omg_package_prefix + s;
69             }
70             else
71                 return s;
72         }
73         else
74             return ScopedName.unPseudoName(name);
75     }
76
77     /**
78      * @return the type name of this alias, which is the name of the
79      * original type
80      */

81
82     public String JavaDoc typeName()
83     {
84         return originalType.typeName();
85     }
86
87     public TypeSpec typeSpec()
88     {
89         return this;
90     }
91
92     /**
93      * @return the original type for which this is an alias
94      */

95
96     public TypeSpec originalType()
97     {
98         if (originalType instanceof AliasTypeSpec)
99         {
100             return (((AliasTypeSpec)originalType).originalType ());
101         }
102         return originalType;
103     }
104
105     public void setPackage(String JavaDoc s)
106     {
107         if (pack_name.length() > 0)
108             pack_name = s + "." + pack_name;
109         else
110             pack_name = s;
111         pack_name = parser.pack_replace(pack_name);
112     }
113
114     public void setEnclosingSymbol(IdlSymbol s)
115     {
116         if (enclosing_symbol != null && enclosing_symbol != s)
117             throw new RuntimeException JavaDoc("Compiler Error: trying to reassign container for " + name);
118         enclosing_symbol = s;
119     }
120
121     /**
122      * @return true if this is a basic type
123      */

124
125     public boolean basic()
126     {
127         return false;
128     }
129
130     /**
131      * Perform the parsing phase, must be called before code
132      * generation
133      */

134
135     public void parse()
136     {
137         if (originalType instanceof TemplateTypeSpec)
138         {
139             ((TemplateTypeSpec)originalType).markTypeDefd();
140         }
141
142         if (originalType instanceof ConstrTypeSpec ||
143             originalType instanceof FixedPointType ||
144             originalType instanceof SequenceType ||
145             originalType instanceof ArrayTypeSpec)
146         {
147             originalType.parse();
148             if (originalType.typeName().indexOf('.') < 0)
149             {
150                 String JavaDoc tName = null;
151                 if (originalType instanceof VectorType)
152                 {
153                     tName =
154                         originalType.typeName().substring(0,
155                               originalType.typeName().indexOf('['));
156                 }
157                 else
158                 {
159                     tName = originalType.typeName();
160                 }
161
162                 addImportedName(tName);
163             }
164         }
165
166         if (originalType instanceof ScopedName)
167         {
168             if (logger.isDebugEnabled())
169                 logger.debug(" Alias " + name +
170                              " has scoped name orig Type : " +
171                              ((ScopedName)originalType).toString());
172
173             originalType = ((ScopedName)originalType).resolvedTypeSpec();
174             originalTypeWasScopedName = true;
175
176             if (originalType instanceof AliasTypeSpec)
177                 addImportedAlias(originalType.full_name());
178             else
179                 addImportedName(originalType.typeName());
180         }
181     }
182
183
184
185     public String JavaDoc toString()
186     {
187         return originalType.toString();
188     }
189
190
191     /**
192      * @return a string for an expression of type TypeCode that describes this type
193      * Note that this is the TypeSpec for the alias type and is not unwound to
194      * the original type.
195      */

196
197     public String JavaDoc getTypeCodeExpression()
198     {
199         return full_name() + "Helper.type()";
200     }
201
202     public String JavaDoc className()
203     {
204         String JavaDoc fullName = full_name();
205         String JavaDoc cName;
206         if (fullName.indexOf('.') > 0)
207         {
208             pack_name = fullName.substring(0, fullName.lastIndexOf('.'));
209             cName = fullName.substring(fullName.lastIndexOf('.') + 1);
210         }
211         else
212         {
213             pack_name = "";
214             cName = fullName;
215         }
216         return cName;
217
218     }
219
220     /**
221      * Code generation, generate holder and helper classes. Holder classes
222      * are only generated for array and sequence types.
223      */

224
225     public void print(PrintWriter JavaDoc ps)
226     {
227         setPrintPhaseNames();
228
229         // no code generation for included definitions
230
if (included && !generateIncluded())
231             return;
232
233         if (!written)
234         {
235             // guard against recursive entries, which can happen due to
236
// containments, e.g., an alias within an interface that refers
237
// back to the interface
238
written = true;
239
240             try
241             {
242                 if (!(originalType.typeSpec() instanceof StringType) &&
243                     !(originalType.typeSpec() instanceof SequenceType) &&
244                     ! originalTypeWasScopedName &&
245                     !(originalType instanceof ConstrTypeSpec &&
246                        ((ConstrTypeSpec)originalType).declaration() instanceof Interface )
247                    )
248                 {
249                     // only print local type definitions, not just
250
// scoped names (references to other defs), which would
251
// lead to loops!
252
originalType.print(ps);
253                 }
254
255                 String JavaDoc className = className();
256
257                 String JavaDoc path =
258                     parser.out_dir + fileSeparator +
259                     pack_name.replace('.', fileSeparator);
260
261                 File JavaDoc dir = new File JavaDoc(path);
262                 if (!dir.exists())
263                 {
264                     if (!dir.mkdirs())
265                     {
266                         org.jacorb.idl.parser.fatal_error("Unable to create " + path,
267                                                            null);
268                     }
269                 }
270
271                 String JavaDoc fname = null;
272                 PrintWriter JavaDoc decl_ps = null;
273
274                 if ( originalType instanceof TemplateTypeSpec
275                       && !(originalType instanceof StringType))
276                 {
277                     // print the holder class
278

279                     fname = className + "Holder.java";
280                     File JavaDoc f = new File JavaDoc(dir, fname);
281
282                     if (GlobalInputStream.isMoreRecentThan(f))
283                     {
284                         decl_ps = new PrintWriter JavaDoc(new java.io.FileWriter JavaDoc(f));
285                         printHolderClass(className, decl_ps);
286                         decl_ps.close();
287                     }
288                 }
289
290                 fname = className + "Helper.java";
291                 File JavaDoc f = new File JavaDoc(dir, fname);
292
293                 if (GlobalInputStream.isMoreRecentThan(f))
294                 {
295                     // print the helper class
296
decl_ps = new PrintWriter JavaDoc(new java.io.FileWriter JavaDoc(f));
297                     printHelperClass(className, decl_ps);
298                     decl_ps.close();
299                 }
300             }
301             catch(java.io.IOException JavaDoc i)
302             {
303                 throw new RuntimeException JavaDoc("File IO error" + i);
304             }
305         }
306     }
307
308     public String JavaDoc printReadStatement(String JavaDoc varname, String JavaDoc streamname)
309     {
310         if (doUnwind())
311         {
312             return originalType.printReadStatement(varname, streamname);
313         }
314         else
315         {
316             return varname + " = " + full_name() + "Helper.read(" + streamname + ");";
317         }
318     }
319
320     public String JavaDoc printReadExpression(String JavaDoc streamname)
321     {
322         if (doUnwind())
323         {
324             return originalType.printReadExpression(streamname);
325         }
326         else
327         {
328             return full_name() + "Helper.read(" + streamname + ")";
329         }
330     }
331
332     public String JavaDoc printWriteStatement(String JavaDoc var_name, String JavaDoc streamname)
333     {
334         if (doUnwind())
335         {
336             return originalType.printWriteStatement(var_name, streamname);
337         }
338         else
339         {
340             return full_name() + "Helper.write(" + streamname + "," + var_name + ");";
341         }
342     }
343
344     private void printClassComment(String JavaDoc className, PrintWriter JavaDoc ps)
345     {
346         ps.println("/**");
347         ps.println(" *\tGenerated from IDL definition of alias " +
348                 "\"" + className + "\"");
349         ps.println(" *\t@author JacORB IDL compiler ");
350         ps.println(" */\n");
351     }
352
353     /**
354      * @return true iff the original type is such that the alias should
355      * be unwound to it, either anothetr alias, a constructed type (e.g a struct),
356      * an any, a basic type (long, short, etc.)
357      */

358
359     private boolean doUnwind()
360     {
361         return
362             (
363              originalType.basic() &&
364              (
365               !(originalType instanceof TemplateTypeSpec)
366               || originalType instanceof StringType
367              )
368             )
369         || originalType instanceof AliasTypeSpec
370         || originalType instanceof ConstrTypeSpec
371         || originalType instanceof AnyType
372         ;
373     }
374
375
376     public String JavaDoc holderName()
377     {
378         if (doUnwind())
379         {
380             return originalType.holderName();
381         }
382         else
383         {
384             return full_name() + "Holder";
385         }
386     }
387
388     /**
389      * generates the holder class for this alias type
390      */

391
392     private void printHolderClass(String JavaDoc className, PrintWriter JavaDoc ps)
393     {
394         if (Environment.JAVA14 && pack_name.equals(""))
395             lexer.emit_warn
396                 ("No package defined for " + className + " - illegal in JDK1.4", token);
397         if (!pack_name.equals(""))
398             ps.println("package " + pack_name + ";");
399
400         printImport(ps);
401
402         printClassComment(className, ps);
403
404         ps.println("public" + parser.getFinalString() + " class " + className + "Holder");
405         ps.println("\timplements org.omg.CORBA.portable.Streamable");
406         ps.println("{");
407
408         ps.println("\tpublic " + originalType.typeName() + " value;\n");
409
410         ps.println("\tpublic " + className + "Holder ()");
411         ps.println("\t{");
412         ps.println("\t}");
413
414         ps.println("\tpublic " + className + "Holder (final " + originalType.typeName() + " initial)");
415         ps.println("\t{");
416         ps.println("\t\tvalue = initial;");
417         ps.println("\t}");
418
419         ps.println("\tpublic org.omg.CORBA.TypeCode _type ()");
420         ps.println("\t{");
421         ps.println("\t\treturn " + className + "Helper.type ();");
422         ps.println("\t}");
423
424         ps.println("\tpublic void _read (final org.omg.CORBA.portable.InputStream in)");
425         ps.println("\t{");
426         ps.println("\t\tvalue = " + className + "Helper.read (in);");
427         ps.println("\t}");
428
429         ps.println("\tpublic void _write (final org.omg.CORBA.portable.OutputStream out)");
430         ps.println("\t{");
431         ps.println("\t\t" + className + "Helper.write (out,value);");
432         ps.println("\t}");
433
434         ps.println("}");
435     }
436
437     /**
438      * generates the holder class for this alias type
439      */

440
441     private void printHelperClass(String JavaDoc className, PrintWriter JavaDoc ps)
442     {
443         if (Environment.JAVA14 && pack_name.equals(""))
444             lexer.emit_warn
445                 ("No package defined for " + className + " - illegal in JDK1.4", token);
446         if (!pack_name.equals(""))
447             ps.println("package " + pack_name + ";");
448
449         printImport(ps);
450
451         printClassComment(className, ps);
452
453         ps.println("public" + parser.getFinalString() + " class " +
454                     className + "Helper");
455         ps.println("{");
456
457         ps.println("\tprivate static org.omg.CORBA.TypeCode _type = null;\n");
458         String JavaDoc type = originalType.typeName();
459
460         ps.println("\tpublic static void insert (org.omg.CORBA.Any any, " +
461                     type + " s)");
462         ps.println("\t{");
463         ps.println("\t\tany.type (type ());");
464         ps.println("\t\twrite (any.create_output_stream (), s);");
465         ps.println("\t}\n");
466
467         ps.println("\tpublic static " + type + " extract (final org.omg.CORBA.Any any)");
468         ps.println("\t{");
469         ps.println("\t\treturn read (any.create_input_stream ());");
470         ps.println("\t}\n");
471
472         ps.println("\tpublic static org.omg.CORBA.TypeCode type ()");
473         ps.println("\t{");
474         ps.println("\t\tif (_type == null)");
475         ps.println("\t\t{");
476
477         ps.println("\t\t\t_type = org.omg.CORBA.ORB.init().create_alias_tc(" +
478                     full_name() + "Helper.id(), \"" + name + "\"," +
479                     originalType.typeSpec().getTypeCodeExpression() + ");");
480
481         ps.println("\t\t}");
482         ps.println("\t\treturn _type;");
483         ps.println("\t}\n");
484
485         printIdMethod(ps); // inherited from IdlSymbol
486

487         /* read */
488         ps.println("\tpublic static " + type +
489                     " read (final org.omg.CORBA.portable.InputStream _in)");
490         ps.println("\t{");
491         ps.println("\t\t" + type + " _result;");
492         ps.println("\t\t" + originalType.printReadStatement("_result", "_in"));
493         ps.println("\t\treturn _result;");
494         ps.println("\t}\n");
495
496         /* write */
497         ps.println("\tpublic static void write (final org.omg.CORBA.portable.OutputStream _out, " + type + " _s)");
498         ps.println("\t{");
499         ps.println("\t\t" + originalType.printWriteStatement("_s", "_out"));
500         ps.println("\t}");
501         ps.println("}");
502     }
503
504     public void accept(IDLTreeVisitor visitor)
505     {
506         visitor.visitAlias(this);
507     }
508
509
510
511 }
512
Popular Tags