rt/emul/mini/src/main/java/java/lang/reflect/Modifier.java
changeset 772 d382dacfd73f
parent 554 05224402145d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/mini/src/main/java/java/lang/reflect/Modifier.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,437 @@
     1.4 +/*
     1.5 + * Copyright (c) 1996, 2008, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.lang.reflect;
    1.30 +
    1.31 +/**
    1.32 + * The Modifier class provides {@code static} methods and
    1.33 + * constants to decode class and member access modifiers.  The sets of
    1.34 + * modifiers are represented as integers with distinct bit positions
    1.35 + * representing different modifiers.  The values for the constants
    1.36 + * representing the modifiers are taken from the tables in sections 4.1, 4.4, 4.5, and 4.7 of
    1.37 + * <cite>The Java&trade; Virtual Machine Specification</cite>.
    1.38 + *
    1.39 + * @see Class#getModifiers()
    1.40 + * @see Member#getModifiers()
    1.41 + *
    1.42 + * @author Nakul Saraiya
    1.43 + * @author Kenneth Russell
    1.44 + */
    1.45 +public
    1.46 +class Modifier {
    1.47 +
    1.48 +    /**
    1.49 +     * Return {@code true} if the integer argument includes the
    1.50 +     * {@code public} modifier, {@code false} otherwise.
    1.51 +     *
    1.52 +     * @param   mod a set of modifiers
    1.53 +     * @return {@code true} if {@code mod} includes the
    1.54 +     * {@code public} modifier; {@code false} otherwise.
    1.55 +     */
    1.56 +    public static boolean isPublic(int mod) {
    1.57 +        return (mod & PUBLIC) != 0;
    1.58 +    }
    1.59 +
    1.60 +    /**
    1.61 +     * Return {@code true} if the integer argument includes the
    1.62 +     * {@code private} modifier, {@code false} otherwise.
    1.63 +     *
    1.64 +     * @param   mod a set of modifiers
    1.65 +     * @return {@code true} if {@code mod} includes the
    1.66 +     * {@code private} modifier; {@code false} otherwise.
    1.67 +     */
    1.68 +    public static boolean isPrivate(int mod) {
    1.69 +        return (mod & PRIVATE) != 0;
    1.70 +    }
    1.71 +
    1.72 +    /**
    1.73 +     * Return {@code true} if the integer argument includes the
    1.74 +     * {@code protected} modifier, {@code false} otherwise.
    1.75 +     *
    1.76 +     * @param   mod a set of modifiers
    1.77 +     * @return {@code true} if {@code mod} includes the
    1.78 +     * {@code protected} modifier; {@code false} otherwise.
    1.79 +     */
    1.80 +    public static boolean isProtected(int mod) {
    1.81 +        return (mod & PROTECTED) != 0;
    1.82 +    }
    1.83 +
    1.84 +    /**
    1.85 +     * Return {@code true} if the integer argument includes the
    1.86 +     * {@code static} modifier, {@code false} otherwise.
    1.87 +     *
    1.88 +     * @param   mod a set of modifiers
    1.89 +     * @return {@code true} if {@code mod} includes the
    1.90 +     * {@code static} modifier; {@code false} otherwise.
    1.91 +     */
    1.92 +    public static boolean isStatic(int mod) {
    1.93 +        return (mod & STATIC) != 0;
    1.94 +    }
    1.95 +
    1.96 +    /**
    1.97 +     * Return {@code true} if the integer argument includes the
    1.98 +     * {@code final} modifier, {@code false} otherwise.
    1.99 +     *
   1.100 +     * @param   mod a set of modifiers
   1.101 +     * @return {@code true} if {@code mod} includes the
   1.102 +     * {@code final} modifier; {@code false} otherwise.
   1.103 +     */
   1.104 +    public static boolean isFinal(int mod) {
   1.105 +        return (mod & FINAL) != 0;
   1.106 +    }
   1.107 +
   1.108 +    /**
   1.109 +     * Return {@code true} if the integer argument includes the
   1.110 +     * {@code synchronized} modifier, {@code false} otherwise.
   1.111 +     *
   1.112 +     * @param   mod a set of modifiers
   1.113 +     * @return {@code true} if {@code mod} includes the
   1.114 +     * {@code synchronized} modifier; {@code false} otherwise.
   1.115 +     */
   1.116 +    public static boolean isSynchronized(int mod) {
   1.117 +        return (mod & SYNCHRONIZED) != 0;
   1.118 +    }
   1.119 +
   1.120 +    /**
   1.121 +     * Return {@code true} if the integer argument includes the
   1.122 +     * {@code volatile} modifier, {@code false} otherwise.
   1.123 +     *
   1.124 +     * @param   mod a set of modifiers
   1.125 +     * @return {@code true} if {@code mod} includes the
   1.126 +     * {@code volatile} modifier; {@code false} otherwise.
   1.127 +     */
   1.128 +    public static boolean isVolatile(int mod) {
   1.129 +        return (mod & VOLATILE) != 0;
   1.130 +    }
   1.131 +
   1.132 +    /**
   1.133 +     * Return {@code true} if the integer argument includes the
   1.134 +     * {@code transient} modifier, {@code false} otherwise.
   1.135 +     *
   1.136 +     * @param   mod a set of modifiers
   1.137 +     * @return {@code true} if {@code mod} includes the
   1.138 +     * {@code transient} modifier; {@code false} otherwise.
   1.139 +     */
   1.140 +    public static boolean isTransient(int mod) {
   1.141 +        return (mod & TRANSIENT) != 0;
   1.142 +    }
   1.143 +
   1.144 +    /**
   1.145 +     * Return {@code true} if the integer argument includes the
   1.146 +     * {@code native} modifier, {@code false} otherwise.
   1.147 +     *
   1.148 +     * @param   mod a set of modifiers
   1.149 +     * @return {@code true} if {@code mod} includes the
   1.150 +     * {@code native} modifier; {@code false} otherwise.
   1.151 +     */
   1.152 +    public static boolean isNative(int mod) {
   1.153 +        return (mod & NATIVE) != 0;
   1.154 +    }
   1.155 +
   1.156 +    /**
   1.157 +     * Return {@code true} if the integer argument includes the
   1.158 +     * {@code interface} modifier, {@code false} otherwise.
   1.159 +     *
   1.160 +     * @param   mod a set of modifiers
   1.161 +     * @return {@code true} if {@code mod} includes the
   1.162 +     * {@code interface} modifier; {@code false} otherwise.
   1.163 +     */
   1.164 +    public static boolean isInterface(int mod) {
   1.165 +        return (mod & INTERFACE) != 0;
   1.166 +    }
   1.167 +
   1.168 +    /**
   1.169 +     * Return {@code true} if the integer argument includes the
   1.170 +     * {@code abstract} modifier, {@code false} otherwise.
   1.171 +     *
   1.172 +     * @param   mod a set of modifiers
   1.173 +     * @return {@code true} if {@code mod} includes the
   1.174 +     * {@code abstract} modifier; {@code false} otherwise.
   1.175 +     */
   1.176 +    public static boolean isAbstract(int mod) {
   1.177 +        return (mod & ABSTRACT) != 0;
   1.178 +    }
   1.179 +
   1.180 +    /**
   1.181 +     * Return {@code true} if the integer argument includes the
   1.182 +     * {@code strictfp} modifier, {@code false} otherwise.
   1.183 +     *
   1.184 +     * @param   mod a set of modifiers
   1.185 +     * @return {@code true} if {@code mod} includes the
   1.186 +     * {@code strictfp} modifier; {@code false} otherwise.
   1.187 +     */
   1.188 +    public static boolean isStrict(int mod) {
   1.189 +        return (mod & STRICT) != 0;
   1.190 +    }
   1.191 +
   1.192 +    /**
   1.193 +     * Return a string describing the access modifier flags in
   1.194 +     * the specified modifier. For example:
   1.195 +     * <blockquote><pre>
   1.196 +     *    public final synchronized strictfp
   1.197 +     * </pre></blockquote>
   1.198 +     * The modifier names are returned in an order consistent with the
   1.199 +     * suggested modifier orderings given in sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of
   1.200 +     * <cite>The Java&trade; Language Specification</cite>.
   1.201 +     * The full modifier ordering used by this method is:
   1.202 +     * <blockquote> {@code
   1.203 +     * public protected private abstract static final transient
   1.204 +     * volatile synchronized native strictfp
   1.205 +     * interface } </blockquote>
   1.206 +     * The {@code interface} modifier discussed in this class is
   1.207 +     * not a true modifier in the Java language and it appears after
   1.208 +     * all other modifiers listed by this method.  This method may
   1.209 +     * return a string of modifiers that are not valid modifiers of a
   1.210 +     * Java entity; in other words, no checking is done on the
   1.211 +     * possible validity of the combination of modifiers represented
   1.212 +     * by the input.
   1.213 +     *
   1.214 +     * Note that to perform such checking for a known kind of entity,
   1.215 +     * such as a constructor or method, first AND the argument of
   1.216 +     * {@code toString} with the appropriate mask from a method like
   1.217 +     * {@link #constructorModifiers} or {@link #methodModifiers}.
   1.218 +     *
   1.219 +     * @param   mod a set of modifiers
   1.220 +     * @return  a string representation of the set of modifiers
   1.221 +     * represented by {@code mod}
   1.222 +     */
   1.223 +    public static String toString(int mod) {
   1.224 +        StringBuffer sb = new StringBuffer();
   1.225 +        int len;
   1.226 +
   1.227 +        if ((mod & PUBLIC) != 0)        sb.append("public ");
   1.228 +        if ((mod & PROTECTED) != 0)     sb.append("protected ");
   1.229 +        if ((mod & PRIVATE) != 0)       sb.append("private ");
   1.230 +
   1.231 +        /* Canonical order */
   1.232 +        if ((mod & ABSTRACT) != 0)      sb.append("abstract ");
   1.233 +        if ((mod & STATIC) != 0)        sb.append("static ");
   1.234 +        if ((mod & FINAL) != 0)         sb.append("final ");
   1.235 +        if ((mod & TRANSIENT) != 0)     sb.append("transient ");
   1.236 +        if ((mod & VOLATILE) != 0)      sb.append("volatile ");
   1.237 +        if ((mod & SYNCHRONIZED) != 0)  sb.append("synchronized ");
   1.238 +        if ((mod & NATIVE) != 0)        sb.append("native ");
   1.239 +        if ((mod & STRICT) != 0)        sb.append("strictfp ");
   1.240 +        if ((mod & INTERFACE) != 0)     sb.append("interface ");
   1.241 +
   1.242 +        if ((len = sb.length()) > 0)    /* trim trailing space */
   1.243 +            return sb.toString().substring(0, len-1);
   1.244 +        return "";
   1.245 +    }
   1.246 +
   1.247 +    /*
   1.248 +     * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of
   1.249 +     * <cite>The Java&trade; Virtual Machine Specification</cite>
   1.250 +     */
   1.251 +
   1.252 +    /**
   1.253 +     * The {@code int} value representing the {@code public}
   1.254 +     * modifier.
   1.255 +     */
   1.256 +    public static final int PUBLIC           = 0x00000001;
   1.257 +
   1.258 +    /**
   1.259 +     * The {@code int} value representing the {@code private}
   1.260 +     * modifier.
   1.261 +     */
   1.262 +    public static final int PRIVATE          = 0x00000002;
   1.263 +
   1.264 +    /**
   1.265 +     * The {@code int} value representing the {@code protected}
   1.266 +     * modifier.
   1.267 +     */
   1.268 +    public static final int PROTECTED        = 0x00000004;
   1.269 +
   1.270 +    /**
   1.271 +     * The {@code int} value representing the {@code static}
   1.272 +     * modifier.
   1.273 +     */
   1.274 +    public static final int STATIC           = 0x00000008;
   1.275 +
   1.276 +    /**
   1.277 +     * The {@code int} value representing the {@code final}
   1.278 +     * modifier.
   1.279 +     */
   1.280 +    public static final int FINAL            = 0x00000010;
   1.281 +
   1.282 +    /**
   1.283 +     * The {@code int} value representing the {@code synchronized}
   1.284 +     * modifier.
   1.285 +     */
   1.286 +    public static final int SYNCHRONIZED     = 0x00000020;
   1.287 +
   1.288 +    /**
   1.289 +     * The {@code int} value representing the {@code volatile}
   1.290 +     * modifier.
   1.291 +     */
   1.292 +    public static final int VOLATILE         = 0x00000040;
   1.293 +
   1.294 +    /**
   1.295 +     * The {@code int} value representing the {@code transient}
   1.296 +     * modifier.
   1.297 +     */
   1.298 +    public static final int TRANSIENT        = 0x00000080;
   1.299 +
   1.300 +    /**
   1.301 +     * The {@code int} value representing the {@code native}
   1.302 +     * modifier.
   1.303 +     */
   1.304 +    public static final int NATIVE           = 0x00000100;
   1.305 +
   1.306 +    /**
   1.307 +     * The {@code int} value representing the {@code interface}
   1.308 +     * modifier.
   1.309 +     */
   1.310 +    public static final int INTERFACE        = 0x00000200;
   1.311 +
   1.312 +    /**
   1.313 +     * The {@code int} value representing the {@code abstract}
   1.314 +     * modifier.
   1.315 +     */
   1.316 +    public static final int ABSTRACT         = 0x00000400;
   1.317 +
   1.318 +    /**
   1.319 +     * The {@code int} value representing the {@code strictfp}
   1.320 +     * modifier.
   1.321 +     */
   1.322 +    public static final int STRICT           = 0x00000800;
   1.323 +
   1.324 +    // Bits not (yet) exposed in the public API either because they
   1.325 +    // have different meanings for fields and methods and there is no
   1.326 +    // way to distinguish between the two in this class, or because
   1.327 +    // they are not Java programming language keywords
   1.328 +    static final int BRIDGE    = 0x00000040;
   1.329 +    static final int VARARGS   = 0x00000080;
   1.330 +    static final int SYNTHETIC = 0x00001000;
   1.331 +    static final int ANNOTATION= 0x00002000;
   1.332 +    static final int ENUM      = 0x00004000;
   1.333 +    static boolean isSynthetic(int mod) {
   1.334 +      return (mod & SYNTHETIC) != 0;
   1.335 +    }
   1.336 +
   1.337 +    /**
   1.338 +     * See JLSv3 section 8.1.1.
   1.339 +     */
   1.340 +    private static final int CLASS_MODIFIERS =
   1.341 +        Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
   1.342 +        Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
   1.343 +        Modifier.STRICT;
   1.344 +
   1.345 +    /**
   1.346 +     * See JLSv3 section 9.1.1.
   1.347 +     */
   1.348 +    private static final int INTERFACE_MODIFIERS =
   1.349 +        Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
   1.350 +        Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.STRICT;
   1.351 +
   1.352 +
   1.353 +    /**
   1.354 +     * See JLSv3 section 8.8.3.
   1.355 +     */
   1.356 +    private static final int CONSTRUCTOR_MODIFIERS =
   1.357 +        Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE;
   1.358 +
   1.359 +    /**
   1.360 +     * See JLSv3 section 8.4.3.
   1.361 +     */
   1.362 +    private static final int METHOD_MODIFIERS =
   1.363 +        Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
   1.364 +        Modifier.ABSTRACT       | Modifier.STATIC       | Modifier.FINAL   |
   1.365 +        Modifier.SYNCHRONIZED   | Modifier.NATIVE       | Modifier.STRICT;
   1.366 +
   1.367 +    /**
   1.368 +     * See JLSv3 section 8.3.1.
   1.369 +     */
   1.370 +    private static final int FIELD_MODIFIERS =
   1.371 +        Modifier.PUBLIC         | Modifier.PROTECTED    | Modifier.PRIVATE |
   1.372 +        Modifier.STATIC         | Modifier.FINAL        | Modifier.TRANSIENT |
   1.373 +        Modifier.VOLATILE;
   1.374 +
   1.375 +    /**
   1.376 +     * Return an {@code int} value OR-ing together the source language
   1.377 +     * modifiers that can be applied to a class.
   1.378 +     * @return an {@code int} value OR-ing together the source language
   1.379 +     * modifiers that can be applied to a class.
   1.380 +     *
   1.381 +     * @jls 8.1.1 Class Modifiers
   1.382 +     * @since 1.7
   1.383 +     */
   1.384 +    public static int classModifiers() {
   1.385 +        return CLASS_MODIFIERS;
   1.386 +    }
   1.387 +
   1.388 +    /**
   1.389 +     * Return an {@code int} value OR-ing together the source language
   1.390 +     * modifiers that can be applied to an interface.
   1.391 +     * @return an {@code int} value OR-ing together the source language
   1.392 +     * modifiers that can be applied to an inteface.
   1.393 +     *
   1.394 +     * @jls 9.1.1 Interface Modifiers
   1.395 +     * @since 1.7
   1.396 +     */
   1.397 +    public static int interfaceModifiers() {
   1.398 +        return INTERFACE_MODIFIERS;
   1.399 +    }
   1.400 +
   1.401 +    /**
   1.402 +     * Return an {@code int} value OR-ing together the source language
   1.403 +     * modifiers that can be applied to a constructor.
   1.404 +     * @return an {@code int} value OR-ing together the source language
   1.405 +     * modifiers that can be applied to a constructor.
   1.406 +     *
   1.407 +     * @jls 8.8.3 Constructor Modifiers
   1.408 +     * @since 1.7
   1.409 +     */
   1.410 +    public static int constructorModifiers() {
   1.411 +        return CONSTRUCTOR_MODIFIERS;
   1.412 +    }
   1.413 +
   1.414 +    /**
   1.415 +     * Return an {@code int} value OR-ing together the source language
   1.416 +     * modifiers that can be applied to a method.
   1.417 +     * @return an {@code int} value OR-ing together the source language
   1.418 +     * modifiers that can be applied to a method.
   1.419 +     *
   1.420 +     * @jls 8.4.3 Method Modifiers
   1.421 +     * @since 1.7
   1.422 +     */
   1.423 +    public static int methodModifiers() {
   1.424 +        return METHOD_MODIFIERS;
   1.425 +    }
   1.426 +
   1.427 +
   1.428 +    /**
   1.429 +     * Return an {@code int} value OR-ing together the source language
   1.430 +     * modifiers that can be applied to a field.
   1.431 +     * @return an {@code int} value OR-ing together the source language
   1.432 +     * modifiers that can be applied to a field.
   1.433 +     *
   1.434 +     * @jls 8.3.1 Field Modifiers
   1.435 +     * @since 1.7
   1.436 +     */
   1.437 +    public static int fieldModifiers() {
   1.438 +        return FIELD_MODIFIERS;
   1.439 +    }
   1.440 +}