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