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: * Represents the set of modifiers for a Method or Member variable.
046: *
047: * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
048: * @version $Revision: 6669 $ $Date: 2005-02-26 17:30:28 -0700 (Sat, 26 Feb 2005) $
049: */
050: public final class JModifiers {
051: //--------------------------------------------------------------------------
052:
053: /** String that marks a method or member as being abstract. */
054: private static final String ABSTRACT = "abstract";
055:
056: /** String that marks a method or member as being final. */
057: private static final String FINAL = "final";
058:
059: /** String that marks a method or member as having private scope. */
060: private static final String PRIVATE = "private";
061:
062: /** String that marks a method or member as having protected scope. */
063: private static final String PROTECTED = "protected";
064:
065: /** String that marks a method or member as having package scope. */
066: private static final String PACKAGE = "";
067:
068: /** String that marks a method or member as having public scope. */
069: private static final String PUBLIC = "public";
070:
071: /** String that marks a method or member as being static. */
072: private static final String STATIC = "static";
073:
074: /** String that marks a method or member as being transient. */
075: private static final String TRANSIENT = "transient";
076:
077: /** Visibility is private. */
078: private static final short VISIBILITY_PRIVATE = 1;
079:
080: /** Visibility is protected. */
081: private static final short VISIBILITY_PROTECTED = 2;
082:
083: /** Visibility is public. */
084: private static final short VISIBILITY_PUBLIC = 3;
085:
086: /** Visibility is package. */
087: private static final short VISIBILITY_PACKAGE = 4;
088:
089: //--------------------------------------------------------------------------
090:
091: /** The visibility modifier for the object associated with this JModifiers. */
092: private short _visibility = VISIBILITY_PUBLIC;
093:
094: /** A flag indicating whether or not the object associated with this
095: * JModifiers is static. */
096: private boolean _isStatic = false;
097:
098: /** A flag indicating whether or not the object associated with this
099: * JModifiers is final. */
100: private boolean _isFinal = false;
101:
102: /** A flag indicating whether or not the object associated with this
103: * JModifiers is abstract. */
104: private boolean _isAbstract = false;
105:
106: /** A flag indicating whether or not the object associated with this
107: * JModifiers is transient. */
108: private boolean _isTransient = false;
109:
110: //--------------------------------------------------------------------------
111:
112: /**
113: * Creates a new JModifiers class. By default the only modifier present is
114: * public.
115: */
116: public JModifiers() {
117: super ();
118: }
119:
120: /**
121: * Creates a new JModifiers instance.
122: *
123: * @param visibility The visibility qualifier.
124: * @param isStatic A boolean, if true, indicating that this JModifiers
125: * includes "static" as a qualifier.
126: * @param isFinal A boolean, if true, indicating that this JModifiers
127: * includes "final" as a qualifier.
128: */
129: private JModifiers(final short visibility, final boolean isStatic,
130: final boolean isFinal) {
131: _visibility = visibility;
132: _isStatic = isStatic;
133: _isFinal = isFinal;
134: }
135:
136: //--------------------------------------------------------------------------
137:
138: /**
139: * Creates a copy of this JModifiers instance.
140: *
141: * @return A copy of this JModifiers.
142: */
143: public JModifiers copy() {
144: JModifiers mods = new JModifiers(_visibility, _isStatic,
145: _isFinal);
146: mods.setAbstract(_isAbstract);
147: mods.setTransient(_isTransient);
148: return mods;
149: }
150:
151: //--------------------------------------------------------------------------
152:
153: /**
154: * Changes the visibility qualifier to "private".
155: */
156: public void makePrivate() {
157: _visibility = VISIBILITY_PRIVATE;
158: }
159:
160: /**
161: * Changes the visibility qualifier to "protected".
162: */
163: public void makeProtected() {
164: _visibility = VISIBILITY_PROTECTED;
165: }
166:
167: /**
168: * Changes the visibility qualifier to "public".
169: */
170: public void makePublic() {
171: _visibility = VISIBILITY_PUBLIC;
172: }
173:
174: /**
175: * Changes the visibility qualifier to package (= without qualifier).
176: */
177: public void makePackage() {
178: _visibility = VISIBILITY_PACKAGE;
179: }
180:
181: /**
182: * Returns true if this JModifiers includes the qualifier "final". This
183: * is only applicable to methods and classes.
184: *
185: * @return True if this JModifiers includes the qualifier "final". This
186: * is only applicable to methods and classes.
187: */
188: public boolean isFinal() {
189: return _isFinal;
190: }
191:
192: /**
193: * Returns true if this JModifiers includes the qualifier "abstract". This
194: * is only applicable to methods and classes.
195: *
196: * @return True if this JModifiers includes the qualifier "abstract". This
197: * is only applicable to methods and classes.
198: */
199: public boolean isAbstract() {
200: return _isAbstract;
201: }
202:
203: /**
204: * Returns true if the visibility modifier for this JModifier is "private".
205: *
206: * @return True if the visibility modifier for this JModifier is "private".
207: */
208: public boolean isPrivate() {
209: return (_visibility == VISIBILITY_PRIVATE);
210: }
211:
212: /**
213: * Returns true if the visibility modifier for this JModifier is "protected".
214: *
215: * @return True if the visibility modifier for this JModifier is "protected".
216: */
217: public boolean isProtected() {
218: return (_visibility == VISIBILITY_PROTECTED);
219: }
220:
221: /**
222: * Returns true if the visibility modifier for this JModifier is "public".
223: *
224: * @return True if the visibility modifier for this JModifier is "public".
225: */
226: public boolean isPublic() {
227: return (_visibility == VISIBILITY_PUBLIC);
228: }
229:
230: /**
231: * Returns true if the visibility modifier for this JModifier is package
232: * (i.e., without qualifier).
233: *
234: * @return True if the visibility modifier for this JModifier is package
235: * (i.e., without qualifier).
236: */
237: public boolean isPackage() {
238: return (_visibility == VISIBILITY_PACKAGE);
239: }
240:
241: /**
242: * Returns true if this JModifier includes the qualifier "static".
243: *
244: * @return True if this JModifier includes the qualifier "static".
245: */
246: public boolean isStatic() {
247: return _isStatic;
248: }
249:
250: /**
251: * Returns true if this JModifier includes the qualifier "transient".
252: *
253: * @return True if this JModifier includes the qualifier "transient".
254: */
255: public boolean isTransient() {
256: return _isTransient;
257: }
258:
259: /**
260: * Sets whether or not this JModifiers includes the qualifier "abstract".
261: * This applies only to methods or classes.
262: *
263: * @param isAbstract If true, indicates that this JModifier should include
264: * the qualifier "abstract".
265: */
266: public void setAbstract(final boolean isAbstract) {
267: _isAbstract = isAbstract;
268: }
269:
270: /**
271: * Sets whether or not this JModifiers includes the qualifier "final".
272: *
273: * @param isFinal If true, indicates that this JModifier should include the
274: * qualifier "final".
275: */
276: public void setFinal(final boolean isFinal) {
277: _isFinal = isFinal;
278: }
279:
280: /**
281: * Sets whether or not this JModifiers includes the qualifier "static".
282: *
283: * @param isStatic If true, indicates that this JModifier should include the
284: * qualifier "static".
285: */
286: public void setStatic(final boolean isStatic) {
287: _isStatic = isStatic;
288: }
289:
290: /**
291: * Sets whether or not this JModifiers includes the qualifier "transient".
292: *
293: * @param isTransient Is a boolean which when true indicates that this
294: * JModifier should include the qualifier "transient".
295: */
296: public void setTransient(final boolean isTransient) {
297: _isTransient = isTransient;
298: }
299:
300: //--------------------------------------------------------------------------
301:
302: /**
303: * {@inheritDoc}
304: */
305: public String toString() {
306: StringBuffer sb = new StringBuffer();
307:
308: //-- visibility
309: switch (_visibility) {
310: case VISIBILITY_PRIVATE:
311: sb.append(PRIVATE);
312: break;
313: case VISIBILITY_PROTECTED:
314: sb.append(PROTECTED);
315: break;
316: case VISIBILITY_PACKAGE:
317: sb.append(PACKAGE);
318: break;
319: default:
320: sb.append(PUBLIC);
321: break;
322: }
323:
324: //-- static
325: if (_isStatic) {
326: if (sb.length() > 0) {
327: sb.append(' ');
328: }
329: sb.append(STATIC);
330: }
331:
332: //-- final
333: if (_isFinal) {
334: if (sb.length() > 0) {
335: sb.append(' ');
336: }
337: sb.append(FINAL);
338: }
339:
340: //-- abstract
341: if (_isAbstract) {
342: if (sb.length() > 0) {
343: sb.append(' ');
344: }
345: sb.append(ABSTRACT);
346: }
347:
348: //-- transient
349: if (_isTransient) {
350: if (sb.length() > 0) {
351: sb.append(' ');
352: }
353: sb.append(TRANSIENT);
354: }
355:
356: return sb.toString();
357: }
358:
359: //--------------------------------------------------------------------------
360: }
|