KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > dom > Modifier


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 org.eclipse.jdt.core.dom;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
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  */

44 public final class Modifier extends ASTNode implements IExtendedModifier {
45
46     /**
47      * Modifier keywords (typesafe enumeration).
48      * @since 3.0
49      */

50     public static class ModifierKeyword {
51     
52         /** "abstract" modifier with flag value {@link Modifier#ABSTRACT}. */
53         public static final ModifierKeyword ABSTRACT_KEYWORD = new ModifierKeyword("abstract", ABSTRACT);//$NON-NLS-1$
54

55         /** "final" modifier with flag value {@link Modifier#FINAL}. */
56         public static final ModifierKeyword FINAL_KEYWORD = new ModifierKeyword("final", FINAL);//$NON-NLS-1$
57

58         /**
59          * Map from token to operator (key type: <code>String</code>;
60          * value type: <code>Operator</code>).
61          */

62         private static final Map JavaDoc KEYWORDS;
63         
64         /** "native" modifier with flag value {@link Modifier#NATIVE}. */
65         public static final ModifierKeyword NATIVE_KEYWORD = new ModifierKeyword("native", NATIVE);//$NON-NLS-1$
66

67         /** "private" modifier with flag value {@link Modifier#PRIVATE}. */
68         public static final ModifierKeyword PRIVATE_KEYWORD = new ModifierKeyword("private", PRIVATE);//$NON-NLS-1$
69

70         /** "protected" modifier with flag value {@link Modifier#PROTECTED}. */
71         public static final ModifierKeyword PROTECTED_KEYWORD = new ModifierKeyword("protected", PROTECTED);//$NON-NLS-1$
72

73         /** "public" modifier with flag value {@link Modifier#PUBLIC}. */
74         public static final ModifierKeyword PUBLIC_KEYWORD = new ModifierKeyword("public", PUBLIC);//$NON-NLS-1$
75

76         /** "static" modifier with flag value {@link Modifier#STATIC}. */
77         public static final ModifierKeyword STATIC_KEYWORD = new ModifierKeyword("static", STATIC);//$NON-NLS-1$
78

79         /** "strictfp" modifier with flag value {@link Modifier#STRICTFP}. */
80         public static final ModifierKeyword STRICTFP_KEYWORD = new ModifierKeyword("strictfp", STRICTFP);//$NON-NLS-1$
81

82         /** "synchronized" modifier with flag value {@link Modifier#SYNCHRONIZED}. */
83         public static final ModifierKeyword SYNCHRONIZED_KEYWORD = new ModifierKeyword("synchronized", SYNCHRONIZED);//$NON-NLS-1$
84

85         /** "transient" modifier with flag value {@link Modifier#TRANSIENT}. */
86         public static final ModifierKeyword TRANSIENT_KEYWORD = new ModifierKeyword("transient", TRANSIENT);//$NON-NLS-1$
87

88         /** "volatile" modifier with flag value {@link Modifier#VOLATILE}. */
89         public static final ModifierKeyword VOLATILE_KEYWORD = new ModifierKeyword("volatile", VOLATILE);//$NON-NLS-1$
90
static {
91             KEYWORDS = new HashMap JavaDoc(20);
92             ModifierKeyword[] ops = {
93                     PUBLIC_KEYWORD,
94                     PROTECTED_KEYWORD,
95                     PRIVATE_KEYWORD,
96                     STATIC_KEYWORD,
97                     ABSTRACT_KEYWORD,
98                     FINAL_KEYWORD,
99                     NATIVE_KEYWORD,
100                     SYNCHRONIZED_KEYWORD,
101                     TRANSIENT_KEYWORD,
102                     VOLATILE_KEYWORD,
103                     STRICTFP_KEYWORD
104                 };
105             for (int i = 0; i < ops.length; i++) {
106                 KEYWORDS.put(ops[i].toString(), ops[i]);
107             }
108         }
109
110         /**
111          * Returns the modifier corresponding to the given single-bit flag value,
112          * or <code>null</code> if none or if more than one bit is set.
113          * <p>
114          * <code>fromFlagValue</code> is the converse of <code>toFlagValue</code>:
115          * that is, <code>ModifierKind.fromFlagValue(k.toFlagValue()) == k</code> for
116          * all modifier keywords <code>k</code>.
117          * </p>
118          *
119          * @param flagValue the single-bit flag value for the modifier
120          * @return the modifier keyword, or <code>null</code> if none
121          * @see #toFlagValue()
122          */

123         public static ModifierKeyword fromFlagValue(int flagValue) {
124             for (Iterator JavaDoc it = KEYWORDS.values().iterator(); it.hasNext(); ) {
125                 ModifierKeyword k = (ModifierKeyword) it.next();
126                 if (k.toFlagValue() == flagValue) {
127                     return k;
128                 }
129             }
130             return null;
131         }
132         
133         /**
134          * Returns the modifier corresponding to the given string,
135          * or <code>null</code> if none.
136          * <p>
137          * <code>toKeyword</code> is the converse of <code>toString</code>:
138          * that is, <code>ModifierKind.toKeyword(k.toString()) == k</code> for
139          * all modifier keywords <code>k</code>.
140          * </p>
141          *
142          * @param keyword the lowercase string name for the modifier
143          * @return the modifier keyword, or <code>null</code> if none
144          * @see #toString()
145          */

146         public static ModifierKeyword toKeyword(String JavaDoc keyword) {
147             return (ModifierKeyword) KEYWORDS.get(keyword);
148         }
149         
150         /**
151          * The flag value for the modifier.
152          */

153         private int flagValue;
154         
155         /**
156          * The keyword modifier string.
157          */

158         private String JavaDoc keyword;
159         
160         /**
161          * Creates a new modifier with the given keyword.
162          * <p>
163          * Note: this constructor is private. The only instances
164          * ever created are the ones for the standard modifiers.
165          * </p>
166          *
167          * @param keyword the character sequence for the modifier
168          * @param flagValue flag value as described in the Java Virtual Machine Specification
169          */

170         private ModifierKeyword(String JavaDoc keyword, int flagValue) {
171             this.keyword = keyword;
172             this.flagValue = flagValue;
173         }
174         
175         /**
176          * Returns the modifier flag value corresponding to this modifier keyword.
177          * These flag values are as described in the Java Virtual Machine Specification.
178          *
179          * @return one of the <code>Modifier</code> constants
180          * @see #fromFlagValue(int)
181          */

182         public int toFlagValue() {
183             return this.flagValue;
184         }
185
186         /**
187          * Returns the keyword for the modifier.
188          *
189          * @return the keyword for the modifier
190          * @see #toKeyword(String)
191          */

192         public String JavaDoc toString() {
193             return this.keyword;
194         }
195     }
196
197     /**
198      * "abstract" modifier constant (bit mask).
199      * Applicable to types and methods.
200      * @since 2.0
201      */

202     public static final int ABSTRACT = 0x0400;
203
204     /**
205      * "final" modifier constant (bit mask).
206      * Applicable to types, methods, fields, and variables.
207      * @since 2.0
208      */

209     public static final int FINAL = 0x0010;
210
211     /**
212      * The "keyword" structural property of this node type.
213      * @since 3.0
214      */

215     public static final SimplePropertyDescriptor KEYWORD_PROPERTY =
216         new SimplePropertyDescriptor(Modifier.class, "keyword", Modifier.ModifierKeyword.class, MANDATORY); //$NON-NLS-1$
217

218     /**
219      * "native" modifier constant (bit mask).
220      * Applicable only to methods.
221      * @since 2.0
222      */

223     public static final int NATIVE = 0x0100;
224
225     /**
226      * Modifier constant (bit mask, value 0) indicating no modifiers.
227      * @since 2.0
228      */

229     public static final int NONE = 0x0000;
230
231     /**
232      * "private" modifier constant (bit mask).
233      * Applicable to types, methods, constructors, and fields.
234      * @since 2.0
235      */

236     public static final int PRIVATE = 0x0002;
237
238     /**
239      * A list of property descriptors (element type:
240      * {@link StructuralPropertyDescriptor}),
241      * or null if uninitialized.
242      */

243     private static final List JavaDoc PROPERTY_DESCRIPTORS;
244
245     /**
246      * "protected" modifier constant (bit mask).
247      * Applicable to types, methods, constructors, and fields.
248      * @since 2.0
249      */

250     public static final int PROTECTED = 0x0004;
251
252     /**
253      * "public" modifier constant (bit mask).
254      * Applicable to types, methods, constructors, and fields.
255      * @since 2.0
256      */

257     public static final int PUBLIC = 0x0001;
258
259     /**
260      * "static" modifier constant (bit mask).
261      * Applicable to types, methods, fields, and initializers.
262      * @since 2.0
263      */

264     public static final int STATIC = 0x0008;
265
266     /**
267      * "strictfp" modifier constant (bit mask).
268      * Applicable to types and methods.
269      * @since 2.0
270      */

271     public static final int STRICTFP = 0x0800;
272
273     /**
274      * "synchronized" modifier constant (bit mask).
275      * Applicable only to methods.
276      * @since 2.0
277      */

278     public static final int SYNCHRONIZED = 0x0020;
279
280     /**
281      * "transient" modifier constant (bit mask).
282      * Applicable only to fields.
283      * @since 2.0
284      */

285     public static final int TRANSIENT = 0x0080;
286
287     /**
288      * "volatile" modifier constant (bit mask).
289      * Applicable only to fields.
290      * @since 2.0
291      */

292     public static final int VOLATILE = 0x0040;
293
294     static {
295         List JavaDoc properyList = new ArrayList JavaDoc(2);
296         createPropertyList(Modifier.class, properyList);
297         addProperty(KEYWORD_PROPERTY, properyList);
298         PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
299     }
300
301     /**
302      * Returns whether the given flags includes the "abstract" modifier.
303      * Applicable to types and methods.
304      *
305      * @param flags the modifier flags
306      * @return <code>true</code> if the <code>ABSTRACT</code> bit is
307      * set, and <code>false</code> otherwise
308      * @since 2.0
309      */

310     public static boolean isAbstract(int flags) {
311         return (flags & ABSTRACT) != 0;
312     }
313
314     /**
315      * Returns whether the given flags includes the "final" modifier.
316      * Applicable to types, methods, fields, and variables.
317      *
318      * @param flags the modifier flags
319      * @return <code>true</code> if the <code>FINAL</code> bit is
320      * set, and <code>false</code> otherwise
321      * @since 2.0
322      */

323     public static boolean isFinal(int flags) {
324         return (flags & FINAL) != 0;
325     }
326
327     /**
328      * Returns whether the given flags includes the "native" modifier.
329      * Applicable only to methods.
330      *
331      * @param flags the modifier flags
332      * @return <code>true</code> if the <code>NATIVE</code> bit is
333      * set, and <code>false</code> otherwise
334      * @since 2.0
335      */

336     public static boolean isNative(int flags) {
337         return (flags & NATIVE) != 0;
338     }
339
340     /**
341      * Returns whether the given flags includes the "private" modifier.
342      * Applicable to types, methods, constructors, and fields.
343      *
344      * @param flags the modifier flags
345      * @return <code>true</code> if the <code>PRIVATE</code> bit is
346      * set, and <code>false</code> otherwise
347      * @since 2.0
348      */

349     public static boolean isPrivate(int flags) {
350         return (flags & PRIVATE) != 0;
351     }
352
353     /**
354      * Returns whether the given flags includes the "protected" modifier.
355      * Applicable to types, methods, constructors, and fields.
356      *
357      * @param flags the modifier flags
358      * @return <code>true</code> if the <code>PROTECTED</code> bit is
359      * set, and <code>false</code> otherwise
360      * @since 2.0
361      */

362     public static boolean isProtected(int flags) {
363         return (flags & PROTECTED) != 0;
364     }
365
366     /**
367      * Returns whether the given flags includes the "public" modifier.
368      * Applicable to types, methods, constructors, and fields.
369      *
370      * @param flags the modifier flags
371      * @return <code>true</code> if the <code>PUBLIC</code> bit is
372      * set, and <code>false</code> otherwise
373      * @since 2.0
374      */

375     public static boolean isPublic(int flags) {
376         return (flags & PUBLIC) != 0;
377     }
378
379     /**
380      * Returns whether the given flags includes the "static" modifier.
381      * Applicable to types, methods, fields, and initializers.
382      *
383      * @param flags the modifier flags
384      * @return <code>true</code> if the <code>STATIC</code> bit is
385      * set, and <code>false</code> otherwise
386      * @since 2.0
387      */

388     public static boolean isStatic(int flags) {
389         return (flags & STATIC) != 0;
390     }
391     
392     /**
393      * Returns whether the given flags includes the "strictfp" modifier.
394      * Applicable to types and methods.
395      *
396      * @param flags the modifier flags
397      * @return <code>true</code> if the <code>STRICTFP</code> bit is
398      * set, and <code>false</code> otherwise
399      * @since 2.0
400      */

401     public static boolean isStrictfp(int flags) {
402         return (flags & STRICTFP) != 0;
403     }
404     
405     /**
406      * Returns whether the given flags includes the "synchronized" modifier.
407      * Applicable only to methods.
408      *
409      * @param flags the modifier flags
410      * @return <code>true</code> if the <code>SYNCHRONIZED</code> bit is
411      * set, and <code>false</code> otherwise
412      * @since 2.0
413      */

414     public static boolean isSynchronized(int flags) {
415         return (flags & SYNCHRONIZED) != 0;
416     }
417     
418     /**
419      * Returns whether the given flags includes the "transient" modifier.
420      * Applicable only to fields.
421      *
422      * @param flags the modifier flags
423      * @return <code>true</code> if the <code>TRANSIENT</code> bit is
424      * set, and <code>false</code> otherwise
425      * @since 2.0
426      */

427     public static boolean isTransient(int flags) {
428         return (flags & TRANSIENT) != 0;
429     }
430     
431     /**
432      * Returns whether the given flags includes the "volatile" modifier.
433      * Applicable only to fields.
434      *
435      * @param flags the modifier flags
436      * @return <code>true</code> if the <code>VOLATILE</code> bit is
437      * set, and <code>false</code> otherwise
438      * @since 2.0
439      */

440     public static boolean isVolatile(int flags) {
441         return (flags & VOLATILE) != 0;
442     }
443
444     /**
445      * Returns a list of structural property descriptors for this node type.
446      * Clients must not modify the result.
447      *
448      * @param apiLevel the API level; one of the
449      * <code>AST.JLS*</code> constants
450
451      * @return a list of property descriptors (element type:
452      * {@link StructuralPropertyDescriptor})
453      * @since 3.0
454      */

455     public static List JavaDoc propertyDescriptors(int apiLevel) {
456         return PROPERTY_DESCRIPTORS;
457     }
458             
459     /**
460      * The modifier keyword; defaults to an unspecified modifier.
461      * @since 3.0
462      */

463     private ModifierKeyword modifierKeyword = ModifierKeyword.PUBLIC_KEYWORD;
464
465     /**
466      * Creates a new unparented modifier node owned by the given AST.
467      * By default, the node has unspecified (but legal) modifier.
468      * <p>
469      * N.B. This constructor is package-private.
470      * </p>
471      *
472      * @param ast the AST that is to own this node
473      * @since 3.0
474      */

475     Modifier(AST ast) {
476         super(ast);
477         unsupportedIn2();
478     }
479
480     /* (omit javadoc for this method)
481      * Method declared on ASTNode.
482      * @since 3.0
483      */

484     void accept0(ASTVisitor visitor) {
485         visitor.visit(this);
486         visitor.endVisit(this);
487     }
488     
489     /* (omit javadoc for this method)
490      * Method declared on ASTNode.
491      * @since 3.0
492      */

493     ASTNode clone0(AST target) {
494         Modifier result = new Modifier(target);
495         result.setSourceRange(this.getStartPosition(), this.getLength());
496         result.setKeyword(getKeyword());
497         return result;
498     }
499
500     /**
501      * Returns the modifier keyword of this modifier node.
502      *
503      * @return the modifier keyword
504      * @since 3.0
505      */

506     public ModifierKeyword getKeyword() {
507         return this.modifierKeyword;
508     }
509
510     /* (omit javadoc for this method)
511      * Method declared on ASTNode.
512      * @since 3.0
513      */

514     final int getNodeType0() {
515         return MODIFIER;
516     }
517     
518     /* (omit javadoc for this method)
519      * Method declared on ASTNode.
520      */

521     final Object JavaDoc internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object JavaDoc value) {
522         if (property == KEYWORD_PROPERTY) {
523             if (get) {
524                 return getKeyword();
525             } else {
526                 setKeyword((ModifierKeyword) value);
527                 return null;
528             }
529         }
530         // allow default implementation to flag the error
531
return super.internalGetSetObjectProperty(property, get, value);
532     }
533
534     /* (omit javadoc for this method)
535      * Method declared on ASTNode.
536      */

537     final List JavaDoc internalStructuralPropertiesForType(int apiLevel) {
538         return propertyDescriptors(apiLevel);
539     }
540
541     /**
542      * Answer true if the receiver is the abstract modifier, false otherwise.
543      *
544      * @return true if the receiver is the abstract modifier, false otherwise
545      * @since 3.2
546      */

547     public boolean isAbstract() {
548         return this.modifierKeyword == ModifierKeyword.ABSTRACT_KEYWORD;
549     }
550
551     /**
552      * @see IExtendedModifier#isAnnotation()
553      */

554     public boolean isAnnotation() {
555         return false;
556     }
557     
558     /**
559      * Answer true if the receiver is the final modifier, false otherwise.
560      *
561      * @return true if the receiver is the final modifier, false otherwise
562      * @since 3.2
563      */

564     public boolean isFinal() {
565         return this.modifierKeyword == ModifierKeyword.FINAL_KEYWORD;
566     }
567
568     /**
569      * @see IExtendedModifier#isModifier()
570      */

571     public boolean isModifier() {
572         return true;
573     }
574
575     /**
576      * Answer true if the receiver is the native modifier, false otherwise.
577      *
578      * @return true if the receiver is the native modifier, false otherwise
579      * @since 3.2
580      */

581     public boolean isNative() {
582         return this.modifierKeyword == ModifierKeyword.NATIVE_KEYWORD;
583     }
584     
585     /**
586      * Answer true if the receiver is the private modifier, false otherwise.
587      *
588      * @return true if the receiver is the private modifier, false otherwise
589      * @since 3.2
590      */

591     public boolean isPrivate() {
592         return this.modifierKeyword == ModifierKeyword.PRIVATE_KEYWORD;
593     }
594     
595     /**
596      * Answer true if the receiver is the protected modifier, false otherwise.
597      *
598      * @return true if the receiver is the protected modifier, false otherwise
599      * @since 3.2
600      */

601     public boolean isProtected() {
602         return this.modifierKeyword == ModifierKeyword.PROTECTED_KEYWORD;
603     }
604     
605     /**
606      * Answer true if the receiver is the public modifier, false otherwise.
607      *
608      * @return true if the receiver is the public modifier, false otherwise
609      * @since 3.2
610      */

611     public boolean isPublic() {
612         return this.modifierKeyword == ModifierKeyword.PUBLIC_KEYWORD;
613     }
614     
615     /**
616      * Answer true if the receiver is the static modifier, false otherwise.
617      *
618      * @return true if the receiver is the static modifier, false otherwise
619      * @since 3.2
620      */

621     public boolean isStatic() {
622         return this.modifierKeyword == ModifierKeyword.STATIC_KEYWORD;
623     }
624     
625     /**
626      * Answer true if the receiver is the strictfp modifier, false otherwise.
627      *
628      * @return true if the receiver is the strictfp modifier, false otherwise
629      * @since 3.2
630      */

631     public boolean isStrictfp() {
632         return this.modifierKeyword == ModifierKeyword.STRICTFP_KEYWORD;
633     }
634     
635     /**
636      * Answer true if the receiver is the synchronized modifier, false otherwise.
637      *
638      * @return true if the receiver is the synchronized modifier, false otherwise
639      * @since 3.2
640      */

641     public boolean isSynchronized() {
642         return this.modifierKeyword == ModifierKeyword.SYNCHRONIZED_KEYWORD;
643     }
644     
645     /**
646      * Answer true if the receiver is the transient modifier, false otherwise.
647      *
648      * @return true if the receiver is the transient modifier, false otherwise
649      * @since 3.2
650      */

651     public boolean isTransient() {
652         return this.modifierKeyword == ModifierKeyword.TRANSIENT_KEYWORD;
653     }
654     
655     /**
656      * Answer true if the receiver is the volatile modifier, false otherwise.
657      *
658      * @return true if the receiver is the volatile modifier, false otherwise
659      * @since 3.2
660      */

661     public boolean isVolatile() {
662         return this.modifierKeyword == ModifierKeyword.VOLATILE_KEYWORD;
663     }
664     
665     /* (omit javadoc for this method)
666      * Method declared on ASTNode.
667      * @since 3.0
668      */

669     int memSize() {
670         // treat ModifierKeyword as free
671
return BASE_NODE_SIZE + 1 * 4;
672     }
673     
674     /**
675      * Sets the modifier keyword of this modifier node.
676      *
677      * @param modifierKeyord the modifier keyword
678      * @exception IllegalArgumentException if the argument is <code>null</code>
679      * @since 3.0
680      */

681     public void setKeyword(ModifierKeyword modifierKeyord) {
682         if (modifierKeyord == null) {
683             throw new IllegalArgumentException JavaDoc();
684         }
685         preValueChange(KEYWORD_PROPERTY);
686         this.modifierKeyword = modifierKeyord;
687         postValueChange(KEYWORD_PROPERTY);
688     }
689     
690     /* (omit javadoc for this method)
691      * Method declared on ASTNode.
692      * @since 3.0
693      */

694     final boolean subtreeMatch0(ASTMatcher matcher, Object JavaDoc other) {
695         // dispatch to correct overloaded match method
696
return matcher.match(this, other);
697     }
698     
699     /* (omit javadoc for this method)
700      * Method declared on ASTNode.
701      * @since 3.0
702      */

703     int treeSize() {
704         return memSize();
705     }
706 }
707
Popular Tags