KickJava   Java API By Example, From Geeks To Geeks.

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


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: Interface.java,v 1.60 2004/05/06 12:39:58 nicolas Exp $
26  */

27
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31 import java.util.*;
32
33 public class Interface
34     extends TypeDeclaration
35     implements Scope
36 {
37     public InterfaceBody body = null;
38     public SymbolList inheritanceSpec = null;
39
40     private String JavaDoc[] ids = null;
41     private boolean is_local = false;
42     private boolean is_abstract = false;
43     private ScopeData scopeData;
44
45     private ReplyHandler replyHandler = null;
46
47     /** IR information that would otherwise be lost */
48     private Hashtable irInfoTable = new Hashtable();
49
50     /** <code>abstractInterfaces</code> is to keep a record of those interfaces
51      * that are abstract so any inheriting interface know what to inherit from.
52      */

53     protected static HashSet abstractInterfaces;
54
55
56     public Interface(int num)
57     {
58         super(num);
59         pack_name = "";
60     }
61
62     public void setScopeData(ScopeData data)
63     {
64         scopeData = data;
65     }
66
67     public ScopeData getScopeData()
68     {
69         return scopeData;
70     }
71
72     public void setPackage(String JavaDoc s)
73     {
74         if (logger.isDebugEnabled())
75             logger.debug("Interface setPackage " + s);
76
77         s = parser.pack_replace(s);
78
79         if (pack_name.length() > 0)
80             pack_name = s + "." + pack_name;
81         else
82             pack_name = s;
83
84         if (body != null) // could've been a forward declaration)
85
body.setPackage(s); // a new scope!
86

87         if (inheritanceSpec != null)
88             inheritanceSpec.setPackage(s);
89     }
90
91     public void set_abstract()
92     {
93         is_abstract = true;
94     }
95
96
97     /** override methods from superclass TypeDeclaration */
98
99     public TypeDeclaration declaration()
100     {
101         return this;
102     };
103
104     public String JavaDoc typeName()
105     {
106         return full_name();
107     }
108
109     public Object JavaDoc clone()
110     {
111         throw new RuntimeException JavaDoc("Don't clone me, i am an interface!");
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
119         enclosing_symbol = s;
120
121         if (inheritanceSpec != null)
122             inheritanceSpec.setEnclosingSymbol(s);
123     }
124
125     /**
126      * set by the parser after creating this object depending
127      * on the presence of the "local" modifier.
128      */

129
130     public void set_locality(boolean local)
131     {
132         this.is_local = local;
133     }
134
135
136     /**
137      * <code>getTypeCodeExpression</code> produces a string for an expression
138      * of type TypeCode that describes this type.
139      * @return a string value.
140      */

141
142     public String JavaDoc getTypeCodeExpression()
143     {
144         if (is_abstract)
145         {
146             return
147                 (
148                  "org.omg.CORBA.ORB.init().create_abstract_interface_tc(\"" +
149                  id() +
150                  "\", \"" +
151                  name +
152                  "\")"
153                  );
154         }
155         else
156         {
157             return
158                 (
159                  "org.omg.CORBA.ORB.init().create_interface_tc(\"" +
160                  id() +
161                  "\", \"" +
162                  name +
163                  "\")"
164                  );
165         }
166     }
167
168     public String JavaDoc getTypeCodeExpression(Set knownTypes)
169     {
170         if (knownTypes.contains(this))
171         {
172             return this.getRecursiveTypeCodeExpression();
173         }
174         else
175         {
176             return this.getTypeCodeExpression();
177         }
178     }
179
180
181     public boolean basic()
182     {
183         return true;
184     }
185
186
187     public String JavaDoc holderName()
188     {
189         return toString() + "Holder";
190     }
191
192     public String JavaDoc toString()
193     {
194         String JavaDoc n = typeName();
195
196         if (!n.startsWith("org.omg"))
197         {
198             return omgPrefix() + n;
199         }
200         else
201             return n;
202     }
203
204     public void set_included(boolean i)
205     {
206         included = i;
207     }
208
209     public String JavaDoc printReadExpression(String JavaDoc Streamname)
210     {
211         return javaName() + "Helper.read(" + Streamname + ")";
212     }
213
214     public String JavaDoc printWriteStatement(String JavaDoc var_name, String JavaDoc Streamname)
215     {
216         return javaName() + "Helper.write(" + Streamname + "," + var_name + ");";
217     }
218
219     public void parse()
220     {
221         boolean justAnotherOne = false;
222
223         escapeName();
224
225         ConstrTypeSpec ctspec = new ConstrTypeSpec(new_num());
226
227         if (is_abstract)
228         {
229             if (logger.isDebugEnabled())
230             {
231                 logger.debug
232                     ("Adding " + full_name() + " to abstract interface list");
233             }
234
235             if (abstractInterfaces == null)
236             {
237                 abstractInterfaces = new HashSet();
238             }
239
240             abstractInterfaces.add(full_name());
241         }
242
243         try
244         {
245             ScopedName.definePseudoScope(full_name());
246             ctspec.c_type_spec = this;
247
248             if (is_pseudo)
249                 NameTable.define(full_name(), "pseudo interface");
250             else
251                 NameTable.define(full_name(), "interface");
252
253             TypeMap.typedef(full_name(), ctspec);
254         }
255         catch (IllegalRedefinition ill)
256         {
257             parser.fatal_error("Illegal Redefinition of " +
258                                ill.oldDef + " in nested scope as " + ill.newDef, token);
259         }
260         catch (NameAlreadyDefined nad)
261         {
262             // if we get here, there is already a type spec for this interface
263
// in the global type table for a forward declaration of this
264
// interface. We must replace that table entry with this type spec
265
// unless this is yet another forward declaration
266

267             if (parser.get_pending (full_name()) != null)
268             {
269                 if (body == null)
270                 {
271                     justAnotherOne = true;
272                 }
273
274                 // else actual definition
275

276                 if (!full_name().equals("org.omg.CORBA.TypeCode") && body != null)
277                 {
278                     TypeMap.replaceForwardDeclaration(full_name(), ctspec);
279                 }
280             }
281             else
282             {
283                 // this is another forward declaration, ignore
284
}
285         }
286
287         if (body != null)
288         {
289             if (inheritanceSpec != null && inheritanceSpec.v.size() > 0)
290             {
291                 if (logger.isDebugEnabled())
292                     logger.debug("Checking inheritanceSpec of " + full_name());
293
294                 Hashtable h = new Hashtable();
295
296                 for (Enumeration e = inheritanceSpec.v.elements(); e.hasMoreElements();)
297                 {
298                     ScopedName name = (ScopedName) e.nextElement();
299
300                     TypeSpec resolvedTSpec = name.resolvedTypeSpec();
301
302                     // unwind any typedef's interface names
303
while (resolvedTSpec instanceof AliasTypeSpec )
304                     {
305                         resolvedTSpec =
306                             ((AliasTypeSpec)resolvedTSpec).originalType();
307                     }
308
309                     if (! (resolvedTSpec instanceof ConstrTypeSpec))
310                     {
311                         if (logger.isDebugEnabled())
312                         {
313                             logger.debug("Illegal inheritance spec, not a constr. type but " +
314                                          resolvedTSpec.getClass() + ", name " + name );
315                         }
316                         parser.fatal_error("Illegal inheritance spec (not a constr. type): " +
317                                            inheritanceSpec, token);
318                     }
319
320                     ConstrTypeSpec ts = (ConstrTypeSpec) resolvedTSpec;
321
322                     if (ts.declaration() instanceof Interface)
323                     {
324                         if (h.containsKey(ts.full_name()))
325                         {
326                             parser.fatal_error("Illegal inheritance spec: " +
327                                                inheritanceSpec +
328                                                " (repeated inheritance not allowed).",
329                                                token);
330                         }
331
332                         // else:
333
h.put(ts.full_name(), "");
334
335                         continue;
336                     }
337
338                     // else:
339
parser.fatal_error("Illegal inheritance spec: " +
340                                        inheritanceSpec + " (ancestor " +
341                                        ts.full_name() + " not an interface)",
342                                        token);
343                 }
344
345                 body.set_ancestors(inheritanceSpec);
346             }
347
348             body.parse();
349             NameTable.parsed_interfaces.put(full_name(), "");
350
351             if (parser.generate_ami_callback)
352             {
353                 replyHandler = new ReplyHandler (this);
354                 replyHandler.parse();
355             }
356
357         }
358         else if (!justAnotherOne)
359         {
360             // i am forward declared, must set myself as
361
// pending further parsing
362
parser.set_pending(full_name());
363         }
364     }
365
366
367     InterfaceBody getBody()
368     {
369         if (parser.get_pending(full_name()) != null)
370         {
371             parser.fatal_error(full_name() +
372                                " is forward declared and still pending!",
373                                token);
374         }
375         else if (body == null)
376         {
377             if (((Interface) ((ConstrTypeSpec) TypeMap.map(full_name())).c_type_spec) != this)
378                 body = ((Interface) ((ConstrTypeSpec) TypeMap.map(full_name())).c_type_spec).getBody();
379
380             if (body == null)
381                 parser.fatal_error(full_name() + " still has an empty body!", token);
382         }
383
384         return body;
385     }
386
387     /**
388      * Open a PrintWriter to write to the .java file for typeName.
389      * @return null, if the output file already exists and is more
390      * recent than the input IDL file.
391      */

392
393     protected PrintWriter JavaDoc openOutput(String JavaDoc typeName)
394     {
395         String JavaDoc path =
396             parser.out_dir + fileSeparator + pack_name.replace('.', fileSeparator);
397         File JavaDoc dir = new File JavaDoc(path);
398
399         if (!dir.exists())
400         {
401             if (!dir.mkdirs())
402             {
403                 org.jacorb.idl.parser.fatal_error("Unable to create " + path, null);
404             }
405         }
406
407         try
408         {
409             File JavaDoc f = new File JavaDoc(dir, typeName + ".java");
410             if (GlobalInputStream.isMoreRecentThan(f))
411             {
412                 PrintWriter JavaDoc ps = new PrintWriter JavaDoc(new java.io.FileWriter JavaDoc(f));
413                 return ps;
414             }
415             else
416             {
417                 // no need to open file for printing, existing file is more
418
// recent than IDL file.
419

420                 return null;
421             }
422         }
423         catch (IOException JavaDoc e)
424         {
425             throw new RuntimeException JavaDoc ("Could not open output file for "
426                                         + typeName + " (" + e + ")");
427         }
428     }
429
430     protected void printPackage (PrintWriter JavaDoc ps)
431     {
432         if (Environment.JAVA14 && pack_name.equals(""))
433             lexer.emit_warn
434                 ("No package defined for " + name + " - illegal in JDK1.4", token);
435
436         if (!pack_name.equals (""))
437             ps.println ("package " + pack_name + ";\n");
438     }
439
440     protected void printClassComment(String JavaDoc className, PrintWriter JavaDoc ps)
441     {
442         ps.println("/**");
443         ps.println(" *\tGenerated from IDL interface " +
444                    "\"" + className + "\"");
445         ps.println(" *\t@author JacORB IDL compiler V " + parser.compiler_version);
446         ps.println(" */\n");
447     }
448
449     /**
450      * If this interface inherits from classes in the unnamed package,
451      * generate explicit import statements for them.
452      */

453     protected void printSuperclassImports(PrintWriter JavaDoc ps)
454     {
455         if (inheritanceSpec.v.size() > 0)
456         {
457             Enumeration e = inheritanceSpec.v.elements();
458
459             for (; e.hasMoreElements();)
460             {
461                 ScopedName sn = (ScopedName) e.nextElement();
462
463                 if (sn.resolvedName().indexOf('.') < 0)
464                 {
465                     ps.println("import " + sn + ";");
466                 }
467             }
468         }
469     }
470
471     /**
472      * generate the signature interface
473      */

474
475     protected void printInterface()
476     {
477         PrintWriter JavaDoc ps = openOutput(name);
478         if (ps == null)
479         {
480             return;
481         }
482
483         printPackage(ps);
484         printClassComment(name, ps);
485         printSuperclassImports(ps);
486
487         if (is_pseudo)
488         {
489             ps.println("public abstract class " + name);
490
491             if (inheritanceSpec.v.size() > 0)
492             {
493                 StringBuffer JavaDoc pseudo_bases = new StringBuffer JavaDoc();
494                 StringBuffer JavaDoc regular_bases = new StringBuffer JavaDoc();
495                 String JavaDoc comma = " ";
496
497                 for (Enumeration e = inheritanceSpec.v.elements(); e.hasMoreElements();)
498                 {
499                     ScopedName sn = ((ScopedName) e.nextElement());
500                     String JavaDoc name = sn.resolvedName();
501
502                     if (sn.is_pseudo())
503                     {
504                         pseudo_bases.append(comma + name);
505                     }
506                     else
507                     {
508                         regular_bases.append(comma + name);
509                     }
510
511                     if (inheritanceSpec.v.size() > 1)
512                         comma = ",";
513                 }
514
515                 if (pseudo_bases.length() > 0)
516                     ps.println("\textends " + pseudo_bases.toString());
517
518                 if (regular_bases.length() > 0)
519                     ps.println("\timplements " + regular_bases.toString());
520
521             }
522         }
523         else
524         {
525             ps.println("public interface " + name);
526
527             if (is_abstract)
528             {
529                 ps.print("\textends org.omg.CORBA.portable.IDLEntity");
530             }
531             else
532             {
533                 ps.print("\textends " + name + "Operations");
534
535                 if (is_local)
536                 {
537                     // Looking at RTF work it
538
// seems a new interface 'LocalInterface' will be used for this purpose.
539

540                     ps.print(", org.omg.CORBA.LocalInterface, org.omg.CORBA.portable.IDLEntity");
541                 }
542                 else
543                 {
544                     ps.print(", org.omg.CORBA.Object, org.omg.CORBA.portable.IDLEntity");
545                 }
546             }
547
548             if (inheritanceSpec.v.size() > 0)
549             {
550                 Enumeration e = inheritanceSpec.v.elements();
551
552                 while (e.hasMoreElements())
553                 {
554                     ps.print(", " + (ScopedName) e.nextElement());
555                 }
556             }
557         }
558
559         ps.println("\n{");
560
561         // body can be null for forward declaration
562
if (body != null)
563         {
564             body.printInterfaceMethods(ps);
565
566             // for an abstract interface, the generated abstract class contains
567
// the operation signatures since there is no separate signature
568
// interface
569
if (is_abstract)
570             {
571                 body.printConstants(ps);
572                 body.printOperationSignatures(ps);
573             }
574         }
575
576         ps.println("}");
577         ps.close();
578     }
579
580     /**
581      * generate the operations Java interface (not for pseudo interfaces)
582      */

583
584     protected void printOperations()
585     {
586         PrintWriter JavaDoc ps = openOutput(name + "Operations");
587         if (ps == null)
588         {
589             return;
590         }
591
592         printPackage(ps);
593         printClassComment(name, ps);
594         printSuperclassImports(ps);
595         printImport(ps);
596
597         ps.println("public interface " + name + "Operations");
598
599         if (inheritanceSpec.v.size() > 0)
600         {
601             ps.print("\textends ");
602             Enumeration e = inheritanceSpec.v.elements();
603
604             do
605             {
606                 ScopedName sne = (ScopedName) e.nextElement();
607
608                 // See description of abstractInterfaces for logic here.
609
if (abstractInterfaces != null &&
610                     abstractInterfaces.contains(sne.toString()))
611                 {
612                     ps.print(sne);
613                 }
614                 else
615                 {
616                     ps.print(sne + "Operations");
617                 }
618
619                 if (e.hasMoreElements())
620                 {
621                     ps.print(" , ");
622                 }
623             }
624             while (e.hasMoreElements());
625
626             ps.print("\n");
627         }
628
629         ps.println("{");
630
631         if (body != null)
632         {
633             // forward declaration
634
body.printConstants(ps);
635             body.printOperationSignatures(ps);
636         }
637
638         ps.println("}");
639         ps.close();
640     }
641
642
643     /**
644      * Print the holder class for the interface.
645      */

646     protected void printHolder()
647     {
648         PrintWriter JavaDoc ps = openOutput(name + "Holder");
649         if (ps == null)
650         {
651             return;
652         }
653
654         printPackage(ps);
655         printClassComment(name, ps);
656
657         ps.print("public" + parser.getFinalString() + " class " + name + "Holder");
658         ps.print("\timplements org.omg.CORBA.portable.Streamable");
659
660         ps.println("{");
661         ps.println("\t public " + name + " value;");
662
663         ps.println("\tpublic " + name + "Holder()");
664         ps.println("\t{");
665         ps.println("\t}");
666
667         ps.println("\tpublic " + name + "Holder (final " + name + " initial)");
668         ps.println("\t{");
669         ps.println("\t\tvalue = initial;");
670         ps.println("\t}");
671
672         ps.println("\tpublic org.omg.CORBA.TypeCode _type()");
673         ps.println("\t{");
674         ps.println("\t\treturn " + name + "Helper.type();");
675         ps.println("\t}");
676
677         ps.println("\tpublic void _read (final org.omg.CORBA.portable.InputStream in)");
678         ps.println("\t{");
679         ps.println("\t\tvalue = " + name + "Helper.read (in);");
680         ps.println("\t}");
681
682         ps.println("\tpublic void _write (final org.omg.CORBA.portable.OutputStream _out)");
683         ps.println("\t{");
684         ps.println("\t\t" + name + "Helper.write (_out,value);");
685         ps.println("\t}");
686
687         ps.println("}");
688         ps.close();
689     }
690
691
692     /**
693      * Generate the helper class for an interface
694      */

695     protected void printHelper()
696     {
697         PrintWriter JavaDoc ps = openOutput(name + "Helper");
698         if (ps == null)
699         {
700             return;
701         }
702
703         printPackage(ps);
704         printImport(ps);
705
706         printClassComment(name, ps);
707
708         ps.println("public" + parser.getFinalString() + " class " + name + "Helper");
709         ps.println("{");
710
711         // Generate insert (handle either CORBA.Object or Serializable case)
712
ps.println("\tpublic static void insert (final org.omg.CORBA.Any any, final " + typeName() + " s)");
713         ps.println("\t{");
714
715         if (is_abstract)
716         {
717             ps.println("\t\tif (s instanceof org.omg.CORBA.Object)");
718             ps.println("\t\t{");
719             ps.println("\t\t\tany.insert_Object((org.omg.CORBA.Object)s);");
720             ps.println("\t\t}");
721             ps.println("\t\telse if (s instanceof java.io.Serializable)");
722             ps.println("\t\t{");
723             ps.println("\t\t\tany.insert_Value((java.io.Serializable)s);");
724             ps.println("\t\t}");
725             ps.println("\t\telse");
726             ps.println("\t\t{");
727             ps.println("\t\t\tthrow new org.omg.CORBA.BAD_PARAM(\"Failed to insert in helper\");");
728             ps.println("\t\t}");
729         }
730         else
731         {
732             ps.println("\t\t\tany.insert_Object(s);");
733         }
734
735         ps.println("\t}");
736
737         // Generate extract
738
ps.println("\tpublic static " + typeName() + " extract(final org.omg.CORBA.Any any)");
739         ps.println("\t{");
740
741         if (is_abstract)
742         {
743             ps.println("\t\ttry");
744             ps.println("\t\t{");
745             ps.println("\t\t\treturn narrow(any.extract_Object());");
746             ps.println("\t\t}");
747             ps.println("\t\tcatch (org.omg.CORBA.BAD_OPERATION ex)");
748             ps.println("\t\t{");
749             ps.println("\t\t\ttry");
750             ps.println("\t\t\t{");
751             ps.println("\t\t\t\treturn (" + typeName() + ")any.extract_Value();");
752             ps.println("\t\t\t}");
753             ps.println("\t\t\tcatch (ClassCastException e)");
754             ps.println("\t\t\t{");
755             ps.println("\t\t\t\tthrow new org.omg.CORBA.MARSHAL(e.getMessage());");
756             ps.println("\t\t\t}");
757             ps.println("\t\t}");
758         }
759         else
760         {
761             if( parser.useUncheckedNarrow )
762             {
763                 ps.println( "\t\treturn unchecked_narrow(any.extract_Object());");
764             }
765             else
766             {
767                 ps.println("\t\treturn narrow(any.extract_Object()) ;");
768             }
769         }
770
771         ps.println("\t}");
772
773         // Generate the typecode
774
ps.println("\tpublic static org.omg.CORBA.TypeCode type()");
775         ps.println("\t{");
776         ps.println("\t\treturn " + getTypeCodeExpression() + ";");
777         ps.println("\t}");
778
779         printIdMethod(ps);
780
781         // Generate the read
782
ps.println("\tpublic static " + name + " read(final org.omg.CORBA.portable.InputStream in)");
783         ps.println("\t{");
784
785         if (is_local)
786         {
787             ps.println("\t\tthrow new org.omg.CORBA.MARSHAL();");
788         }
789         else
790         {
791             if (is_abstract)
792             {
793                 ps.println("\t\treturn narrow(((org.omg.CORBA_2_3.portable.InputStream)in).read_abstract_interface());");
794             }
795             else
796             {
797                 if( parser.useUncheckedNarrow )
798                 {
799                     ps.println( "\t\treturn unchecked_narrow(in.read_Object());" );
800                 }
801                 else
802                 {
803                     ps.println("\t\treturn narrow(in.read_Object());");
804                 }
805             }
806         }
807
808         ps.println("\t}");
809
810         // Generate the write
811
ps.println("\tpublic static void write(final org.omg.CORBA.portable.OutputStream _out, final " + typeName() + " s)");
812         ps.println("\t{");
813
814         if (is_local)
815         {
816             ps.println("\t\tthrow new org.omg.CORBA.MARSHAL();");
817         }
818         else
819         {
820             if (is_abstract)
821             {
822                 ps.println("\t\t((org.omg.CORBA_2_3.portable.OutputStream)_out).write_abstract_interface(s);");
823             }
824             else
825             {
826                 ps.println("\t\t_out.write_Object(s);");
827             }
828         }
829
830         ps.println("\t}");
831
832         // Generate narrow - only used by abstract_interfaces.
833
ps.println("\tpublic static " + typeName() + " narrow(final java.lang.Object obj)");
834         ps.println("\t{");
835         ps.println("\t\tif (obj instanceof " + typeName() + ')');
836         ps.println("\t\t{");
837         ps.println("\t\t\treturn (" + typeName() + ")obj;");
838         ps.println("\t\t}");
839         ps.println("\t\telse if (obj instanceof org.omg.CORBA.Object)");
840         ps.println("\t\t{");
841         ps.println("\t\t\treturn narrow((org.omg.CORBA.Object)obj);");
842         ps.println("\t\t}");
843         ps.println("\t\tthrow new org.omg.CORBA.BAD_PARAM(\"Failed to narrow in helper\");");
844         ps.println("\t}");
845
846         // Generate narrow
847

848         ps.println("\tpublic static " + typeName() + " narrow(final org.omg.CORBA.Object obj)");
849         ps.println("\t{");
850         ps.println("\t\tif (obj == null)");
851         ps.println("\t\t\treturn null;");
852
853         if (parser.generate_stubs && !is_local)
854         {
855             ps.println("\t\ttry");
856             ps.println("\t\t{");
857             ps.println("\t\t\treturn (" + typeName() + ")obj;");
858             ps.println("\t\t}");
859             ps.println("\t\tcatch (ClassCastException c)");
860             ps.println("\t\t{");
861             ps.println("\t\t\tif (obj._is_a(\"" + id() + "\"))");
862             ps.println("\t\t\t{");
863
864             String JavaDoc stub_name = typeName();
865
866             if (stub_name.indexOf('.') > -1)
867             {
868                 stub_name = stub_name.substring(0, typeName().lastIndexOf('.')) +
869                     "._" + stub_name.substring(stub_name.lastIndexOf('.') + 1) + "Stub";
870             }
871             else
872                 stub_name = "_" + stub_name + "Stub";
873
874             ps.println("\t\t\t\t" + stub_name + " stub;");
875
876             ps.println("\t\t\t\tstub = new " + stub_name + "();");
877
878             ps.println("\t\t\t\tstub._set_delegate(((org.omg.CORBA.portable.ObjectImpl)obj)._get_delegate());");
879
880             ps.println("\t\t\t\treturn stub;");
881
882             ps.println("\t\t\t}");
883
884             ps.println("\t\t}");
885
886             ps.println("\t\tthrow new org.omg.CORBA.BAD_PARAM(\"Narrow failed\");");
887         }
888         else
889         {
890             ps.println("\t\tif (obj instanceof " + typeName() + ")");
891             ps.println("\t\t\treturn (" + typeName() + ")obj;");
892             ps.println("\t\telse");
893             ps.println("\t\tthrow new org.omg.CORBA.BAD_PARAM(\"Narrow failed, not a " + typeName() + "\");");
894         }
895
896         ps.println("\t}");
897
898         // Generate the unchecked_narrow
899
ps.println("\tpublic static " + typeName() + " unchecked_narrow(final org.omg.CORBA.Object obj)");
900         ps.println("\t{");
901         ps.println("\t\tif (obj == null)");
902         ps.println("\t\t\treturn null;");
903
904         if (parser.generate_stubs && ! is_local)
905         {
906             ps.println("\t\ttry");
907             ps.println("\t\t{");
908             ps.println("\t\t\treturn (" + typeName() + ")obj;");
909             ps.println("\t\t}");
910             ps.println("\t\tcatch (ClassCastException c)");
911             ps.println("\t\t{");
912
913             String JavaDoc stub_name = typeName();
914
915             if (stub_name.indexOf('.') > -1)
916             {
917                 stub_name = stub_name.substring(0, typeName().lastIndexOf('.')) +
918                     "._" + stub_name.substring(stub_name.lastIndexOf('.') + 1) + "Stub";
919             }
920             else
921                 stub_name = "_" + stub_name + "Stub";
922
923             ps.println("\t\t\t\t" + stub_name + " stub;");
924
925             ps.println("\t\t\t\tstub = new " + stub_name + "();");
926
927             ps.println("\t\t\t\tstub._set_delegate(((org.omg.CORBA.portable.ObjectImpl)obj)._get_delegate());");
928
929             ps.println("\t\t\t\treturn stub;");
930
931             ps.println("\t\t}");
932         }
933         else
934         {
935             ps.println("\t\tif (obj instanceof " + typeName() + ")");
936             ps.println("\t\t\treturn (" + typeName() + ")obj;");
937             ps.println("\t\telse");
938             ps.println("\t\tthrow new org.omg.CORBA.BAD_PARAM(\"unchecked_narrow failed, not a " + typeName() + "\");");
939         }
940
941         ps.println("\t}");
942
943         ps.println("}");
944         ps.close();
945     }
946
947     public String JavaDoc[] get_ids()
948     {
949         if (ids == null)
950         {
951             Set base_ids = new HashSet();
952
953             if (inheritanceSpec != null && inheritanceSpec.v.size() > 0)
954             {
955                 for (Iterator i = inheritanceSpec.v.iterator(); i.hasNext();)
956                 {
957                     TypeSpec ts = ((ScopedName) i.next()).resolvedTypeSpec();
958
959                     if (ts instanceof ConstrTypeSpec)
960                     {
961                         Interface base = (Interface) ((ConstrTypeSpec) ts).c_type_spec;
962                         base_ids.addAll (Arrays.asList (base.get_ids()));
963                     }
964                     else if (ts instanceof ReplyHandlerTypeSpec)
965                     {
966                         base_ids.add("IDL:omg.org/Messaging/ReplyHandler:1.0");
967                     }
968                 }
969             }
970
971             ids = new String JavaDoc[ base_ids.size() + 1 ];
972             ids[ 0 ] = id();
973             int i = 1;
974
975             for (Iterator j = base_ids.iterator(); j.hasNext(); i++)
976             {
977                 ids[ i ] = (String JavaDoc) j.next();
978             }
979         }
980
981         return ids;
982     }
983
984     /**
985      * Generates a stub class for this Interface
986      */

987
988     protected void printStub()
989     {
990         PrintWriter JavaDoc ps = openOutput("_" + name + "Stub");
991         if (ps == null)
992         {
993             return;
994         }
995
996         printPackage(ps);
997         printImport(ps);
998         printClassComment(name, ps);
999
1000        ps.println("public class _" + name + "Stub");
1001        ps.println("\textends org.omg.CORBA.portable.ObjectImpl");
1002
1003        ps.println("\timplements " + javaName());
1004        ps.println("{");
1005
1006        ps.print("\tprivate String[] ids = {");
1007        String JavaDoc[] ids = get_ids();
1008        for (int i = 0; i < ids.length - 1; i++)
1009            ps.print("\"" + ids[ i ] + "\",");
1010
1011        ps.println("\"" + ids[ ids.length - 1 ] + "\"};");
1012        ps.println("\tpublic String[] _ids()");
1013        ps.println("\t{");
1014        ps.println("\t\treturn ids;");
1015        ps.println("\t}\n");
1016
1017        if (!parser.cldc10)
1018        {
1019            ps.print("\tpublic final static java.lang.Class _opsClass = ");
1020            if (!pack_name.equals(""))
1021            {
1022                ps.print(pack_name + ".");
1023            }
1024            if (is_abstract)
1025            {
1026                ps.println(name + ".class;");
1027            }
1028            else
1029            {
1030                ps.println(name + "Operations.class;");
1031            }
1032        }
1033        else
1034        {
1035            // code supplied byte Nokia for CLDC10 compatibility
1036
// avoids use of the static .class variable.
1037
String JavaDoc fullName = null;
1038            if(!pack_name.equals(""))
1039            {
1040                fullName = pack_name + "." + name;
1041            }
1042            else
1043            {
1044                fullName = name;
1045            }
1046
1047            ps.println( "\tpublic static final java.lang.Class _opsClass;" );
1048            ps.println("\tstatic");
1049            ps.println("\t{");
1050            ps.println("\t\ttry"); //try
1051
ps.println("\t\t{"); //{
1052

1053            ps.print( "\t\t\t_opsClass = Class.forName(\"" );
1054            if( !pack_name.equals( "" ) ) ps.print( pack_name + "." );
1055            ps.println( name + "Operations\");" );
1056
1057            ps.println("\t\t}"); //}
1058
ps.println("\t\tcatch(ClassNotFoundException cnfe)");
1059            ps.println("\t\t{"); //{
1060
ps.println("\t\t\tthrow new RuntimeException(\"Class " + fullName + " was not found.\");");
1061            ps.println("\t\t}"); //}
1062
ps.println("\t}\n");
1063        }
1064
1065        body.printStubMethods(ps, name, is_local, is_abstract);
1066
1067        ps.println("}");
1068        ps.close();
1069    }
1070
1071    protected void printImplSkeleton()
1072    {
1073        PrintWriter JavaDoc ps = openOutput(name + "POA");
1074        if (ps == null)
1075        {
1076            return;
1077        }
1078
1079        printPackage(ps);
1080        printClassComment(name, ps);
1081
1082        printImport(ps);
1083
1084        ps.print("public abstract class " + name + "POA");
1085        ps.println("\n\textends org.omg.PortableServer.Servant");
1086        ps.println("\timplements org.omg.CORBA.portable.InvokeHandler, " + javaName() + "Operations");
1087        ps.println("{");
1088
1089        body.printOperationsHash(ps);
1090
1091        ps.print("\tprivate String[] ids = {");
1092        String JavaDoc[] ids = get_ids();
1093
1094        for (int i = 0; i < ids.length - 1; i++)
1095            ps.print("\"" + ids[ i ] + "\",");
1096
1097        ps.println("\"" + ids[ ids.length - 1 ] + "\"};");
1098
1099
1100        ps.println("\tpublic " + javaName() + " _this()");
1101
1102        ps.println("\t{");
1103
1104        ps.println("\t\treturn " + javaName() + "Helper.narrow(_this_object());");
1105
1106        ps.println("\t}");
1107
1108        ps.println("\tpublic " + javaName() + " _this(org.omg.CORBA.ORB orb)");
1109
1110        ps.println("\t{");
1111
1112        ps.println("\t\treturn " + javaName() + "Helper.narrow(_this_object(orb));");
1113
1114        ps.println("\t}");
1115
1116        ps.println("\tpublic org.omg.CORBA.portable.OutputStream _invoke(String method, org.omg.CORBA.portable.InputStream _input, org.omg.CORBA.portable.ResponseHandler handler)");
1117
1118        ps.println("\t\tthrows org.omg.CORBA.SystemException");
1119
1120        ps.println("\t{");
1121
1122        ps.println("\t\torg.omg.CORBA.portable.OutputStream _out = null;");
1123
1124        ps.println("\t\t// do something");
1125
1126        body.printSkelInvocations(ps);
1127
1128        ps.println("\t}\n");
1129
1130        ps.println("\tpublic String[] _all_interfaces(org.omg.PortableServer.POA poa, byte[] obj_id)");
1131
1132        ps.println("\t{");
1133
1134        ps.println("\t\treturn ids;");
1135
1136        ps.println("\t}");
1137
1138        ps.println("}");
1139
1140        ps.close();
1141    }
1142
1143    /**
1144     * print the stream-based skeleton class
1145     */

1146
1147    protected void printTieSkeleton()
1148    {
1149        PrintWriter JavaDoc ps = openOutput(name + "POATie");
1150        if (ps == null)
1151        {
1152            return;
1153        }
1154
1155        printPackage(ps);
1156        ps.println("import org.omg.PortableServer.POA;");
1157        printImport(ps);
1158
1159        printClassComment(name, ps);
1160
1161        ps.println("public class " + name + "POATie");
1162        ps.println("\textends " + name + "POA");
1163        ps.println("{");
1164
1165        ps.println("\tprivate " + name + "Operations _delegate;\n");
1166        ps.println("\tprivate POA _poa;");
1167
1168        ps.println("\tpublic " + name + "POATie(" + name + "Operations delegate)");
1169        ps.println("\t{");
1170        ps.println("\t\t_delegate = delegate;");
1171        ps.println("\t}");
1172
1173        ps.println("\tpublic " + name + "POATie(" + name + "Operations delegate, POA poa)");
1174        ps.println("\t{");
1175        ps.println("\t\t_delegate = delegate;");
1176        ps.println("\t\t_poa = poa;");
1177        ps.println("\t}");
1178
1179        ps.println("\tpublic " + javaName() + " _this()");
1180        ps.println("\t{");
1181        ps.println("\t\treturn " + javaName() + "Helper.narrow(_this_object());");
1182        ps.println("\t}");
1183
1184        ps.println("\tpublic " + javaName() + " _this(org.omg.CORBA.ORB orb)");
1185        ps.println("\t{");
1186        ps.println("\t\treturn " + javaName() + "Helper.narrow(_this_object(orb));");
1187        ps.println("\t}");
1188
1189        ps.println("\tpublic " + name + "Operations _delegate()");
1190        ps.println("\t{");
1191        ps.println("\t\treturn _delegate;");
1192        ps.println("\t}");
1193
1194        ps.println("\tpublic void _delegate(" + name + "Operations delegate)");
1195        ps.println("\t{");
1196        ps.println("\t\t_delegate = delegate;");
1197        ps.println("\t}");
1198
1199        ps.println("\tpublic POA _default_POA()");
1200        ps.println("\t{");
1201        ps.println("\t\tif (_poa != null)");
1202        ps.println("\t\t{");
1203        ps.println("\t\t\treturn _poa;");
1204        ps.println("\t\t}");
1205        ps.println("\t\telse");
1206        ps.println("\t\t{");
1207        ps.println("\t\t\treturn super._default_POA();");
1208        ps.println("\t\t}");
1209        ps.println("\t}");
1210
1211        body.printDelegatedMethods(ps);
1212        ps.println("}");
1213        ps.close();
1214    }
1215
1216    protected void printIRHelper()
1217    {
1218        PrintWriter JavaDoc ps = openOutput(name + "IRHelper");
1219        if (ps == null)
1220        {
1221            return;
1222        }
1223
1224        printPackage(ps);
1225        ps.println("\n/**");
1226        ps.println(" * This class contains generated Interface Repository information.");
1227        ps.println(" * @author JacORB IDL compiler.");
1228        ps.println(" */");
1229
1230        ps.println("\npublic class " + name + "IRHelper");
1231        ps.println("{");
1232
1233        String JavaDoc HASHTABLE = System.getProperty ("java.version").startsWith ("1.1")
1234            ? "com.sun.java.util.collections.Hashtable"
1235            : "java.util.Hashtable";
1236
1237        ps.println("\tpublic static " + HASHTABLE
1238                   + " irInfo = new " + HASHTABLE + "();");
1239        ps.println("\tstatic");
1240        ps.println("\t{");
1241        body.getIRInfo(irInfoTable);
1242
1243        for (Enumeration e = irInfoTable.keys(); e.hasMoreElements();)
1244        {
1245            String JavaDoc key = (String JavaDoc) e.nextElement();
1246            ps.println("\t\tirInfo.put(\"" + key + "\", \"" + (String JavaDoc) irInfoTable.get(key) + "\");");
1247        }
1248
1249        ps.println("\t}");
1250        ps.println("}");
1251        ps.close();
1252    }
1253
1254    protected void printLocalBase()
1255    {
1256        PrintWriter JavaDoc ps = openOutput("_" + name + "LocalBase");
1257        if (ps == null)
1258        {
1259            return;
1260        }
1261
1262        printPackage(ps);
1263        ps.println("\n/**");
1264        ps.println(" * Abstract base class for implementations of local interface " + name);
1265        ps.println(" * @author JacORB IDL compiler.");
1266        ps.println(" */");
1267
1268        ps.println("\npublic abstract class _" + name + "LocalBase");
1269        ps.println("\textends org.omg.CORBA.LocalObject");
1270        ps.println("\timplements " + name);
1271        ps.println("{");
1272        ps.print("\tprivate String[] _type_ids = {");
1273        String JavaDoc[] ids = get_ids();
1274
1275        for (int i = 0; i < ids.length - 1; i++)
1276            ps.print("\"" + ids[ i ] + "\",");
1277
1278        ps.println("\"" + ids[ ids.length - 1 ] + "\"};");
1279
1280        ps.print("\tpublic String[] _ids()");
1281
1282        ps.println("\t{");
1283
1284        ps.println("\t\treturn(String[])_type_ids.clone();");
1285
1286        ps.println("\t}");
1287
1288        ps.println("}");
1289
1290        ps.close();
1291    }
1292
1293
1294    protected void printLocalTie()
1295    {
1296        PrintWriter JavaDoc ps = openOutput(name + "LocalTie");
1297        if (ps == null)
1298        {
1299            return;
1300        }
1301
1302        printPackage(ps);
1303        ps.println("import org.omg.PortableServer.POA;");
1304        printImport(ps);
1305
1306        printClassComment(name, ps);
1307
1308        ps.println("public class " + name + "LocalTie");
1309        ps.println("\textends _" + name + "LocalBase");
1310        ps.println("{");
1311
1312        ps.println("\tprivate " + name + "Operations _delegate;\n");
1313        ps.println("\tprivate POA _poa;");
1314
1315        ps.println("\tpublic " + name + "LocalTie(" + name + "Operations delegate)");
1316        ps.println("\t{");
1317        ps.println("\t\t_delegate = delegate;");
1318        ps.println("\t}");
1319
1320        ps.println("\tpublic " + name + "Operations _delegate()");
1321        ps.println("\t{");
1322        ps.println("\t\treturn _delegate;");
1323        ps.println("\t}");
1324
1325        ps.println("\tpublic void _delegate(" + name + "Operations delegate)");
1326        ps.println("\t{");
1327        ps.println("\t\t_delegate = delegate;");
1328        ps.println("\t}");
1329
1330        body.printDelegatedMethods(ps);
1331        ps.println("}");
1332        ps.close();
1333    }
1334
1335
1336
1337    public void print(PrintWriter JavaDoc _ps)
1338    {
1339        if (included && !generateIncluded())
1340            return ;
1341
1342        // divert output into individual .java files
1343
if (body != null) // forward declaration
1344
{
1345            printInterface();
1346
1347            if (!is_pseudo)
1348            {
1349                if (!is_abstract)
1350                {
1351                    printOperations();
1352                }
1353
1354                printHelper();
1355                printHolder();
1356
1357                if (parser.generate_stubs && !is_local)
1358                {
1359                    printStub();
1360                }
1361
1362                if (parser.generate_skeletons && !is_local && !is_abstract)
1363                {
1364                    printImplSkeleton();
1365                    printTieSkeleton();
1366                }
1367
1368                if (parser.generateIR)
1369                {
1370                    printIRHelper();
1371                }
1372
1373                if (is_local)
1374                {
1375                    printLocalBase();
1376                    printLocalTie();
1377                }
1378            }
1379
1380            // print class files for interface local definitions
1381
body.print(null);
1382
1383            if (replyHandler != null)
1384                replyHandler.print (_ps);
1385        }
1386    }
1387
1388    public void accept(IDLTreeVisitor visitor)
1389    {
1390        visitor.visitInterface(this);
1391    }
1392}
1393
Popular Tags