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