001: /*
002: * Javassist, a Java-bytecode translator toolkit.
003: * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
004: *
005: * The contents of this file are subject to the Mozilla Public License Version
006: * 1.1 (the "License"); you may not use this file except in compliance with
007: * the License. Alternatively, the contents of this file may be used under
008: * the terms of the GNU Lesser General Public License Version 2.1 or later.
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the
013: * License.
014: */
015:
016: package javassist;
017:
018: import javassist.bytecode.AccessFlag;
019:
020: /**
021: * The Modifier class provides static methods and constants to decode
022: * class and member access modifiers. The constant values are equivalent
023: * to the corresponding values in <code>javassist.bytecode.AccessFlag</code>.
024: *
025: * <p>All the methods/constants in this class are compatible with
026: * ones in <code>java.lang.reflect.Modifier</code>.
027: *
028: * @see CtClass#getModifiers()
029: */
030: public class Modifier {
031: public static final int PUBLIC = AccessFlag.PUBLIC;
032: public static final int PRIVATE = AccessFlag.PRIVATE;
033: public static final int PROTECTED = AccessFlag.PROTECTED;
034: public static final int STATIC = AccessFlag.STATIC;
035: public static final int FINAL = AccessFlag.FINAL;
036: public static final int SYNCHRONIZED = AccessFlag.SYNCHRONIZED;
037: public static final int VOLATILE = AccessFlag.VOLATILE;
038: public static final int TRANSIENT = AccessFlag.TRANSIENT;
039: public static final int NATIVE = AccessFlag.NATIVE;
040: public static final int INTERFACE = AccessFlag.INTERFACE;
041: public static final int ABSTRACT = AccessFlag.ABSTRACT;
042: public static final int STRICT = AccessFlag.STRICT;
043: public static final int ANNOTATION = AccessFlag.ANNOTATION;
044: public static final int ENUM = AccessFlag.ENUM;
045:
046: /**
047: * Returns true if the modifiers include the <tt>public</tt>
048: * modifier.
049: */
050: public static boolean isPublic(int mod) {
051: return (mod & PUBLIC) != 0;
052: }
053:
054: /**
055: * Returns true if the modifiers include the <tt>private</tt>
056: * modifier.
057: */
058: public static boolean isPrivate(int mod) {
059: return (mod & PRIVATE) != 0;
060: }
061:
062: /**
063: * Returns true if the modifiers include the <tt>protected</tt>
064: * modifier.
065: */
066: public static boolean isProtected(int mod) {
067: return (mod & PROTECTED) != 0;
068: }
069:
070: /**
071: * Returns true if the modifiers do not include either
072: * <tt>public</tt>, <tt>protected</tt>, or <tt>private</tt>.
073: */
074: public static boolean isPackage(int mod) {
075: return (mod & (PUBLIC | PRIVATE | PROTECTED)) == 0;
076: }
077:
078: /**
079: * Returns true if the modifiers include the <tt>static</tt>
080: * modifier.
081: */
082: public static boolean isStatic(int mod) {
083: return (mod & STATIC) != 0;
084: }
085:
086: /**
087: * Returns true if the modifiers include the <tt>final</tt>
088: * modifier.
089: */
090: public static boolean isFinal(int mod) {
091: return (mod & FINAL) != 0;
092: }
093:
094: /**
095: * Returns true if the modifiers include the <tt>synchronized</tt>
096: * modifier.
097: */
098: public static boolean isSynchronized(int mod) {
099: return (mod & SYNCHRONIZED) != 0;
100: }
101:
102: /**
103: * Returns true if the modifiers include the <tt>volatile</tt>
104: * modifier.
105: */
106: public static boolean isVolatile(int mod) {
107: return (mod & VOLATILE) != 0;
108: }
109:
110: /**
111: * Returns true if the modifiers include the <tt>transient</tt>
112: * modifier.
113: */
114: public static boolean isTransient(int mod) {
115: return (mod & TRANSIENT) != 0;
116: }
117:
118: /**
119: * Returns true if the modifiers include the <tt>native</tt>
120: * modifier.
121: */
122: public static boolean isNative(int mod) {
123: return (mod & NATIVE) != 0;
124: }
125:
126: /**
127: * Returns true if the modifiers include the <tt>interface</tt>
128: * modifier.
129: */
130: public static boolean isInterface(int mod) {
131: return (mod & INTERFACE) != 0;
132: }
133:
134: /**
135: * Returns true if the modifiers include the <tt>annotation</tt>
136: * modifier.
137: *
138: * @since 3.2
139: */
140: public static boolean isAnnotation(int mod) {
141: return (mod & ANNOTATION) != 0;
142: }
143:
144: /**
145: * Returns true if the modifiers include the <tt>enum</tt>
146: * modifier.
147: *
148: * @since 3.2
149: */
150: public static boolean isEnum(int mod) {
151: return (mod & ENUM) != 0;
152: }
153:
154: /**
155: * Returns true if the modifiers include the <tt>abstract</tt>
156: * modifier.
157: */
158: public static boolean isAbstract(int mod) {
159: return (mod & ABSTRACT) != 0;
160: }
161:
162: /**
163: * Returns true if the modifiers include the <tt>strictfp</tt>
164: * modifier.
165: */
166: public static boolean isStrict(int mod) {
167: return (mod & STRICT) != 0;
168: }
169:
170: /**
171: * Truns the public bit on. The protected and private bits are
172: * cleared.
173: */
174: public static int setPublic(int mod) {
175: return (mod & ~(PRIVATE | PROTECTED)) | PUBLIC;
176: }
177:
178: /**
179: * Truns the protected bit on. The protected and public bits are
180: * cleared.
181: */
182: public static int setProtected(int mod) {
183: return (mod & ~(PRIVATE | PUBLIC)) | PROTECTED;
184: }
185:
186: /**
187: * Truns the private bit on. The protected and private bits are
188: * cleared.
189: */
190: public static int setPrivate(int mod) {
191: return (mod & ~(PROTECTED | PUBLIC)) | PRIVATE;
192: }
193:
194: /**
195: * Clears the public, protected, and private bits.
196: */
197: public static int setPackage(int mod) {
198: return (mod & ~(PROTECTED | PUBLIC | PRIVATE));
199: }
200:
201: /**
202: * Clears a specified bit in <code>mod</code>.
203: */
204: public static int clear(int mod, int clearBit) {
205: return mod & ~clearBit;
206: }
207:
208: /**
209: * Return a string describing the access modifier flags in
210: * the specified modifier.
211: *
212: * @param mod modifier flags.
213: */
214: public static String toString(int mod) {
215: return java.lang.reflect.Modifier.toString(mod);
216: }
217: }
|