001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 1999-2002 (C) Intalio, Inc. All Rights Reserved.
042: */package org.exolab.javasource;
043:
044: import java.util.Enumeration;
045:
046: /**
047: * A representation of the Java Source code for a Java Class. This is a useful
048: * utility when creating in memory source code. This package was modelled after
049: * the Java Reflection API as much as possible to reduce the learning curve.
050: *
051: * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
052: * @author <a href="mailto:skopp AT riege DOT de">Martin Skopp</a>
053: * @version $Revision: 6807 $ $Date: 2005-05-08 12:32:06 -0600 (Sun, 08 May 2005) $
054: */
055: public class JClass extends AbstractJClass {
056: //--------------------------------------------------------------------------
057:
058: /** The superclass for this JClass. */
059: private JTypeName _super Class;
060:
061: //--------------------------------------------------------------------------
062:
063: /**
064: * Creates a new JClass with the given name.
065: *
066: * @param name The name of the JClass to create.
067: */
068: public JClass(final String name) {
069: super (name);
070:
071: _super Class = null;
072: }
073:
074: //--------------------------------------------------------------------------
075:
076: /**
077: * {@inheritDoc}
078: */
079: public final void addImport(final String className) {
080: if ((className == null) || (className.length() == 0)) {
081: return;
082: }
083:
084: JTypeName jtName = new JTypeName(className);
085:
086: String cls = jtName.getLocalName();
087: String pkg = jtName.getPackageName();
088:
089: // If we are extending a class from the default package which conflicts
090: // with this import then we cannot import this class
091: if (_super Class != null
092: && _super Class.getLocalName().equals(cls)) {
093: if (_super Class.getPackageName() == null && pkg != null) {
094: return;
095: }
096: }
097:
098: addImportInternal(className);
099: }
100:
101: /**
102: * {@inheritDoc}
103: */
104: public final void addMember(final JMember jMember) {
105: if (jMember instanceof JField) {
106: addField((JField) jMember);
107: } else if (jMember instanceof JMethod) {
108: addMethod((JMethod) jMember);
109: } else {
110: String error = null;
111: if (jMember == null) {
112: error = "the argument 'jMember' must not be null.";
113: } else {
114: error = "Cannot add JMember '"
115: + jMember.getClass().getName()
116: + "' to JClass, unrecognized type.";
117: }
118: throw new IllegalArgumentException(error);
119: }
120: }
121:
122: /**
123: * Returns the super class that this class extends.
124: *
125: * @return superClass The super class that this class extends.
126: */
127: public final JTypeName getSuperClass() {
128: return _super Class;
129: }
130:
131: /**
132: * Returns the qualified name of the super class that this class extends.
133: *
134: * @return superClass The qualified name of the super class that this class extends.
135: */
136: public final String getSuperClassQualifiedName() {
137: if (_super Class == null) {
138: return null;
139: }
140: return _super Class.getQualifiedName();
141: }
142:
143: /**
144: * Sets the super Class that this class extends.
145: *
146: * @param superClass The super Class that this Class extends.
147: */
148: public final void setSuperClass(final String super Class) {
149: if (super Class == null) {
150: _super Class = null;
151: } else {
152: _super Class = new JTypeName(super Class);
153: }
154: }
155:
156: //--------------------------------------------------------------------------
157:
158: /**
159: * {@inheritDoc}
160: */
161: public final void print(final JSourceWriter jsw,
162: final boolean classOnly) {
163: if (jsw == null) {
164: throw new IllegalArgumentException(
165: "argument 'jsw' should not be null.");
166: }
167:
168: //-- print class headers (comment header, package, imports) if desired
169: if (!classOnly) {
170: printClassHeaders(jsw);
171: }
172:
173: getJDocComment().print(jsw);
174:
175: printClassDefinitionLine(jsw); // includes the opening '{'
176:
177: jsw.writeln();
178: jsw.indent();
179:
180: printMemberVariables(jsw);
181: printStaticInitializers(jsw);
182: printConstructors(jsw);
183: printMethods(jsw);
184: printInnerClasses(jsw);
185:
186: jsw.unindent();
187: jsw.writeln('}');
188: jsw.flush();
189: }
190:
191: /**
192: * Writes to the JSourceWriter the line that defines this class. This
193: * line includes the class name, extends and implements entries, and
194: * any modifiers such as "private".
195: *
196: * @param jsw The JSourceWriter to be used.
197: */
198: private void printClassDefinitionLine(final JSourceWriter jsw) {
199: StringBuffer buffer = new StringBuffer();
200:
201: //-- first print our annotations
202: getAnnotatedElementHelper().printAnnotations(jsw);
203:
204: //-- next our modifiers
205: JModifiers modifiers = getModifiers();
206: if (modifiers.isPrivate()) {
207: buffer.append("private ");
208: } else if (modifiers.isPublic()) {
209: buffer.append("public ");
210: }
211:
212: if (modifiers.isFinal()) {
213: buffer.append("final ");
214: }
215:
216: if (modifiers.isAbstract()) {
217: buffer.append("abstract ");
218: }
219:
220: //-- next our class name plus extends and implements entries
221: buffer.append("class ");
222: buffer.append(getLocalName());
223: buffer.append(' ');
224: if (_super Class != null) {
225: buffer.append("extends ");
226: buffer.append(_super Class);
227: buffer.append(' ');
228: }
229: if (getInterfaceCount() > 0) {
230: boolean endl = false;
231: if ((getInterfaceCount() > 1) || (_super Class != null)) {
232: jsw.writeln(buffer.toString());
233: buffer.setLength(0);
234: endl = true;
235: }
236: buffer.append("implements ");
237:
238: Enumeration enumeration = getInterfaces();
239: while (enumeration.hasMoreElements()) {
240: buffer.append(enumeration.nextElement());
241: if (enumeration.hasMoreElements()) {
242: buffer.append(", ");
243: }
244: }
245: if (endl) {
246: jsw.writeln(buffer.toString());
247: buffer.setLength(0);
248: } else {
249: buffer.append(' ');
250: }
251: }
252:
253: //-- and we're done
254: buffer.append('{');
255: jsw.writeln(buffer.toString());
256: buffer.setLength(0);
257: }
258:
259: /**
260: * Changes the local name of this class type.
261: * @param localName The new local name to be used.
262: */
263: public void changeLocalName(final String localName) {
264: String packageName = getPackageName();
265: if (packageName != null) {
266: setName(packageName + "." + localName);
267: } else {
268: setName(localName);
269: }
270: }
271:
272: //--------------------------------------------------------------------------
273: }
|