1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/src/main/java/java/lang/reflect/Modifier.java Tue Dec 04 13:29:17 2012 +0100
1.3 @@ -0,0 +1,452 @@
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 +import java.security.AccessController;
1.32 +import sun.reflect.LangReflectAccess;
1.33 +import sun.reflect.ReflectionFactory;
1.34 +
1.35 +/**
1.36 + * The Modifier class provides {@code static} methods and
1.37 + * constants to decode class and member access modifiers. The sets of
1.38 + * modifiers are represented as integers with distinct bit positions
1.39 + * representing different modifiers. The values for the constants
1.40 + * representing the modifiers are taken from the tables in sections 4.1, 4.4, 4.5, and 4.7 of
1.41 + * <cite>The Java™ Virtual Machine Specification</cite>.
1.42 + *
1.43 + * @see Class#getModifiers()
1.44 + * @see Member#getModifiers()
1.45 + *
1.46 + * @author Nakul Saraiya
1.47 + * @author Kenneth Russell
1.48 + */
1.49 +public
1.50 +class Modifier {
1.51 +
1.52 + /*
1.53 + * Bootstrapping protocol between java.lang and java.lang.reflect
1.54 + * packages
1.55 + */
1.56 + static {
1.57 + sun.reflect.ReflectionFactory factory =
1.58 + AccessController.doPrivileged(
1.59 + new ReflectionFactory.GetReflectionFactoryAction());
1.60 + factory.setLangReflectAccess(new java.lang.reflect.ReflectAccess());
1.61 + }
1.62 +
1.63 + /**
1.64 + * Return {@code true} if the integer argument includes the
1.65 + * {@code public} modifier, {@code false} otherwise.
1.66 + *
1.67 + * @param mod a set of modifiers
1.68 + * @return {@code true} if {@code mod} includes the
1.69 + * {@code public} modifier; {@code false} otherwise.
1.70 + */
1.71 + public static boolean isPublic(int mod) {
1.72 + return (mod & PUBLIC) != 0;
1.73 + }
1.74 +
1.75 + /**
1.76 + * Return {@code true} if the integer argument includes the
1.77 + * {@code private} modifier, {@code false} otherwise.
1.78 + *
1.79 + * @param mod a set of modifiers
1.80 + * @return {@code true} if {@code mod} includes the
1.81 + * {@code private} modifier; {@code false} otherwise.
1.82 + */
1.83 + public static boolean isPrivate(int mod) {
1.84 + return (mod & PRIVATE) != 0;
1.85 + }
1.86 +
1.87 + /**
1.88 + * Return {@code true} if the integer argument includes the
1.89 + * {@code protected} modifier, {@code false} otherwise.
1.90 + *
1.91 + * @param mod a set of modifiers
1.92 + * @return {@code true} if {@code mod} includes the
1.93 + * {@code protected} modifier; {@code false} otherwise.
1.94 + */
1.95 + public static boolean isProtected(int mod) {
1.96 + return (mod & PROTECTED) != 0;
1.97 + }
1.98 +
1.99 + /**
1.100 + * Return {@code true} if the integer argument includes the
1.101 + * {@code static} modifier, {@code false} otherwise.
1.102 + *
1.103 + * @param mod a set of modifiers
1.104 + * @return {@code true} if {@code mod} includes the
1.105 + * {@code static} modifier; {@code false} otherwise.
1.106 + */
1.107 + public static boolean isStatic(int mod) {
1.108 + return (mod & STATIC) != 0;
1.109 + }
1.110 +
1.111 + /**
1.112 + * Return {@code true} if the integer argument includes the
1.113 + * {@code final} modifier, {@code false} otherwise.
1.114 + *
1.115 + * @param mod a set of modifiers
1.116 + * @return {@code true} if {@code mod} includes the
1.117 + * {@code final} modifier; {@code false} otherwise.
1.118 + */
1.119 + public static boolean isFinal(int mod) {
1.120 + return (mod & FINAL) != 0;
1.121 + }
1.122 +
1.123 + /**
1.124 + * Return {@code true} if the integer argument includes the
1.125 + * {@code synchronized} modifier, {@code false} otherwise.
1.126 + *
1.127 + * @param mod a set of modifiers
1.128 + * @return {@code true} if {@code mod} includes the
1.129 + * {@code synchronized} modifier; {@code false} otherwise.
1.130 + */
1.131 + public static boolean isSynchronized(int mod) {
1.132 + return (mod & SYNCHRONIZED) != 0;
1.133 + }
1.134 +
1.135 + /**
1.136 + * Return {@code true} if the integer argument includes the
1.137 + * {@code volatile} modifier, {@code false} otherwise.
1.138 + *
1.139 + * @param mod a set of modifiers
1.140 + * @return {@code true} if {@code mod} includes the
1.141 + * {@code volatile} modifier; {@code false} otherwise.
1.142 + */
1.143 + public static boolean isVolatile(int mod) {
1.144 + return (mod & VOLATILE) != 0;
1.145 + }
1.146 +
1.147 + /**
1.148 + * Return {@code true} if the integer argument includes the
1.149 + * {@code transient} modifier, {@code false} otherwise.
1.150 + *
1.151 + * @param mod a set of modifiers
1.152 + * @return {@code true} if {@code mod} includes the
1.153 + * {@code transient} modifier; {@code false} otherwise.
1.154 + */
1.155 + public static boolean isTransient(int mod) {
1.156 + return (mod & TRANSIENT) != 0;
1.157 + }
1.158 +
1.159 + /**
1.160 + * Return {@code true} if the integer argument includes the
1.161 + * {@code native} modifier, {@code false} otherwise.
1.162 + *
1.163 + * @param mod a set of modifiers
1.164 + * @return {@code true} if {@code mod} includes the
1.165 + * {@code native} modifier; {@code false} otherwise.
1.166 + */
1.167 + public static boolean isNative(int mod) {
1.168 + return (mod & NATIVE) != 0;
1.169 + }
1.170 +
1.171 + /**
1.172 + * Return {@code true} if the integer argument includes the
1.173 + * {@code interface} modifier, {@code false} otherwise.
1.174 + *
1.175 + * @param mod a set of modifiers
1.176 + * @return {@code true} if {@code mod} includes the
1.177 + * {@code interface} modifier; {@code false} otherwise.
1.178 + */
1.179 + public static boolean isInterface(int mod) {
1.180 + return (mod & INTERFACE) != 0;
1.181 + }
1.182 +
1.183 + /**
1.184 + * Return {@code true} if the integer argument includes the
1.185 + * {@code abstract} modifier, {@code false} otherwise.
1.186 + *
1.187 + * @param mod a set of modifiers
1.188 + * @return {@code true} if {@code mod} includes the
1.189 + * {@code abstract} modifier; {@code false} otherwise.
1.190 + */
1.191 + public static boolean isAbstract(int mod) {
1.192 + return (mod & ABSTRACT) != 0;
1.193 + }
1.194 +
1.195 + /**
1.196 + * Return {@code true} if the integer argument includes the
1.197 + * {@code strictfp} modifier, {@code false} otherwise.
1.198 + *
1.199 + * @param mod a set of modifiers
1.200 + * @return {@code true} if {@code mod} includes the
1.201 + * {@code strictfp} modifier; {@code false} otherwise.
1.202 + */
1.203 + public static boolean isStrict(int mod) {
1.204 + return (mod & STRICT) != 0;
1.205 + }
1.206 +
1.207 + /**
1.208 + * Return a string describing the access modifier flags in
1.209 + * the specified modifier. For example:
1.210 + * <blockquote><pre>
1.211 + * public final synchronized strictfp
1.212 + * </pre></blockquote>
1.213 + * The modifier names are returned in an order consistent with the
1.214 + * suggested modifier orderings given in sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of
1.215 + * <cite>The Java™ Language Specification</cite>.
1.216 + * The full modifier ordering used by this method is:
1.217 + * <blockquote> {@code
1.218 + * public protected private abstract static final transient
1.219 + * volatile synchronized native strictfp
1.220 + * interface } </blockquote>
1.221 + * The {@code interface} modifier discussed in this class is
1.222 + * not a true modifier in the Java language and it appears after
1.223 + * all other modifiers listed by this method. This method may
1.224 + * return a string of modifiers that are not valid modifiers of a
1.225 + * Java entity; in other words, no checking is done on the
1.226 + * possible validity of the combination of modifiers represented
1.227 + * by the input.
1.228 + *
1.229 + * Note that to perform such checking for a known kind of entity,
1.230 + * such as a constructor or method, first AND the argument of
1.231 + * {@code toString} with the appropriate mask from a method like
1.232 + * {@link #constructorModifiers} or {@link #methodModifiers}.
1.233 + *
1.234 + * @param mod a set of modifiers
1.235 + * @return a string representation of the set of modifiers
1.236 + * represented by {@code mod}
1.237 + */
1.238 + public static String toString(int mod) {
1.239 + StringBuffer sb = new StringBuffer();
1.240 + int len;
1.241 +
1.242 + if ((mod & PUBLIC) != 0) sb.append("public ");
1.243 + if ((mod & PROTECTED) != 0) sb.append("protected ");
1.244 + if ((mod & PRIVATE) != 0) sb.append("private ");
1.245 +
1.246 + /* Canonical order */
1.247 + if ((mod & ABSTRACT) != 0) sb.append("abstract ");
1.248 + if ((mod & STATIC) != 0) sb.append("static ");
1.249 + if ((mod & FINAL) != 0) sb.append("final ");
1.250 + if ((mod & TRANSIENT) != 0) sb.append("transient ");
1.251 + if ((mod & VOLATILE) != 0) sb.append("volatile ");
1.252 + if ((mod & SYNCHRONIZED) != 0) sb.append("synchronized ");
1.253 + if ((mod & NATIVE) != 0) sb.append("native ");
1.254 + if ((mod & STRICT) != 0) sb.append("strictfp ");
1.255 + if ((mod & INTERFACE) != 0) sb.append("interface ");
1.256 +
1.257 + if ((len = sb.length()) > 0) /* trim trailing space */
1.258 + return sb.toString().substring(0, len-1);
1.259 + return "";
1.260 + }
1.261 +
1.262 + /*
1.263 + * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of
1.264 + * <cite>The Java™ Virtual Machine Specification</cite>
1.265 + */
1.266 +
1.267 + /**
1.268 + * The {@code int} value representing the {@code public}
1.269 + * modifier.
1.270 + */
1.271 + public static final int PUBLIC = 0x00000001;
1.272 +
1.273 + /**
1.274 + * The {@code int} value representing the {@code private}
1.275 + * modifier.
1.276 + */
1.277 + public static final int PRIVATE = 0x00000002;
1.278 +
1.279 + /**
1.280 + * The {@code int} value representing the {@code protected}
1.281 + * modifier.
1.282 + */
1.283 + public static final int PROTECTED = 0x00000004;
1.284 +
1.285 + /**
1.286 + * The {@code int} value representing the {@code static}
1.287 + * modifier.
1.288 + */
1.289 + public static final int STATIC = 0x00000008;
1.290 +
1.291 + /**
1.292 + * The {@code int} value representing the {@code final}
1.293 + * modifier.
1.294 + */
1.295 + public static final int FINAL = 0x00000010;
1.296 +
1.297 + /**
1.298 + * The {@code int} value representing the {@code synchronized}
1.299 + * modifier.
1.300 + */
1.301 + public static final int SYNCHRONIZED = 0x00000020;
1.302 +
1.303 + /**
1.304 + * The {@code int} value representing the {@code volatile}
1.305 + * modifier.
1.306 + */
1.307 + public static final int VOLATILE = 0x00000040;
1.308 +
1.309 + /**
1.310 + * The {@code int} value representing the {@code transient}
1.311 + * modifier.
1.312 + */
1.313 + public static final int TRANSIENT = 0x00000080;
1.314 +
1.315 + /**
1.316 + * The {@code int} value representing the {@code native}
1.317 + * modifier.
1.318 + */
1.319 + public static final int NATIVE = 0x00000100;
1.320 +
1.321 + /**
1.322 + * The {@code int} value representing the {@code interface}
1.323 + * modifier.
1.324 + */
1.325 + public static final int INTERFACE = 0x00000200;
1.326 +
1.327 + /**
1.328 + * The {@code int} value representing the {@code abstract}
1.329 + * modifier.
1.330 + */
1.331 + public static final int ABSTRACT = 0x00000400;
1.332 +
1.333 + /**
1.334 + * The {@code int} value representing the {@code strictfp}
1.335 + * modifier.
1.336 + */
1.337 + public static final int STRICT = 0x00000800;
1.338 +
1.339 + // Bits not (yet) exposed in the public API either because they
1.340 + // have different meanings for fields and methods and there is no
1.341 + // way to distinguish between the two in this class, or because
1.342 + // they are not Java programming language keywords
1.343 + static final int BRIDGE = 0x00000040;
1.344 + static final int VARARGS = 0x00000080;
1.345 + static final int SYNTHETIC = 0x00001000;
1.346 + static final int ANNOTATION= 0x00002000;
1.347 + static final int ENUM = 0x00004000;
1.348 + static boolean isSynthetic(int mod) {
1.349 + return (mod & SYNTHETIC) != 0;
1.350 + }
1.351 +
1.352 + /**
1.353 + * See JLSv3 section 8.1.1.
1.354 + */
1.355 + private static final int CLASS_MODIFIERS =
1.356 + Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
1.357 + Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL |
1.358 + Modifier.STRICT;
1.359 +
1.360 + /**
1.361 + * See JLSv3 section 9.1.1.
1.362 + */
1.363 + private static final int INTERFACE_MODIFIERS =
1.364 + Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
1.365 + Modifier.ABSTRACT | Modifier.STATIC | Modifier.STRICT;
1.366 +
1.367 +
1.368 + /**
1.369 + * See JLSv3 section 8.8.3.
1.370 + */
1.371 + private static final int CONSTRUCTOR_MODIFIERS =
1.372 + Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
1.373 +
1.374 + /**
1.375 + * See JLSv3 section 8.4.3.
1.376 + */
1.377 + private static final int METHOD_MODIFIERS =
1.378 + Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
1.379 + Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL |
1.380 + Modifier.SYNCHRONIZED | Modifier.NATIVE | Modifier.STRICT;
1.381 +
1.382 + /**
1.383 + * See JLSv3 section 8.3.1.
1.384 + */
1.385 + private static final int FIELD_MODIFIERS =
1.386 + Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
1.387 + Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT |
1.388 + Modifier.VOLATILE;
1.389 +
1.390 + /**
1.391 + * Return an {@code int} value OR-ing together the source language
1.392 + * modifiers that can be applied to a class.
1.393 + * @return an {@code int} value OR-ing together the source language
1.394 + * modifiers that can be applied to a class.
1.395 + *
1.396 + * @jls 8.1.1 Class Modifiers
1.397 + * @since 1.7
1.398 + */
1.399 + public static int classModifiers() {
1.400 + return CLASS_MODIFIERS;
1.401 + }
1.402 +
1.403 + /**
1.404 + * Return an {@code int} value OR-ing together the source language
1.405 + * modifiers that can be applied to an interface.
1.406 + * @return an {@code int} value OR-ing together the source language
1.407 + * modifiers that can be applied to an inteface.
1.408 + *
1.409 + * @jls 9.1.1 Interface Modifiers
1.410 + * @since 1.7
1.411 + */
1.412 + public static int interfaceModifiers() {
1.413 + return INTERFACE_MODIFIERS;
1.414 + }
1.415 +
1.416 + /**
1.417 + * Return an {@code int} value OR-ing together the source language
1.418 + * modifiers that can be applied to a constructor.
1.419 + * @return an {@code int} value OR-ing together the source language
1.420 + * modifiers that can be applied to a constructor.
1.421 + *
1.422 + * @jls 8.8.3 Constructor Modifiers
1.423 + * @since 1.7
1.424 + */
1.425 + public static int constructorModifiers() {
1.426 + return CONSTRUCTOR_MODIFIERS;
1.427 + }
1.428 +
1.429 + /**
1.430 + * Return an {@code int} value OR-ing together the source language
1.431 + * modifiers that can be applied to a method.
1.432 + * @return an {@code int} value OR-ing together the source language
1.433 + * modifiers that can be applied to a method.
1.434 + *
1.435 + * @jls 8.4.3 Method Modifiers
1.436 + * @since 1.7
1.437 + */
1.438 + public static int methodModifiers() {
1.439 + return METHOD_MODIFIERS;
1.440 + }
1.441 +
1.442 +
1.443 + /**
1.444 + * Return an {@code int} value OR-ing together the source language
1.445 + * modifiers that can be applied to a field.
1.446 + * @return an {@code int} value OR-ing together the source language
1.447 + * modifiers that can be applied to a field.
1.448 + *
1.449 + * @jls 8.3.1 Field Modifiers
1.450 + * @since 1.7
1.451 + */
1.452 + public static int fieldModifiers() {
1.453 + return FIELD_MODIFIERS;
1.454 + }
1.455 +}