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