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