KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > generator > JClass


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
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 implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.generator;
19
20 import java.lang.reflect.Modifier JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 public class JClass extends JEntity {
24     private JPackage pkg;
25     private Vector JavaDoc imports;
26     private Vector JavaDoc impls;
27     private String JavaDoc baseClassName;
28     private Vector JavaDoc constructors;
29     private Vector JavaDoc methods;
30     private Vector JavaDoc fields;
31     private Vector JavaDoc classes = new Vector JavaDoc();
32     private JClass parent;
33
34     protected JClass(String JavaDoc name) {
35         super(name, Modifier.PUBLIC);
36
37         imports = new Vector JavaDoc();
38         impls = new Vector JavaDoc();
39         baseClassName = "";
40
41         constructors = new Vector JavaDoc();
42         methods = new Vector JavaDoc();
43         fields = new Vector JavaDoc();
44     }
45
46     protected JClass(String JavaDoc name, JPackage pkg) {
47         this(name);
48
49         if (pkg == null) {
50             pkg = new JPackage("");
51         }
52
53         this.pkg = pkg;
54     }
55
56     protected JClass(String JavaDoc name, JClass parent) {
57         this(name);
58
59         this.parent = parent;
60     }
61
62     public JConstructor newConstructor(JParameter parms[], Class JavaDoc thrown[]) {
63         JConstructor c = new JConstructor(parms, thrown);
64         c.setParent(this);
65         constructors.add(c);
66         return c;
67     }
68
69     public void deleteConstructor(JConstructor m) {
70         constructors.removeElement(m);
71     }
72
73     public Vector JavaDoc getConstructors() {
74         return constructors;
75     }
76
77     public JMethod newMethod(JReturnType rt, String JavaDoc name, JParameter parms[], Class JavaDoc thrown[]) {
78         JMethod m = new JMethod(rt, name, parms, thrown);
79         m.setParent(this);
80         methods.add(m);
81         return m;
82     }
83
84     public void deleteMethod(JMethod m) {
85         methods.removeElement(m);
86     }
87
88     public Vector JavaDoc getMethods() {
89         return methods;
90     }
91
92     protected void setFieldParentAndModifier(JField f) {
93         f.setParent(this);
94
95         if (Modifier.isPublic(this.getModifiers())) {
96             f.setModifiers(f.getModifiers() | Modifier.PUBLIC);
97         }
98
99         if (Modifier.isProtected(this.getModifiers())) {
100             f.setModifiers(f.getModifiers() | Modifier.PROTECTED);
101         }
102
103         if (Modifier.isPrivate(this.getModifiers())) {
104             f.setModifiers(f.getModifiers() | Modifier.PRIVATE);
105         }
106     }
107
108     public JField newField(Class JavaDoc type, String JavaDoc name) {
109         return newField(type, name, null);
110     }
111
112     public JField newField(Class JavaDoc type, String JavaDoc name, JExpression initExpr) {
113         return newField(type, name, initExpr, false);
114     }
115
116     public JField newField(Class JavaDoc type, String JavaDoc name, JExpression initExpr, boolean isArray) {
117         JField f = new JField(type, name);
118
119         setFieldParentAndModifier(f);
120         f.setInitExpression(initExpr);
121
122         fields.add(f);
123
124         return f;
125     }
126
127     public void deleteField(JField f) {
128         fields.remove(f);
129     }
130
131     public Vector JavaDoc getFields() {
132         return fields;
133     }
134
135     public JClass newClass(String JavaDoc name) {
136         JClass c = new JClass(name, this);
137         classes.add(c);
138         return c;
139     }
140
141     public JPackage getPackage() {
142         if (parent != null) {
143             return parent.getPackage();
144         } else {
145             return pkg;
146         }
147     }
148
149     public String JavaDoc getName() {
150         if (parent != null) {
151             return parent.getName() + "$" + super.getName();
152         } else {
153             return super.getName();
154         }
155     }
156
157     public void setExtends(String JavaDoc bcl) {
158         setBaseClassName(bcl);
159     }
160
161     public String JavaDoc getExtends() {
162         return getBaseClassName();
163     }
164
165     public void setBaseClassName(String JavaDoc bcl) {
166         baseClassName = bcl;
167     }
168
169     public String JavaDoc getBaseClassName() {
170         return baseClassName;
171     }
172
173     public void addImplements(String JavaDoc className) {
174         impls.add(className);
175     }
176
177     public void removeImplements(String JavaDoc className) {
178         impls.remove(className);
179     }
180
181     public Vector JavaDoc getImplements() {
182         return impls;
183     }
184
185     /*
186      * Adding Imports
187      */

188     public void addImport(Package JavaDoc pkg, String JavaDoc itemName) {
189         if (pkg != null) {
190             addImport(pkg.getName(), itemName);
191         }
192     }
193
194     public void addImport(Package JavaDoc pkg) {
195         if (pkg != null) {
196             addImport(pkg.getName(), "*");
197         }
198     }
199
200     public void addImport(String JavaDoc name, String JavaDoc itemName) {
201         addImport(name + "." + itemName);
202     }
203
204     public void addImport(String JavaDoc fqName) {
205         imports.add(fqName);
206     }
207
208     public void removeImport(Package JavaDoc pkg, String JavaDoc itemName) {
209         if (pkg != null) {
210             removeImport(pkg.getName(), itemName);
211         }
212     }
213
214     public void removeImport(Package JavaDoc pkg) {
215         if (pkg != null) {
216             removeImport(pkg.getName());
217         }
218     }
219
220     public void removeImport(String JavaDoc name, String JavaDoc itemName) {
221         removeImport(name + "." + itemName);
222     }
223
224     public void removeImport(String JavaDoc name) {
225         imports.remove(name);
226     }
227
228     public Vector JavaDoc getImports() {
229         return imports;
230     }
231 }
232
Popular Tags