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@codehaus.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.codehaus.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   * $Id$
44   */
45  
46  package org.codehaus.modello.plugin.java.javasource;
47  
48  /*
49   * Copyright (c) 2004, Codehaus.org
50   *
51   * Permission is hereby granted, free of charge, to any person obtaining a copy of
52   * this software and associated documentation files (the "Software"), to deal in
53   * the Software without restriction, including without limitation the rights to
54   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
55   * of the Software, and to permit persons to whom the Software is furnished to do
56   * so, subject to the following conditions:
57   *
58   * The above copyright notice and this permission notice shall be included in all
59   * copies or substantial portions of the Software.
60   *
61   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
62   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
63   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
64   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
65   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
66   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
67   * SOFTWARE.
68   */
69  
70  /**
71   * The set of modifiers for a Method or Member variable
72   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
73   * @version $Revision$ $Date$
74   **/
75  public class JModifiers
76  {
77  
78  
79      /* static members */
80  
81      private static final String sAbstract = "abstract";
82      private static final String sFinal = "final";
83      private static final String sPrivate = "private";
84      private static final String sProtected = "protected";
85      private static final String sPackage = "";
86      private static final String sPublic = "public";
87      private static final String sStatic = "static";
88      private static final String sTransient = "transient";
89  
90      private static final short vPrivate = 1;
91      private static final short vProtected = 2;
92      private static final short vPublic = 3;
93      private static final short vPackage = 4;
94  
95  
96      /* local members */
97  
98      /**
99       * The visibility
100      **/
101     private short visibility = vPublic;
102 
103     /**
104      * A flag indicating whether or not the object associated
105      * with this JModifiers is static
106      **/
107     private boolean isStatic = false;
108 
109     /**
110      * A flag indicating whether or not the object associated
111      * with this JModifiers is final
112      **/
113     private boolean isFinal = false;
114 
115     /**
116      * A flag indicating whether or not the object associated
117      * with this JModifiers is abstract
118      **/
119     private boolean isAbstract = false;
120 
121     /**
122      * A flag indicating whether or not the object associated
123      * with this JModifiers is transient
124      **/
125     private boolean isTransient = false;
126 
127     /**
128      * Creates a new JModifiers class, by default the
129      * modifiers presented are public.
130      **/
131     public JModifiers()
132     {
133         super();
134     } //-- JModifiers
135 
136     /**
137      * Creates a new JModifiers
138      * @param visibility the visibile qualifier
139      * @param isStatic a boolean indicating the static qualifier.
140      * A value of true indicates that this static qualifier is present.
141      * @param isFinal a boolean indicating the final qualifier. A value
142      * of true indicates that the final qualifier is present.
143      **/
144     private JModifiers( short visibility, boolean isStatic, boolean isFinal )
145     {
146         this.visibility = visibility;
147         this.isStatic = isStatic;
148         this.isFinal = isFinal;
149     } //-- JModifiers
150 
151     /**
152      * Creates a copy of this JModifiers
153      * @return the copy of this JModifiers
154      **/
155     public JModifiers copy()
156     {
157         JModifiers mods = new JModifiers( visibility, isStatic, isFinal );
158         mods.setAbstract( isAbstract );
159         mods.setTransient( isTransient );
160         return mods;
161     } //-- copy
162 
163     /**
164      * Changes the visibility qualifier to "private"
165      **/
166     public void makePrivate()
167     {
168         this.visibility = vPrivate;
169     } //-- makePrivate
170 
171     /**
172      * Changes the visibility qualifier to "protected"
173      **/
174     public void makeProtected()
175     {
176         this.visibility = vProtected;
177     } //-- makeProtected
178 
179     /**
180      * Changes the visibility qualifier to "public"
181      **/
182     public void makePublic()
183     {
184         this.visibility = vPublic;
185     } //-- makePublic
186 
187     /**
188      * Changes the visibility qualifier to package (= without qualifier).
189      **/
190     public void makePackage()
191     {
192         this.visibility = vPackage;
193     } //-- makePackage
194 
195     /**
196      * Returns true if the abstract qualifier is present.
197      * <BR> This is only applicable to methods and classes.
198      * @return true if the abstract qualifier is present
199      **/
200     public boolean isAbstract()
201     {
202         return isAbstract;
203     } //-- isAbstract
204 
205     /**
206      * Returns true if the final qualifier is present.
207      * <BR> This is only applicable to methods and classes.
208      * @return true if the final qualifier is present
209      **/
210     public boolean isFinal()
211     {
212         return isFinal;
213     } //-- isFinal
214 
215     /**
216      * Returns true if the modifier represented is private.
217      * @return true if the modifier represented is private.
218      **/
219     public boolean isPrivate()
220     {
221         return ( visibility == vPrivate );
222     } //-- isPrivate
223 
224     /**
225      * Returns true if the modifier represented is protected.
226      * @return true if the modifier represented is protected.
227      **/
228     public boolean isProtected()
229     {
230         return ( visibility == vProtected );
231     } //-- isProtected
232 
233     /**
234      * Returns true if the modifier represented is public.
235      * @return true if the modifier represented is public.
236      **/
237     public boolean isPublic()
238     {
239         return ( visibility == vPublic );
240     } //-- isPublic
241 
242     /**
243      * Returns true if the modifier represented is package (= without qualifier).
244      * @return true if the modifier represented is package (= without qualifier).
245      **/
246     public boolean isPackage()
247     {
248         return ( visibility == vPackage );
249     } //-- isPackage
250 
251     /**
252      * Returns true if the modifier represented is static.
253      * @return true if the modifier represented is static.
254      **/
255     public boolean isStatic()
256     {
257         return this.isStatic;
258     } //-- isPublic
259 
260     /**
261      * Returns true if the modifier represented is transient.
262      * @return true if the modifier represented is transient.
263      **/
264     public boolean isTransient()
265     {
266         return this.isTransient;
267     } //-- isTransient
268 
269     /**
270      * Sets whether or not the "abstract" qualifier is present
271      * <BR> This applies only to methods or classes.
272      * @param isAbstract is a boolean which when true will indicate
273      * that the abstract qualifier should be present
274      **/
275     public void setAbstract( boolean isAbstract )
276     {
277         this.isAbstract = isAbstract;
278     } //-- setAbstract
279 
280     /**
281      * Sets whether or not the "final" qualifier is present
282      * @param isFinal is a boolean which when true will indicate
283      * the final qualifiter is present
284      **/
285     public void setFinal( boolean isFinal )
286     {
287         this.isFinal = isFinal;
288     } //-- setFinal
289 
290     /**
291      * Sets whether or not the "static" qualifier is present
292      * @param isStatic is a boolean which when true will indicate
293      * the "static" qualifiter is present
294      **/
295     public void setStatic( boolean isStatic )
296     {
297         this.isStatic = isStatic;
298     } //-- setStatic
299 
300     /**
301      * Sets whether or not the "transient" qualifier is present
302      * @param isTransient is a boolean which when true will indicate
303      * the "transient" qualifiter is present
304      **/
305     public void setTransient( boolean isTransient )
306     {
307         this.isTransient = isTransient;
308     } //-- setTransient
309 
310     /**
311      * Returns the String represetation of this JModifiers
312      * @return the String represetation of this JModifiers
313      **/
314     public String toString()
315     {
316         StringBuffer sb = new StringBuffer();
317 
318         //-- visibility
319         switch ( visibility )
320         {
321             case vPrivate:
322                 sb.append( sPrivate );
323                 break;
324             case vProtected:
325                 sb.append( sProtected );
326                 break;
327             case vPackage:
328                 sb.append( sPackage );
329                 break;
330             default:
331                 sb.append( sPublic );
332                 break;
333         }
334 
335         //-- static
336         if ( isStatic )
337         {
338             if ( sb.length() > 0 )
339             {
340                 sb.append( ' ' );
341             }
342             sb.append( sStatic );
343         }
344 
345         //-- final
346         if ( isFinal )
347         {
348             if ( sb.length() > 0 )
349             {
350                 sb.append( ' ' );
351             }
352             sb.append( sFinal );
353         }
354 
355         //-- abstract
356         if ( isAbstract )
357         {
358             if ( sb.length() > 0 )
359             {
360                 sb.append( ' ' );
361             }
362             sb.append( sAbstract );
363         }
364 
365         //-- transient
366         if ( isTransient )
367         {
368             if ( sb.length() > 0 )
369             {
370                 sb.append( ' ' );
371             }
372             sb.append( sTransient );
373         }
374 
375 
376         return sb.toString();
377     } //-- toString
378 
379 } //-- JModifiers
380