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-2000 (C) Intalio, Inc. All Rights Reserved.
042: */package org.exolab.javasource;
043:
044: /**
045: * A class which holds information about a field. Modelled closely after the
046: * Java Reflection API. This class is part of package which is used to create
047: * source code in memory.
048: *
049: * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
050: * @version $Revision: 6674 $ $Date: 2005-02-26 17:30:28 -0700 (Sat, 26 Feb 2005) $
051: */
052: public final class JField extends JAnnotatedElementHelper implements
053: JMember {
054: //--------------------------------------------------------------------------
055:
056: /** The set of modifiers for this JField. */
057: private JModifiers _modifiers = null;
058:
059: /** Type of this field. */
060: private final JType _type;
061:
062: /** Name of the field. */
063: private String _name = null;
064:
065: /** JavaDoc for this field. */
066: private JDocComment _comment = null;
067:
068: /** Initialization string for this field. */
069: private String _initString = null;
070:
071: /** Indicates whether this field is of type date/time. */
072: private boolean _isDateTime = false;
073:
074: /** The Class in this JField has been declared. */
075: private JClass _declaringClass = null;
076:
077: //--------------------------------------------------------------------------
078: /**
079: * Creates a new JField.
080: *
081: * @param type JType of this new field.
082: * @param name Name of this new field.
083: */
084: public JField(final JType type, final String name) {
085: setName(name);
086:
087: _type = type;
088: _modifiers = new JModifiers();
089: _modifiers.makePrivate();
090: _comment = new JDocComment();
091: _comment.appendComment("Field " + name + ".");
092: }
093:
094: //--------------------------------------------------------------------------
095:
096: /**
097: * Returns the JavaDoc comment describing this member.
098: *
099: * @return The JavaDoc comment describing this member, or null if no comment
100: * has been set.
101: */
102: public JDocComment getComment() {
103: return _comment;
104: }
105:
106: /**
107: * Returns the class in which this JField has been declared.
108: *
109: * @return The class in which this JField has been declared.
110: */
111: public JClass getDeclaringClass() {
112: return _declaringClass;
113: }
114:
115: /**
116: * Returns the initialization String for this JField.
117: *
118: * @return The initialization String for this JField, or null if no
119: * initialization String was specified.
120: */
121: public String getInitString() {
122: return _initString;
123: }
124:
125: /**
126: * Returns the modifiers for this JField.
127: *
128: * @return The modifiers for this JField.
129: */
130: public JModifiers getModifiers() {
131: return _modifiers;
132: }
133:
134: /**
135: * Returns the name of this JField.
136: *
137: * @return The name of this JField.
138: */
139: public String getName() {
140: return _name;
141: }
142:
143: /**
144: * Returns the JType representing the type of this JField.
145: *
146: * @return The JType representing the type of this JField.
147: */
148: public JType getType() {
149: return _type;
150: }
151:
152: /**
153: * Sets the JavaDoc comment describing this JField.
154: *
155: * @param comment The JavaDoc comment for this JField.
156: */
157: public void setComment(final JDocComment comment) {
158: _comment = comment;
159: }
160:
161: /**
162: * Sets the JavaDoc comment describing this JField.
163: *
164: * @param comment The JavaDoc comment for this JField.
165: */
166: public void setComment(final String comment) {
167: if (_comment == null) {
168: _comment = new JDocComment();
169: }
170: _comment.setComment(comment);
171: }
172:
173: /**
174: * Sets the initialization string for this JField. This allows some
175: * flexibility in declaring default values.
176: *
177: * @param init The initialization string for this member.
178: */
179: public void setInitString(final String init) {
180: _initString = init;
181: }
182:
183: /**
184: * Sets the name of this JField.
185: *
186: * @param name The name of this JField.
187: */
188: public void setName(final String name) {
189: if (!JNaming.isValidJavaIdentifier(name)) {
190: String err = "'" + name + "' is ";
191: if (JNaming.isKeyword(name)) {
192: err += "a reserved word and may not be used as "
193: + " a field name.";
194: } else {
195: err += "not a valid Java identifier.";
196: }
197: throw new IllegalArgumentException(err);
198: }
199: _name = name;
200: }
201:
202: /**
203: * Sets the access modifiers on this JField.
204: *
205: * @param modifiers The access modifiers to be used for this JField.
206: */
207: public void setModifiers(final JModifiers modifiers) {
208: _modifiers = modifiers;
209: }
210:
211: /**
212: * Sets the class that declares this JField.
213: *
214: * @param declaringClass The class in which this Jfield is declared.
215: */
216: protected void setDeclaringClass(final JClass declaringClass) {
217: _declaringClass = declaringClass;
218: }
219:
220: /**
221: * Indicates whether this JField instance represents a field of type date/time.
222: *
223: * @return True if this field is of type date/time.
224: */
225: public boolean isDateTime() {
226: return _isDateTime;
227: }
228:
229: /**
230: * To indicate whether this JField instance represents a field of type date/time.
231: *
232: * @param isDateTime True if this field is of type date/time.
233: */
234: public void setDateTime(final boolean isDateTime) {
235: _isDateTime = isDateTime;
236: }
237:
238: //--------------------------------------------------------------------------
239:
240: /**
241: * {@inheritDoc}
242: */
243: public String toString() {
244: StringBuffer sb = new StringBuffer();
245: sb.append(_modifiers.toString());
246: sb.append(' ');
247: sb.append(_type);
248: sb.append(' ');
249: sb.append(_name);
250: return sb.toString();
251: }
252:
253: //--------------------------------------------------------------------------
254: }
|