1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
/* * @(#)Modifier.java 1.28 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.lang.reflect; import java.security.AccessController; import sun.reflect.LangReflectAccess; import sun.reflect.ReflectionFactory; /** * The Modifier class provides <code>static</code> methods and * constants to decode class and member access modifiers. The sets of * modifiers are represented as integers with distinct bit positions * representing different modifiers. The values for the constants * representing the modifiers are taken from <a * href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/VMSpecTOC.doc.html"><i>The * Java</i><sup><small>TM</small></sup> <i>Virtual Machine Specification, Second * edition</i></a> tables * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75734">4.1</a>, * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88358">4.4</a>, * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75568">4.5</a>, and * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88478">4.7</a>. * * @see Class#getModifiers() * @see Member#getModifiers() * * @author Nakul Saraiya * @author Kenneth Russell */ public class Modifier { /* * Bootstrapping protocol between java.lang and java.lang.reflect * packages */ static { sun.reflect.ReflectionFactory factory = (sun.reflect.ReflectionFactory) AccessController.doPrivileged( new ReflectionFactory.GetReflectionFactoryAction() ); factory.setLangReflectAccess(new java.lang.reflect.ReflectAccess()); } /** * Return <tt>true</tt> if the integer argument includes the * <tt>public</tt> modifier, <tt>false</tt> otherwise. * * @param mod a set of modifiers * @return <tt>true</tt> if <code>mod</code> includes the * <tt>public</tt> modifier; <tt>false</tt> otherwise. */ public static boolean isPublic(int mod) { return (mod & PUBLIC) != 0; } /** * Return <tt>true</tt> if the integer argument includes the * <tt>private</tt> modifier, <tt>false</tt> otherwise. * * @param mod a set of modifiers * @return <tt>true</tt> if <code>mod</code> includes the * <tt>private</tt> modifier; <tt>false</tt> otherwise. */ public static boolean isPrivate(int mod) { return (mod & PRIVATE) != 0; } /** * Return <tt>true</tt> if the integer argument includes the * <tt>protected</tt> modifier, <tt>false</tt> otherwise. * * @param mod a set of modifiers * @return <tt>true</tt> if <code>mod</code> includes the * <tt>protected</tt> modifier; <tt>false</tt> otherwise. */ public static boolean isProtected(int mod) { return (mod & PROTECTED) != 0; } /** * Return <tt>true</tt> if the integer argument includes the * <tt>static</tt> modifier, <tt>false</tt> otherwise. * * @param mod a set of modifiers * @return <tt>true</tt> if <code>mod</code> includes the * <tt>static</tt> modifier; <tt>false</tt> otherwise. */ public static boolean isStatic(int mod) { return (mod & STATIC) != 0; } /** * Return <tt>true</tt> if the integer argument includes the * <tt>final</tt> modifier, <tt>false</tt> otherwise. * * @param mod a set of modifiers * @return <tt>true</tt> if <code>mod</code> includes the * <tt>final</tt> modifier; <tt>false</tt> otherwise. */ public static boolean isFinal(int mod) { return (mod & FINAL) != 0; } /** * Return <tt>true</tt> if the integer argument includes the * <tt>synchronized</tt> modifier, <tt>false</tt> otherwise. * * @param mod a set of modifiers * @return <tt>true</tt> if <code>mod</code> includes the * <tt>synchronized</tt> modifier; <tt>false</tt> otherwise. */ public static boolean isSynchronized(int mod) { return (mod & SYNCHRONIZED) != 0; } /** * Return <tt>true</tt> if the integer argument includes the * <tt>volatile</tt> modifier, <tt>false</tt> otherwise. * * @param mod a set of modifiers * @return <tt>true</tt> if <code>mod</code> includes the * <tt>volatile</tt> modifier; <tt>false</tt> otherwise. */ public static boolean isVolatile(int mod) { return (mod & VOLATILE) != 0; } /** * Return <tt>true</tt> if the integer argument includes the * <tt>transient</tt> modifier, <tt>false</tt> otherwise. * * @param mod a set of modifiers * @return <tt>true</tt> if <code>mod</code> includes the * <tt>transient</tt> modifier; <tt>false</tt> otherwise. */ public static boolean isTransient(int mod) { return (mod & TRANSIENT) != 0; } /** * Return <tt>true</tt> if the integer argument includes the * <tt>native</tt> modifier, <tt>false</tt> otherwise. * * @param mod a set of modifiers * @return <tt>true</tt> if <code>mod</code> includes the * <tt>native</tt> modifier; <tt>false</tt> otherwise. */ public static boolean isNative(int mod) { return (mod & NATIVE) != 0; } /** * Return <tt>true</tt> if the integer argument includes the * <tt>interface</tt> modifier, <tt>false</tt> otherwise. * * @param mod a set of modifiers * @return <tt>true</tt> if <code>mod</code> includes the * <tt>interface</tt> modifier; <tt>false</tt> otherwise. */ public static boolean isInterface(int mod) { return (mod & INTERFACE) != 0; } /** * Return <tt>true</tt> if the integer argument includes the * <tt>abstract</tt> modifier, <tt>false</tt> otherwise. * * @param mod a set of modifiers * @return <tt>true</tt> if <code>mod</code> includes the * <tt>abstract</tt> modifier; <tt>false</tt> otherwise. */ public static boolean isAbstract(int mod) { return (mod & ABSTRACT) != 0; } /** * Return <tt>true</tt> if the integer argument includes the * <tt>strictfp</tt> modifier, <tt>false</tt> otherwise. * * @param mod a set of modifiers * @return <tt>true</tt> if <code>mod</code> includes the * <tt>strictfp</tt> modifier; <tt>false</tt> otherwise. */ public static boolean isStrict(int mod) { return (mod & STRICT) != 0; } /** * Return a string describing the access modifier flags in * the specified modifier. For example: * <blockquote><pre> * public final synchronized strictfp * </pre></blockquote> * The modifier names are returned in an order consistent with the * suggested modifier orderings given in <a * href="http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html"><em>The * Java Language Specification, Second Edition</em></a> sections * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#21613">§8.1.1</a>, * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78091">§8.3.1</a>, * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78188">§8.4.3</a>, * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#42018">§8.8.3</a>, and * <a href="http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html#235947">§9.1.1</a>. * The full modifier ordering used by this method is: * <blockquote> <code> * public protected private abstract static final transient * volatile synchronized native strictfp * interface </code> </blockquote> * The <code>interface</code> modifier discussed in this class is * not a true modifier in the Java language and it appears after * all other modifiers listed by this method. This method may * return a string of modifiers that are not valid modifiers of a * Java entity; in other words, no checking is done on the * possible validity of the combination of modifiers represented * by the input. * * @param mod a set of modifiers * @return a string representation of the set of modifiers * represented by <code>mod</code> */ public static String toString(int mod) { StringBuffer sb = new StringBuffer(); int len; if ((mod & PUBLIC) != 0) sb.append("public "); if ((mod & PROTECTED) != 0) sb.append("protected "); if ((mod & PRIVATE) != 0) sb.append("private "); /* Canonical order */ if ((mod & ABSTRACT) != 0) sb.append("abstract "); if ((mod & STATIC) != 0) sb.append("static "); if ((mod & FINAL) != 0) sb.append("final "); if ((mod & TRANSIENT) != 0) sb.append("transient "); if ((mod & VOLATILE) != 0) sb.append("volatile "); if ((mod & SYNCHRONIZED) != 0) sb.append("synchronized "); if ((mod & NATIVE) != 0) sb.append("native "); if ((mod & STRICT) != 0) sb.append("strictfp "); if ((mod & INTERFACE) != 0) sb.append("interface "); if ((len = sb.length()) > 0) /* trim trailing space */ return sb.toString().substring(0, len-1); return ""; } /* * Access modifier flag constants from <em>The Java Virtual * Machine Specification, Second Edition</em>, tables 4.1, 4.4, * 4.5, and 4.7. */ /** * The <code>int</code> value representing the <code>public</code> * modifier. */ public static final int PUBLIC = 0x00000001; /** * The <code>int</code> value representing the <code>private</code> * modifier. */ public static final int PRIVATE = 0x00000002; /** * The <code>int</code> value representing the <code>protected</code> * modifier. */ public static final int PROTECTED = 0x00000004; /** * The <code>int</code> value representing the <code>static</code> * modifier. */ public static final int STATIC = 0x00000008; /** * The <code>int</code> value representing the <code>final</code> * modifier. */ public static final int FINAL = 0x00000010; /** * The <code>int</code> value representing the <code>synchronized</code> * modifier. */ public static final int SYNCHRONIZED = 0x00000020; /** * The <code>int</code> value representing the <code>volatile</code> * modifier. */ public static final int VOLATILE = 0x00000040; /** * The <code>int</code> value representing the <code>transient</code> * modifier. */ public static final int TRANSIENT = 0x00000080; /** * The <code>int</code> value representing the <code>native</code> * modifier. */ public static final int NATIVE = 0x00000100; /** * The <code>int</code> value representing the <code>interface</code> * modifier. */ public static final int INTERFACE = 0x00000200; /** * The <code>int</code> value representing the <code>abstract</code> * modifier. */ public static final int ABSTRACT = 0x00000400; /** * The <code>int</code> value representing the <code>strictfp</code> * modifier. */ public static final int STRICT = 0x00000800; // Bits not (yet) exposed in the public API either because they // have different meanings for fields and methods and there is no // way to distinguish between the two in this class, or because // they are not Java programming language keywords static final int BRIDGE = 0x00000040; static final int VARARGS = 0x00000080; static final int SYNTHETIC = 0x00001000; static final int ANNOTATION= 0x00002000; static final int ENUM = 0x00004000; static boolean isSynthetic(int mod) { return (mod & SYNTHETIC) != 0; } }