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