KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.PrintWriter JavaDoc;
24 import java.util.*;
25
26 /**
27  * @author Gerald Brose
28  * @version $Id: TypeSpec.java,v 1.23 2004/05/06 12:39:59 nicolas Exp $
29  */

30
31
32 public class TypeSpec
33     extends IdlSymbol
34 {
35     protected String JavaDoc alias = null;
36     public TypeSpec type_spec;
37
38     public TypeSpec( int num )
39     {
40         super( num );
41     }
42
43     public Object JavaDoc clone()
44     {
45         TypeSpec ts = new TypeSpec( new_num() );
46         ts.type_spec = (TypeSpec)type_spec.clone();
47         return ts;
48     }
49
50     /**
51      * @deprecated use either getJavaTypeName() or getIDLTypeName()
52      */

53
54
55     public String JavaDoc typeName()
56     {
57         return type_spec.typeName();
58     }
59
60     /**
61      * get this types's mapped Java name
62      */

63
64     public String JavaDoc getJavaTypeName()
65     {
66         return typeName();
67     }
68
69     /**
70      * get this symbol's IDL type name
71      */

72
73     public String JavaDoc getIDLTypeName()
74     {
75         return typeName();
76     }
77
78
79     public TypeSpec typeSpec()
80     {
81         return type_spec.typeSpec();
82     }
83
84     public void setPackage( String JavaDoc s )
85     {
86         s = parser.pack_replace( s );
87         type_spec.setPackage( s );
88     }
89
90     public void setEnclosingSymbol( IdlSymbol s )
91     {
92         if( enclosing_symbol != null && enclosing_symbol != s )
93             throw new RuntimeException JavaDoc( "Compiler Error: trying to reassign container for " + name );
94         enclosing_symbol = s;
95         type_spec.setEnclosingSymbol( s );
96     }
97
98     /**
99      * @return true if this is a basic type
100      */

101
102     public boolean basic()
103     {
104         if( type_spec == null )
105             logger.warn("Typespec null " + getClass().getName() );
106         return type_spec.basic();
107     }
108
109     public void set_constr( TypeDeclaration td )
110     {
111         ConstrTypeSpec c = new ConstrTypeSpec( new_num() );
112         c.c_type_spec = td;
113         type_spec = c;
114     }
115
116     public void parse()
117     {
118         type_spec.parse();
119     }
120
121     public String JavaDoc toString()
122     {
123         try
124         {
125             return type_spec.toString();
126         }
127         catch( NullPointerException JavaDoc np )
128         {
129             np.printStackTrace();
130             org.jacorb.idl.parser.fatal_error( "Compiler Error for " +
131                                                type_spec + " " + typeName(), null );
132         }
133         return null;
134     }
135
136     public String JavaDoc getTypeCodeExpression( Set knownTypes )
137     {
138         if( type_spec instanceof ConstrTypeSpec )
139             return type_spec.getTypeCodeExpression( knownTypes );
140         else
141             return getTypeCodeExpression();
142     }
143
144     /**
145      * @return a string for an expression of type TypeCode
146      * that describes this type
147      */

148     public String JavaDoc getTypeCodeExpression()
149     {
150         return type_spec.getTypeCodeExpression();
151     }
152
153     public void print( PrintWriter JavaDoc ps )
154     {
155         if( !included )
156             type_spec.print( ps );
157     }
158
159     public String JavaDoc holderName()
160     {
161         return type_spec.holderName();
162     }
163
164     /* helpers are not generated for base types, so
165        there is no equivalent method to return helper
166        names here. Such an operation is really only
167        necessary for sequence types as a sequence's
168        helper is named according to the sequence's
169        element type
170     */

171
172     public String JavaDoc printReadExpression( String JavaDoc streamname )
173     {
174         return type_spec.printReadExpression( streamname );
175     }
176
177     public String JavaDoc printReadStatement( String JavaDoc var_name, String JavaDoc streamname )
178     {
179         return var_name + "=" + printReadExpression( streamname ) + ";";
180     }
181
182     public String JavaDoc printWriteStatement( String JavaDoc var_name, String JavaDoc streamname )
183     {
184         return type_spec.printWriteStatement( var_name, streamname );
185     }
186
187     public String JavaDoc printInsertExpression()
188     {
189         return type_spec.printInsertExpression();
190     }
191
192     public String JavaDoc printExtractExpression()
193     {
194         return type_spec.printExtractExpression();
195     }
196
197     /**
198      * for use by subclasses when generating helper classes. Writes
199      * common methods for all helpers to the helper class file. Must
200      * be called after beginning the class definition itself
201      */

202
203     static void printHelperClassMethods( PrintWriter JavaDoc ps, String JavaDoc type )
204     {
205         printInsertExtractMethods( ps, type );
206
207         ps.println( "\tpublic static org.omg.CORBA.TypeCode type()" );
208         ps.println( "\t{" );
209         ps.println( "\t\treturn _type;" );
210         ps.println( "\t}" );
211     }
212
213     static void printInsertExtractMethods( PrintWriter JavaDoc ps, String JavaDoc type )
214     {
215         ps.println( "\tpublic static void insert (final org.omg.CORBA.Any any, final " + type + " s)" );
216         ps.println( "\t{" );
217         ps.println( "\t\tany.type(type());" );
218         ps.println( "\t\twrite( any.create_output_stream(),s);" );
219         ps.println( "\t}\n" );
220
221         ps.println( "\tpublic static " + type + " extract (final org.omg.CORBA.Any any)" );
222         ps.println( "\t{" );
223         ps.println( "\t\treturn read(any.create_input_stream());" );
224         ps.println( "\t}\n" );
225     }
226
227
228
229 }
230
Popular Tags