KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > java > gen > BaseClass


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.java.gen;
31
32 import com.caucho.bytecode.JMethod;
33 import com.caucho.java.JavaWriter;
34 import com.caucho.util.L10N;
35
36 import java.io.IOException JavaDoc;
37 import java.util.ArrayList JavaDoc;
38
39 /**
40  * Basic class generation.
41  */

42 public class BaseClass extends ClassComponent {
43   private static final L10N L = new L10N(BaseClass.class);
44
45   private String JavaDoc _className;
46   private String JavaDoc _superClassName;
47
48   private boolean _isStatic;
49   private String JavaDoc _visibility = "public";
50   
51   private ArrayList JavaDoc<String JavaDoc> _interfaceNames =
52     new ArrayList JavaDoc<String JavaDoc>();
53
54   private ArrayList JavaDoc<ClassComponent> _components =
55     new ArrayList JavaDoc<ClassComponent>();
56
57   private DependencyComponent _dependencyComponent;
58
59   /**
60    * Creates the base class
61    */

62   public BaseClass()
63   {
64   }
65
66   /**
67    * Creates the base class
68    */

69   public BaseClass(String JavaDoc className)
70   {
71     _className = className;
72   }
73
74   /**
75    * Creates the base class
76    */

77   public BaseClass(String JavaDoc className, String JavaDoc superClassName)
78   {
79     _className = className;
80     _superClassName = superClassName;
81   }
82
83   /**
84    * Sets the class name.
85    */

86   public void setClassName(String JavaDoc className)
87   {
88     _className = className;
89   }
90
91   /**
92    * Gets the class name.
93    */

94   public String JavaDoc getClassName()
95   {
96     return _className;
97   }
98
99   /**
100    * Sets the superclass name.
101    */

102   public void setSuperClassName(String JavaDoc superClassName)
103   {
104     _superClassName = superClassName;
105   }
106
107   /**
108    * Adds an interface.
109    */

110   public void addInterfaceName(String JavaDoc name)
111   {
112     _interfaceNames.add(name);
113   }
114
115   /**
116    * Sets the class static property.
117    */

118   public void setStatic(boolean isStatic)
119   {
120     _isStatic = isStatic;
121   }
122
123   /**
124    * Sets the class visibility property.
125    */

126   public void setVisibility(String JavaDoc visibility)
127   {
128     _visibility = visibility;
129   }
130
131   /**
132    * Adds a method
133    */

134   public void addMethod(BaseMethod method)
135   {
136     addComponent(method);
137   }
138
139   /**
140    * Creates the dependency component.
141    */

142   public DependencyComponent addDependencyComponent()
143   {
144     if (_dependencyComponent == null) {
145       _dependencyComponent = new DependencyComponent();
146       addComponent(_dependencyComponent);
147     }
148
149     return _dependencyComponent;
150   }
151
152   /**
153    * Finds a method
154    */

155   public BaseMethod findMethod(JMethod method)
156   {
157     for (ClassComponent component : _components) {
158       if (component instanceof BaseMethod) {
159     BaseMethod baseMethod = (BaseMethod) component;
160     
161     if (baseMethod.getMethod().equals(method))
162       return baseMethod;
163       }
164     }
165
166     return null;
167   }
168
169   /**
170    * Creates a method
171    */

172   public BaseMethod createMethod(JMethod method)
173   {
174     BaseMethod baseMethod = findMethod(method);
175
176     if (baseMethod == null) {
177       baseMethod = new BaseMethod(method, method);
178
179       _components.add(baseMethod);
180     }
181
182     return baseMethod;
183   }
184
185   /**
186    * Adds a class component.
187    */

188   public void addComponent(ClassComponent component)
189   {
190     _components.add(component);
191   }
192   
193   /**
194    * Generates the code for the class.
195    *
196    * @param out the writer to the output stream.
197    */

198   public void generate(JavaWriter out)
199     throws IOException JavaDoc
200   {
201     if (_visibility != null && ! _visibility.equals(""))
202       out.print(_visibility + " ");
203
204     if (_isStatic)
205       out.print("static ");
206
207     out.print("class " + _className);
208     
209     if (_superClassName != null)
210       out.print(" extends " + _superClassName);
211
212     if (_interfaceNames.size() > 0) {
213       out.print(" implements ");
214       
215       for (int i = 0; i < _interfaceNames.size(); i++) {
216     if (i != 0)
217       out.print(", ");
218
219     out.print(_interfaceNames.get(i));
220       }
221     }
222
223     out.println(" {");
224     out.pushDepth();
225
226     generateClassContent(out);
227     
228     out.popDepth();
229     out.println("}");
230   }
231
232   /**
233    * Generates the class content.
234    */

235   protected void generateClassContent(JavaWriter out)
236     throws IOException JavaDoc
237   {
238     generateComponents(out);
239   }
240
241   /**
242    * Generates the class components.
243    */

244   protected void generateComponents(JavaWriter out)
245     throws IOException JavaDoc
246   {
247     for (int i = 0; i < _components.size(); i++) {
248       if (i != 0)
249     out.println();
250
251       _components.get(i).generate(out);
252     }
253   }
254 }
255
Popular Tags