1/*
2 * Javassist, a Java-bytecode translator toolkit.
3 * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved.
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License.  Alternatively, the contents of this file may be used under
8 * the terms of the GNU Lesser General Public License Version 2.1 or later.
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 */
15
16package javassist;
17
18import javassist.bytecode.AccessFlag;
19
20/**
21 * The Modifier class provides static methods and constants to decode
22 * class and member access modifiers.  The constant values are equivalent
23 * to the corresponding values in <code>javassist.bytecode.AccessFlag</code>.
24 *
25 * <p>All the methods/constants in this class are compatible with
26 * ones in <code>java.lang.reflect.Modifier</code>.
27 *
28 * @see CtClass#getModifiers()
29 */
30public class Modifier {
31    public static final int PUBLIC    = AccessFlag.PUBLIC;
32    public static final int PRIVATE   = AccessFlag.PRIVATE;
33    public static final int PROTECTED = AccessFlag.PROTECTED;
34    public static final int STATIC    = AccessFlag.STATIC;
35    public static final int FINAL     = AccessFlag.FINAL;
36    public static final int SYNCHRONIZED = AccessFlag.SYNCHRONIZED;
37    public static final int VOLATILE  = AccessFlag.VOLATILE;
38    public static final int VARARGS = AccessFlag.VARARGS;
39    public static final int TRANSIENT = AccessFlag.TRANSIENT;
40    public static final int NATIVE    = AccessFlag.NATIVE;
41    public static final int INTERFACE = AccessFlag.INTERFACE;
42    public static final int ABSTRACT  = AccessFlag.ABSTRACT;
43    public static final int STRICT    = AccessFlag.STRICT;
44    public static final int ANNOTATION = AccessFlag.ANNOTATION;
45    public static final int ENUM      = AccessFlag.ENUM;
46
47    /**
48     * Returns true if the modifiers include the <tt>public</tt>
49     * modifier.
50     */
51    public static boolean isPublic(int mod) {
52        return (mod & PUBLIC) != 0;
53    }
54
55    /**
56     * Returns true if the modifiers include the <tt>private</tt>
57     * modifier.
58     */
59    public static boolean isPrivate(int mod) {
60        return (mod & PRIVATE) != 0;
61    }
62
63    /**
64     * Returns true if the modifiers include the <tt>protected</tt>
65     * modifier.
66     */
67    public static boolean isProtected(int mod) {
68        return (mod & PROTECTED) != 0;
69    }
70
71    /**
72     * Returns true if the modifiers do not include either
73     * <tt>public</tt>, <tt>protected</tt>, or <tt>private</tt>.
74     */
75    public static boolean isPackage(int mod) {
76        return (mod & (PUBLIC | PRIVATE | PROTECTED)) == 0;
77    }
78
79    /**
80     * Returns true if the modifiers include the <tt>static</tt>
81     * modifier.
82     */
83    public static boolean isStatic(int mod) {
84        return (mod & STATIC) != 0;
85    }
86
87    /**
88     * Returns true if the modifiers include the <tt>final</tt>
89     * modifier.
90     */
91    public static boolean isFinal(int mod) {
92        return (mod & FINAL) != 0;
93    }
94
95    /**
96     * Returns true if the modifiers include the <tt>synchronized</tt>
97     * modifier.
98     */
99    public static boolean isSynchronized(int mod) {
100        return (mod & SYNCHRONIZED) != 0;
101    }
102
103    /**
104     * Returns true if the modifiers include the <tt>volatile</tt>
105     * modifier.
106     */
107    public static boolean isVolatile(int mod) {
108        return (mod & VOLATILE) != 0;
109    }
110
111    /**
112     * Returns true if the modifiers include the <tt>transient</tt>
113     * modifier.
114     */
115    public static boolean isTransient(int mod) {
116        return (mod & TRANSIENT) != 0;
117    }
118
119    /**
120     * Returns true if the modifiers include the <tt>native</tt>
121     * modifier.
122     */
123    public static boolean isNative(int mod) {
124        return (mod & NATIVE) != 0;
125    }
126
127    /**
128     * Returns true if the modifiers include the <tt>interface</tt>
129     * modifier.
130     */
131    public static boolean isInterface(int mod) {
132        return (mod & INTERFACE) != 0;
133    }
134
135    /**
136     * Returns true if the modifiers include the <tt>annotation</tt>
137     * modifier.
138     *
139     * @since 3.2
140     */
141    public static boolean isAnnotation(int mod) {
142        return (mod & ANNOTATION) != 0;
143    }
144
145    /**
146     * Returns true if the modifiers include the <tt>enum</tt>
147     * modifier.
148     *
149     * @since 3.2
150     */
151    public static boolean isEnum(int mod) {
152        return (mod & ENUM) != 0;
153    }
154
155    /**
156     * Returns true if the modifiers include the <tt>abstract</tt>
157     * modifier.
158     */
159    public static boolean isAbstract(int mod) {
160        return (mod & ABSTRACT) != 0;
161    }
162
163    /**
164     * Returns true if the modifiers include the <tt>strictfp</tt>
165     * modifier.
166     */
167    public static boolean isStrict(int mod) {
168        return (mod & STRICT) != 0;
169    }
170
171    /**
172     * Truns the public bit on.  The protected and private bits are
173     * cleared.
174     */
175    public static int setPublic(int mod) {
176        return (mod & ~(PRIVATE | PROTECTED)) | PUBLIC;
177    }
178
179    /**
180     * Truns the protected bit on.  The protected and public bits are
181     * cleared.
182     */
183    public static int setProtected(int mod) {
184        return (mod & ~(PRIVATE | PUBLIC)) | PROTECTED;
185    }
186
187    /**
188     * Truns the private bit on.  The protected and private bits are
189     * cleared.
190     */
191    public static int setPrivate(int mod) {
192        return (mod & ~(PROTECTED | PUBLIC)) | PRIVATE;
193    }
194
195    /**
196     * Clears the public, protected, and private bits.
197     */
198    public static int setPackage(int mod) {
199        return (mod & ~(PROTECTED | PUBLIC | PRIVATE));
200    }
201
202    /**
203     * Clears a specified bit in <code>mod</code>.
204     */
205    public static int clear(int mod, int clearBit) {
206        return mod & ~clearBit;
207    }
208
209    /**
210     * Return a string describing the access modifier flags in
211     * the specified modifier.
212     *
213     * @param mod   modifier flags.
214     */
215    public static String toString(int mod) {
216        return java.lang.reflect.Modifier.toString(mod);
217    }
218}
219