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