001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.lang.reflect;
019:
020: /**
021: * This class provides methods to decode class and member modifiers.
022: *
023: * @see Class#getModifiers()
024: * @see Member#getModifiers()
025: */
026: public class Modifier {
027:
028: public static final int PUBLIC = 0x1;
029:
030: public static final int PRIVATE = 0x2;
031:
032: public static final int PROTECTED = 0x4;
033:
034: public static final int STATIC = 0x8;
035:
036: public static final int FINAL = 0x10;
037:
038: public static final int SYNCHRONIZED = 0x20;
039:
040: public static final int VOLATILE = 0x40;
041:
042: public static final int TRANSIENT = 0x80;
043:
044: public static final int NATIVE = 0x100;
045:
046: public static final int INTERFACE = 0x200;
047:
048: public static final int ABSTRACT = 0x400;
049:
050: public static final int STRICT = 0x800;
051:
052: // Non-public types required by Java 5 update to class file format
053: static final int BRIDGE = 0x40;
054:
055: static final int VARARGS = 0x80;
056:
057: static final int SYNTHETIC = 0x1000;
058:
059: static final int ANNOTATION = 0x2000;
060:
061: static final int ENUM = 0x4000;
062:
063: public Modifier() {
064: }
065:
066: /**
067: * Return true if the specified modifiers contain the <code>abstract</code>
068: * modifier, false otherwise.
069: *
070: * @param modifiers
071: * the modifiers to test
072: * @return if the modifiers contain the abstract modifier
073: */
074: public static boolean isAbstract(int modifiers) {
075: return ((modifiers & ABSTRACT) != 0);
076: }
077:
078: /**
079: * Return true if the specified modifiers contain the <code>final</code>
080: * modifier, false otherwise.
081: *
082: * @param modifiers
083: * the modifiers to test
084: * @return if the modifiers contain the final modifier
085: */
086: public static boolean isFinal(int modifiers) {
087: return ((modifiers & FINAL) != 0);
088: }
089:
090: /**
091: * Return true if the specified modifiers contain the <code>interface</code>
092: * modifier, false otherwise.
093: *
094: * @param modifiers
095: * the modifiers to test
096: * @return if the modifiers contain the interface modifier
097: */
098: public static boolean isInterface(int modifiers) {
099: return ((modifiers & INTERFACE) != 0);
100: }
101:
102: /**
103: * Return true if the specified modifiers contain the <code>native</code>
104: * modifier, false otherwise.
105: *
106: * @param modifiers
107: * the modifiers to test
108: * @return if the modifiers contain the native modifier
109: */
110: public static boolean isNative(int modifiers) {
111: return ((modifiers & NATIVE) != 0);
112: }
113:
114: /**
115: * Return true if the specified modifiers contain the <code>private</code>
116: * modifier, false otherwise.
117: *
118: * @param modifiers
119: * the modifiers to test
120: * @return if the modifiers contain the private modifier
121: */
122: public static boolean isPrivate(int modifiers) {
123: return ((modifiers & PRIVATE) != 0);
124: }
125:
126: /**
127: * Return true if the specified modifiers contain the <code>protected</code>
128: * modifier, false otherwise.
129: *
130: * @param modifiers
131: * the modifiers to test
132: * @return if the modifiers contain the protected modifier
133: */
134: public static boolean isProtected(int modifiers) {
135: return ((modifiers & PROTECTED) != 0);
136: }
137:
138: /**
139: * Return true if the specified modifiers contain the <code>public</code>
140: * modifier, false otherwise.
141: *
142: * @param modifiers
143: * the modifiers to test
144: * @return if the modifiers contain the abstract modifier
145: */
146: public static boolean isPublic(int modifiers) {
147: return ((modifiers & PUBLIC) != 0);
148: }
149:
150: /**
151: * Return true if the specified modifiers contain the <code>static</code>
152: * modifier, false otherwise.
153: *
154: * @param modifiers
155: * the modifiers to test
156: * @return if the modifiers contain the static modifier
157: */
158: public static boolean isStatic(int modifiers) {
159: return ((modifiers & STATIC) != 0);
160: }
161:
162: /**
163: * Return true if the specified modifiers contain the <code>strict</code>
164: * modifier, false otherwise.
165: *
166: * @param modifiers
167: * the modifiers to test
168: * @return if the modifiers contain the strict modifier
169: */
170: public static boolean isStrict(int modifiers) {
171: return ((modifiers & STRICT) != 0);
172: }
173:
174: /**
175: * Return true if the specified modifiers contain the
176: * <code>synchronized</code> modifier, false otherwise.
177: *
178: * @param modifiers
179: * the modifiers to test
180: * @return if the modifiers contain the synchronized modifier
181: */
182: public static boolean isSynchronized(int modifiers) {
183: return ((modifiers & SYNCHRONIZED) != 0);
184: }
185:
186: /**
187: * Return true if the specified modifiers contain the <code>transient</code>
188: * modifier, false otherwise.
189: *
190: * @param modifiers
191: * the modifiers to test
192: * @return if the modifiers contain the transient modifier
193: */
194: public static boolean isTransient(int modifiers) {
195: return ((modifiers & TRANSIENT) != 0);
196: }
197:
198: /**
199: * Return true if the specified modifiers contain the <code>volatile</code>
200: * modifier, false otherwise.
201: *
202: * @param modifiers
203: * the modifiers to test
204: * @return if the modifiers contain the volatile modifier
205: */
206: public static boolean isVolatile(int modifiers) {
207: return ((modifiers & VOLATILE) != 0);
208: }
209:
210: /**
211: * Answers a string containing the string representation of all modifiers
212: * present in the specified modifiers.
213: *
214: * Modifiers appear in the order specified by the Java Language
215: * Specification:
216: * <code>public private protected abstract static final transient volatile native synchronized interface strict</code>
217: *
218: * @param modifiers
219: * the modifiers to print
220: * @return a printable representation of the modifiers
221: */
222: @SuppressWarnings("nls")
223: public static java.lang.String toString(int modifiers) {
224: StringBuffer buf;
225:
226: buf = new StringBuffer();
227:
228: if (isPublic(modifiers)) {
229: buf.append("public ");
230: }
231: if (isProtected(modifiers)) {
232: buf.append("protected ");
233: }
234: if (isPrivate(modifiers)) {
235: buf.append("private ");
236: }
237: if (isAbstract(modifiers)) {
238: buf.append("abstract ");
239: }
240: if (isStatic(modifiers)) {
241: buf.append("static ");
242: }
243: if (isFinal(modifiers)) {
244: buf.append("final ");
245: }
246: if (isTransient(modifiers)) {
247: buf.append("transient ");
248: }
249: if (isVolatile(modifiers)) {
250: buf.append("volatile ");
251: }
252: if (isSynchronized(modifiers)) {
253: buf.append("synchronized ");
254: }
255: if (isNative(modifiers)) {
256: buf.append("native ");
257: }
258: if (isStrict(modifiers)) {
259: buf.append("strictfp ");
260: }
261: if (isInterface(modifiers)) {
262: buf.append("interface ");
263: }
264: if (buf.length() == 0) {
265: return "";
266: }
267: buf.setLength(buf.length() - 1);
268: return buf.toString();
269: }
270: }
|