1 /*
   2  * Copyright (c) 1996, 2019, 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.util.StringJoiner;
  29 
  30 /**
  31  * The Modifier class provides {@code static} methods and
  32  * constants to decode class and member access modifiers.  The sets of
  33  * modifiers are represented as integers with distinct bit positions
  34  * representing different modifiers.  The values for the constants
  35  * representing the modifiers are taken from the tables in sections 4.1, 4.4, 4.5, and 4.7 of
  36  * <cite>The Java&trade; Virtual Machine Specification</cite>.
  37  *
  38  * @see Class#getModifiers()
  39  * @see Member#getModifiers()
  40  *
  41  * @author Nakul Saraiya
  42  * @author Kenneth Russell
  43  * @since 1.1
  44  */
  45 public class Modifier {
  46 
  47     /**
  48      * Return {@code true} if the integer argument includes the
  49      * {@code public} modifier, {@code false} otherwise.
  50      *
  51      * @param   mod a set of modifiers
  52      * @return {@code true} if {@code mod} includes the
  53      * {@code public} modifier; {@code false} otherwise.
  54      */
  55     public static boolean isPublic(int mod) {
  56         return (mod & PUBLIC) != 0;
  57     }
  58 
  59     /**
  60      * Return {@code true} if the integer argument includes the
  61      * {@code private} modifier, {@code false} otherwise.
  62      *
  63      * @param   mod a set of modifiers
  64      * @return {@code true} if {@code mod} includes the
  65      * {@code private} modifier; {@code false} otherwise.
  66      */
  67     public static boolean isPrivate(int mod) {
  68         return (mod & PRIVATE) != 0;
  69     }
  70 
  71     /**
  72      * Return {@code true} if the integer argument includes the
  73      * {@code protected} modifier, {@code false} otherwise.
  74      *
  75      * @param   mod a set of modifiers
  76      * @return {@code true} if {@code mod} includes the
  77      * {@code protected} modifier; {@code false} otherwise.
  78      */
  79     public static boolean isProtected(int mod) {
  80         return (mod & PROTECTED) != 0;
  81     }
  82 
  83     /**
  84      * Return {@code true} if the integer argument includes the
  85      * {@code static} modifier, {@code false} otherwise.
  86      *
  87      * @param   mod a set of modifiers
  88      * @return {@code true} if {@code mod} includes the
  89      * {@code static} modifier; {@code false} otherwise.
  90      */
  91     public static boolean isStatic(int mod) {
  92         return (mod & STATIC) != 0;
  93     }
  94 
  95     /**
  96      * Return {@code true} if the integer argument includes the
  97      * {@code final} modifier, {@code false} otherwise.
  98      *
  99      * @param   mod a set of modifiers
 100      * @return {@code true} if {@code mod} includes the
 101      * {@code final} modifier; {@code false} otherwise.
 102      */
 103     public static boolean isFinal(int mod) {
 104         return (mod & FINAL) != 0;
 105     }
 106 
 107     /**
 108      * Return {@code true} if the integer argument includes the
 109      * {@code synchronized} modifier, {@code false} otherwise.
 110      *
 111      * @param   mod a set of modifiers
 112      * @return {@code true} if {@code mod} includes the
 113      * {@code synchronized} modifier; {@code false} otherwise.
 114      */
 115     public static boolean isSynchronized(int mod) {
 116         return (mod & SYNCHRONIZED) != 0;
 117     }
 118 
 119     /**
 120      * Return {@code true} if the integer argument includes the
 121      * {@code volatile} modifier, {@code false} otherwise.
 122      *
 123      * @param   mod a set of modifiers
 124      * @return {@code true} if {@code mod} includes the
 125      * {@code volatile} modifier; {@code false} otherwise.
 126      */
 127     public static boolean isVolatile(int mod) {
 128         return (mod & VOLATILE) != 0;
 129     }
 130 
 131     /**
 132      * Return {@code true} if the integer argument includes the
 133      * {@code transient} modifier, {@code false} otherwise.
 134      *
 135      * @param   mod a set of modifiers
 136      * @return {@code true} if {@code mod} includes the
 137      * {@code transient} modifier; {@code false} otherwise.
 138      */
 139     public static boolean isTransient(int mod) {
 140         return (mod & TRANSIENT) != 0;
 141     }
 142 
 143     /**
 144      * Return {@code true} if the integer argument includes the
 145      * {@code native} modifier, {@code false} otherwise.
 146      *
 147      * @param   mod a set of modifiers
 148      * @return {@code true} if {@code mod} includes the
 149      * {@code native} modifier; {@code false} otherwise.
 150      */
 151     public static boolean isNative(int mod) {
 152         return (mod & NATIVE) != 0;
 153     }
 154 
 155     /**
 156      * Return {@code true} if the integer argument includes the
 157      * {@code interface} modifier, {@code false} otherwise.
 158      *
 159      * @param   mod a set of modifiers
 160      * @return {@code true} if {@code mod} includes the
 161      * {@code interface} modifier; {@code false} otherwise.
 162      */
 163     public static boolean isInterface(int mod) {
 164         return (mod & INTERFACE) != 0;
 165     }
 166 
 167     /**
 168      * Return {@code true} if the integer argument includes the
 169      * {@code abstract} modifier, {@code false} otherwise.
 170      *
 171      * @param   mod a set of modifiers
 172      * @return {@code true} if {@code mod} includes the
 173      * {@code abstract} modifier; {@code false} otherwise.
 174      */
 175     public static boolean isAbstract(int mod) {
 176         return (mod & ABSTRACT) != 0;
 177     }
 178 
 179     /**
 180      * Return {@code true} if the integer argument includes the
 181      * {@code strictfp} modifier, {@code false} otherwise.
 182      *
 183      * @param   mod a set of modifiers
 184      * @return {@code true} if {@code mod} includes the
 185      * {@code strictfp} modifier; {@code false} otherwise.
 186      */
 187     public static boolean isStrict(int mod) {
 188         return (mod & STRICT) != 0;
 189     }
 190 
 191     /**
 192      * Return a string describing the access modifier flags in
 193      * the specified modifier. For example:
 194      * <blockquote><pre>
 195      *    public final synchronized strictfp
 196      * </pre></blockquote>
 197      * The modifier names are returned in an order consistent with the
 198      * suggested modifier orderings given in sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of
 199      * <cite>The Java&trade; Language Specification</cite>.
 200      * The full modifier ordering used by this method is:
 201      * <blockquote> {@code
 202      * public protected private abstract static final transient
 203      * volatile synchronized native strictfp
 204      * interface } </blockquote>
 205      * The {@code interface} modifier discussed in this class is
 206      * not a true modifier in the Java language and it appears after
 207      * all other modifiers listed by this method.  This method may
 208      * return a string of modifiers that are not valid modifiers of a
 209      * Java entity; in other words, no checking is done on the
 210      * possible validity of the combination of modifiers represented
 211      * by the input.
 212      *
 213      * Note that to perform such checking for a known kind of entity,
 214      * such as a constructor or method, first AND the argument of
 215      * {@code toString} with the appropriate mask from a method like
 216      * {@link #constructorModifiers} or {@link #methodModifiers}.
 217      *
 218      * @param   mod a set of modifiers
 219      * @return  a string representation of the set of modifiers
 220      * represented by {@code mod}
 221      */
 222     public static String toString(int mod) {
 223         StringJoiner sj = new StringJoiner(" ");
 224 
 225         if ((mod & PUBLIC) != 0)        sj.add("public");
 226         if ((mod & PROTECTED) != 0)     sj.add("protected");
 227         if ((mod & PRIVATE) != 0)       sj.add("private");
 228 
 229         /* Canonical order */
 230         if ((mod & ABSTRACT) != 0)      sj.add("abstract");
 231         if ((mod & STATIC) != 0)        sj.add("static");
 232         if ((mod & FINAL) != 0)         sj.add("final");
 233         if ((mod & TRANSIENT) != 0)     sj.add("transient");
 234         if ((mod & VOLATILE) != 0)      sj.add("volatile");
 235         if ((mod & SYNCHRONIZED) != 0)  sj.add("synchronized");
 236         if ((mod & NATIVE) != 0)        sj.add("native");
 237         if ((mod & STRICT) != 0)        sj.add("strictfp");
 238         if ((mod & INTERFACE) != 0)     sj.add("interface");
 239 
 240         return sj.toString();
 241     }
 242 
 243     /*
 244      * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of
 245      * <cite>The Java&trade; Virtual Machine Specification</cite>
 246      */
 247 
 248     /**
 249      * The {@code int} value representing the {@code public}
 250      * modifier.
 251      */
 252     public static final int PUBLIC           = 0x00000001;
 253 
 254     /**
 255      * The {@code int} value representing the {@code private}
 256      * modifier.
 257      */
 258     public static final int PRIVATE          = 0x00000002;
 259 
 260     /**
 261      * The {@code int} value representing the {@code protected}
 262      * modifier.
 263      */
 264     public static final int PROTECTED        = 0x00000004;
 265 
 266     /**
 267      * The {@code int} value representing the {@code static}
 268      * modifier.
 269      */
 270     public static final int STATIC           = 0x00000008;
 271 
 272     /**
 273      * The {@code int} value representing the {@code final}
 274      * modifier.
 275      */
 276     public static final int FINAL            = 0x00000010;
 277 
 278     /**
 279      * The {@code int} value representing the {@code synchronized}
 280      * modifier.
 281      */
 282     public static final int SYNCHRONIZED     = 0x00000020;
 283 
 284     /**
 285      * The {@code int} value representing the {@code volatile}
 286      * modifier.
 287      */
 288     public static final int VOLATILE         = 0x00000040;
 289 
 290     /**
 291      * The {@code int} value representing the {@code transient}
 292      * modifier.
 293      */
 294     public static final int TRANSIENT        = 0x00000080;
 295 
 296     /**
 297      * The {@code int} value representing the {@code native}
 298      * modifier.
 299      */
 300     public static final int NATIVE           = 0x00000100;
 301 
 302     /**
 303      * The {@code int} value representing the {@code interface}
 304      * modifier.
 305      */
 306     public static final int INTERFACE        = 0x00000200;
 307 
 308     /**
 309      * The {@code int} value representing the {@code abstract}
 310      * modifier.
 311      */
 312     public static final int ABSTRACT         = 0x00000400;
 313 
 314     /**
 315      * The {@code int} value representing the {@code strictfp}
 316      * modifier.
 317      */
 318     public static final int STRICT           = 0x00000800;
 319 
 320     // Bits not (yet) exposed in the public API either because they
 321     // have different meanings for fields and methods and there is no
 322     // way to distinguish between the two in this class, or because
 323     // they are not Java programming language keywords
 324     static final int BRIDGE    = 0x00000040;
 325     static final int VARARGS   = 0x00000080;
 326     static final int SYNTHETIC = 0x00001000;
 327     static final int ANNOTATION  = 0x00002000;
 328     static final int ENUM      = 0x00004000;
 329     static final int MANDATED  = 0x00008000;
 330     static boolean isSynthetic(int mod) {
 331       return (mod & SYNTHETIC) != 0;
 332     }
 333 
 334     static boolean isMandated(int mod) {
 335       return (mod & MANDATED) != 0;
 336     }
 337 
 338     // Note on the FOO_MODIFIERS fields and fooModifiers() methods:
 339     // the sets of modifiers are not guaranteed to be constants
 340     // across time and Java SE releases. Therefore, it would not be
 341     // appropriate to expose an external interface to this information
 342     // that would allow the values to be treated as Java-level
 343     // constants since the values could be constant folded and updates
 344     // to the sets of modifiers missed. Thus, the fooModifiers()
 345     // methods return an unchanging values for a given release, but a
 346     // value that can potentially change over time.
 347 
 348     /**
 349      * The Java source modifiers that can be applied to a class.
 350      * @jls 8.1.1 Class Modifiers
 351      */
 352     private static final int CLASS_MODIFIERS =
 353         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
 354         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
 355         Modifier.STRICT;
 356 
 357     /**
 358      * The Java source modifiers that can be applied to an interface.
 359      * @jls 9.1.1 Interface Modifiers
 360      */
 361     private static final int INTERFACE_MODIFIERS =
 362         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
 363         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.STRICT;
 364 
 365 
 366     /**
 367      * The Java source modifiers that can be applied to a constructor.
 368      * @jls 8.8.3 Constructor Modifiers
 369      */
 370     private static final int CONSTRUCTOR_MODIFIERS =
 371         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE;
 372 
 373     /**
 374      * The Java source modifiers that can be applied to a method.
 375      * @jls8.4.3  Method Modifiers
 376      */
 377     private static final int METHOD_MODIFIERS =
 378         Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
 379         Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
 380         Modifier.SYNCHRONIZED   | Modifier.NATIVE       | Modifier.STRICT;
 381 
 382     /**
 383      * The Java source modifiers that can be applied to a field.
 384      * @jls 8.3.1 Field Modifiers
 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      * The Java source modifiers that can be applied to a method or constructor parameter.
 393      * @jls 8.4.1 Formal Parameters
 394      */
 395     private static final int PARAMETER_MODIFIERS =
 396         Modifier.FINAL;
 397 
 398     /**
 399      *
 400      */
 401     static final int ACCESS_MODIFIERS =
 402         Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
 403 
 404     /**
 405      * Return an {@code int} value OR-ing together the source language
 406      * modifiers that can be applied to a class.
 407      * @return an {@code int} value OR-ing together the source language
 408      * modifiers that can be applied to a class.
 409      *
 410      * @jls 8.1.1 Class Modifiers
 411      * @since 1.7
 412      */
 413     public static int classModifiers() {
 414         return CLASS_MODIFIERS;
 415     }
 416 
 417     /**
 418      * Return an {@code int} value OR-ing together the source language
 419      * modifiers that can be applied to an interface.
 420      * @return an {@code int} value OR-ing together the source language
 421      * modifiers that can be applied to an interface.
 422      *
 423      * @jls 9.1.1 Interface Modifiers
 424      * @since 1.7
 425      */
 426     public static int interfaceModifiers() {
 427         return INTERFACE_MODIFIERS;
 428     }
 429 
 430     /**
 431      * Return an {@code int} value OR-ing together the source language
 432      * modifiers that can be applied to a constructor.
 433      * @return an {@code int} value OR-ing together the source language
 434      * modifiers that can be applied to a constructor.
 435      *
 436      * @jls 8.8.3 Constructor Modifiers
 437      * @since 1.7
 438      */
 439     public static int constructorModifiers() {
 440         return CONSTRUCTOR_MODIFIERS;
 441     }
 442 
 443     /**
 444      * Return an {@code int} value OR-ing together the source language
 445      * modifiers that can be applied to a method.
 446      * @return an {@code int} value OR-ing together the source language
 447      * modifiers that can be applied to a method.
 448      *
 449      * @jls 8.4.3 Method Modifiers
 450      * @since 1.7
 451      */
 452     public static int methodModifiers() {
 453         return METHOD_MODIFIERS;
 454     }
 455 
 456     /**
 457      * Return an {@code int} value OR-ing together the source language
 458      * modifiers that can be applied to a field.
 459      * @return an {@code int} value OR-ing together the source language
 460      * modifiers that can be applied to a field.
 461      *
 462      * @jls 8.3.1 Field Modifiers
 463      * @since 1.7
 464      */
 465     public static int fieldModifiers() {
 466         return FIELD_MODIFIERS;
 467     }
 468 
 469     /**
 470      * Return an {@code int} value OR-ing together the source language
 471      * modifiers that can be applied to a parameter.
 472      * @return an {@code int} value OR-ing together the source language
 473      * modifiers that can be applied to a parameter.
 474      *
 475      * @jls 8.4.1 Formal Parameters
 476      * @since 1.8
 477      */
 478     public static int parameterModifiers() {
 479         return PARAMETER_MODIFIERS;
 480     }
 481 }