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