001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.java.gen;
031:
032: import com.caucho.bytecode.JMethod;
033: import com.caucho.java.JavaWriter;
034: import com.caucho.util.L10N;
035:
036: import java.io.IOException;
037: import java.util.ArrayList;
038: import java.lang.reflect.*;
039:
040: /**
041: * Basic class generation.
042: */
043: public class BaseClass extends ClassComponent {
044: private static final L10N L = new L10N(BaseClass.class);
045:
046: private String _className;
047: private String _super ClassName;
048:
049: private boolean _isStatic;
050: private String _visibility = "public";
051:
052: private ArrayList<String> _interfaceNames = new ArrayList<String>();
053:
054: private ArrayList<ClassComponent> _components = new ArrayList<ClassComponent>();
055:
056: private DependencyComponent _dependencyComponent;
057:
058: /**
059: * Creates the base class
060: */
061: public BaseClass() {
062: }
063:
064: /**
065: * Creates the base class
066: */
067: public BaseClass(String className) {
068: _className = className;
069: }
070:
071: /**
072: * Creates the base class
073: */
074: public BaseClass(String className, String super ClassName) {
075: _className = className;
076: _super ClassName = super ClassName;
077: }
078:
079: /**
080: * Sets the class name.
081: */
082: public void setClassName(String className) {
083: _className = className;
084: }
085:
086: /**
087: * Gets the class name.
088: */
089: public String getClassName() {
090: return _className;
091: }
092:
093: /**
094: * Sets the superclass name.
095: */
096: public void setSuperClassName(String super ClassName) {
097: _super ClassName = super ClassName;
098: }
099:
100: /**
101: * Adds an interface.
102: */
103: public void addInterfaceName(String name) {
104: if (!_interfaceNames.contains(name))
105: _interfaceNames.add(name);
106: }
107:
108: /**
109: * Sets the class static property.
110: */
111: public void setStatic(boolean isStatic) {
112: _isStatic = isStatic;
113: }
114:
115: /**
116: * Sets the class visibility property.
117: */
118: public void setVisibility(String visibility) {
119: _visibility = visibility;
120: }
121:
122: /**
123: * Adds a method
124: */
125: public void addMethod(BaseMethod method) {
126: addComponent(method);
127: }
128:
129: /**
130: * Creates the dependency component.
131: */
132: public DependencyComponent addDependencyComponent() {
133: if (_dependencyComponent == null) {
134: _dependencyComponent = new DependencyComponent();
135: addComponent(_dependencyComponent);
136: }
137:
138: return _dependencyComponent;
139: }
140:
141: /**
142: * Finds a method
143: */
144: public BaseMethod findMethod(Method method) {
145: for (ClassComponent component : _components) {
146: if (component instanceof BaseMethod) {
147: BaseMethod baseMethod = (BaseMethod) component;
148:
149: if (baseMethod.getMethod().equals(method))
150: return baseMethod;
151: }
152: }
153:
154: return null;
155: }
156:
157: /**
158: * Creates a method
159: */
160: public BaseMethod createMethod(Method method) {
161: BaseMethod baseMethod = findMethod(method);
162:
163: if (baseMethod == null) {
164: baseMethod = new BaseMethod(method, method);
165:
166: _components.add(baseMethod);
167: }
168:
169: return baseMethod;
170: }
171:
172: /**
173: * Adds a class component.
174: */
175: public void addComponent(ClassComponent component) {
176: _components.add(component);
177: }
178:
179: /**
180: * Generates the code for the class.
181: *
182: * @param out the writer to the output stream.
183: */
184: public void generate(JavaWriter out) throws IOException {
185: if (_visibility != null && !_visibility.equals(""))
186: out.print(_visibility + " ");
187:
188: if (_isStatic)
189: out.print("static ");
190:
191: out.print("class " + _className);
192:
193: if (_super ClassName != null)
194: out.print(" extends " + _super ClassName);
195:
196: if (_interfaceNames.size() > 0) {
197: out.print(" implements ");
198:
199: for (int i = 0; i < _interfaceNames.size(); i++) {
200: if (i != 0)
201: out.print(", ");
202:
203: out.print(_interfaceNames.get(i));
204: }
205: }
206:
207: out.println(" {");
208: out.pushDepth();
209:
210: generateClassContent(out);
211:
212: out.popDepth();
213: out.println("}");
214: }
215:
216: /**
217: * Generates the class content.
218: */
219: protected void generateClassContent(JavaWriter out)
220: throws IOException {
221: generateComponents(out);
222: }
223:
224: /**
225: * Generates the class components.
226: */
227: protected void generateComponents(JavaWriter out)
228: throws IOException {
229: for (int i = 0; i < _components.size(); i++) {
230: if (i != 0)
231: out.println();
232:
233: _components.get(i).generate(out);
234: }
235: }
236: }
|