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