removed unused private methods.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / dom / Modifier.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.core.dom;
12
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map;
18
19 /**
20  * Modifier node.
21  * <pre>
22  * Modifier:
23  *    <b>public</b>
24  *    <b>protected</b>
25  *    <b>private</b>
26  *    <b>static</b>
27  *    <b>abstract</b>
28  *    <b>final</b>
29  *    <b>native</b>
30  *    <b>synchronized</b>
31  *    <b>transient</b>
32  *    <b>volatile</b>
33  *    <b>strictfp</b>
34  * </pre>
35  * <p>
36  * The numeric values of these flags match the ones for class
37  * files as described in the Java Virtual Machine Specification.
38  * Note that Java model class {@link org.eclipse.jdt.core.Flags} also
39  * provides the same constants as this class.
40  * </p>
41  * 
42  * @since 2.0
43  * @noinstantiate This class is not intended to be instantiated by clients.
44  */
45 public final class Modifier extends ASTNode implements IExtendedModifier {
46
47         /**
48          * Modifier keywords (typesafe enumeration).
49          * @since 3.0
50          */
51         public static class ModifierKeyword {
52         
53                 /** "abstract" modifier with flag value {@link Modifier#ABSTRACT}. */
54                 public static final ModifierKeyword ABSTRACT_KEYWORD = new ModifierKeyword("abstract", ABSTRACT);//$NON-NLS-1$
55                 
56                 /** "final" modifier with flag value {@link Modifier#FINAL}. */
57                 public static final ModifierKeyword FINAL_KEYWORD = new ModifierKeyword("final", FINAL);//$NON-NLS-1$
58                 
59                 /**
60                  * Map from token to operator (key type: <code>String</code>;
61                  * value type: <code>Operator</code>).
62                  */
63                 private static final Map KEYWORDS;
64                 
65                 /** "native" modifier with flag value {@link Modifier#NATIVE}. */
66                 public static final ModifierKeyword NATIVE_KEYWORD = new ModifierKeyword("native", NATIVE);//$NON-NLS-1$
67                 
68                 /** "private" modifier with flag value {@link Modifier#PRIVATE}. */
69                 public static final ModifierKeyword PRIVATE_KEYWORD = new ModifierKeyword("private", PRIVATE);//$NON-NLS-1$
70                 
71                 /** "protected" modifier with flag value {@link Modifier#PROTECTED}. */
72                 public static final ModifierKeyword PROTECTED_KEYWORD = new ModifierKeyword("protected", PROTECTED);//$NON-NLS-1$
73                 
74                 /** "public" modifier with flag value {@link Modifier#PUBLIC}. */
75                 public static final ModifierKeyword PUBLIC_KEYWORD = new ModifierKeyword("public", PUBLIC);//$NON-NLS-1$
76                 
77                 /** "static" modifier with flag value {@link Modifier#STATIC}. */
78                 public static final ModifierKeyword STATIC_KEYWORD = new ModifierKeyword("static", STATIC);//$NON-NLS-1$
79                 
80                 /** "strictfp" modifier with flag value {@link Modifier#STRICTFP}. */
81                 public static final ModifierKeyword STRICTFP_KEYWORD = new ModifierKeyword("strictfp", STRICTFP);//$NON-NLS-1$
82                 
83                 /** "synchronized" modifier with flag value {@link Modifier#SYNCHRONIZED}. */
84                 public static final ModifierKeyword SYNCHRONIZED_KEYWORD = new ModifierKeyword("synchronized", SYNCHRONIZED);//$NON-NLS-1$
85                 
86                 /** "transient" modifier with flag value {@link Modifier#TRANSIENT}. */
87                 public static final ModifierKeyword TRANSIENT_KEYWORD = new ModifierKeyword("transient", TRANSIENT);//$NON-NLS-1$
88                 
89                 /** "volatile" modifier with flag value {@link Modifier#VOLATILE}. */
90                 public static final ModifierKeyword VOLATILE_KEYWORD = new ModifierKeyword("volatile", VOLATILE);//$NON-NLS-1$
91                 static {
92                         KEYWORDS = new HashMap(20);
93                         ModifierKeyword[] ops = {
94                                         PUBLIC_KEYWORD,
95                                         PROTECTED_KEYWORD,
96                                         PRIVATE_KEYWORD,
97                                         STATIC_KEYWORD,
98                                         ABSTRACT_KEYWORD,
99                                         FINAL_KEYWORD,
100                                         NATIVE_KEYWORD,
101                                         SYNCHRONIZED_KEYWORD,
102                                         TRANSIENT_KEYWORD,
103                                         VOLATILE_KEYWORD,
104                                         STRICTFP_KEYWORD
105                                 };
106                         for (int i = 0; i < ops.length; i++) {
107                                 KEYWORDS.put(ops[i].toString(), ops[i]);
108                         }
109                 }
110
111                 /**
112                  * Returns the modifier corresponding to the given single-bit flag value,
113                  * or <code>null</code> if none or if more than one bit is set.
114                  * <p>
115                  * <code>fromFlagValue</code> is the converse of <code>toFlagValue</code>:
116                  * that is, <code>ModifierKind.fromFlagValue(k.toFlagValue()) == k</code> for 
117                  * all modifier keywords <code>k</code>.
118                  * </p>
119                  * 
120                  * @param flagValue the single-bit flag value for the modifier
121                  * @return the modifier keyword, or <code>null</code> if none
122                  * @see #toFlagValue()
123                  */
124                 public static ModifierKeyword fromFlagValue(int flagValue) {
125                         for (Iterator it = KEYWORDS.values().iterator(); it.hasNext(); ) {
126                                 ModifierKeyword k = (ModifierKeyword) it.next();
127                                 if (k.toFlagValue() == flagValue) {
128                                         return k;
129                                 }
130                         }
131                         return null;
132                 }
133                 
134                 /**
135                  * Returns the modifier corresponding to the given string,
136                  * or <code>null</code> if none.
137                  * <p>
138                  * <code>toKeyword</code> is the converse of <code>toString</code>:
139                  * that is, <code>ModifierKind.toKeyword(k.toString()) == k</code> for 
140                  * all modifier keywords <code>k</code>.
141                  * </p>
142                  * 
143                  * @param keyword the lowercase string name for the modifier
144                  * @return the modifier keyword, or <code>null</code> if none
145                  * @see #toString()
146                  */
147                 public static ModifierKeyword toKeyword(String keyword) {
148                         return (ModifierKeyword) KEYWORDS.get(keyword);
149                 }
150                 
151                 /**
152                  * The flag value for the modifier.
153                  */
154                 private int flagValue;
155                 
156                 /**
157                  * The keyword modifier string.
158                  */
159                 private String keyword;
160                 
161                 /**
162                  * Creates a new modifier with the given keyword.
163                  * <p>
164                  * Note: this constructor is private. The only instances
165                  * ever created are the ones for the standard modifiers.
166                  * </p>
167                  * 
168                  * @param keyword the character sequence for the modifier
169                  * @param flagValue flag value as described in the Java Virtual Machine Specification
170                  */
171                 private ModifierKeyword(String keyword, int flagValue) {
172                         this.keyword = keyword;
173                         this.flagValue = flagValue;
174                 }
175                 
176                 /**
177                  * Returns the modifier flag value corresponding to this modifier keyword.
178                  * These flag values are as described in the Java Virtual Machine Specification.
179                  * 
180                  * @return one of the <code>Modifier</code> constants
181                  * @see #fromFlagValue(int)
182                  */ 
183                 public int toFlagValue() {
184                         return this.flagValue;
185                 }
186
187                 /**
188                  * Returns the keyword for the modifier.
189                  * 
190                  * @return the keyword for the modifier
191                  * @see #toKeyword(String)
192                  */
193                 public String toString() {
194                         return this.keyword;
195                 }
196         }
197
198         /**
199          * "abstract" modifier constant (bit mask).
200          * Applicable to types and methods.
201          * @since 2.0
202          */
203         public static final int ABSTRACT = 0x0400;
204
205         /**
206          * "final" modifier constant (bit mask).
207          * Applicable to types, methods, fields, and variables.
208          * @since 2.0
209          */
210         public static final int FINAL = 0x0010;
211
212         /**
213          * The "keyword" structural property of this node type.
214          * @since 3.0
215          */
216         public static final SimplePropertyDescriptor KEYWORD_PROPERTY = 
217                 new SimplePropertyDescriptor(Modifier.class, "keyword", Modifier.ModifierKeyword.class, MANDATORY); //$NON-NLS-1$
218
219         /**
220          * "native" modifier constant (bit mask).
221          * Applicable only to methods.
222          * @since 2.0
223          */
224         public static final int NATIVE = 0x0100;
225
226         /**
227          * Modifier constant (bit mask, value 0) indicating no modifiers.
228          * @since 2.0
229          */
230         public static final int NONE = 0x0000;
231
232         /**
233          * "private" modifier constant (bit mask).
234          * Applicable to types, methods, constructors, and fields.
235          * @since 2.0
236          */
237         public static final int PRIVATE = 0x0002;
238
239         /**
240          * A list of property descriptors (element type: 
241          * {@link StructuralPropertyDescriptor}),
242          * or null if uninitialized.
243          */
244         private static final List PROPERTY_DESCRIPTORS;
245
246         /**
247          * "protected" modifier constant (bit mask).
248          * Applicable to types, methods, constructors, and fields.
249          * @since 2.0
250          */
251         public static final int PROTECTED = 0x0004;
252
253         /**
254          * "public" modifier constant (bit mask).
255          * Applicable to types, methods, constructors, and fields.
256          * @since 2.0
257          */
258         public static final int PUBLIC = 0x0001;
259
260         /**
261          * "static" modifier constant (bit mask).
262          * Applicable to types, methods, fields, and initializers.
263          * @since 2.0
264          */
265         public static final int STATIC = 0x0008;
266
267         /**
268          * "strictfp" modifier constant (bit mask).
269          * Applicable to types and methods.
270          * @since 2.0
271          */
272         public static final int STRICTFP = 0x0800;
273
274         /**
275          * "synchronized" modifier constant (bit mask).
276          * Applicable only to methods.
277          * @since 2.0
278          */
279         public static final int SYNCHRONIZED = 0x0020;
280
281         /**
282          * "transient" modifier constant (bit mask).
283          * Applicable only to fields.
284          * @since 2.0
285          */
286         public static final int TRANSIENT = 0x0080;
287
288         /**
289          * "volatile" modifier constant (bit mask).
290          * Applicable only to fields.
291          * @since 2.0
292          */
293         public static final int VOLATILE = 0x0040;
294
295         static {
296                 List properyList = new ArrayList(2);
297                 createPropertyList(Modifier.class, properyList);
298                 addProperty(KEYWORD_PROPERTY, properyList);
299                 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
300         }
301
302         /**
303          * Returns whether the given flags includes the "abstract" modifier.
304          * Applicable to types and methods.
305          * 
306          * @param flags the modifier flags
307          * @return <code>true</code> if the <code>ABSTRACT</code> bit is
308          *   set, and <code>false</code> otherwise
309          * @since 2.0
310          */
311         public static boolean isAbstract(int flags) {
312                 return (flags & ABSTRACT) != 0;
313         }
314
315         /**
316          * Returns whether the given flags includes the "final" modifier.
317          * Applicable to types, methods, fields, and variables.
318          * 
319          * @param flags the modifier flags
320          * @return <code>true</code> if the <code>FINAL</code> bit is
321          *   set, and <code>false</code> otherwise
322          * @since 2.0
323          */
324         public static boolean isFinal(int flags) {
325                 return (flags & FINAL) != 0;
326         }
327
328         /**
329          * Returns whether the given flags includes the "native" modifier.
330          * Applicable only to methods.
331          * 
332          * @param flags the modifier flags
333          * @return <code>true</code> if the <code>NATIVE</code> bit is
334          *   set, and <code>false</code> otherwise
335          * @since 2.0
336          */
337         public static boolean isNative(int flags) {
338                 return (flags & NATIVE) != 0;
339         }
340
341         /**
342          * Returns whether the given flags includes the "private" modifier.
343          * Applicable to types, methods, constructors, and fields.
344          * 
345          * @param flags the modifier flags
346          * @return <code>true</code> if the <code>PRIVATE</code> bit is
347          *   set, and <code>false</code> otherwise
348          * @since 2.0
349          */
350         public static boolean isPrivate(int flags) {
351                 return (flags & PRIVATE) != 0;
352         }
353
354         /**
355          * Returns whether the given flags includes the "protected" modifier.
356          * Applicable to types, methods, constructors, and fields.
357          * 
358          * @param flags the modifier flags
359          * @return <code>true</code> if the <code>PROTECTED</code> bit is
360          *   set, and <code>false</code> otherwise
361          * @since 2.0
362          */
363         public static boolean isProtected(int flags) {
364                 return (flags & PROTECTED) != 0;
365         }
366
367         /**
368          * Returns whether the given flags includes the "public" modifier.
369          * Applicable to types, methods, constructors, and fields.
370          * 
371          * @param flags the modifier flags
372          * @return <code>true</code> if the <code>PUBLIC</code> bit is
373          *   set, and <code>false</code> otherwise
374          * @since 2.0
375          */
376         public static boolean isPublic(int flags) {
377                 return (flags & PUBLIC) != 0;
378         }
379
380         /**
381          * Returns whether the given flags includes the "static" modifier.
382          * Applicable to types, methods, fields, and initializers.
383          * 
384          * @param flags the modifier flags
385          * @return <code>true</code> if the <code>STATIC</code> bit is
386          *   set, and <code>false</code> otherwise
387          * @since 2.0
388          */
389         public static boolean isStatic(int flags) {
390                 return (flags & STATIC) != 0;
391         }
392         
393         /**
394          * Returns whether the given flags includes the "strictfp" modifier.
395          * Applicable to types and methods.
396          * 
397          * @param flags the modifier flags
398          * @return <code>true</code> if the <code>STRICTFP</code> bit is
399          *   set, and <code>false</code> otherwise
400          * @since 2.0
401          */
402         public static boolean isStrictfp(int flags) {
403                 return (flags & STRICTFP) != 0;
404         }
405         
406         /**
407          * Returns whether the given flags includes the "synchronized" modifier.
408          * Applicable only to methods.
409          * 
410          * @param flags the modifier flags
411          * @return <code>true</code> if the <code>SYNCHRONIZED</code> bit is
412          *   set, and <code>false</code> otherwise
413          * @since 2.0
414          */
415         public static boolean isSynchronized(int flags) {
416                 return (flags & SYNCHRONIZED) != 0;
417         }
418         
419         /**
420          * Returns whether the given flags includes the "transient" modifier.
421          * Applicable only to fields.
422          * 
423          * @param flags the modifier flags
424          * @return <code>true</code> if the <code>TRANSIENT</code> bit is
425          *   set, and <code>false</code> otherwise
426          * @since 2.0
427          */
428         public static boolean isTransient(int flags) {
429                 return (flags & TRANSIENT) != 0;
430         }
431         
432         /**
433          * Returns whether the given flags includes the "volatile" modifier.
434          * Applicable only to fields.
435          * 
436          * @param flags the modifier flags
437          * @return <code>true</code> if the <code>VOLATILE</code> bit is
438          *   set, and <code>false</code> otherwise
439          * @since 2.0
440          */
441         public static boolean isVolatile(int flags) {
442                 return (flags & VOLATILE) != 0;
443         }
444
445         /**
446          * Returns a list of structural property descriptors for this node type.
447          * Clients must not modify the result.
448          * 
449          * @param apiLevel the API level; one of the
450          * <code>AST.JLS*</code> constants
451
452          * @return a list of property descriptors (element type: 
453          * {@link StructuralPropertyDescriptor})
454          * @since 3.0
455          */
456         public static List propertyDescriptors(int apiLevel) {
457                 return PROPERTY_DESCRIPTORS;
458         }
459                         
460         /**
461          * The modifier keyword; defaults to an unspecified modifier.
462          * @since 3.0
463          */
464         private ModifierKeyword modifierKeyword = ModifierKeyword.PUBLIC_KEYWORD;
465
466         /**
467          * Creates a new unparented modifier node owned by the given AST.
468          * By default, the node has unspecified (but legal) modifier.
469          * <p>
470          * N.B. This constructor is package-private.
471          * </p>
472          * 
473          * @param ast the AST that is to own this node
474          * @since 3.0
475          */
476         Modifier(AST ast) {
477                 super(ast);
478             unsupportedIn2();
479         }
480
481         /* (omit javadoc for this method)
482          * Method declared on ASTNode.
483          * @since 3.0
484          */
485         void accept0(ASTVisitor visitor) {
486                 visitor.visit(this);
487                 visitor.endVisit(this);
488         }
489         
490         /* (omit javadoc for this method)
491          * Method declared on ASTNode.
492          * @since 3.0
493          */
494         ASTNode clone0(AST target) {
495                 Modifier result = new Modifier(target);
496                 result.setSourceRange(this.getStartPosition(), this.getLength());
497                 result.setKeyword(getKeyword());
498                 return result;
499         }
500
501         /**
502          * Returns the modifier keyword of this modifier node.
503          * 
504          * @return the modifier keyword
505          * @since 3.0
506          */ 
507         public ModifierKeyword getKeyword() {
508                 return this.modifierKeyword;
509         }
510
511         /* (omit javadoc for this method)
512          * Method declared on ASTNode.
513          * @since 3.0
514          */
515         final int getNodeType0() {
516                 return MODIFIER;
517         }
518         
519         /* (omit javadoc for this method)
520          * Method declared on ASTNode.
521          */
522         final Object internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object value) {
523                 if (property == KEYWORD_PROPERTY) {
524                         if (get) {
525                                 return getKeyword();
526                         } else {
527                                 setKeyword((ModifierKeyword) value);
528                                 return null;
529                         }
530                 }
531                 // allow default implementation to flag the error
532                 return super.internalGetSetObjectProperty(property, get, value);
533         }
534
535         /* (omit javadoc for this method)
536          * Method declared on ASTNode.
537          */
538         final List internalStructuralPropertiesForType(int apiLevel) {
539                 return propertyDescriptors(apiLevel);
540         }
541
542         /**
543          * Answer true if the receiver is the abstract modifier, false otherwise.
544          * 
545          * @return true if the receiver is the abstract modifier, false otherwise
546          * @since 3.2
547          */
548         public boolean isAbstract() {
549                 return this.modifierKeyword == ModifierKeyword.ABSTRACT_KEYWORD;
550         }
551
552         /**
553          * @see IExtendedModifier#isAnnotation()
554          */ 
555         public boolean isAnnotation() {
556                 return false;
557         }
558         
559         /**
560          * Answer true if the receiver is the final modifier, false otherwise.
561          * 
562          * @return true if the receiver is the final modifier, false otherwise
563          * @since 3.2
564          */
565         public boolean isFinal() {
566                 return this.modifierKeyword == ModifierKeyword.FINAL_KEYWORD;
567         }
568
569         /**
570          * @see IExtendedModifier#isModifier()
571          */ 
572         public boolean isModifier() {
573                 return true;
574         }
575
576         /**
577          * Answer true if the receiver is the native modifier, false otherwise.
578          * 
579          * @return true if the receiver is the native modifier, false otherwise
580          * @since 3.2
581          */
582         public boolean isNative() {
583                 return this.modifierKeyword == ModifierKeyword.NATIVE_KEYWORD;
584         }
585         
586         /**
587          * Answer true if the receiver is the private modifier, false otherwise.
588          * 
589          * @return true if the receiver is the private modifier, false otherwise
590          * @since 3.2
591          */
592         public boolean isPrivate() {
593                 return this.modifierKeyword == ModifierKeyword.PRIVATE_KEYWORD;
594         }
595         
596         /**
597          * Answer true if the receiver is the protected modifier, false otherwise.
598          * 
599          * @return true if the receiver is the protected modifier, false otherwise
600          * @since 3.2
601          */
602         public boolean isProtected() {
603                 return this.modifierKeyword == ModifierKeyword.PROTECTED_KEYWORD;
604         }
605         
606         /**
607          * Answer true if the receiver is the public modifier, false otherwise.
608          * 
609          * @return true if the receiver is the public modifier, false otherwise
610          * @since 3.2
611          */
612         public boolean isPublic() {
613                 return this.modifierKeyword == ModifierKeyword.PUBLIC_KEYWORD;
614         }
615         
616         /**
617          * Answer true if the receiver is the static modifier, false otherwise.
618          * 
619          * @return true if the receiver is the static modifier, false otherwise
620          * @since 3.2
621          */
622         public boolean isStatic() {
623                 return this.modifierKeyword == ModifierKeyword.STATIC_KEYWORD;
624         }
625         
626         /**
627          * Answer true if the receiver is the strictfp modifier, false otherwise.
628          * 
629          * @return true if the receiver is the strictfp modifier, false otherwise
630          * @since 3.2
631          */
632         public boolean isStrictfp() {
633                 return this.modifierKeyword == ModifierKeyword.STRICTFP_KEYWORD;
634         }
635         
636         /**
637          * Answer true if the receiver is the synchronized modifier, false otherwise.
638          * 
639          * @return true if the receiver is the synchronized modifier, false otherwise
640          * @since 3.2
641          */
642         public boolean isSynchronized() {
643                 return this.modifierKeyword == ModifierKeyword.SYNCHRONIZED_KEYWORD;
644         }
645         
646         /**
647          * Answer true if the receiver is the transient modifier, false otherwise.
648          * 
649          * @return true if the receiver is the transient modifier, false otherwise
650          * @since 3.2
651          */
652         public boolean isTransient() {
653                 return this.modifierKeyword == ModifierKeyword.TRANSIENT_KEYWORD;
654         }
655         
656         /**
657          * Answer true if the receiver is the volatile modifier, false otherwise.
658          * 
659          * @return true if the receiver is the volatile modifier, false otherwise
660          * @since 3.2
661          */
662         public boolean isVolatile() {
663                 return this.modifierKeyword == ModifierKeyword.VOLATILE_KEYWORD;
664         }
665         
666         /* (omit javadoc for this method)
667          * Method declared on ASTNode.
668          * @since 3.0
669          */
670         int memSize() {
671                 // treat ModifierKeyword as free
672                 return BASE_NODE_SIZE + 1 * 4;
673         }
674         
675         /**
676          * Sets the modifier keyword of this modifier node.
677          * 
678          * @param modifierKeyord the modifier keyword 
679          * @exception IllegalArgumentException if the argument is <code>null</code>
680          * @since 3.0
681          */ 
682         public void setKeyword(ModifierKeyword modifierKeyord) {
683                 if (modifierKeyord == null) {
684                         throw new IllegalArgumentException();
685                 }
686                 preValueChange(KEYWORD_PROPERTY);
687                 this.modifierKeyword = modifierKeyord;
688                 postValueChange(KEYWORD_PROPERTY);
689         }
690         
691         /* (omit javadoc for this method)
692          * Method declared on ASTNode.
693          * @since 3.0
694          */
695         final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
696                 // dispatch to correct overloaded match method
697                 return matcher.match(this, other);
698         }
699         
700         /* (omit javadoc for this method)
701          * Method declared on ASTNode.
702          * @since 3.0
703          */
704         int treeSize() {
705                 return memSize();
706         }
707 }