1 /* 2 * Copyright (c) 1996, 2013, 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.security.AccessController; 29 import sun.reflect.LangReflectAccess; 30 import sun.reflect.ReflectionFactory; 31 32 /** 33 * The Modifier class provides {@code static} methods and 34 * constants to decode class and member access modifiers. The sets of 35 * modifiers are represented as integers with distinct bit positions 36 * representing different modifiers. The values for the constants 37 * representing the modifiers are taken from the tables in sections 4.1, 4.4, 4.5, and 4.7 of 38 * <cite>The Java™ Virtual Machine Specification</cite>. 39 * 40 * @see Class#getModifiers() 41 * @see Member#getModifiers() 42 * 43 * @author Nakul Saraiya 44 * @author Kenneth Russell 45 */ 46 public class Modifier { 47 48 /* 49 * Bootstrapping protocol between java.lang and java.lang.reflect 50 * packages 51 */ 52 static { 53 sun.reflect.ReflectionFactory factory = 54 AccessController.doPrivileged( 55 new ReflectionFactory.GetReflectionFactoryAction()); 56 factory.setLangReflectAccess(new java.lang.reflect.ReflectAccess()); 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 */ 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 */ 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 */ 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 */ 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 */ 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 */ 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 */ 139 public static boolean isVolatile(int mod) { 140 return (mod & VOLATILE) != 0; 141 } 142 143 /** 144 * Return {@code true} if the integer argument includes the 145 * {@code transient} modifier, {@code false} otherwise. 146 * 147 * @param mod a set of modifiers 148 * @return {@code true} if {@code mod} includes the 149 * {@code transient} modifier; {@code false} otherwise. 150 */ 151 public static boolean isTransient(int mod) { 152 return (mod & TRANSIENT) != 0; 153 } 154 155 /** 156 * Return {@code true} if the integer argument includes the 157 * {@code native} modifier, {@code false} otherwise. 158 * 159 * @param mod a set of modifiers 160 * @return {@code true} if {@code mod} includes the 161 * {@code native} modifier; {@code false} otherwise. 162 */ 163 public static boolean isNative(int mod) { 164 return (mod & NATIVE) != 0; 165 } 166 167 /** 168 * Return {@code true} if the integer argument includes the 169 * {@code interface} modifier, {@code false} otherwise. 170 * 171 * @param mod a set of modifiers 172 * @return {@code true} if {@code mod} includes the 173 * {@code interface} modifier; {@code false} otherwise. 174 */ 175 public static boolean isInterface(int mod) { 176 return (mod & INTERFACE) != 0; 177 } 178 179 /** 180 * Return {@code true} if the integer argument includes the 181 * {@code abstract} modifier, {@code false} otherwise. 182 * 183 * @param mod a set of modifiers 184 * @return {@code true} if {@code mod} includes the 185 * {@code abstract} modifier; {@code false} otherwise. 186 */ 187 public static boolean isAbstract(int mod) { 188 return (mod & ABSTRACT) != 0; 189 } 190 191 /** 192 * Return {@code true} if the integer argument includes the 193 * {@code strictfp} modifier, {@code false} otherwise. 194 * 195 * @param mod a set of modifiers 196 * @return {@code true} if {@code mod} includes the 197 * {@code strictfp} modifier; {@code false} otherwise. 198 */ 199 public static boolean isStrict(int mod) { 200 return (mod & STRICT) != 0; 201 } 202 203 /** 204 * Return a string describing the access modifier flags in 205 * the specified modifier. For example: 206 * <blockquote><pre> 207 * public final synchronized strictfp 208 * </pre></blockquote> 209 * The modifier names are returned in an order consistent with the 210 * suggested modifier orderings given in sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of 211 * <cite>The Java™ Language Specification</cite>. 212 * The full modifier ordering used by this method is: 213 * <blockquote> {@code 214 * public protected private abstract static final transient 215 * volatile synchronized native strictfp 216 * interface } </blockquote> 217 * The {@code interface} modifier discussed in this class is 218 * not a true modifier in the Java language and it appears after 219 * all other modifiers listed by this method. This method may 220 * return a string of modifiers that are not valid modifiers of a 221 * Java entity; in other words, no checking is done on the 222 * possible validity of the combination of modifiers represented 223 * by the input. 224 * 225 * Note that to perform such checking for a known kind of entity, 226 * such as a constructor or method, first AND the argument of 227 * {@code toString} with the appropriate mask from a method like 228 * {@link #constructorModifiers} or {@link #methodModifiers}. 229 * 230 * @param mod a set of modifiers 231 * @return a string representation of the set of modifiers 232 * represented by {@code mod} 233 */ 234 public static String toString(int mod) { 235 StringBuffer sb = new StringBuffer(); 236 int len; 237 238 if ((mod & PUBLIC) != 0) sb.append("public "); 239 if ((mod & PROTECTED) != 0) sb.append("protected "); 240 if ((mod & PRIVATE) != 0) sb.append("private "); 241 242 /* Canonical order */ 243 if ((mod & ABSTRACT) != 0) sb.append("abstract "); 244 if ((mod & STATIC) != 0) sb.append("static "); 245 if ((mod & FINAL) != 0) sb.append("final "); 246 if ((mod & TRANSIENT) != 0) sb.append("transient "); 247 if ((mod & VOLATILE) != 0) sb.append("volatile "); 248 if ((mod & SYNCHRONIZED) != 0) sb.append("synchronized "); 249 if ((mod & NATIVE) != 0) sb.append("native "); 250 if ((mod & STRICT) != 0) sb.append("strictfp "); 251 if ((mod & INTERFACE) != 0) sb.append("interface "); 252 253 if ((len = sb.length()) > 0) /* trim trailing space */ 254 return sb.toString().substring(0, len-1); 255 return ""; 256 } 257 258 /* 259 * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of 260 * <cite>The Java™ Virtual Machine Specification</cite> 261 */ 262 263 /** 264 * The {@code int} value representing the {@code public} 265 * modifier. 266 */ 267 public static final int PUBLIC = 0x00000001; 268 269 /** 270 * The {@code int} value representing the {@code private} 271 * modifier. 272 */ 273 public static final int PRIVATE = 0x00000002; 274 275 /** 276 * The {@code int} value representing the {@code protected} 277 * modifier. 278 */ 279 public static final int PROTECTED = 0x00000004; 280 281 /** 282 * The {@code int} value representing the {@code static} 283 * modifier. 284 */ 285 public static final int STATIC = 0x00000008; 286 287 /** 288 * The {@code int} value representing the {@code final} 289 * modifier. 290 */ 291 public static final int FINAL = 0x00000010; 292 293 /** 294 * The {@code int} value representing the {@code synchronized} 295 * modifier. 296 */ 297 public static final int SYNCHRONIZED = 0x00000020; 298 299 /** 300 * The {@code int} value representing the {@code volatile} 301 * modifier. 302 */ 303 public static final int VOLATILE = 0x00000040; 304 305 /** 306 * The {@code int} value representing the {@code transient} 307 * modifier. 308 */ 309 public static final int TRANSIENT = 0x00000080; 310 311 /** 312 * The {@code int} value representing the {@code native} 313 * modifier. 314 */ 315 public static final int NATIVE = 0x00000100; 316 317 /** 318 * The {@code int} value representing the {@code interface} 319 * modifier. 320 */ 321 public static final int INTERFACE = 0x00000200; 322 323 /** 324 * The {@code int} value representing the {@code abstract} 325 * modifier. 326 */ 327 public static final int ABSTRACT = 0x00000400; 328 329 /** 330 * The {@code int} value representing the {@code strictfp} 331 * modifier. 332 */ 333 public static final int STRICT = 0x00000800; 334 335 // Bits not (yet) exposed in the public API either because they 336 // have different meanings for fields and methods and there is no 337 // way to distinguish between the two in this class, or because 338 // they are not Java programming language keywords 339 static final int BRIDGE = 0x00000040; 340 static final int VARARGS = 0x00000080; 341 static final int SYNTHETIC = 0x00001000; 342 static final int ANNOTATION = 0x00002000; 343 static final int ENUM = 0x00004000; 344 static final int MANDATED = 0x00008000; 345 static boolean isSynthetic(int mod) { 346 return (mod & SYNTHETIC) != 0; 347 } 348 349 static boolean isMandated(int mod) { 350 return (mod & MANDATED) != 0; 351 } 352 353 /** 354 * See JLSv3 section 8.1.1. 355 */ 356 private static final int CLASS_MODIFIERS = 357 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | 358 Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL | 359 Modifier.STRICT; 360 361 /** 362 * See JLSv3 section 9.1.1. 363 */ 364 private static final int INTERFACE_MODIFIERS = 365 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | 366 Modifier.ABSTRACT | Modifier.STATIC | Modifier.STRICT; 367 368 369 /** 370 * See JLSv3 section 8.8.3. 371 */ 372 private static final int CONSTRUCTOR_MODIFIERS = 373 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE; 374 375 /** 376 * See JLSv3 section 8.4.3. 377 */ 378 private static final int METHOD_MODIFIERS = 379 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | 380 Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL | 381 Modifier.SYNCHRONIZED | Modifier.NATIVE | Modifier.STRICT; 382 383 /** 384 * See JLSv3 section 8.3.1. 385 */ 386 private static final int FIELD_MODIFIERS = 387 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | 388 Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT | 389 Modifier.VOLATILE; 390 391 /** 392 * 393 */ 394 static final int ACCESS_MODIFIERS = 395 Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE; 396 397 /** 398 * Return an {@code int} value OR-ing together the source language 399 * modifiers that can be applied to a class. 400 * @return an {@code int} value OR-ing together the source language 401 * modifiers that can be applied to a class. 402 * 403 * @jls 8.1.1 Class Modifiers 404 * @since 1.7 405 */ 406 public static int classModifiers() { 407 return CLASS_MODIFIERS; 408 } 409 410 /** 411 * Return an {@code int} value OR-ing together the source language 412 * modifiers that can be applied to an interface. 413 * @return an {@code int} value OR-ing together the source language 414 * modifiers that can be applied to an inteface. 415 * 416 * @jls 9.1.1 Interface Modifiers 417 * @since 1.7 418 */ 419 public static int interfaceModifiers() { 420 return INTERFACE_MODIFIERS; 421 } 422 423 /** 424 * Return an {@code int} value OR-ing together the source language 425 * modifiers that can be applied to a constructor. 426 * @return an {@code int} value OR-ing together the source language 427 * modifiers that can be applied to a constructor. 428 * 429 * @jls 8.8.3 Constructor Modifiers 430 * @since 1.7 431 */ 432 public static int constructorModifiers() { 433 return CONSTRUCTOR_MODIFIERS; 434 } 435 436 /** 437 * Return an {@code int} value OR-ing together the source language 438 * modifiers that can be applied to a method. 439 * @return an {@code int} value OR-ing together the source language 440 * modifiers that can be applied to a method. 441 * 442 * @jls 8.4.3 Method Modifiers 443 * @since 1.7 444 */ 445 public static int methodModifiers() { 446 return METHOD_MODIFIERS; 447 } 448 449 450 /** 451 * Return an {@code int} value OR-ing together the source language 452 * modifiers that can be applied to a field. 453 * @return an {@code int} value OR-ing together the source language 454 * modifiers that can be applied to a field. 455 * 456 * @jls 8.3.1 Field Modifiers 457 * @since 1.7 458 */ 459 public static int fieldModifiers() { 460 return FIELD_MODIFIERS; 461 } 462 }