1 /* 2 * Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang.reflect; 27 28 import java.util.StringJoiner; 29 30 /** 31 * The Modifier class provides {@code static} methods and 32 * constants to decode class and member access modifiers. 33 * The {@link AccessFlag} class should be used instead of this class. 34 * The sets of modifiers are represented as integers with non-distinct bit positions 35 * representing different modifiers. The values for the constants 36 * representing the modifiers are taken from the tables in sections 37 * {@jvms 4.1}, {@jvms 4.4}, {@jvms 4.5}, and {@jvms 4.7} of 38 * <cite>The Java Virtual Machine Specification</cite>. 39 * 40 * @see Class#accessFlags() 41 * @see Member#accessFlags() 42 * @see Class#getModifiers() 43 * @see Member#getModifiers() 44 * 45 * @author Nakul Saraiya 46 * @author Kenneth Russell 47 * @since 1.1 48 */ 49 public class Modifier { 50 /** 51 * Do not call. 52 */ 53 private Modifier() {throw new AssertionError();} 54 55 56 /** 57 * Return {@code true} if the integer argument includes the 58 * {@code public} modifier, {@code false} otherwise. 59 * 60 * @param mod a set of modifiers 61 * @return {@code true} if {@code mod} includes the 62 * {@code public} modifier; {@code false} otherwise. 63 */ 64 public static boolean isPublic(int mod) { 65 return (mod & PUBLIC) != 0; 66 } 67 68 /** 69 * Return {@code true} if the integer argument includes the 70 * {@code private} modifier, {@code false} otherwise. 71 * 72 * @param mod a set of modifiers 73 * @return {@code true} if {@code mod} includes the 74 * {@code private} modifier; {@code false} otherwise. 75 */ 76 public static boolean isPrivate(int mod) { 77 return (mod & PRIVATE) != 0; 78 } 79 80 /** 81 * Return {@code true} if the integer argument includes the 82 * {@code protected} modifier, {@code false} otherwise. 83 * 84 * @param mod a set of modifiers 85 * @return {@code true} if {@code mod} includes the 86 * {@code protected} modifier; {@code false} otherwise. 87 */ 88 public static boolean isProtected(int mod) { 89 return (mod & PROTECTED) != 0; 90 } 91 92 /** 93 * Return {@code true} if the integer argument includes the 94 * {@code static} modifier, {@code false} otherwise. 95 * 96 * @param mod a set of modifiers 97 * @return {@code true} if {@code mod} includes the 98 * {@code static} modifier; {@code false} otherwise. 99 */ 100 public static boolean isStatic(int mod) { 101 return (mod & STATIC) != 0; 102 } 103 104 /** 105 * Return {@code true} if the integer argument includes the 106 * {@code final} modifier, {@code false} otherwise. 107 * 108 * @param mod a set of modifiers 109 * @return {@code true} if {@code mod} includes the 110 * {@code final} modifier; {@code false} otherwise. 111 */ 112 public static boolean isFinal(int mod) { 113 return (mod & FINAL) != 0; 114 } 115 116 /** 117 * Return {@code true} if the integer argument includes the 118 * {@code synchronized} modifier, {@code false} otherwise. 119 * 120 * @apiNote {@code isSynchronized} should only be called with the modifiers 121 * of a {@linkplain Method#getModifiers() method}. 122 * 123 * @param mod a set of modifiers 124 * @return {@code true} if {@code mod} includes the 125 * {@code synchronized} modifier; {@code false} otherwise. 126 */ 127 public static boolean isSynchronized(int mod) { 128 return (mod & SYNCHRONIZED) != 0; 129 } 130 131 /** 132 * Return {@code true} if the integer argument includes the 133 * {@code identity} modifier, {@code false} otherwise. 134 * 135 * @apiNote {@code isIdentity} should only be called with the modifiers 136 * of a {@linkplain Class#getModifiers() class}. 137 * 138 * @param mod a set of modifiers 139 * @return {@code true} if {@code mod} includes the 140 * {@code identity} modifier; {@code false} otherwise. 141 */ 142 public static boolean isIdentity(int mod) { 143 return (mod & IDENTITY) != 0; 144 } 145 146 /** 147 * Return {@code true} if the integer argument includes the 148 * {@code volatile} modifier, {@code false} otherwise. 149 * 150 * @param mod a set of modifiers 151 * @return {@code true} if {@code mod} includes the 152 * {@code volatile} modifier; {@code false} otherwise. 153 */ 154 public static boolean isVolatile(int mod) { 155 return (mod & VOLATILE) != 0; 156 } 157 158 /** 159 * Return {@code true} if the integer argument includes the 160 * {@code value} modifier, {@code false} otherwise. 161 * 162 * @apiNote {@code isValue} should only be called with the modifiers 163 * of a {@linkplain Class#getModifiers() class}. 164 * 165 * @param mod a set of modifiers 166 * @return {@code true} if {@code mod} includes the 167 * {@code value} modifier; {@code false} otherwise. 168 */ 169 public static boolean isValue(int mod) { 170 return (mod & VALUE) != 0; 171 } 172 173 /** 174 * Return {@code true} if the integer argument includes the 175 * {@code transient} modifier, {@code false} otherwise. 176 * 177 * @param mod a set of modifiers 178 * @return {@code true} if {@code mod} includes the 179 * {@code transient} modifier; {@code false} otherwise. 180 */ 181 public static boolean isTransient(int mod) { 182 return (mod & TRANSIENT) != 0; 183 } 184 185 /** 186 * Return {@code true} if the integer argument includes the 187 * {@code native} modifier, {@code false} otherwise. 188 * 189 * @param mod a set of modifiers 190 * @return {@code true} if {@code mod} includes the 191 * {@code native} modifier; {@code false} otherwise. 192 */ 193 public static boolean isNative(int mod) { 194 return (mod & NATIVE) != 0; 195 } 196 197 /** 198 * Return {@code true} if the integer argument includes the 199 * {@code interface} modifier, {@code false} otherwise. 200 * 201 * @param mod a set of modifiers 202 * @return {@code true} if {@code mod} includes the 203 * {@code interface} modifier; {@code false} otherwise. 204 */ 205 public static boolean isInterface(int mod) { 206 return (mod & INTERFACE) != 0; 207 } 208 209 /** 210 * Return {@code true} if the integer argument includes the 211 * {@code abstract} modifier, {@code false} otherwise. 212 * 213 * @param mod a set of modifiers 214 * @return {@code true} if {@code mod} includes the 215 * {@code abstract} modifier; {@code false} otherwise. 216 */ 217 public static boolean isAbstract(int mod) { 218 return (mod & ABSTRACT) != 0; 219 } 220 221 /** 222 * Return {@code true} if the integer argument includes the 223 * {@code strictfp} modifier, {@code false} otherwise. 224 * 225 * @param mod a set of modifiers 226 * @return {@code true} if {@code mod} includes the 227 * {@code strictfp} modifier; {@code false} otherwise. 228 */ 229 public static boolean isStrict(int mod) { 230 return (mod & STRICT) != 0; 231 } 232 233 /** 234 * Return a string describing the access modifier flags in 235 * the specified modifier. For example: 236 * <blockquote><pre> 237 * public final synchronized strictfp 238 * </pre></blockquote> 239 * The modifier names are returned in an order consistent with the 240 * suggested modifier orderings given in sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of 241 * <cite>The Java Language Specification</cite>. 242 * The full modifier ordering used by this method is: 243 * <blockquote> {@code 244 * public protected private abstract static final transient 245 * volatile synchronized native strictfp 246 * interface } </blockquote> 247 * The {@code interface} modifier discussed in this class is 248 * not a true modifier in the Java language and it appears after 249 * all other modifiers listed by this method. This method may 250 * return a string of modifiers that are not valid modifiers of a 251 * Java entity; in other words, no checking is done on the 252 * possible validity of the combination of modifiers represented 253 * by the input. 254 * 255 * Note that to perform such checking for a known kind of entity, 256 * such as a constructor or method, first AND the argument of 257 * {@code toString} with the appropriate mask from a method like 258 * {@link #constructorModifiers} or {@link #methodModifiers}. 259 * 260 * @param mod a set of modifiers 261 * @return a string representation of the set of modifiers 262 * represented by {@code mod} 263 */ 264 public static String toString(int mod) { 265 StringJoiner sj = new StringJoiner(" "); 266 267 if ((mod & PUBLIC) != 0) sj.add("public"); 268 if ((mod & PROTECTED) != 0) sj.add("protected"); 269 if ((mod & PRIVATE) != 0) sj.add("private"); 270 271 /* Canonical order */ 272 if ((mod & ABSTRACT) != 0) sj.add("abstract"); 273 if ((mod & STATIC) != 0) sj.add("static"); 274 if ((mod & FINAL) != 0) sj.add("final"); 275 if ((mod & TRANSIENT) != 0) sj.add("transient"); 276 if ((mod & VOLATILE) != 0) sj.add("volatile"); 277 if ((mod & SYNCHRONIZED) != 0) sj.add("synchronized"); 278 if ((mod & NATIVE) != 0) sj.add("native"); 279 if ((mod & STRICT) != 0) sj.add("strictfp"); 280 if ((mod & INTERFACE) != 0) sj.add("interface"); 281 282 return sj.toString(); 283 } 284 285 /* 286 * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of 287 * <cite>The Java Virtual Machine Specification</cite> 288 */ 289 290 /** 291 * The {@code int} value representing the {@code public} 292 * modifier. 293 * @see AccessFlag#PUBLIC 294 */ 295 public static final int PUBLIC = 0x00000001; 296 297 /** 298 * The {@code int} value representing the {@code private} 299 * modifier. 300 * @see AccessFlag#PRIVATE 301 */ 302 public static final int PRIVATE = 0x00000002; 303 304 /** 305 * The {@code int} value representing the {@code protected} 306 * modifier. 307 * @see AccessFlag#PROTECTED 308 */ 309 public static final int PROTECTED = 0x00000004; 310 311 /** 312 * The {@code int} value representing the {@code static} 313 * modifier. 314 * @see AccessFlag#STATIC 315 */ 316 public static final int STATIC = 0x00000008; 317 318 /** 319 * The {@code int} value representing the {@code final} 320 * modifier. 321 * @see AccessFlag#FINAL 322 */ 323 public static final int FINAL = 0x00000010; 324 325 /** 326 * The {@code int} value representing the {@code synchronized} 327 * modifier. 328 * @see AccessFlag#SYNCHRONIZED 329 */ 330 public static final int SYNCHRONIZED = 0x00000020; 331 332 /** 333 * The {@code int} value representing the {@code ACC_IDENTITY} 334 * modifier. 335 */ 336 public static final int IDENTITY = 0x00000020; 337 338 /** 339 * The {@code int} value representing the {@code value} 340 * modifier. 341 * @see AccessFlag#VALUE 342 */ 343 public static final int VALUE = 0x00000040; 344 345 /** 346 * The {@code int} value representing the {@code volatile} 347 * modifier. 348 * @see AccessFlag#VOLATILE 349 */ 350 public static final int VOLATILE = 0x00000040; 351 352 /** 353 * The {@code int} value representing the {@code transient} 354 * modifier. 355 * @see AccessFlag#TRANSIENT 356 */ 357 public static final int TRANSIENT = 0x00000080; 358 359 /** 360 * The {@code int} value representing the {@code native} 361 * modifier. 362 * @see AccessFlag#NATIVE 363 */ 364 public static final int NATIVE = 0x00000100; 365 366 /** 367 * The {@code int} value representing the {@code interface} 368 * modifier. 369 * @see AccessFlag#INTERFACE 370 */ 371 public static final int INTERFACE = 0x00000200; 372 373 /** 374 * The {@code int} value representing the {@code abstract} 375 * modifier. 376 * @see AccessFlag#ABSTRACT 377 */ 378 public static final int ABSTRACT = 0x00000400; 379 380 /** 381 * The {@code int} value representing the {@code strictfp} 382 * modifier. 383 * @see AccessFlag#STRICT 384 */ 385 public static final int STRICT = 0x00000800; 386 387 // Bits not (yet) exposed in the public API either because they 388 // have different meanings for fields and methods and there is no 389 // way to distinguish between the two in this class, or because 390 // they are not Java programming language keywords 391 static final int BRIDGE = 0x00000040; 392 static final int VARARGS = 0x00000080; 393 static final int SYNTHETIC = 0x00001000; 394 static final int ANNOTATION = 0x00002000; 395 static final int ENUM = 0x00004000; 396 static final int MANDATED = 0x00008000; 397 static boolean isSynthetic(int mod) { 398 return (mod & SYNTHETIC) != 0; 399 } 400 401 static boolean isMandated(int mod) { 402 return (mod & MANDATED) != 0; 403 } 404 405 // Note on the FOO_MODIFIERS fields and fooModifiers() methods: 406 // the sets of modifiers are not guaranteed to be constants 407 // across time and Java SE releases. Therefore, it would not be 408 // appropriate to expose an external interface to this information 409 // that would allow the values to be treated as Java-level 410 // constants since the values could be constant folded and updates 411 // to the sets of modifiers missed. Thus, the fooModifiers() 412 // methods return an unchanging values for a given release, but a 413 // value that can potentially change over time. 414 415 /** 416 * The Java source modifiers that can be applied to a class. 417 * @jls 8.1.1 Class Modifiers 418 */ 419 private static final int CLASS_MODIFIERS = 420 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | 421 Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL | 422 Modifier.IDENTITY | Modifier.VALUE | 423 Modifier.STRICT; 424 425 /** 426 * The Java source modifiers that can be applied to an interface. 427 * @jls 9.1.1 Interface Modifiers 428 */ 429 private static final int INTERFACE_MODIFIERS = 430 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | 431 Modifier.ABSTRACT | Modifier.STATIC | Modifier.STRICT; 432 433 434 /** 435 * The Java source modifiers that can be applied to a constructor. 436 * @jls 8.8.3 Constructor Modifiers 437 */ 438 private static final int CONSTRUCTOR_MODIFIERS = 439 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE; 440 441 /** 442 * The Java source modifiers that can be applied to a method. 443 * @jls 8.4.3 Method Modifiers 444 */ 445 private static final int METHOD_MODIFIERS = 446 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | 447 Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL | 448 Modifier.SYNCHRONIZED | Modifier.NATIVE | Modifier.STRICT; 449 450 /** 451 * The Java source modifiers that can be applied to a field. 452 * @jls 8.3.1 Field Modifiers 453 */ 454 private static final int FIELD_MODIFIERS = 455 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | 456 Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT | 457 Modifier.VOLATILE; 458 459 /** 460 * The Java source modifiers that can be applied to a method or constructor parameter. 461 * @jls 8.4.1 Formal Parameters 462 */ 463 private static final int PARAMETER_MODIFIERS = 464 Modifier.FINAL; 465 466 static final int ACCESS_MODIFIERS = 467 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE; 468 469 /** 470 * Return an {@code int} value OR-ing together the source language 471 * modifiers that can be applied to a class. 472 * @return an {@code int} value OR-ing together the source language 473 * modifiers that can be applied to a class. 474 * 475 * @jls 8.1.1 Class Modifiers 476 * @since 1.7 477 */ 478 public static int classModifiers() { 479 return CLASS_MODIFIERS; 480 } 481 482 /** 483 * Return an {@code int} value OR-ing together the source language 484 * modifiers that can be applied to an interface. 485 * @return an {@code int} value OR-ing together the source language 486 * modifiers that can be applied to an interface. 487 * 488 * @jls 9.1.1 Interface Modifiers 489 * @since 1.7 490 */ 491 public static int interfaceModifiers() { 492 return INTERFACE_MODIFIERS; 493 } 494 495 /** 496 * Return an {@code int} value OR-ing together the source language 497 * modifiers that can be applied to a constructor. 498 * @return an {@code int} value OR-ing together the source language 499 * modifiers that can be applied to a constructor. 500 * 501 * @jls 8.8.3 Constructor Modifiers 502 * @since 1.7 503 */ 504 public static int constructorModifiers() { 505 return CONSTRUCTOR_MODIFIERS; 506 } 507 508 /** 509 * Return an {@code int} value OR-ing together the source language 510 * modifiers that can be applied to a method. 511 * @return an {@code int} value OR-ing together the source language 512 * modifiers that can be applied to a method. 513 * 514 * @jls 8.4.3 Method Modifiers 515 * @since 1.7 516 */ 517 public static int methodModifiers() { 518 return METHOD_MODIFIERS; 519 } 520 521 /** 522 * Return an {@code int} value OR-ing together the source language 523 * modifiers that can be applied to a field. 524 * @return an {@code int} value OR-ing together the source language 525 * modifiers that can be applied to a field. 526 * 527 * @jls 8.3.1 Field Modifiers 528 * @since 1.7 529 */ 530 public static int fieldModifiers() { 531 return FIELD_MODIFIERS; 532 } 533 534 /** 535 * Return an {@code int} value OR-ing together the source language 536 * modifiers that can be applied to a parameter. 537 * @return an {@code int} value OR-ing together the source language 538 * modifiers that can be applied to a parameter. 539 * 540 * @jls 8.4.1 Formal Parameters 541 * @since 1.8 542 */ 543 public static int parameterModifiers() { 544 return PARAMETER_MODIFIERS; 545 } 546 } --- EOF ---