J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1996-2004 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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 <a |
| 38 | * href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/VMSpecTOC.doc.html"><i>The |
| 39 | * Java</i><sup><small>TM</small></sup> <i>Virtual Machine Specification, Second |
| 40 | * edition</i></a> tables |
| 41 | * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75734">4.1</a>, |
| 42 | * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88358">4.4</a>, |
| 43 | * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75568">4.5</a>, and |
| 44 | * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88478">4.7</a>. |
| 45 | * |
| 46 | * @see Class#getModifiers() |
| 47 | * @see Member#getModifiers() |
| 48 | * |
| 49 | * @author Nakul Saraiya |
| 50 | * @author Kenneth Russell |
| 51 | */ |
| 52 | public |
| 53 | class Modifier { |
| 54 | |
| 55 | /* |
| 56 | * Bootstrapping protocol between java.lang and java.lang.reflect |
| 57 | * packages |
| 58 | */ |
| 59 | static { |
| 60 | sun.reflect.ReflectionFactory factory = |
| 61 | (sun.reflect.ReflectionFactory) AccessController.doPrivileged( |
| 62 | new ReflectionFactory.GetReflectionFactoryAction() |
| 63 | ); |
| 64 | factory.setLangReflectAccess(new java.lang.reflect.ReflectAccess()); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Return {@code true} if the integer argument includes the |
| 69 | * {@code public} modifier, {@code false} otherwise. |
| 70 | * |
| 71 | * @param mod a set of modifiers |
| 72 | * @return {@code true} if {@code mod} includes the |
| 73 | * {@code public} modifier; {@code false} otherwise. |
| 74 | */ |
| 75 | public static boolean isPublic(int mod) { |
| 76 | return (mod & PUBLIC) != 0; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Return {@code true} if the integer argument includes the |
| 81 | * {@code private} modifier, {@code false} otherwise. |
| 82 | * |
| 83 | * @param mod a set of modifiers |
| 84 | * @return {@code true} if {@code mod} includes the |
| 85 | * {@code private} modifier; {@code false} otherwise. |
| 86 | */ |
| 87 | public static boolean isPrivate(int mod) { |
| 88 | return (mod & PRIVATE) != 0; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Return {@code true} if the integer argument includes the |
| 93 | * {@code protected} modifier, {@code false} otherwise. |
| 94 | * |
| 95 | * @param mod a set of modifiers |
| 96 | * @return {@code true} if {@code mod} includes the |
| 97 | * {@code protected} modifier; {@code false} otherwise. |
| 98 | */ |
| 99 | public static boolean isProtected(int mod) { |
| 100 | return (mod & PROTECTED) != 0; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Return {@code true} if the integer argument includes the |
| 105 | * {@code static} modifier, {@code false} otherwise. |
| 106 | * |
| 107 | * @param mod a set of modifiers |
| 108 | * @return {@code true} if {@code mod} includes the |
| 109 | * {@code static} modifier; {@code false} otherwise. |
| 110 | */ |
| 111 | public static boolean isStatic(int mod) { |
| 112 | return (mod & STATIC) != 0; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Return {@code true} if the integer argument includes the |
| 117 | * {@code final} modifier, {@code false} otherwise. |
| 118 | * |
| 119 | * @param mod a set of modifiers |
| 120 | * @return {@code true} if {@code mod} includes the |
| 121 | * {@code final} modifier; {@code false} otherwise. |
| 122 | */ |
| 123 | public static boolean isFinal(int mod) { |
| 124 | return (mod & FINAL) != 0; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Return {@code true} if the integer argument includes the |
| 129 | * {@code synchronized} modifier, {@code false} otherwise. |
| 130 | * |
| 131 | * @param mod a set of modifiers |
| 132 | * @return {@code true} if {@code mod} includes the |
| 133 | * {@code synchronized} modifier; {@code false} otherwise. |
| 134 | */ |
| 135 | public static boolean isSynchronized(int mod) { |
| 136 | return (mod & SYNCHRONIZED) != 0; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Return {@code true} if the integer argument includes the |
| 141 | * {@code volatile} modifier, {@code false} otherwise. |
| 142 | * |
| 143 | * @param mod a set of modifiers |
| 144 | * @return {@code true} if {@code mod} includes the |
| 145 | * {@code volatile} modifier; {@code false} otherwise. |
| 146 | */ |
| 147 | public static boolean isVolatile(int mod) { |
| 148 | return (mod & VOLATILE) != 0; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Return {@code true} if the integer argument includes the |
| 153 | * {@code transient} modifier, {@code false} otherwise. |
| 154 | * |
| 155 | * @param mod a set of modifiers |
| 156 | * @return {@code true} if {@code mod} includes the |
| 157 | * {@code transient} modifier; {@code false} otherwise. |
| 158 | */ |
| 159 | public static boolean isTransient(int mod) { |
| 160 | return (mod & TRANSIENT) != 0; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Return {@code true} if the integer argument includes the |
| 165 | * {@code native} modifier, {@code false} otherwise. |
| 166 | * |
| 167 | * @param mod a set of modifiers |
| 168 | * @return {@code true} if {@code mod} includes the |
| 169 | * {@code native} modifier; {@code false} otherwise. |
| 170 | */ |
| 171 | public static boolean isNative(int mod) { |
| 172 | return (mod & NATIVE) != 0; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Return {@code true} if the integer argument includes the |
| 177 | * {@code interface} modifier, {@code false} otherwise. |
| 178 | * |
| 179 | * @param mod a set of modifiers |
| 180 | * @return {@code true} if {@code mod} includes the |
| 181 | * {@code interface} modifier; {@code false} otherwise. |
| 182 | */ |
| 183 | public static boolean isInterface(int mod) { |
| 184 | return (mod & INTERFACE) != 0; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Return {@code true} if the integer argument includes the |
| 189 | * {@code abstract} modifier, {@code false} otherwise. |
| 190 | * |
| 191 | * @param mod a set of modifiers |
| 192 | * @return {@code true} if {@code mod} includes the |
| 193 | * {@code abstract} modifier; {@code false} otherwise. |
| 194 | */ |
| 195 | public static boolean isAbstract(int mod) { |
| 196 | return (mod & ABSTRACT) != 0; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Return {@code true} if the integer argument includes the |
| 201 | * {@code strictfp} modifier, {@code false} otherwise. |
| 202 | * |
| 203 | * @param mod a set of modifiers |
| 204 | * @return {@code true} if {@code mod} includes the |
| 205 | * {@code strictfp} modifier; {@code false} otherwise. |
| 206 | */ |
| 207 | public static boolean isStrict(int mod) { |
| 208 | return (mod & STRICT) != 0; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Return a string describing the access modifier flags in |
| 213 | * the specified modifier. For example: |
| 214 | * <blockquote><pre> |
| 215 | * public final synchronized strictfp |
| 216 | * </pre></blockquote> |
| 217 | * The modifier names are returned in an order consistent with the |
| 218 | * suggested modifier orderings given in <a |
| 219 | * href="http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html"><em>The |
| 220 | * Java Language Specification, Second Edition</em></a> sections |
| 221 | * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#21613">§8.1.1</a>, |
| 222 | * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78091">§8.3.1</a>, |
| 223 | * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78188">§8.4.3</a>, |
| 224 | * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#42018">§8.8.3</a>, and |
| 225 | * <a href="http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html#235947">§9.1.1</a>. |
| 226 | * The full modifier ordering used by this method is: |
| 227 | * <blockquote> {@code |
| 228 | * public protected private abstract static final transient |
| 229 | * volatile synchronized native strictfp |
| 230 | * interface } </blockquote> |
| 231 | * The {@code interface} modifier discussed in this class is |
| 232 | * not a true modifier in the Java language and it appears after |
| 233 | * all other modifiers listed by this method. This method may |
| 234 | * return a string of modifiers that are not valid modifiers of a |
| 235 | * Java entity; in other words, no checking is done on the |
| 236 | * possible validity of the combination of modifiers represented |
| 237 | * by the input. |
| 238 | * |
| 239 | * @param mod a set of modifiers |
| 240 | * @return a string representation of the set of modifiers |
| 241 | * represented by {@code mod} |
| 242 | */ |
| 243 | public static String toString(int mod) { |
| 244 | StringBuffer sb = new StringBuffer(); |
| 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 <em>The Java Virtual |
| 269 | * Machine Specification, Second Edition</em>, tables 4.1, 4.4, |
| 270 | * 4.5, and 4.7. |
| 271 | */ |
| 272 | |
| 273 | /** |
| 274 | * The {@code int} value representing the {@code public} |
| 275 | * modifier. |
| 276 | */ |
| 277 | public static final int PUBLIC = 0x00000001; |
| 278 | |
| 279 | /** |
| 280 | * The {@code int} value representing the {@code private} |
| 281 | * modifier. |
| 282 | */ |
| 283 | public static final int PRIVATE = 0x00000002; |
| 284 | |
| 285 | /** |
| 286 | * The {@code int} value representing the {@code protected} |
| 287 | * modifier. |
| 288 | */ |
| 289 | public static final int PROTECTED = 0x00000004; |
| 290 | |
| 291 | /** |
| 292 | * The {@code int} value representing the {@code static} |
| 293 | * modifier. |
| 294 | */ |
| 295 | public static final int STATIC = 0x00000008; |
| 296 | |
| 297 | /** |
| 298 | * The {@code int} value representing the {@code final} |
| 299 | * modifier. |
| 300 | */ |
| 301 | public static final int FINAL = 0x00000010; |
| 302 | |
| 303 | /** |
| 304 | * The {@code int} value representing the {@code synchronized} |
| 305 | * modifier. |
| 306 | */ |
| 307 | public static final int SYNCHRONIZED = 0x00000020; |
| 308 | |
| 309 | /** |
| 310 | * The {@code int} value representing the {@code volatile} |
| 311 | * modifier. |
| 312 | */ |
| 313 | public static final int VOLATILE = 0x00000040; |
| 314 | |
| 315 | /** |
| 316 | * The {@code int} value representing the {@code transient} |
| 317 | * modifier. |
| 318 | */ |
| 319 | public static final int TRANSIENT = 0x00000080; |
| 320 | |
| 321 | /** |
| 322 | * The {@code int} value representing the {@code native} |
| 323 | * modifier. |
| 324 | */ |
| 325 | public static final int NATIVE = 0x00000100; |
| 326 | |
| 327 | /** |
| 328 | * The {@code int} value representing the {@code interface} |
| 329 | * modifier. |
| 330 | */ |
| 331 | public static final int INTERFACE = 0x00000200; |
| 332 | |
| 333 | /** |
| 334 | * The {@code int} value representing the {@code abstract} |
| 335 | * modifier. |
| 336 | */ |
| 337 | public static final int ABSTRACT = 0x00000400; |
| 338 | |
| 339 | /** |
| 340 | * The {@code int} value representing the {@code strictfp} |
| 341 | * modifier. |
| 342 | */ |
| 343 | public static final int STRICT = 0x00000800; |
| 344 | |
| 345 | // Bits not (yet) exposed in the public API either because they |
| 346 | // have different meanings for fields and methods and there is no |
| 347 | // way to distinguish between the two in this class, or because |
| 348 | // they are not Java programming language keywords |
| 349 | static final int BRIDGE = 0x00000040; |
| 350 | static final int VARARGS = 0x00000080; |
| 351 | static final int SYNTHETIC = 0x00001000; |
| 352 | static final int ANNOTATION= 0x00002000; |
| 353 | static final int ENUM = 0x00004000; |
| 354 | static boolean isSynthetic(int mod) { |
| 355 | return (mod & SYNTHETIC) != 0; |
| 356 | } |
| 357 | } |