improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / Flags.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.core;
12
13 import net.sourceforge.phpdt.internal.compiler.env.IConstants;
14
15
16
17 /**
18  * Utility class for decoding modifier flags in Java elements.
19  * <p>
20  * This class provides static methods only; it is not intended to be
21  * instantiated or subclassed by clients.
22  * </p>
23  * <p>
24  * Note that the numeric values of these flags match the ones for class files
25  * as described in the Java Virtual Machine Specification. The AST class
26  * <code>Modifier</code> provides the same functionality as this class, only in
27  * the <code>net.sourceforge.phpdt.core.dom</code> package.
28  * </p>
29  *
30  * @see IMember#getFlags
31  */
32 public final class Flags {
33   /**
34          * Constant representing the absence of any flag
35          * @since 3.0
36          */
37         public static final int AccDefault = 0;
38         /**
39          * Public access flag. See The Java Virtual Machine Specification for more details.
40          * @since 2.0
41          */
42         public static final int AccPublic = IConstants.AccPublic;
43         /**
44          * Private access flag. See The Java Virtual Machine Specification for more details.
45          * @since 2.0
46          */
47         public static final int AccPrivate = IConstants.AccPrivate;
48         /**
49          * Protected access flag. See The Java Virtual Machine Specification for more details.
50          * @since 2.0
51          */
52         public static final int AccProtected = IConstants.AccProtected;
53         /**
54          * Static access flag. See The Java Virtual Machine Specification for more details.
55          * @since 2.0
56          */
57         public static final int AccStatic = IConstants.AccStatic;
58         /**
59          * Final access flag. See The Java Virtual Machine Specification for more details.
60          * @since 2.0
61          */
62         public static final int AccFinal = IConstants.AccFinal;
63         /**
64          * Synchronized access flag. See The Java Virtual Machine Specification for more details.
65          * @since 2.0
66          */
67 //      public static final int AccSynchronized = IConstants.AccSynchronized;
68         /**
69          * Volatile property flag. See The Java Virtual Machine Specification for more details.
70          * @since 2.0
71          */
72 //      public static final int AccVolatile = IConstants.AccVolatile;
73         /**
74          * Transient property flag. See The Java Virtual Machine Specification for more details.
75          * @since 2.0
76          */
77 //      public static final int AccTransient = IConstants.AccTransient;
78         /**
79          * Native property flag. See The Java Virtual Machine Specification for more details.
80          * @since 2.0
81          */
82 //      public static final int AccNative = IConstants.AccNative;
83         /**
84          * Interface property flag. See The Java Virtual Machine Specification for more details.
85          * @since 2.0
86          */
87         public static final int AccInterface = IConstants.AccInterface;
88         /**
89          * Abstract property flag. See The Java Virtual Machine Specification for more details.
90          * @since 2.0
91          */
92         public static final int AccAbstract = IConstants.AccAbstract;
93         /**
94          * Strictfp property flag. See The Java Virtual Machine Specification for more details.
95          * @since 2.0
96          */
97 //      public static final int AccStrictfp = IConstants.AccStrictfp;
98         /**
99          * Super property flag. See The Java Virtual Machine Specification for more details.
100          * @since 2.0
101          */
102         public static final int AccSuper = IConstants.AccSuper;
103         /**
104          * Synthetic property flag. See The Java Virtual Machine Specification for more details.
105          * @since 2.0
106          */
107 //      public static final int AccSynthetic = IConstants.AccSynthetic;
108         /**
109          * Deprecated property flag. See The Java Virtual Machine Specification for more details.
110          * @since 2.0
111          */
112         public static final int AccDeprecated = IConstants.AccDeprecated;
113         
114         /**
115          * Not instantiable.
116          */
117         private Flags() {
118         }
119         /**
120          * Returns whether the given integer includes the <code>abstract</code> modifier.
121          *
122          * @param flags the flags
123          * @return <code>true</code> if the <code>abstract</code> modifier is included
124          */
125         public static boolean isAbstract(int flags) {
126                 return (flags & AccAbstract) != 0;
127         }
128         /**
129          * Returns whether the given integer includes the indication that the 
130          * element is deprecated (<code>@deprecated</code> tag in Javadoc comment).
131          *
132          * @param flags the flags
133          * @return <code>true</code> if the element is marked as deprecated
134          */
135         public static boolean isDeprecated(int flags) {
136                 return (flags & AccDeprecated) != 0;
137         }
138         /**
139          * Returns whether the given integer includes the <code>final</code> modifier.
140          *
141          * @param flags the flags
142          * @return <code>true</code> if the <code>final</code> modifier is included
143          */
144         public static boolean isFinal(int flags) {
145                 return (flags & AccFinal) != 0;
146         }
147         /**
148          * Returns whether the given integer includes the <code>interface</code> modifier.
149          *
150          * @param flags the flags
151          * @return <code>true</code> if the <code>interface</code> modifier is included
152          * @since 2.0
153          */
154         public static boolean isInterface(int flags) {
155                 return (flags & AccInterface) != 0;
156         }
157         /**
158          * Returns whether the given integer includes the <code>native</code> modifier.
159          *
160          * @param flags the flags
161          * @return <code>true</code> if the <code>native</code> modifier is included
162          */
163 //      public static boolean isNative(int flags) {
164 //              return (flags & AccNative) != 0;
165 //      }
166         /**
167          * Returns whether the given integer includes the <code>private</code> modifier.
168          *
169          * @param flags the flags
170          * @return <code>true</code> if the <code>private</code> modifier is included
171          */
172         public static boolean isPrivate(int flags) {
173                 return (flags & AccPrivate) != 0;
174         }
175         /**
176          * Returns whether the given integer includes the <code>protected</code> modifier.
177          *
178          * @param flags the flags
179          * @return <code>true</code> if the <code>protected</code> modifier is included
180          */
181         public static boolean isProtected(int flags) {
182                 return (flags & AccProtected) != 0;
183         }
184         /**
185          * Returns whether the given integer includes the <code>public</code> modifier.
186          *
187          * @param flags the flags
188          * @return <code>true</code> if the <code>public</code> modifier is included
189          */
190         public static boolean isPublic(int flags) {
191                 return (flags & AccPublic) != 0;
192         }
193         /**
194          * Returns whether the given integer includes the <code>static</code> modifier.
195          *
196          * @param flags the flags
197          * @return <code>true</code> if the <code>static</code> modifier is included
198          */
199         public static boolean isStatic(int flags) {
200                 return (flags & AccStatic) != 0;
201         }
202         /**
203          * Returns whether the given integer includes the <code>strictfp</code> modifier.
204          *
205          * @param flags the flags
206          * @return <code>true</code> if the <code>strictfp</code> modifier is included
207          */
208 //      public static boolean isStrictfp(int flags) {
209 //              return (flags & AccStrictfp) != 0;
210 //      }
211         /**
212          * Returns whether the given integer includes the <code>synchronized</code> modifier.
213          *
214          * @param flags the flags
215          * @return <code>true</code> if the <code>synchronized</code> modifier is included
216          */
217 //      public static boolean isSynchronized(int flags) {
218 //              return (flags & AccSynchronized) != 0;
219 //      }
220         /**
221          * Returns whether the given integer includes the indication that the 
222          * element is synthetic.
223          *
224          * @param flags the flags
225          * @return <code>true</code> if the element is marked synthetic
226          */
227 //      public static boolean isSynthetic(int flags) {
228 //              return (flags & AccSynthetic) != 0;
229 //      }
230         /**
231          * Returns whether the given integer includes the <code>transient</code> modifier.
232          *
233          * @param flags the flags
234          * @return <code>true</code> if the <code>transient</code> modifier is included
235          */
236 //      public static boolean isTransient(int flags) {
237 //              return (flags & AccTransient) != 0;
238 //      }
239         /**
240          * Returns whether the given integer includes the <code>volatile</code> modifier.
241          *
242          * @param flags the flags
243          * @return <code>true</code> if the <code>volatile</code> modifier is included
244          */
245 //      public static boolean isVolatile(int flags) {
246 //              return (flags & AccVolatile) != 0;
247 //      }
248         /**
249          * Returns a standard string describing the given modifier flags.
250          * Only modifier flags are included in the output; the deprecated and
251          * synthetic flags are ignored if set.
252          * <p>
253          * The flags are output in the following order:
254          * <pre>
255          *   <code>public</code> <code>protected</code> <code>private</code> 
256          *   <code>static</code> 
257          *   <code>abstract</code> <code>final</code> <code>native</code> <code>synchronized</code> <code>transient</code> <code>volatile</code> <code>strictfp</code>
258          * </pre>
259          * This is a compromise between the orders specified in sections 8.1.1,
260          * 8.3.1, 8.4.3, 8.8.3, 9.1.1, and 9.3 of <em>The Java Language 
261          * Specification, Second Edition</em> (JLS2).
262          * </p> 
263          * <p>
264          * Examples results:
265          * <pre>
266          *        <code>"public static final"</code>
267          *        <code>"private native"</code>
268          * </pre>
269          * </p>
270          *
271          * @param flags the flags
272          * @return the standard string representation of the given flags
273          */
274         public static String toString(int flags) {
275                 StringBuffer sb = new StringBuffer();
276
277                 if (isPublic(flags))
278                         sb.append("public "); //$NON-NLS-1$
279                 if (isProtected(flags))
280                         sb.append("protected "); //$NON-NLS-1$
281                 if (isPrivate(flags))
282                         sb.append("private "); //$NON-NLS-1$
283                 if (isStatic(flags))
284                         sb.append("static "); //$NON-NLS-1$
285 //              if (isAbstract(flags))
286 //                      sb.append("abstract "); //$NON-NLS-1$
287                 if (isFinal(flags))
288                         sb.append("final "); //$NON-NLS-1$
289 //              if (isNative(flags))
290 //                      sb.append("native "); //$NON-NLS-1$
291 //              if (isSynchronized(flags))
292 //                      sb.append("synchronized "); //$NON-NLS-1$
293 //              if (isTransient(flags))
294 //                      sb.append("transient "); //$NON-NLS-1$
295 //              if (isVolatile(flags))
296 //                      sb.append("volatile "); //$NON-NLS-1$
297 //              if (isStrictfp(flags))
298 //                      sb.append("strictfp "); //$NON-NLS-1$
299
300                 int len = sb.length();
301                 if (len == 0)
302                         return ""; //$NON-NLS-1$
303                 sb.setLength(len - 1);
304                 return sb.toString();
305         }
306 }