View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 1999-2000 (C) Intalio, Inc. All Rights Reserved.
32   */
33  package org.exolab.javasource;
34  
35  /**
36   * Represents the set of modifiers for a Method or Member variable.
37   *
38   * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
39   * @version $Revision$ $Date: 2005-02-26 17:30:28 -0700 (Sat, 26 Feb 2005) $
40   */
41  public final class JModifiers {
42    // --------------------------------------------------------------------------
43  
44    /** String that marks a method or member as being abstract. */
45    private static final String ABSTRACT = "abstract";
46  
47    /** String that marks a method or member as being final. */
48    private static final String FINAL = "final";
49  
50    /** String that marks a method or member as having private scope. */
51    private static final String PRIVATE = "private";
52  
53    /** String that marks a method or member as having protected scope. */
54    private static final String PROTECTED = "protected";
55  
56    /** String that marks a method or member as having package scope. */
57    private static final String PACKAGE = "";
58  
59    /** String that marks a method or member as having public scope. */
60    private static final String PUBLIC = "public";
61  
62    /** String that marks a method or member as being static. */
63    private static final String STATIC = "static";
64  
65    /** String that marks a method or member as being transient. */
66    private static final String TRANSIENT = "transient";
67  
68    /** Visibility is private. */
69    private static final short VISIBILITY_PRIVATE = 1;
70  
71    /** Visibility is protected. */
72    private static final short VISIBILITY_PROTECTED = 2;
73  
74    /** Visibility is public. */
75    private static final short VISIBILITY_PUBLIC = 3;
76  
77    /** Visibility is package. */
78    private static final short VISIBILITY_PACKAGE = 4;
79  
80    // --------------------------------------------------------------------------
81  
82    /** The visibility modifier for the object associated with this JModifiers. */
83    private short _visibility = VISIBILITY_PUBLIC;
84  
85    /**
86     * A flag indicating whether or not the object associated with this JModifiers is static.
87     */
88    private boolean _isStatic = false;
89  
90    /**
91     * A flag indicating whether or not the object associated with this JModifiers is final.
92     */
93    private boolean _isFinal = false;
94  
95    /**
96     * A flag indicating whether or not the object associated with this JModifiers is abstract.
97     */
98    private boolean _isAbstract = false;
99  
100   /**
101    * A flag indicating whether or not the object associated with this JModifiers is transient.
102    */
103   private boolean _isTransient = false;
104 
105   // --------------------------------------------------------------------------
106 
107   /**
108    * Creates a new JModifiers class. By default the only modifier present is public.
109    */
110   public JModifiers() {
111     super();
112   }
113 
114   /**
115    * Creates a new JModifiers instance.
116    *
117    * @param visibility The visibility qualifier.
118    * @param isStatic A boolean, if true, indicating that this JModifiers includes "static" as a
119    *        qualifier.
120    * @param isFinal A boolean, if true, indicating that this JModifiers includes "final" as a
121    *        qualifier.
122    */
123   private JModifiers(final short visibility, final boolean isStatic, final boolean isFinal) {
124     _visibility = visibility;
125     _isStatic = isStatic;
126     _isFinal = isFinal;
127   }
128 
129   // --------------------------------------------------------------------------
130 
131   /**
132    * Creates a copy of this JModifiers instance.
133    *
134    * @return A copy of this JModifiers.
135    */
136   public JModifiers copy() {
137     JModifiers mods = new JModifiers(_visibility, _isStatic, _isFinal);
138     mods.setAbstract(_isAbstract);
139     mods.setTransient(_isTransient);
140     return mods;
141   }
142 
143   // --------------------------------------------------------------------------
144 
145   /**
146    * Changes the visibility qualifier to "private".
147    */
148   public void makePrivate() {
149     _visibility = VISIBILITY_PRIVATE;
150   }
151 
152   /**
153    * Changes the visibility qualifier to "protected".
154    */
155   public void makeProtected() {
156     _visibility = VISIBILITY_PROTECTED;
157   }
158 
159   /**
160    * Changes the visibility qualifier to "public".
161    */
162   public void makePublic() {
163     _visibility = VISIBILITY_PUBLIC;
164   }
165 
166   /**
167    * Changes the visibility qualifier to package (= without qualifier).
168    */
169   public void makePackage() {
170     _visibility = VISIBILITY_PACKAGE;
171   }
172 
173   /**
174    * Returns true if this JModifiers includes the qualifier "final". This is only applicable to
175    * methods and classes.
176    *
177    * @return True if this JModifiers includes the qualifier "final". This is only applicable to
178    *         methods and classes.
179    */
180   public boolean isFinal() {
181     return _isFinal;
182   }
183 
184   /**
185    * Returns true if this JModifiers includes the qualifier "abstract". This is only applicable to
186    * methods and classes.
187    *
188    * @return True if this JModifiers includes the qualifier "abstract". This is only applicable to
189    *         methods and classes.
190    */
191   public boolean isAbstract() {
192     return _isAbstract;
193   }
194 
195   /**
196    * Returns true if the visibility modifier for this JModifier is "private".
197    *
198    * @return True if the visibility modifier for this JModifier is "private".
199    */
200   public boolean isPrivate() {
201     return (_visibility == VISIBILITY_PRIVATE);
202   }
203 
204   /**
205    * Returns true if the visibility modifier for this JModifier is "protected".
206    *
207    * @return True if the visibility modifier for this JModifier is "protected".
208    */
209   public boolean isProtected() {
210     return (_visibility == VISIBILITY_PROTECTED);
211   }
212 
213   /**
214    * Returns true if the visibility modifier for this JModifier is "public".
215    *
216    * @return True if the visibility modifier for this JModifier is "public".
217    */
218   public boolean isPublic() {
219     return (_visibility == VISIBILITY_PUBLIC);
220   }
221 
222   /**
223    * Returns true if the visibility modifier for this JModifier is package (i.e., without
224    * qualifier).
225    *
226    * @return True if the visibility modifier for this JModifier is package (i.e., without
227    *         qualifier).
228    */
229   public boolean isPackage() {
230     return (_visibility == VISIBILITY_PACKAGE);
231   }
232 
233   /**
234    * Returns true if this JModifier includes the qualifier "static".
235    *
236    * @return True if this JModifier includes the qualifier "static".
237    */
238   public boolean isStatic() {
239     return _isStatic;
240   }
241 
242   /**
243    * Returns true if this JModifier includes the qualifier "transient".
244    *
245    * @return True if this JModifier includes the qualifier "transient".
246    */
247   public boolean isTransient() {
248     return _isTransient;
249   }
250 
251   /**
252    * Sets whether or not this JModifiers includes the qualifier "abstract". This applies only to
253    * methods or classes.
254    *
255    * @param isAbstract If true, indicates that this JModifier should include the qualifier
256    *        "abstract".
257    */
258   public void setAbstract(final boolean isAbstract) {
259     _isAbstract = isAbstract;
260   }
261 
262   /**
263    * Sets whether or not this JModifiers includes the qualifier "final".
264    *
265    * @param isFinal If true, indicates that this JModifier should include the qualifier "final".
266    */
267   public void setFinal(final boolean isFinal) {
268     _isFinal = isFinal;
269   }
270 
271   /**
272    * Sets whether or not this JModifiers includes the qualifier "static".
273    *
274    * @param isStatic If true, indicates that this JModifier should include the qualifier "static".
275    */
276   public void setStatic(final boolean isStatic) {
277     _isStatic = isStatic;
278   }
279 
280   /**
281    * Sets whether or not this JModifiers includes the qualifier "transient".
282    *
283    * @param isTransient Is a boolean which when true indicates that this JModifier should include
284    *        the qualifier "transient".
285    */
286   public void setTransient(final boolean isTransient) {
287     _isTransient = isTransient;
288   }
289 
290   // --------------------------------------------------------------------------
291 
292   /**
293    * {@inheritDoc}
294    */
295   public String toString() {
296     StringBuilder sb = new StringBuilder();
297 
298     // -- visibility
299     switch (_visibility) {
300       case VISIBILITY_PRIVATE:
301         sb.append(PRIVATE);
302         break;
303       case VISIBILITY_PROTECTED:
304         sb.append(PROTECTED);
305         break;
306       case VISIBILITY_PACKAGE:
307         sb.append(PACKAGE);
308         break;
309       default:
310         sb.append(PUBLIC);
311         break;
312     }
313 
314     // -- static
315     if (_isStatic) {
316       if (sb.length() > 0) {
317         sb.append(' ');
318       }
319       sb.append(STATIC);
320     }
321 
322     // -- final
323     if (_isFinal) {
324       if (sb.length() > 0) {
325         sb.append(' ');
326       }
327       sb.append(FINAL);
328     }
329 
330     // -- abstract
331     if (_isAbstract) {
332       if (sb.length() > 0) {
333         sb.append(' ');
334       }
335       sb.append(ABSTRACT);
336     }
337 
338     // -- transient
339     if (_isTransient) {
340       if (sb.length() > 0) {
341         sb.append(' ');
342       }
343       sb.append(TRANSIENT);
344     }
345 
346     return sb.toString();
347   }
348 
349   // --------------------------------------------------------------------------
350 }