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