jtulach@258: /* jtulach@258: * Copyright (c) 1996, 2008, Oracle and/or its affiliates. All rights reserved. jtulach@258: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jtulach@258: * jtulach@258: * This code is free software; you can redistribute it and/or modify it jtulach@258: * under the terms of the GNU General Public License version 2 only, as jtulach@258: * published by the Free Software Foundation. Oracle designates this jtulach@258: * particular file as subject to the "Classpath" exception as provided jtulach@258: * by Oracle in the LICENSE file that accompanied this code. jtulach@258: * jtulach@258: * This code is distributed in the hope that it will be useful, but WITHOUT jtulach@258: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jtulach@258: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jtulach@258: * version 2 for more details (a copy is included in the LICENSE file that jtulach@258: * accompanied this code). jtulach@258: * jtulach@258: * You should have received a copy of the GNU General Public License version jtulach@258: * 2 along with this work; if not, write to the Free Software Foundation, jtulach@258: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jtulach@258: * jtulach@258: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jtulach@258: * or visit www.oracle.com if you need additional information or have any jtulach@258: * questions. jtulach@258: */ jtulach@258: jtulach@258: package java.lang.reflect; jtulach@258: jtulach@258: /** jtulach@258: * The Modifier class provides {@code static} methods and jtulach@258: * constants to decode class and member access modifiers. The sets of jtulach@258: * modifiers are represented as integers with distinct bit positions jtulach@258: * representing different modifiers. The values for the constants jtulach@258: * representing the modifiers are taken from the tables in sections 4.1, 4.4, 4.5, and 4.7 of jtulach@258: * The Java™ Virtual Machine Specification. jtulach@258: * jtulach@258: * @see Class#getModifiers() jtulach@258: * @see Member#getModifiers() jtulach@258: * jtulach@258: * @author Nakul Saraiya jtulach@258: * @author Kenneth Russell jtulach@258: */ jtulach@258: public jtulach@258: class Modifier { jtulach@258: jtulach@258: /** jtulach@258: * Return {@code true} if the integer argument includes the jtulach@258: * {@code public} modifier, {@code false} otherwise. jtulach@258: * jtulach@258: * @param mod a set of modifiers jtulach@258: * @return {@code true} if {@code mod} includes the jtulach@258: * {@code public} modifier; {@code false} otherwise. jtulach@258: */ jtulach@258: public static boolean isPublic(int mod) { jtulach@258: return (mod & PUBLIC) != 0; jtulach@258: } jtulach@258: jtulach@258: /** jtulach@258: * Return {@code true} if the integer argument includes the jtulach@258: * {@code private} modifier, {@code false} otherwise. jtulach@258: * jtulach@258: * @param mod a set of modifiers jtulach@258: * @return {@code true} if {@code mod} includes the jtulach@258: * {@code private} modifier; {@code false} otherwise. jtulach@258: */ jtulach@258: public static boolean isPrivate(int mod) { jtulach@258: return (mod & PRIVATE) != 0; jtulach@258: } jtulach@258: jtulach@258: /** jtulach@258: * Return {@code true} if the integer argument includes the jtulach@258: * {@code protected} modifier, {@code false} otherwise. jtulach@258: * jtulach@258: * @param mod a set of modifiers jtulach@258: * @return {@code true} if {@code mod} includes the jtulach@258: * {@code protected} modifier; {@code false} otherwise. jtulach@258: */ jtulach@258: public static boolean isProtected(int mod) { jtulach@258: return (mod & PROTECTED) != 0; jtulach@258: } jtulach@258: jtulach@258: /** jtulach@258: * Return {@code true} if the integer argument includes the jtulach@258: * {@code static} modifier, {@code false} otherwise. jtulach@258: * jtulach@258: * @param mod a set of modifiers jtulach@258: * @return {@code true} if {@code mod} includes the jtulach@258: * {@code static} modifier; {@code false} otherwise. jtulach@258: */ jtulach@258: public static boolean isStatic(int mod) { jtulach@258: return (mod & STATIC) != 0; jtulach@258: } jtulach@258: jtulach@258: /** jtulach@258: * Return {@code true} if the integer argument includes the jtulach@258: * {@code final} modifier, {@code false} otherwise. jtulach@258: * jtulach@258: * @param mod a set of modifiers jtulach@258: * @return {@code true} if {@code mod} includes the jtulach@258: * {@code final} modifier; {@code false} otherwise. jtulach@258: */ jtulach@258: public static boolean isFinal(int mod) { jtulach@258: return (mod & FINAL) != 0; jtulach@258: } jtulach@258: jtulach@258: /** jtulach@258: * Return {@code true} if the integer argument includes the jtulach@258: * {@code synchronized} modifier, {@code false} otherwise. jtulach@258: * jtulach@258: * @param mod a set of modifiers jtulach@258: * @return {@code true} if {@code mod} includes the jtulach@258: * {@code synchronized} modifier; {@code false} otherwise. jtulach@258: */ jtulach@258: public static boolean isSynchronized(int mod) { jtulach@258: return (mod & SYNCHRONIZED) != 0; jtulach@258: } jtulach@258: jtulach@258: /** jtulach@258: * Return {@code true} if the integer argument includes the jtulach@258: * {@code volatile} modifier, {@code false} otherwise. jtulach@258: * jtulach@258: * @param mod a set of modifiers jtulach@258: * @return {@code true} if {@code mod} includes the jtulach@258: * {@code volatile} modifier; {@code false} otherwise. jtulach@258: */ jtulach@258: public static boolean isVolatile(int mod) { jtulach@258: return (mod & VOLATILE) != 0; jtulach@258: } jtulach@258: jtulach@258: /** jtulach@258: * Return {@code true} if the integer argument includes the jtulach@258: * {@code transient} modifier, {@code false} otherwise. jtulach@258: * jtulach@258: * @param mod a set of modifiers jtulach@258: * @return {@code true} if {@code mod} includes the jtulach@258: * {@code transient} modifier; {@code false} otherwise. jtulach@258: */ jtulach@258: public static boolean isTransient(int mod) { jtulach@258: return (mod & TRANSIENT) != 0; jtulach@258: } jtulach@258: jtulach@258: /** jtulach@258: * Return {@code true} if the integer argument includes the jtulach@258: * {@code native} modifier, {@code false} otherwise. jtulach@258: * jtulach@258: * @param mod a set of modifiers jtulach@258: * @return {@code true} if {@code mod} includes the jtulach@258: * {@code native} modifier; {@code false} otherwise. jtulach@258: */ jtulach@258: public static boolean isNative(int mod) { jtulach@258: return (mod & NATIVE) != 0; jtulach@258: } jtulach@258: jtulach@258: /** jtulach@258: * Return {@code true} if the integer argument includes the jtulach@258: * {@code interface} modifier, {@code false} otherwise. jtulach@258: * jtulach@258: * @param mod a set of modifiers jtulach@258: * @return {@code true} if {@code mod} includes the jtulach@258: * {@code interface} modifier; {@code false} otherwise. jtulach@258: */ jtulach@258: public static boolean isInterface(int mod) { jtulach@258: return (mod & INTERFACE) != 0; jtulach@258: } jtulach@258: jtulach@258: /** jtulach@258: * Return {@code true} if the integer argument includes the jtulach@258: * {@code abstract} modifier, {@code false} otherwise. jtulach@258: * jtulach@258: * @param mod a set of modifiers jtulach@258: * @return {@code true} if {@code mod} includes the jtulach@258: * {@code abstract} modifier; {@code false} otherwise. jtulach@258: */ jtulach@258: public static boolean isAbstract(int mod) { jtulach@258: return (mod & ABSTRACT) != 0; jtulach@258: } jtulach@258: jtulach@258: /** jtulach@258: * Return {@code true} if the integer argument includes the jtulach@258: * {@code strictfp} modifier, {@code false} otherwise. jtulach@258: * jtulach@258: * @param mod a set of modifiers jtulach@258: * @return {@code true} if {@code mod} includes the jtulach@258: * {@code strictfp} modifier; {@code false} otherwise. jtulach@258: */ jtulach@258: public static boolean isStrict(int mod) { jtulach@258: return (mod & STRICT) != 0; jtulach@258: } jtulach@258: jtulach@258: /** jtulach@258: * Return a string describing the access modifier flags in jtulach@258: * the specified modifier. For example: jtulach@258: *
jtulach@258:      *    public final synchronized strictfp
jtulach@258:      * 
jtulach@258: * The modifier names are returned in an order consistent with the jtulach@258: * suggested modifier orderings given in sections 8.1.1, 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of jtulach@258: * The Java™ Language Specification. jtulach@258: * The full modifier ordering used by this method is: jtulach@258: *
{@code jtulach@258: * public protected private abstract static final transient jtulach@258: * volatile synchronized native strictfp jtulach@258: * interface }
jtulach@258: * The {@code interface} modifier discussed in this class is jtulach@258: * not a true modifier in the Java language and it appears after jtulach@258: * all other modifiers listed by this method. This method may jtulach@258: * return a string of modifiers that are not valid modifiers of a jtulach@258: * Java entity; in other words, no checking is done on the jtulach@258: * possible validity of the combination of modifiers represented jtulach@258: * by the input. jtulach@258: * jtulach@258: * Note that to perform such checking for a known kind of entity, jtulach@258: * such as a constructor or method, first AND the argument of jtulach@258: * {@code toString} with the appropriate mask from a method like jtulach@258: * {@link #constructorModifiers} or {@link #methodModifiers}. jtulach@258: * jtulach@258: * @param mod a set of modifiers jtulach@258: * @return a string representation of the set of modifiers jtulach@258: * represented by {@code mod} jtulach@258: */ jtulach@258: public static String toString(int mod) { jtulach@258: StringBuffer sb = new StringBuffer(); jtulach@258: int len; jtulach@258: jtulach@258: if ((mod & PUBLIC) != 0) sb.append("public "); jtulach@258: if ((mod & PROTECTED) != 0) sb.append("protected "); jtulach@258: if ((mod & PRIVATE) != 0) sb.append("private "); jtulach@258: jtulach@258: /* Canonical order */ jtulach@258: if ((mod & ABSTRACT) != 0) sb.append("abstract "); jtulach@258: if ((mod & STATIC) != 0) sb.append("static "); jtulach@258: if ((mod & FINAL) != 0) sb.append("final "); jtulach@258: if ((mod & TRANSIENT) != 0) sb.append("transient "); jtulach@258: if ((mod & VOLATILE) != 0) sb.append("volatile "); jtulach@258: if ((mod & SYNCHRONIZED) != 0) sb.append("synchronized "); jtulach@258: if ((mod & NATIVE) != 0) sb.append("native "); jtulach@258: if ((mod & STRICT) != 0) sb.append("strictfp "); jtulach@258: if ((mod & INTERFACE) != 0) sb.append("interface "); jtulach@258: jtulach@258: if ((len = sb.length()) > 0) /* trim trailing space */ jtulach@258: return sb.toString().substring(0, len-1); jtulach@258: return ""; jtulach@258: } jtulach@258: jtulach@258: /* jtulach@258: * Access modifier flag constants from tables 4.1, 4.4, 4.5, and 4.7 of jtulach@258: * The Java™ Virtual Machine Specification jtulach@258: */ jtulach@258: jtulach@258: /** jtulach@258: * The {@code int} value representing the {@code public} jtulach@258: * modifier. jtulach@258: */ jtulach@258: public static final int PUBLIC = 0x00000001; jtulach@258: jtulach@258: /** jtulach@258: * The {@code int} value representing the {@code private} jtulach@258: * modifier. jtulach@258: */ jtulach@258: public static final int PRIVATE = 0x00000002; jtulach@258: jtulach@258: /** jtulach@258: * The {@code int} value representing the {@code protected} jtulach@258: * modifier. jtulach@258: */ jtulach@258: public static final int PROTECTED = 0x00000004; jtulach@258: jtulach@258: /** jtulach@258: * The {@code int} value representing the {@code static} jtulach@258: * modifier. jtulach@258: */ jtulach@258: public static final int STATIC = 0x00000008; jtulach@258: jtulach@258: /** jtulach@258: * The {@code int} value representing the {@code final} jtulach@258: * modifier. jtulach@258: */ jtulach@258: public static final int FINAL = 0x00000010; jtulach@258: jtulach@258: /** jtulach@258: * The {@code int} value representing the {@code synchronized} jtulach@258: * modifier. jtulach@258: */ jtulach@258: public static final int SYNCHRONIZED = 0x00000020; jtulach@258: jtulach@258: /** jtulach@258: * The {@code int} value representing the {@code volatile} jtulach@258: * modifier. jtulach@258: */ jtulach@258: public static final int VOLATILE = 0x00000040; jtulach@258: jtulach@258: /** jtulach@258: * The {@code int} value representing the {@code transient} jtulach@258: * modifier. jtulach@258: */ jtulach@258: public static final int TRANSIENT = 0x00000080; jtulach@258: jtulach@258: /** jtulach@258: * The {@code int} value representing the {@code native} jtulach@258: * modifier. jtulach@258: */ jtulach@258: public static final int NATIVE = 0x00000100; jtulach@258: jtulach@258: /** jtulach@258: * The {@code int} value representing the {@code interface} jtulach@258: * modifier. jtulach@258: */ jtulach@258: public static final int INTERFACE = 0x00000200; jtulach@258: jtulach@258: /** jtulach@258: * The {@code int} value representing the {@code abstract} jtulach@258: * modifier. jtulach@258: */ jtulach@258: public static final int ABSTRACT = 0x00000400; jtulach@258: jtulach@258: /** jtulach@258: * The {@code int} value representing the {@code strictfp} jtulach@258: * modifier. jtulach@258: */ jtulach@258: public static final int STRICT = 0x00000800; jtulach@258: jtulach@258: // Bits not (yet) exposed in the public API either because they jtulach@258: // have different meanings for fields and methods and there is no jtulach@258: // way to distinguish between the two in this class, or because jtulach@258: // they are not Java programming language keywords jtulach@258: static final int BRIDGE = 0x00000040; jtulach@258: static final int VARARGS = 0x00000080; jtulach@258: static final int SYNTHETIC = 0x00001000; jtulach@258: static final int ANNOTATION= 0x00002000; jtulach@258: static final int ENUM = 0x00004000; jtulach@258: static boolean isSynthetic(int mod) { jtulach@258: return (mod & SYNTHETIC) != 0; jtulach@258: } jtulach@258: jtulach@258: /** jtulach@258: * See JLSv3 section 8.1.1. jtulach@258: */ jtulach@258: private static final int CLASS_MODIFIERS = jtulach@258: Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | jtulach@258: Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL | jtulach@258: Modifier.STRICT; jtulach@258: jtulach@258: /** jtulach@258: * See JLSv3 section 9.1.1. jtulach@258: */ jtulach@258: private static final int INTERFACE_MODIFIERS = jtulach@258: Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | jtulach@258: Modifier.ABSTRACT | Modifier.STATIC | Modifier.STRICT; jtulach@258: jtulach@258: jtulach@258: /** jtulach@258: * See JLSv3 section 8.8.3. jtulach@258: */ jtulach@258: private static final int CONSTRUCTOR_MODIFIERS = jtulach@258: Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE; jtulach@258: jtulach@258: /** jtulach@258: * See JLSv3 section 8.4.3. jtulach@258: */ jtulach@258: private static final int METHOD_MODIFIERS = jtulach@258: Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | jtulach@258: Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL | jtulach@258: Modifier.SYNCHRONIZED | Modifier.NATIVE | Modifier.STRICT; jtulach@258: jtulach@258: /** jtulach@258: * See JLSv3 section 8.3.1. jtulach@258: */ jtulach@258: private static final int FIELD_MODIFIERS = jtulach@258: Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | jtulach@258: Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT | jtulach@258: Modifier.VOLATILE; jtulach@258: jtulach@258: /** jtulach@258: * Return an {@code int} value OR-ing together the source language jtulach@258: * modifiers that can be applied to a class. jtulach@258: * @return an {@code int} value OR-ing together the source language jtulach@258: * modifiers that can be applied to a class. jtulach@258: * jtulach@258: * @jls 8.1.1 Class Modifiers jtulach@258: * @since 1.7 jtulach@258: */ jtulach@258: public static int classModifiers() { jtulach@258: return CLASS_MODIFIERS; jtulach@258: } jtulach@258: jtulach@258: /** jtulach@258: * Return an {@code int} value OR-ing together the source language jtulach@258: * modifiers that can be applied to an interface. jtulach@258: * @return an {@code int} value OR-ing together the source language jtulach@258: * modifiers that can be applied to an inteface. jtulach@258: * jtulach@258: * @jls 9.1.1 Interface Modifiers jtulach@258: * @since 1.7 jtulach@258: */ jtulach@258: public static int interfaceModifiers() { jtulach@258: return INTERFACE_MODIFIERS; jtulach@258: } jtulach@258: jtulach@258: /** jtulach@258: * Return an {@code int} value OR-ing together the source language jtulach@258: * modifiers that can be applied to a constructor. jtulach@258: * @return an {@code int} value OR-ing together the source language jtulach@258: * modifiers that can be applied to a constructor. jtulach@258: * jtulach@258: * @jls 8.8.3 Constructor Modifiers jtulach@258: * @since 1.7 jtulach@258: */ jtulach@258: public static int constructorModifiers() { jtulach@258: return CONSTRUCTOR_MODIFIERS; jtulach@258: } jtulach@258: jtulach@258: /** jtulach@258: * Return an {@code int} value OR-ing together the source language jtulach@258: * modifiers that can be applied to a method. jtulach@258: * @return an {@code int} value OR-ing together the source language jtulach@258: * modifiers that can be applied to a method. jtulach@258: * jtulach@258: * @jls 8.4.3 Method Modifiers jtulach@258: * @since 1.7 jtulach@258: */ jtulach@258: public static int methodModifiers() { jtulach@258: return METHOD_MODIFIERS; jtulach@258: } jtulach@258: jtulach@258: jtulach@258: /** jtulach@258: * Return an {@code int} value OR-ing together the source language jtulach@258: * modifiers that can be applied to a field. jtulach@258: * @return an {@code int} value OR-ing together the source language jtulach@258: * modifiers that can be applied to a field. jtulach@258: * jtulach@258: * @jls 8.3.1 Field Modifiers jtulach@258: * @since 1.7 jtulach@258: */ jtulach@258: public static int fieldModifiers() { jtulach@258: return FIELD_MODIFIERS; jtulach@258: } jtulach@258: }