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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.core.dom;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.List;
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.
43 * @noinstantiate This class is not intended to be instantiated by clients.
45 public final class Modifier extends ASTNode implements IExtendedModifier {
48 * Modifier keywords (typesafe enumeration).
51 public static class ModifierKeyword {
53 /** "abstract" modifier with flag value {@link Modifier#ABSTRACT}. */
54 public static final ModifierKeyword ABSTRACT_KEYWORD = new ModifierKeyword("abstract", ABSTRACT);//$NON-NLS-1$
56 /** "final" modifier with flag value {@link Modifier#FINAL}. */
57 public static final ModifierKeyword FINAL_KEYWORD = new ModifierKeyword("final", FINAL);//$NON-NLS-1$
60 * Map from token to operator (key type: <code>String</code>;
61 * value type: <code>Operator</code>).
63 private static final Map KEYWORDS;
65 /** "native" modifier with flag value {@link Modifier#NATIVE}. */
66 public static final ModifierKeyword NATIVE_KEYWORD = new ModifierKeyword("native", NATIVE);//$NON-NLS-1$
68 /** "private" modifier with flag value {@link Modifier#PRIVATE}. */
69 public static final ModifierKeyword PRIVATE_KEYWORD = new ModifierKeyword("private", PRIVATE);//$NON-NLS-1$
71 /** "protected" modifier with flag value {@link Modifier#PROTECTED}. */
72 public static final ModifierKeyword PROTECTED_KEYWORD = new ModifierKeyword("protected", PROTECTED);//$NON-NLS-1$
74 /** "public" modifier with flag value {@link Modifier#PUBLIC}. */
75 public static final ModifierKeyword PUBLIC_KEYWORD = new ModifierKeyword("public", PUBLIC);//$NON-NLS-1$
77 /** "static" modifier with flag value {@link Modifier#STATIC}. */
78 public static final ModifierKeyword STATIC_KEYWORD = new ModifierKeyword("static", STATIC);//$NON-NLS-1$
80 /** "strictfp" modifier with flag value {@link Modifier#STRICTFP}. */
81 public static final ModifierKeyword STRICTFP_KEYWORD = new ModifierKeyword("strictfp", STRICTFP);//$NON-NLS-1$
83 /** "synchronized" modifier with flag value {@link Modifier#SYNCHRONIZED}. */
84 public static final ModifierKeyword SYNCHRONIZED_KEYWORD = new ModifierKeyword("synchronized", SYNCHRONIZED);//$NON-NLS-1$
86 /** "transient" modifier with flag value {@link Modifier#TRANSIENT}. */
87 public static final ModifierKeyword TRANSIENT_KEYWORD = new ModifierKeyword("transient", TRANSIENT);//$NON-NLS-1$
89 /** "volatile" modifier with flag value {@link Modifier#VOLATILE}. */
90 public static final ModifierKeyword VOLATILE_KEYWORD = new ModifierKeyword("volatile", VOLATILE);//$NON-NLS-1$
92 KEYWORDS = new HashMap(20);
93 ModifierKeyword[] ops = {
101 SYNCHRONIZED_KEYWORD,
106 for (int i = 0; i < ops.length; i++) {
107 KEYWORDS.put(ops[i].toString(), ops[i]);
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.
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>.
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()
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) {
135 * Returns the modifier corresponding to the given string,
136 * or <code>null</code> if none.
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>.
143 * @param keyword the lowercase string name for the modifier
144 * @return the modifier keyword, or <code>null</code> if none
147 public static ModifierKeyword toKeyword(String keyword) {
148 return (ModifierKeyword) KEYWORDS.get(keyword);
152 * The flag value for the modifier.
154 private int flagValue;
157 * The keyword modifier string.
159 private String keyword;
162 * Creates a new modifier with the given keyword.
164 * Note: this constructor is private. The only instances
165 * ever created are the ones for the standard modifiers.
168 * @param keyword the character sequence for the modifier
169 * @param flagValue flag value as described in the Java Virtual Machine Specification
171 private ModifierKeyword(String keyword, int flagValue) {
172 this.keyword = keyword;
173 this.flagValue = flagValue;
177 * Returns the modifier flag value corresponding to this modifier keyword.
178 * These flag values are as described in the Java Virtual Machine Specification.
180 * @return one of the <code>Modifier</code> constants
181 * @see #fromFlagValue(int)
183 public int toFlagValue() {
184 return this.flagValue;
188 * Returns the keyword for the modifier.
190 * @return the keyword for the modifier
191 * @see #toKeyword(String)
193 public String toString() {
199 * "abstract" modifier constant (bit mask).
200 * Applicable to types and methods.
203 public static final int ABSTRACT = 0x0400;
206 * "final" modifier constant (bit mask).
207 * Applicable to types, methods, fields, and variables.
210 public static final int FINAL = 0x0010;
213 * The "keyword" structural property of this node type.
216 public static final SimplePropertyDescriptor KEYWORD_PROPERTY =
217 new SimplePropertyDescriptor(Modifier.class, "keyword", Modifier.ModifierKeyword.class, MANDATORY); //$NON-NLS-1$
220 * "native" modifier constant (bit mask).
221 * Applicable only to methods.
224 public static final int NATIVE = 0x0100;
227 * Modifier constant (bit mask, value 0) indicating no modifiers.
230 public static final int NONE = 0x0000;
233 * "private" modifier constant (bit mask).
234 * Applicable to types, methods, constructors, and fields.
237 public static final int PRIVATE = 0x0002;
240 * A list of property descriptors (element type:
241 * {@link StructuralPropertyDescriptor}),
242 * or null if uninitialized.
244 private static final List PROPERTY_DESCRIPTORS;
247 * "protected" modifier constant (bit mask).
248 * Applicable to types, methods, constructors, and fields.
251 public static final int PROTECTED = 0x0004;
254 * "public" modifier constant (bit mask).
255 * Applicable to types, methods, constructors, and fields.
258 public static final int PUBLIC = 0x0001;
261 * "static" modifier constant (bit mask).
262 * Applicable to types, methods, fields, and initializers.
265 public static final int STATIC = 0x0008;
268 * "strictfp" modifier constant (bit mask).
269 * Applicable to types and methods.
272 public static final int STRICTFP = 0x0800;
275 * "synchronized" modifier constant (bit mask).
276 * Applicable only to methods.
279 public static final int SYNCHRONIZED = 0x0020;
282 * "transient" modifier constant (bit mask).
283 * Applicable only to fields.
286 public static final int TRANSIENT = 0x0080;
289 * "volatile" modifier constant (bit mask).
290 * Applicable only to fields.
293 public static final int VOLATILE = 0x0040;
296 List properyList = new ArrayList(2);
297 createPropertyList(Modifier.class, properyList);
298 addProperty(KEYWORD_PROPERTY, properyList);
299 PROPERTY_DESCRIPTORS = reapPropertyList(properyList);
303 * Returns whether the given flags includes the "abstract" modifier.
304 * Applicable to types and methods.
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
311 public static boolean isAbstract(int flags) {
312 return (flags & ABSTRACT) != 0;
316 * Returns whether the given flags includes the "final" modifier.
317 * Applicable to types, methods, fields, and variables.
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
324 public static boolean isFinal(int flags) {
325 return (flags & FINAL) != 0;
329 * Returns whether the given flags includes the "native" modifier.
330 * Applicable only to methods.
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
337 public static boolean isNative(int flags) {
338 return (flags & NATIVE) != 0;
342 * Returns whether the given flags includes the "private" modifier.
343 * Applicable to types, methods, constructors, and fields.
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
350 public static boolean isPrivate(int flags) {
351 return (flags & PRIVATE) != 0;
355 * Returns whether the given flags includes the "protected" modifier.
356 * Applicable to types, methods, constructors, and fields.
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
363 public static boolean isProtected(int flags) {
364 return (flags & PROTECTED) != 0;
368 * Returns whether the given flags includes the "public" modifier.
369 * Applicable to types, methods, constructors, and fields.
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
376 public static boolean isPublic(int flags) {
377 return (flags & PUBLIC) != 0;
381 * Returns whether the given flags includes the "static" modifier.
382 * Applicable to types, methods, fields, and initializers.
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
389 public static boolean isStatic(int flags) {
390 return (flags & STATIC) != 0;
394 * Returns whether the given flags includes the "strictfp" modifier.
395 * Applicable to types and methods.
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
402 public static boolean isStrictfp(int flags) {
403 return (flags & STRICTFP) != 0;
407 * Returns whether the given flags includes the "synchronized" modifier.
408 * Applicable only to methods.
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
415 public static boolean isSynchronized(int flags) {
416 return (flags & SYNCHRONIZED) != 0;
420 * Returns whether the given flags includes the "transient" modifier.
421 * Applicable only to fields.
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
428 public static boolean isTransient(int flags) {
429 return (flags & TRANSIENT) != 0;
433 * Returns whether the given flags includes the "volatile" modifier.
434 * Applicable only to fields.
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
441 public static boolean isVolatile(int flags) {
442 return (flags & VOLATILE) != 0;
446 * Returns a list of structural property descriptors for this node type.
447 * Clients must not modify the result.
449 * @param apiLevel the API level; one of the
450 * <code>AST.JLS*</code> constants
452 * @return a list of property descriptors (element type:
453 * {@link StructuralPropertyDescriptor})
456 public static List propertyDescriptors(int apiLevel) {
457 return PROPERTY_DESCRIPTORS;
461 * The modifier keyword; defaults to an unspecified modifier.
464 private ModifierKeyword modifierKeyword = ModifierKeyword.PUBLIC_KEYWORD;
467 * Creates a new unparented modifier node owned by the given AST.
468 * By default, the node has unspecified (but legal) modifier.
470 * N.B. This constructor is package-private.
473 * @param ast the AST that is to own this node
481 /* (omit javadoc for this method)
482 * Method declared on ASTNode.
485 void accept0(ASTVisitor visitor) {
487 visitor.endVisit(this);
490 /* (omit javadoc for this method)
491 * Method declared on ASTNode.
494 ASTNode clone0(AST target) {
495 Modifier result = new Modifier(target);
496 result.setSourceRange(this.getStartPosition(), this.getLength());
497 result.setKeyword(getKeyword());
502 * Returns the modifier keyword of this modifier node.
504 * @return the modifier keyword
507 public ModifierKeyword getKeyword() {
508 return this.modifierKeyword;
511 /* (omit javadoc for this method)
512 * Method declared on ASTNode.
515 final int getNodeType0() {
519 /* (omit javadoc for this method)
520 * Method declared on ASTNode.
522 final Object internalGetSetObjectProperty(SimplePropertyDescriptor property, boolean get, Object value) {
523 if (property == KEYWORD_PROPERTY) {
527 setKeyword((ModifierKeyword) value);
531 // allow default implementation to flag the error
532 return super.internalGetSetObjectProperty(property, get, value);
535 /* (omit javadoc for this method)
536 * Method declared on ASTNode.
538 final List internalStructuralPropertiesForType(int apiLevel) {
539 return propertyDescriptors(apiLevel);
543 * Answer true if the receiver is the abstract modifier, false otherwise.
545 * @return true if the receiver is the abstract modifier, false otherwise
548 public boolean isAbstract() {
549 return this.modifierKeyword == ModifierKeyword.ABSTRACT_KEYWORD;
553 * @see IExtendedModifier#isAnnotation()
555 public boolean isAnnotation() {
560 * Answer true if the receiver is the final modifier, false otherwise.
562 * @return true if the receiver is the final modifier, false otherwise
565 public boolean isFinal() {
566 return this.modifierKeyword == ModifierKeyword.FINAL_KEYWORD;
570 * @see IExtendedModifier#isModifier()
572 public boolean isModifier() {
577 * Answer true if the receiver is the native modifier, false otherwise.
579 * @return true if the receiver is the native modifier, false otherwise
582 public boolean isNative() {
583 return this.modifierKeyword == ModifierKeyword.NATIVE_KEYWORD;
587 * Answer true if the receiver is the private modifier, false otherwise.
589 * @return true if the receiver is the private modifier, false otherwise
592 public boolean isPrivate() {
593 return this.modifierKeyword == ModifierKeyword.PRIVATE_KEYWORD;
597 * Answer true if the receiver is the protected modifier, false otherwise.
599 * @return true if the receiver is the protected modifier, false otherwise
602 public boolean isProtected() {
603 return this.modifierKeyword == ModifierKeyword.PROTECTED_KEYWORD;
607 * Answer true if the receiver is the public modifier, false otherwise.
609 * @return true if the receiver is the public modifier, false otherwise
612 public boolean isPublic() {
613 return this.modifierKeyword == ModifierKeyword.PUBLIC_KEYWORD;
617 * Answer true if the receiver is the static modifier, false otherwise.
619 * @return true if the receiver is the static modifier, false otherwise
622 public boolean isStatic() {
623 return this.modifierKeyword == ModifierKeyword.STATIC_KEYWORD;
627 * Answer true if the receiver is the strictfp modifier, false otherwise.
629 * @return true if the receiver is the strictfp modifier, false otherwise
632 public boolean isStrictfp() {
633 return this.modifierKeyword == ModifierKeyword.STRICTFP_KEYWORD;
637 * Answer true if the receiver is the synchronized modifier, false otherwise.
639 * @return true if the receiver is the synchronized modifier, false otherwise
642 public boolean isSynchronized() {
643 return this.modifierKeyword == ModifierKeyword.SYNCHRONIZED_KEYWORD;
647 * Answer true if the receiver is the transient modifier, false otherwise.
649 * @return true if the receiver is the transient modifier, false otherwise
652 public boolean isTransient() {
653 return this.modifierKeyword == ModifierKeyword.TRANSIENT_KEYWORD;
657 * Answer true if the receiver is the volatile modifier, false otherwise.
659 * @return true if the receiver is the volatile modifier, false otherwise
662 public boolean isVolatile() {
663 return this.modifierKeyword == ModifierKeyword.VOLATILE_KEYWORD;
666 /* (omit javadoc for this method)
667 * Method declared on ASTNode.
671 // treat ModifierKeyword as free
672 return BASE_NODE_SIZE + 1 * 4;
676 * Sets the modifier keyword of this modifier node.
678 * @param modifierKeyord the modifier keyword
679 * @exception IllegalArgumentException if the argument is <code>null</code>
682 public void setKeyword(ModifierKeyword modifierKeyord) {
683 if (modifierKeyord == null) {
684 throw new IllegalArgumentException();
686 preValueChange(KEYWORD_PROPERTY);
687 this.modifierKeyword = modifierKeyord;
688 postValueChange(KEYWORD_PROPERTY);
691 /* (omit javadoc for this method)
692 * Method declared on ASTNode.
695 final boolean subtreeMatch0(ASTMatcher matcher, Object other) {
696 // dispatch to correct overloaded match method
697 return matcher.match(this, other);
700 /* (omit javadoc for this method)
701 * Method declared on ASTNode.