KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 import java.io.File JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.util.*;
27
28 /**
29  * @author Andre Spiegel, Gerald Brose
30  * @version $Id: ValueAbsDecl.java,v 1.20 2004/05/06 12:39:59 nicolas Exp $
31  *
32  * This class is basically the same as Interface.java, but we can't extend
33  * that one because we have to extend Value, and delegating some parts and
34  * not others is a nuisance...
35  */

36
37 public class ValueAbsDecl
38     extends Value
39 {
40     ValueBody body = null;
41     ValueInheritanceSpec inheritanceSpec;
42
43     public ValueAbsDecl(int num)
44     {
45         super(num);
46         pack_name = "";
47     }
48
49     public void setPackage(String JavaDoc s)
50     {
51         s = parser.pack_replace(s);
52         if (pack_name.length() > 0)
53             pack_name = s + "." + pack_name;
54         else
55             pack_name = s;
56
57         if (body != null) // could've been a forward declaration)
58
body.setPackage(s); // a new scope!
59

60         if (inheritanceSpec != null)
61             inheritanceSpec.setPackage(s);
62     }
63
64     public void setInheritanceSpec(ValueInheritanceSpec spec)
65     {
66         inheritanceSpec = spec;
67     }
68
69     public ValueInheritanceSpec setInheritanceSpec()
70     {
71         return inheritanceSpec;
72     }
73
74     public TypeDeclaration declaration()
75     {
76         return this;
77     };
78
79     public String JavaDoc typeName()
80     {
81         return full_name();
82     }
83
84
85     public Object JavaDoc clone()
86     {
87         throw new RuntimeException JavaDoc("Don't clone me, i am an interface!");
88     }
89
90
91     public void setEnclosingSymbol(IdlSymbol s)
92     {
93         if (enclosing_symbol != null && enclosing_symbol != s)
94         {
95             logger.error("was " + enclosing_symbol.getClass().getName() +
96                                " now: " + s.getClass().getName());
97             throw new RuntimeException JavaDoc("Compiler Error: trying to reassign container for " + name);
98         }
99         enclosing_symbol = s;
100     }
101
102
103     public boolean basic()
104     {
105         return true;
106     }
107
108
109     public String JavaDoc holderName()
110     {
111         return javaName() + "Holder";
112     }
113
114     public String JavaDoc toString()
115     {
116         String JavaDoc n = typeName();
117         if (!n.startsWith("org.omg"))
118         {
119             return omgPrefix() + n;
120         }
121         else
122             return n;
123     }
124
125
126     public void set_included(boolean i)
127     {
128         included = i;
129     }
130
131
132     public void parse()
133     {
134         boolean justAnotherOne = false;
135
136         escapeName();
137
138         ConstrTypeSpec ctspec = new ConstrTypeSpec(new_num());
139         try
140         {
141             ScopedName.definePseudoScope(full_name());
142             ctspec.c_type_spec = this;
143
144             NameTable.define(full_name(), "type");
145             TypeMap.typedef(full_name(), ctspec);
146         }
147         catch (IllegalRedefinition ill)
148         {
149             parser.fatal_error("Illegal Redefinition of " +
150                                ill.oldDef + " in nested scope as " + ill.newDef, token);
151         }
152         catch (NameAlreadyDefined nad)
153         {
154             // if we get here, there is already a type spec for this interface
155
// in the global type table for a forward declaration of this
156
// interface. We must replace that table entry with this type spec
157
// if this is not yet another forwad declaration
158

159             if (body != null)
160             {
161                 justAnotherOne = true;
162             }
163
164             if (!full_name().equals("org.omg.CORBA.TypeCode") && body != null)
165             {
166                 TypeMap.replaceForwardDeclaration(full_name(), ctspec);
167             }
168         }
169
170         if (body != null)
171         {
172             if (inheritanceSpec != null && inheritanceSpec.v.size() > 0)
173             {
174                 if (logger.isDebugEnabled())
175                     logger.debug("Checking inheritanceSpec of " + full_name());
176                 for (Enumeration e = inheritanceSpec.v.elements(); e.hasMoreElements();)
177                 {
178                     ScopedName name = (ScopedName)e.nextElement();
179
180                     TypeSpec resolvedTSpec = name.resolvedTypeSpec();
181
182                     // struct checking can be turned off to tolerate strange
183
// typedefs generated by rmic
184
if (parser.strict_inheritance)
185                     {
186                         // unwind any typedef's interface names
187
while (resolvedTSpec instanceof AliasTypeSpec )
188                         {
189                             resolvedTSpec =
190                                 ((AliasTypeSpec)resolvedTSpec).originalType();
191                         }
192
193                         if (! (resolvedTSpec instanceof ConstrTypeSpec))
194                         {
195                             if (logger.isDebugEnabled())
196                             {
197                                 logger.debug("Illegal inheritance spec, not a constr. type but " +
198                                              resolvedTSpec.getClass() + ", name " + name );
199                             }
200                             parser.fatal_error("Illegal inheritance spec (not a constr. type): " +
201                                            inheritanceSpec, token);
202                         }
203
204                         ConstrTypeSpec ts = (ConstrTypeSpec)resolvedTSpec;
205                         if (!(ts.declaration() instanceof Interface) &&
206                             !(ts.declaration() instanceof ValueAbsDecl))
207                         {
208                             parser.fatal_error("Illegal inheritance spec (not an intf. or abs. value type): " +
209                                                inheritanceSpec, token);
210                         }
211                     }
212                 }
213                 body.set_ancestors(inheritanceSpec);
214             }
215             body.parse();
216             NameTable.parsed_interfaces.put(full_name(), "");
217         }
218         else if (!justAnotherOne)
219         {
220             // i am forward declared, must set myself as
221
// pending further parsing
222
parser.set_pending(full_name());
223         }
224     }
225
226
227     ValueBody getBody()
228     {
229         if (parser.get_pending(full_name()) != null)
230         {
231             parser.fatal_error(full_name() + " is forward declared and still pending!", token);
232         }
233         else if (body == null)
234         {
235             if (((ValueAbsDecl)((ConstrTypeSpec)TypeMap.map(full_name())).c_type_spec) != this)
236                 body = ((ValueAbsDecl)((ConstrTypeSpec)TypeMap.map(full_name())).c_type_spec).getBody();
237             if (body == null)
238                 parser.fatal_error(full_name() + " still has an empty body!", token);
239         }
240         return body;
241     }
242
243     public String JavaDoc getTypeCodeExpression()
244     {
245         return this.getTypeCodeExpression(new HashSet());
246     }
247
248     public String JavaDoc getTypeCodeExpression(Set knownTypes)
249     {
250         if (knownTypes.contains(this))
251         {
252             return this.getRecursiveTypeCodeExpression();
253         }
254         else
255         {
256             knownTypes.add(this);
257
258             return "org.omg.CORBA.ORB.init().create_value_tc(\"" + id() +
259                 "\", \"" + name +
260                 "\", org.omg.CORBA.VM_ABSTRACT.value " +
261                 ", null, null)";
262         }
263     }
264
265
266     public String JavaDoc printReadExpression(String JavaDoc streamname)
267     {
268         return "(" + javaName() + ")"
269             + "((org.omg.CORBA_2_3.portable.InputStream)" + streamname + ")"
270             + ".read_value (\"" + id() + "\")";
271     }
272
273     public String JavaDoc printReadStatement(String JavaDoc var_name, String JavaDoc streamname)
274     {
275         return var_name + " = " + printReadExpression(streamname);
276     }
277
278     public String JavaDoc printWriteStatement(String JavaDoc var_name, String JavaDoc streamname)
279     {
280         return "((org.omg.CORBA_2_3.portable.OutputStream)" + streamname + ")"
281             + ".write_value (" + var_name + ");";
282     }
283
284
285     private void printClassComment(String JavaDoc className, PrintWriter JavaDoc ps)
286     {
287         ps.println("/**");
288         ps.println(" *\tGenerated from IDL definition of abstract value type " +
289                    "\"" + className + "\"");
290         ps.println(" *\t@author JacORB IDL compiler ");
291         ps.println(" */\n");
292     }
293
294     /**
295      * generate the mapped class that extends ValueBase and has the
296      * operations and attributes
297      */

298
299     public void print(PrintWriter JavaDoc unused)
300     {
301         if (included && !generateIncluded())
302             return;
303
304         // divert output into class files
305
if (body != null) // forward declaration
306
{
307             try
308             {
309                 // Java Interface file
310

311                 String JavaDoc path =
312                     parser.out_dir + fileSeparator + pack_name.replace('.', fileSeparator);
313                 File JavaDoc dir = new File JavaDoc(path);
314                 if (!dir.exists())
315                 {
316                     if (!dir.mkdirs())
317                     {
318                         org.jacorb.idl.parser.fatal_error("Unable to create " + path, null);
319                     }
320                 }
321
322                 File JavaDoc f = new File JavaDoc(dir, name + ".java");
323
324                 if (GlobalInputStream.isMoreRecentThan(f))
325                 {
326                     PrintWriter JavaDoc ps = new PrintWriter JavaDoc(new java.io.FileWriter JavaDoc(f));
327
328                     if (Environment.JAVA14 && pack_name.equals(""))
329                         lexer.emit_warn
330                             ("No package defined for " + name + " - illegal in JDK1.4", token);
331                     if (!pack_name.equals(""))
332                         ps.println("package " + pack_name + ";\n");
333
334                     printClassComment(name, ps);
335
336                     // do we inherit from a class in the unnamed package?
337
// if so, we have to import this class explicitly
338

339                     if (inheritanceSpec != null && inheritanceSpec.v.size() > 0)
340                     {
341                         Enumeration e = inheritanceSpec.v.elements();
342                         for (; e.hasMoreElements();)
343                         {
344                             ScopedName sn = (ScopedName)e.nextElement();
345                             if (sn.resolvedName().indexOf('.') < 0)
346                             {
347                                 ps.println("import " + sn + "Operations;");
348                             }
349                         }
350                     }
351                     printImport(ps);
352
353                     ps.println("public interface " + name);
354                     ps.print("\textends org.omg.CORBA.portable.ValueBase ");
355
356                     if (inheritanceSpec != null && inheritanceSpec.v.size() > 0)
357                     {
358                         for (Enumeration e = inheritanceSpec.v.elements(); e.hasMoreElements();)
359                         {
360                             ps.print(", " + (ScopedName)e.nextElement());
361                         }
362                     }
363
364                     ps.println("\n{");
365                     if (body != null)
366                     {
367                         // forward declaration
368
body.printOperationSignatures(ps);
369                     }
370                     ps.println("}");
371                     ps.close();
372                 }
373             }
374             catch (java.io.IOException JavaDoc i)
375             {
376                 throw new RuntimeException JavaDoc("File IO error" + i);
377             }
378         }
379     }
380
381     public void accept(IDLTreeVisitor visitor)
382     {
383         visitor.visitValue(this);
384     }
385 }
386
Popular Tags