View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 1999-2000 (C) Intalio, Inc. All Rights Reserved.
42   */
43  package org.exolab.javasource;
44  
45  /**
46   * Represents the set of modifiers for a Method or Member variable.
47   *
48   * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
49   * @version $Revision$ $Date: 2005-02-26 17:30:28 -0700 (Sat, 26 Feb 2005) $
50   */
51  public final class JModifiers {
52      //--------------------------------------------------------------------------
53  
54      /** String that marks a method or member as being abstract. */
55      private static final String ABSTRACT = "abstract";
56      
57      /** String that marks a method or member as being final. */
58      private static final String FINAL = "final";
59      
60      /** String that marks a method or member as having private scope. */
61      private static final String PRIVATE = "private";
62      
63      /** String that marks a method or member as having protected scope. */
64      private static final String PROTECTED = "protected";
65      
66      /** String that marks a method or member as having package scope. */
67      private static final String PACKAGE = "";
68      
69      /** String that marks a method or member as having public scope. */
70      private static final String PUBLIC = "public";
71      
72      /** String that marks a method or member as being static. */
73      private static final String STATIC = "static";
74      
75      /** String that marks a method or member as being transient. */
76      private static final String TRANSIENT = "transient";
77  
78      /** Visibility is private. */
79      private static final short VISIBILITY_PRIVATE = 1;
80      
81      /** Visibility is protected. */
82      private static final short VISIBILITY_PROTECTED = 2;
83      
84      /** Visibility is public. */
85      private static final short VISIBILITY_PUBLIC = 3;
86      
87      /** Visibility is package. */
88      private static final short VISIBILITY_PACKAGE = 4;
89  
90      //--------------------------------------------------------------------------
91  
92      /** The visibility modifier for the object associated with this JModifiers. */
93      private short _visibility = VISIBILITY_PUBLIC;
94  
95      /** A flag indicating whether or not the object associated with this
96       *  JModifiers is static. */
97      private boolean _isStatic = false;
98  
99      /** A flag indicating whether or not the object associated with this
100      *  JModifiers is final. */
101     private boolean _isFinal  = false;
102 
103     /** A flag indicating whether or not the object associated with this
104      *  JModifiers is abstract. */
105     private boolean _isAbstract = false;
106 
107     /** A flag indicating whether or not the object associated with this
108      *  JModifiers is transient. */
109     private boolean _isTransient = false;
110 
111     //--------------------------------------------------------------------------
112 
113     /**
114      * Creates a new JModifiers class. By default the only modifier present is
115      * public.
116      */
117     public JModifiers() {
118         super();
119     }
120 
121     /**
122      * Creates a new JModifiers instance.
123      *
124      * @param visibility The visibility qualifier.
125      * @param isStatic A boolean, if true, indicating that this JModifiers
126      *        includes "static" as a qualifier.
127      * @param isFinal A boolean, if true, indicating that this JModifiers
128      *        includes "final" as a qualifier.
129      */
130     private JModifiers(final short visibility, final boolean isStatic,
131             final boolean isFinal) {
132         _visibility = visibility;
133         _isStatic = isStatic;
134         _isFinal = isFinal;
135     }
136 
137     //--------------------------------------------------------------------------
138 
139     /**
140      * Creates a copy of this JModifiers instance.
141      *
142      * @return A copy of this JModifiers.
143      */
144     public JModifiers copy() {
145         JModifiers mods = new JModifiers(_visibility, _isStatic, _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         StringBuilder sb = new StringBuilder();
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 }