KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > generator > JClass


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.generator;
20
21 import java.util.Vector;
22 import java.lang.reflect.Modifier;
23
24 public class JClass extends JEntity
25 {
26     protected JPackage _pkg;
27
28     protected Vector _imports;
29     protected Vector _implements;
30     protected String _baseClassName;
31
32     protected Vector _constructors;
33     protected Vector _methods;
34     protected Vector _fields;
35
36     protected Vector _classes = new Vector();
37     protected JClass _parent;
38
39     protected JClass( String name )
40     {
41         super( name, Modifier.PUBLIC );
42
43         _imports = new Vector();
44         _implements = new Vector();
45         _baseClassName = "";
46
47         _constructors = new Vector();
48         _methods = new Vector();
49         _fields = new Vector();
50     }
51
52     protected JClass( String name, JPackage pkg )
53     {
54         this( name );
55
56         if (pkg == null)
57         {
58             pkg = new JPackage( "" );
59         }
60
61         _pkg = pkg;
62     }
63
64     protected JClass( String name, JClass parent )
65     {
66         this( name );
67
68         _parent = parent;
69     }
70
71     public JConstructor newConstructor( JParameter parms[], Class thrown[] )
72     {
73         JConstructor c = new JConstructor( parms, thrown );
74         c.setParent( this );
75         _constructors.add( c );
76         return c;
77     }
78
79     /*
80     public JConstructor newConstructor( JParameter parms[], String thrown[] )
81     {
82         JConstructor c = new JConstructor( parms, thrown );
83         c.setParent( this );
84         _constructors.add( c );
85         return c;
86     }
87     */

88
89     public void deleteConstructor( JConstructor m )
90     {
91         _constructors.removeElement( m );
92     }
93
94     public Vector getConstructors()
95     {
96         return _constructors;
97     }
98
99     public JMethod newMethod( JReturnType rt, String name, JParameter parms[], Class thrown[] )
100     {
101         JMethod m = new JMethod( rt, name, parms, thrown );
102         m.setParent( this );
103         _methods.add( m );
104         return m;
105     }
106
107     /*
108     public JMethod newMethod( JReturnType rt, String name, JParameter parms[], String thrown[] )
109     {
110         JMethod m = new JMethod( rt, name, parms, thrown );
111         m.setParent( this );
112         _methods.add( m );
113         return m;
114     }
115     */

116
117     public void deleteMethod( JMethod m )
118     {
119         _methods.removeElement( m );
120     }
121
122     public Vector getMethods()
123     {
124         return _methods;
125     }
126
127     protected void setFieldParentAndModifier( JField f )
128     {
129         f.setParent( this );
130
131         if (Modifier.isPublic( this.getModifiers() ))
132         {
133             f.setModifiers( f.getModifiers() | Modifier.PUBLIC );
134         }
135
136         if (Modifier.isProtected( this.getModifiers() ))
137         {
138             f.setModifiers( f.getModifiers() | Modifier.PROTECTED );
139         }
140
141         if (Modifier.isPrivate( this.getModifiers() ))
142         {
143             f.setModifiers( f.getModifiers() | Modifier.PRIVATE );
144         }
145     }
146
147     public JField newField( Class type, String name )
148     {
149         return newField( type, name, null );
150     }
151
152     public JField newField( Class type, String name, JExpression initExpr )
153     {
154         return newField( type, name, initExpr, false );
155     }
156
157     public JField newField( Class type, String name, JExpression initExpr, boolean isArray )
158     {
159         JField f = new JField( type, name );
160
161         setFieldParentAndModifier( f );
162         f.setInitExpression( initExpr );
163
164         _fields.add( f );
165
166         return f;
167     }
168
169     public void deleteField( JField f )
170     {
171         _fields.remove( f );
172     }
173
174     public Vector getFields()
175     {
176         return _fields;
177     }
178
179     public JClass newClass( String name )
180     {
181         JClass c = new JClass( name, this );
182         _classes.add(c);
183         return c;
184     }
185
186     public JPackage getPackage()
187     {
188         if (_parent != null)
189         {
190             return _parent.getPackage();
191         }
192         else
193         {
194             return _pkg;
195         }
196     }
197
198     public String getName()
199     {
200         if (_parent != null)
201         {
202             return _parent.getName() + "$" + super.getName();
203         }
204         else
205         {
206             return super.getName();
207         }
208     }
209
210     public void setExtends( String bcl )
211     {
212         setBaseClassName( bcl );
213     }
214
215     public String getExtends()
216     {
217         return getBaseClassName();
218     }
219
220     public void setBaseClassName( String bcl )
221     {
222         _baseClassName = bcl;
223     }
224
225     public String getBaseClassName()
226     {
227         return _baseClassName;
228     }
229
230     public void addImplements( String className )
231     {
232         _implements.add( className );
233     }
234
235     public void removeImplements( String className )
236     {
237         _implements.remove( className );
238     }
239
240     public Vector getImplements()
241     {
242         return _implements;
243     }
244
245     /*
246      * Adding Imports
247      */

248     public void addImport( Package pkg, String itemName )
249     {
250         if (pkg != null)
251         {
252             addImport( pkg.getName(), itemName );
253         }
254     }
255
256     public void addImport( Package pkg )
257     {
258         if (pkg != null)
259         {
260             addImport( pkg.getName(), "*" );
261         }
262     }
263
264     public void addImport( String name, String itemName )
265     {
266         addImport( name + "." + itemName );
267     }
268
269     public void addImport( String fqName )
270     {
271         _imports.add( fqName );
272     }
273
274     public void removeImport( Package pkg, String itemName )
275     {
276         if (pkg != null)
277         {
278             removeImport( pkg.getName(), itemName );
279         }
280     }
281
282     public void removeImport( Package pkg )
283     {
284         if (pkg != null)
285         {
286             removeImport( pkg.getName() );
287         }
288     }
289
290     public void removeImport( String name, String itemName )
291     {
292         removeImport( name + "." + itemName );
293     }
294
295     public void removeImport( String name )
296     {
297         _imports.remove( name );
298     }
299
300     public Vector getImports()
301     {
302         return _imports;
303     }
304 }
305
Popular Tags