169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/*
269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Javassist, a Java-bytecode translator toolkit.
369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The contents of this file are subject to the Mozilla Public License Version
669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1.1 (the "License"); you may not use this file except in compliance with
769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the License.  Alternatively, the contents of this file may be used under
869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the terms of the GNU Lesser General Public License Version 2.1 or later.
969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Software distributed under the License is distributed on an "AS IS" basis,
1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * for the specific language governing rights and limitations under the
1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * License.
1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage javassist;
1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.bytecode.AccessFlag;
1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/**
2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The Modifier class provides static methods and constants to decode
2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * class and member access modifiers.  The constant values are equivalent
2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * to the corresponding values in <code>javassist.bytecode.AccessFlag</code>.
2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p>All the methods/constants in this class are compatible with
2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * ones in <code>java.lang.reflect.Modifier</code>.
2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @see CtClass#getModifiers()
2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpublic class Modifier {
3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final int PUBLIC    = AccessFlag.PUBLIC;
3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final int PRIVATE   = AccessFlag.PRIVATE;
3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final int PROTECTED = AccessFlag.PROTECTED;
3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final int STATIC    = AccessFlag.STATIC;
3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final int FINAL     = AccessFlag.FINAL;
3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final int SYNCHRONIZED = AccessFlag.SYNCHRONIZED;
3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final int VOLATILE  = AccessFlag.VOLATILE;
3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final int VARARGS = AccessFlag.VARARGS;
3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final int TRANSIENT = AccessFlag.TRANSIENT;
4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final int NATIVE    = AccessFlag.NATIVE;
4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final int INTERFACE = AccessFlag.INTERFACE;
4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final int ABSTRACT  = AccessFlag.ABSTRACT;
4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final int STRICT    = AccessFlag.STRICT;
4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final int ANNOTATION = AccessFlag.ANNOTATION;
4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static final int ENUM      = AccessFlag.ENUM;
4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if the modifiers include the <tt>public</tt>
4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * modifier.
5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static boolean isPublic(int mod) {
5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & PUBLIC) != 0;
5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if the modifiers include the <tt>private</tt>
5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * modifier.
5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static boolean isPrivate(int mod) {
6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & PRIVATE) != 0;
6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if the modifiers include the <tt>protected</tt>
6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * modifier.
6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static boolean isProtected(int mod) {
6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & PROTECTED) != 0;
6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
7269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if the modifiers do not include either
7369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <tt>public</tt>, <tt>protected</tt>, or <tt>private</tt>.
7469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
7569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static boolean isPackage(int mod) {
7669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & (PUBLIC | PRIVATE | PROTECTED)) == 0;
7769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
7869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
7969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
8069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if the modifiers include the <tt>static</tt>
8169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * modifier.
8269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
8369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static boolean isStatic(int mod) {
8469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & STATIC) != 0;
8569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
8669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
8769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
8869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if the modifiers include the <tt>final</tt>
8969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * modifier.
9069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
9169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static boolean isFinal(int mod) {
9269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & FINAL) != 0;
9369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
9469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
9569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
9669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if the modifiers include the <tt>synchronized</tt>
9769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * modifier.
9869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
9969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static boolean isSynchronized(int mod) {
10069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & SYNCHRONIZED) != 0;
10169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
10269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
10369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
10469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if the modifiers include the <tt>volatile</tt>
10569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * modifier.
10669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
10769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static boolean isVolatile(int mod) {
10869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & VOLATILE) != 0;
10969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
11069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
11169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
11269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if the modifiers include the <tt>transient</tt>
11369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * modifier.
11469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
11569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static boolean isTransient(int mod) {
11669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & TRANSIENT) != 0;
11769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
11869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
11969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
12069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if the modifiers include the <tt>native</tt>
12169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * modifier.
12269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
12369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static boolean isNative(int mod) {
12469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & NATIVE) != 0;
12569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
12669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
12769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
12869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if the modifiers include the <tt>interface</tt>
12969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * modifier.
13069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
13169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static boolean isInterface(int mod) {
13269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & INTERFACE) != 0;
13369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
13469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
13569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
13669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if the modifiers include the <tt>annotation</tt>
13769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * modifier.
13869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
13969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @since 3.2
14069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
14169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static boolean isAnnotation(int mod) {
14269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & ANNOTATION) != 0;
14369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
14469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
14569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
14669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if the modifiers include the <tt>enum</tt>
14769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * modifier.
14869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
14969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @since 3.2
15069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
15169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static boolean isEnum(int mod) {
15269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & ENUM) != 0;
15369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
15469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
15569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
15669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if the modifiers include the <tt>abstract</tt>
15769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * modifier.
15869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
15969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static boolean isAbstract(int mod) {
16069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & ABSTRACT) != 0;
16169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
16269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
16369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
16469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Returns true if the modifiers include the <tt>strictfp</tt>
16569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * modifier.
16669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
16769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static boolean isStrict(int mod) {
16869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & STRICT) != 0;
16969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
17069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
17169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
17269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Truns the public bit on.  The protected and private bits are
17369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * cleared.
17469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
17569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static int setPublic(int mod) {
17669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & ~(PRIVATE | PROTECTED)) | PUBLIC;
17769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
17869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
17969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
18069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Truns the protected bit on.  The protected and public bits are
18169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * cleared.
18269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
18369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static int setProtected(int mod) {
18469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & ~(PRIVATE | PUBLIC)) | PROTECTED;
18569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
18669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
18769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
18869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Truns the private bit on.  The protected and private bits are
18969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * cleared.
19069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
19169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static int setPrivate(int mod) {
19269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & ~(PROTECTED | PUBLIC)) | PRIVATE;
19369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
19469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
19569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
19669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Clears the public, protected, and private bits.
19769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
19869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static int setPackage(int mod) {
19969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return (mod & ~(PROTECTED | PUBLIC | PRIVATE));
20069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
20169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
20269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
20369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Clears a specified bit in <code>mod</code>.
20469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
20569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static int clear(int mod, int clearBit) {
20669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return mod & ~clearBit;
20769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
20869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
20969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
21069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Return a string describing the access modifier flags in
21169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * the specified modifier.
21269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
21369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param mod   modifier flags.
21469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
21569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static String toString(int mod) {
21669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return java.lang.reflect.Modifier.toString(mod);
21769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
21869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
219