1 /*
   2  * Copyright 1996-2008 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             AccessController.doPrivileged(
  62                 new ReflectionFactory.GetReflectionFactoryAction());
  63         factory.setLangReflectAccess(new java.lang.reflect.ReflectAccess());
  64     }
  65 
  66     /**
  67      * Return {@code true} if the integer argument includes the
  68      * {@code public} modifier, {@code false} otherwise.
  69      *
  70      * @param   mod a set of modifiers
  71      * @return {@code true} if {@code mod} includes the
  72      * {@code public} modifier; {@code false} otherwise.
  73      */
  74     public static boolean isPublic(int mod) {
  75         return (mod & PUBLIC) != 0;
  76     }
  77 
  78     /**
  79      * Return {@code true} if the integer argument includes the
  80      * {@code private} modifier, {@code false} otherwise.
  81      *
  82      * @param   mod a set of modifiers
  83      * @return {@code true} if {@code mod} includes the
  84      * {@code private} modifier; {@code false} otherwise.
  85      */
  86     public static boolean isPrivate(int mod) {
  87         return (mod & PRIVATE) != 0;
  88     }
  89 
  90     /**
  91      * Return {@code true} if the integer argument includes the
  92      * {@code protected} modifier, {@code false} otherwise.
  93      *
  94      * @param   mod a set of modifiers
  95      * @return {@code true} if {@code mod} includes the
  96      * {@code protected} modifier; {@code false} otherwise.
  97      */
  98     public static boolean isProtected(int mod) {
  99         return (mod & PROTECTED) != 0;
 100     }
 101 
 102     /**
 103      * Return {@code true} if the integer argument includes the
 104      * {@code static} modifier, {@code false} otherwise.
 105      *
 106      * @param   mod a set of modifiers
 107      * @return {@code true} if {@code mod} includes the
 108      * {@code static} modifier; {@code false} otherwise.
 109      */
 110     public static boolean isStatic(int mod) {
 111         return (mod & STATIC) != 0;
 112     }
 113 
 114     /**
 115      * Return {@code true} if the integer argument includes the
 116      * {@code final} modifier, {@code false} otherwise.
 117      *
 118      * @param   mod a set of modifiers
 119      * @return {@code true} if {@code mod} includes the
 120      * {@code final} modifier; {@code false} otherwise.
 121      */
 122     public static boolean isFinal(int mod) {
 123         return (mod & FINAL) != 0;
 124     }
 125 
 126     /**
 127      * Return {@code true} if the integer argument includes the
 128      * {@code synchronized} modifier, {@code false} otherwise.
 129      *
 130      * @param   mod a set of modifiers
 131      * @return {@code true} if {@code mod} includes the
 132      * {@code synchronized} modifier; {@code false} otherwise.
 133      */
 134     public static boolean isSynchronized(int mod) {
 135         return (mod & SYNCHRONIZED) != 0;
 136     }
 137 
 138     /**
 139      * Return {@code true} if the integer argument includes the
 140      * {@code volatile} modifier, {@code false} otherwise.
 141      *
 142      * @param   mod a set of modifiers
 143      * @return {@code true} if {@code mod} includes the
 144      * {@code volatile} modifier; {@code false} otherwise.
 145      */
 146     public static boolean isVolatile(int mod) {
 147         return (mod & VOLATILE) != 0;
 148     }
 149 
 150     /**
 151      * Return {@code true} if the integer argument includes the
 152      * {@code transient} modifier, {@code false} otherwise.
 153      *
 154      * @param   mod a set of modifiers
 155      * @return {@code true} if {@code mod} includes the
 156      * {@code transient} modifier; {@code false} otherwise.
 157      */
 158     public static boolean isTransient(int mod) {
 159         return (mod & TRANSIENT) != 0;
 160     }
 161 
 162     /**
 163      * Return {@code true} if the integer argument includes the
 164      * {@code native} modifier, {@code false} otherwise.
 165      *
 166      * @param   mod a set of modifiers
 167      * @return {@code true} if {@code mod} includes the
 168      * {@code native} modifier; {@code false} otherwise.
 169      */
 170     public static boolean isNative(int mod) {
 171         return (mod & NATIVE) != 0;
 172     }
 173 
 174     /**
 175      * Return {@code true} if the integer argument includes the
 176      * {@code interface} modifier, {@code false} otherwise.
 177      *
 178      * @param   mod a set of modifiers
 179      * @return {@code true} if {@code mod} includes the
 180      * {@code interface} modifier; {@code false} otherwise.
 181      */
 182     public static boolean isInterface(int mod) {
 183         return (mod & INTERFACE) != 0;
 184     }
 185 
 186     /**
 187      * Return {@code true} if the integer argument includes the
 188      * {@code abstract} modifier, {@code false} otherwise.
 189      *
 190      * @param   mod a set of modifiers
 191      * @return {@code true} if {@code mod} includes the
 192      * {@code abstract} modifier; {@code false} otherwise.
 193      */
 194     public static boolean isAbstract(int mod) {
 195         return (mod & ABSTRACT) != 0;
 196     }
 197 
 198     /**
 199      * Return {@code true} if the integer argument includes the
 200      * {@code strictfp} modifier, {@code false} otherwise.
 201      *
 202      * @param   mod a set of modifiers
 203      * @return {@code true} if {@code mod} includes the
 204      * {@code strictfp} modifier; {@code false} otherwise.
 205      */
 206     public static boolean isStrict(int mod) {
 207         return (mod & STRICT) != 0;
 208     }
 209 
 210     /**
 211      * Return a string describing the access modifier flags in
 212      * the specified modifier. For example:
 213      * <blockquote><pre>
 214      *    public final synchronized strictfp
 215      * </pre></blockquote>
 216      * The modifier names are returned in an order consistent with the
 217      * suggested modifier orderings given in <a
 218      * href="http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html"><em>The
 219      * Java Language Specification, Second Edition</em></a> sections
 220      * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#21613">&sect;8.1.1</a>,
 221      * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78091">&sect;8.3.1</a>,
 222      * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78188">&sect;8.4.3</a>,
 223      * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#42018">&sect;8.8.3</a>, and
 224      * <a href="http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html#235947">&sect;9.1.1</a>.
 225      * The full modifier ordering used by this method is:
 226      * <blockquote> {@code
 227      * public protected private abstract static final transient
 228      * volatile synchronized native strictfp
 229      * interface } </blockquote>
 230      * The {@code interface} modifier discussed in this class is
 231      * not a true modifier in the Java language and it appears after
 232      * all other modifiers listed by this method.  This method may
 233      * return a string of modifiers that are not valid modifiers of a
 234      * Java entity; in other words, no checking is done on the
 235      * possible validity of the combination of modifiers represented
 236      * by the input.
 237      *
 238      * Note that to perform such checking for a known kind of entity,
 239      * such as a constructor or method, first AND the argument of
 240      * {@code toString} with the appropriate mask from a method like
 241      * {@link #constructorModifiers} or {@link #methodModifiers}.
 242      *
 243      * @param   mod a set of modifiers
 244      * @return  a string representation of the set of modifiers
 245      * represented by {@code mod}
 246      */
 247     public static String toString(int mod) {
 248         StringBuffer sb = new StringBuffer();
 249         int len;
 250 
 251         if ((mod & PUBLIC) != 0)        sb.append("public ");
 252         if ((mod & PROTECTED) != 0)     sb.append("protected ");
 253         if ((mod & PRIVATE) != 0)       sb.append("private ");
 254 
 255         /* Canonical order */
 256         if ((mod & ABSTRACT) != 0)      sb.append("abstract ");
 257         if ((mod & STATIC) != 0)        sb.append("static ");
 258         if ((mod & FINAL) != 0)         sb.append("final ");
 259         if ((mod & TRANSIENT) != 0)     sb.append("transient ");
 260         if ((mod & VOLATILE) != 0)      sb.append("volatile ");
 261         if ((mod & SYNCHRONIZED) != 0)  sb.append("synchronized ");
 262         if ((mod & NATIVE) != 0)        sb.append("native ");
 263         if ((mod & STRICT) != 0)        sb.append("strictfp ");
 264         if ((mod & INTERFACE) != 0)     sb.append("interface ");
 265 
 266         if ((len = sb.length()) > 0)    /* trim trailing space */
 267             return sb.toString().substring(0, len-1);
 268         return "";
 269     }
 270 
 271     /*
 272      * Access modifier flag constants from <em>The Java Virtual
 273      * Machine Specification, Second Edition</em>, tables 4.1, 4.4,
 274      * 4.5, and 4.7.
 275      */
 276 
 277     /**
 278      * The {@code int} value representing the {@code public}
 279      * modifier.
 280      */
 281     public static final int PUBLIC           = 0x00000001;
 282 
 283     /**
 284      * The {@code int} value representing the {@code private}
 285      * modifier.
 286      */
 287     public static final int PRIVATE          = 0x00000002;
 288 
 289     /**
 290      * The {@code int} value representing the {@code protected}
 291      * modifier.
 292      */
 293     public static final int PROTECTED        = 0x00000004;
 294 
 295     /**
 296      * The {@code int} value representing the {@code static}
 297      * modifier.
 298      */
 299     public static final int STATIC           = 0x00000008;
 300 
 301     /**
 302      * The {@code int} value representing the {@code final}
 303      * modifier.
 304      */
 305     public static final int FINAL            = 0x00000010;
 306 
 307     /**
 308      * The {@code int} value representing the {@code synchronized}
 309      * modifier.
 310      */
 311     public static final int SYNCHRONIZED     = 0x00000020;
 312 
 313     /**
 314      * The {@code int} value representing the {@code volatile}
 315      * modifier.
 316      */
 317     public static final int VOLATILE         = 0x00000040;
 318 
 319     /**
 320      * The {@code int} value representing the {@code transient}
 321      * modifier.
 322      */
 323     public static final int TRANSIENT        = 0x00000080;
 324 
 325     /**
 326      * The {@code int} value representing the {@code native}
 327      * modifier.
 328      */
 329     public static final int NATIVE           = 0x00000100;
 330 
 331     /**
 332      * The {@code int} value representing the {@code interface}
 333      * modifier.
 334      */
 335     public static final int INTERFACE        = 0x00000200;
 336 
 337     /**
 338      * The {@code int} value representing the {@code abstract}
 339      * modifier.
 340      */
 341     public static final int ABSTRACT         = 0x00000400;
 342 
 343     /**
 344      * The {@code int} value representing the {@code strictfp}
 345      * modifier.
 346      */
 347     public static final int STRICT           = 0x00000800;
 348 
 349     // Bits not (yet) exposed in the public API either because they
 350     // have different meanings for fields and methods and there is no
 351     // way to distinguish between the two in this class, or because
 352     // they are not Java programming language keywords
 353     static final int BRIDGE    = 0x00000040;
 354     static final int VARARGS   = 0x00000080;
 355     static final int SYNTHETIC = 0x00001000;
 356     static final int ANNOTATION= 0x00002000;
 357     static final int ENUM      = 0x00004000;
 358     static boolean isSynthetic(int mod) {
 359       return (mod & SYNTHETIC) != 0;
 360     }
 361 
 362     /**
 363      * See JLSv3 section 8.1.1.
 364      */
 365     private static final int CLASS_MODIFIERS =
 366         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
 367         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
 368         Modifier.STRICT;
 369 
 370     /**
 371      * See JLSv3 section 9.1.1.
 372      */
 373     private static final int INTERFACE_MODIFIERS =
 374         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
 375         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.STRICT;
 376 
 377 
 378     /**
 379      * See JLSv3 section 8.8.3.
 380      */
 381     private static final int CONSTRUCTOR_MODIFIERS =
 382         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE;
 383 
 384     /**
 385      * See JLSv3 section 8.4.3.
 386      */
 387     private static final int METHOD_MODIFIERS =
 388         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
 389         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
 390         Modifier.SYNCHRONIZED   | Modifier.NATIVE       | Modifier.STRICT;
 391 
 392     /**
 393      * See JLSv3 section 8.3.1.
 394      */
 395     private static final int FIELD_MODIFIERS =
 396         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
 397         Modifier.STATIC         | Modifier.FINAL        | Modifier.TRANSIENT |
 398         Modifier.VOLATILE;
 399 
 400     /**
 401      * Return an {@code int} value OR-ing together the source language
 402      * modifiers that can be applied to a class.
 403      * @return an {@code int} value OR-ing together the source language
 404      * modifiers that can be applied to a class.
 405      *
 406      * @jls3 8.1.1 Class Modifiers
 407      * @since 1.7
 408      */
 409     public static int classModifiers() {
 410         return CLASS_MODIFIERS;
 411     }
 412 
 413     /**
 414      * Return an {@code int} value OR-ing together the source language
 415      * modifiers that can be applied to an interface.
 416      * @return an {@code int} value OR-ing together the source language
 417      * modifiers that can be applied to an inteface.
 418      *
 419      * @jls3 9.1.1 Interface Modifiers
 420      * @since 1.7
 421      */
 422     public static int interfaceModifiers() {
 423         return INTERFACE_MODIFIERS;
 424     }
 425 
 426     /**
 427      * Return an {@code int} value OR-ing together the source language
 428      * modifiers that can be applied to a constructor.
 429      * @return an {@code int} value OR-ing together the source language
 430      * modifiers that can be applied to a constructor.
 431      *
 432      * @jls3 8.8.3 Constructor Modifiers
 433      * @since 1.7
 434      */
 435     public static int constructorModifiers() {
 436         return CONSTRUCTOR_MODIFIERS;
 437     }
 438 
 439     /**
 440      * Return an {@code int} value OR-ing together the source language
 441      * modifiers that can be applied to a method.
 442      * @return an {@code int} value OR-ing together the source language
 443      * modifiers that can be applied to a method.
 444      *
 445      * @jls3 8.4.3 Method Modifiers
 446      * @since 1.7
 447      */
 448     public static int methodModifiers() {
 449         return METHOD_MODIFIERS;
 450     }
 451 
 452 
 453     /**
 454      * Return an {@code int} value OR-ing together the source language
 455      * modifiers that can be applied to a field.
 456      * @return an {@code int} value OR-ing together the source language
 457      * modifiers that can be applied to a field.
 458      *
 459      * @jls3 8.3.1 Field Modifiers
 460      * @since 1.7
 461      */
 462     public static int fieldModifiers() {
 463         return FIELD_MODIFIERS;
 464     }
 465 }