001: /*
002: * @(#)Modifier.java 1.24 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.lang.reflect;
029:
030: /**
031: * The Modifier class provides <code>static</code> methods and
032: * constants to decode class and member access modifiers. The sets of
033: * modifiers are represented as integers with distinct bit positions
034: * representing different modifiers. The values for the constants
035: * representing the modifiers are taken from <a
036: * href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/VMSpecTOC.doc.html"><i>The
037: * Java</i><sup><small>TM</small></sup> <i>Virtual Machine Specification, Second
038: * edition</i></a> tables
039: * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75734">4.1</a>,
040: * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88358">4.4</a>,
041: * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75568">4.5</a>, and
042: * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88478">4.7</a>.
043: *
044: * @see Class#getModifiers()
045: * @see Member#getModifiers()
046: *
047: * @author Nakul Saraiya
048: */
049: public class Modifier {
050:
051: /**
052: * Return <tt>true</tt> if the integer argument includes the
053: * <tt>public</tt> modifer, <tt>false</tt> otherwise.
054: *
055: * @param mod a set of modifers
056: * @return <tt>true</tt> if <code>mod</code> includes the
057: * <tt>public</tt> modifier; <tt>false</tt> otherwise.
058: */
059: public static boolean isPublic(int mod) {
060: return (mod & PUBLIC) != 0;
061: }
062:
063: /**
064: * Return <tt>true</tt> if the integer argument includes the
065: * <tt>private</tt> modifer, <tt>false</tt> otherwise.
066: *
067: * @param mod a set of modifers
068: * @return <tt>true</tt> if <code>mod</code> includes the
069: * <tt>private</tt> modifier; <tt>false</tt> otherwise.
070: */
071: public static boolean isPrivate(int mod) {
072: return (mod & PRIVATE) != 0;
073: }
074:
075: /**
076: * Return <tt>true</tt> if the integer argument includes the
077: * <tt>protected</tt> modifer, <tt>false</tt> otherwise.
078: *
079: * @param mod a set of modifers
080: * @return <tt>true</tt> if <code>mod</code> includes the
081: * <tt>protected</tt> modifier; <tt>false</tt> otherwise.
082: */
083: public static boolean isProtected(int mod) {
084: return (mod & PROTECTED) != 0;
085: }
086:
087: /**
088: * Return <tt>true</tt> if the integer argument includes the
089: * <tt>static</tt> modifer, <tt>false</tt> otherwise.
090: *
091: * @param mod a set of modifers
092: * @return <tt>true</tt> if <code>mod</code> includes the
093: * <tt>static</tt> modifier; <tt>false</tt> otherwise.
094: */
095: public static boolean isStatic(int mod) {
096: return (mod & STATIC) != 0;
097: }
098:
099: /**
100: * Return <tt>true</tt> if the integer argument includes the
101: * <tt>final</tt> modifer, <tt>false</tt> otherwise.
102: *
103: * @param mod a set of modifers
104: * @return <tt>true</tt> if <code>mod</code> includes the
105: * <tt>final</tt> modifier; <tt>false</tt> otherwise.
106: */
107: public static boolean isFinal(int mod) {
108: return (mod & FINAL) != 0;
109: }
110:
111: /**
112: * Return <tt>true</tt> if the integer argument includes the
113: * <tt>synchronized</tt> modifer, <tt>false</tt> otherwise.
114: *
115: * @param mod a set of modifers
116: * @return <tt>true</tt> if <code>mod</code> includes the
117: * <tt>synchronized</tt> modifier; <tt>false</tt> otherwise.
118: */
119: public static boolean isSynchronized(int mod) {
120: return (mod & SYNCHRONIZED) != 0;
121: }
122:
123: /**
124: * Return <tt>true</tt> if the integer argument includes the
125: * <tt>volatile</tt> modifer, <tt>false</tt> otherwise.
126: *
127: * @param mod a set of modifers
128: * @return <tt>true</tt> if <code>mod</code> includes the
129: * <tt>volatile</tt> modifier; <tt>false</tt> otherwise.
130: */
131: public static boolean isVolatile(int mod) {
132: return (mod & VOLATILE) != 0;
133: }
134:
135: /**
136: * Return <tt>true</tt> if the integer argument includes the
137: * <tt>transient</tt> modifer, <tt>false</tt> otherwise.
138: *
139: * @param mod a set of modifers
140: * @return <tt>true</tt> if <code>mod</code> includes the
141: * <tt>transient</tt> modifier; <tt>false</tt> otherwise.
142: */
143: public static boolean isTransient(int mod) {
144: return (mod & TRANSIENT) != 0;
145: }
146:
147: /**
148: * Return <tt>true</tt> if the integer argument includes the
149: * <tt>native</tt> modifer, <tt>false</tt> otherwise.
150: *
151: * @param mod a set of modifers
152: * @return <tt>true</tt> if <code>mod</code> includes the
153: * <tt>native</tt> modifier; <tt>false</tt> otherwise.
154: */
155: public static boolean isNative(int mod) {
156: return (mod & NATIVE) != 0;
157: }
158:
159: /**
160: * Return <tt>true</tt> if the integer argument includes the
161: * <tt>interface</tt> modifer, <tt>false</tt> otherwise.
162: *
163: * @param mod a set of modifers
164: * @return <tt>true</tt> if <code>mod</code> includes the
165: * <tt>interface</tt> modifier; <tt>false</tt> otherwise.
166: */
167: public static boolean isInterface(int mod) {
168: return (mod & INTERFACE) != 0;
169: }
170:
171: /**
172: * Return <tt>true</tt> if the integer argument includes the
173: * <tt>abstract</tt> modifer, <tt>false</tt> otherwise.
174: *
175: * @param mod a set of modifers
176: * @return <tt>true</tt> if <code>mod</code> includes the
177: * <tt>abstract</tt> modifier; <tt>false</tt> otherwise.
178: */
179: public static boolean isAbstract(int mod) {
180: return (mod & ABSTRACT) != 0;
181: }
182:
183: /**
184: * Return <tt>true</tt> if the integer argument includes the
185: * <tt>strictfp</tt> modifer, <tt>false</tt> otherwise.
186: *
187: * @param mod a set of modifers
188: * @return <tt>true</tt> if <code>mod</code> includes the
189: * <tt>strictfp</tt> modifier; <tt>false</tt> otherwise.
190: */
191: public static boolean isStrict(int mod) {
192: return (mod & STRICT) != 0;
193: }
194:
195: /**
196: * Return a string describing the access modifier flags in
197: * the specified modifier. For example:
198: * <blockquote><pre>
199: * public final synchronized strictfp
200: * </pre></blockquote>
201: * The modifier names are returned in an order consistent with the
202: * suggested modifier orderings given in <a
203: * href="http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html"><em>The
204: * Java Language Specification, Second Edition</em></a> sections
205: * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#21613">§8.1.1</a>,
206: * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78091">§8.3.1</a>,
207: * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78188">§8.4.3</a>,
208: * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#42018">§8.8.3</a>, and
209: * <a href="http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html#235947">§9.1.1</a>.
210: * The full modifier ordering used by this method is:
211: * <blockquote> <code>
212: * public protected private abstract static final transient
213: * volatile synchronized native strictfp
214: * interface </code> </blockquote>
215: * The <code>interface</code> modifier discussed in this class is
216: * not a true modifier in the Java language and it appears after
217: * all other modifiers listed by this method. This method may
218: * return a string of modifiers that are not valid modifiers of a
219: * Java entity; in other words, no checking is done on the
220: * possible validity of the combination of modifiers represented
221: * by the input.
222: *
223: * @param mod a set of modifers
224: * @return a string representation of the set of modifers
225: * represented by <code>mod</code>
226: */
227: public static String toString(int mod) {
228: StringBuffer sb = new StringBuffer();
229: int len;
230:
231: if ((mod & PUBLIC) != 0)
232: sb.append("public ");
233: if ((mod & PROTECTED) != 0)
234: sb.append("protected ");
235: if ((mod & PRIVATE) != 0)
236: sb.append("private ");
237:
238: /* Canonical order */
239: if ((mod & ABSTRACT) != 0)
240: sb.append("abstract ");
241: if ((mod & STATIC) != 0)
242: sb.append("static ");
243: if ((mod & FINAL) != 0)
244: sb.append("final ");
245: if ((mod & TRANSIENT) != 0)
246: sb.append("transient ");
247: if ((mod & VOLATILE) != 0)
248: sb.append("volatile ");
249: if ((mod & SYNCHRONIZED) != 0)
250: sb.append("synchronized ");
251: if ((mod & NATIVE) != 0)
252: sb.append("native ");
253: if ((mod & STRICT) != 0)
254: sb.append("strictfp ");
255: if ((mod & INTERFACE) != 0)
256: sb.append("interface ");
257:
258: if ((len = sb.length()) > 0) /* trim trailing space */
259: return sb.toString().substring(0, len - 1);
260: return "";
261: }
262:
263: /*
264: * Access modifier flag constants from <em>The Java Virtual
265: * Machine Specification, Second Edition</em>, tables 4.1, 4.4,
266: * 4.5, and 4.7.
267: */
268:
269: /**
270: * The <code>int</code> value representing the <code>public</code>
271: * modifier.
272: */
273: public static final int PUBLIC = 0x00000001;
274:
275: /**
276: * The <code>int</code> value representing the <code>private</code>
277: * modifier.
278: */
279: public static final int PRIVATE = 0x00000002;
280:
281: /**
282: * The <code>int</code> value representing the <code>protected</code>
283: * modifier.
284: */
285: public static final int PROTECTED = 0x00000004;
286:
287: /**
288: * The <code>int</code> value representing the <code>static</code>
289: * modifier.
290: */
291: public static final int STATIC = 0x00000008;
292:
293: /**
294: * The <code>int</code> value representing the <code>final</code>
295: * modifier.
296: */
297: public static final int FINAL = 0x00000010;
298:
299: /**
300: * The <code>int</code> value representing the <code>synchronized</code>
301: * modifier.
302: */
303: public static final int SYNCHRONIZED = 0x00000020;
304:
305: /**
306: * The <code>int</code> value representing the <code>volatile</code>
307: * modifier.
308: */
309: public static final int VOLATILE = 0x00000040;
310:
311: /**
312: * The <code>int</code> value representing the <code>transient</code>
313: * modifier.
314: */
315: public static final int TRANSIENT = 0x00000080;
316:
317: /**
318: * The <code>int</code> value representing the <code>native</code>
319: * modifier.
320: */
321: public static final int NATIVE = 0x00000100;
322:
323: /**
324: * The <code>int</code> value representing the <code>interface</code>
325: * modifier.
326: */
327: public static final int INTERFACE = 0x00000200;
328:
329: /**
330: * The <code>int</code> value representing the <code>abstract</code>
331: * modifier.
332: */
333: public static final int ABSTRACT = 0x00000400;
334:
335: /**
336: * The <code>int</code> value representing the <code>strictfp</code>
337: * modifier.
338: */
339: public static final int STRICT = 0x00000800;
340:
341: }
|