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