Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / lang / reflect / Modifier.java
blob3b3baa6722e1e1f5350e8c1553604a2ed1cfbbca
1 /* java.lang.reflect.Modifier
2 Copyright (C) 1998, 1999, 2001, 2002, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.lang.reflect;
41 /**
42 * Modifier is a helper class with static methods to determine whether an
43 * int returned from getModifiers() represents static, public, protected,
44 * native, final, etc... and provides an additional method to print
45 * out all of the modifiers in an int in order.
46 * <p>
47 * The methods in this class use the bitmask values in the VM spec to
48 * determine the modifiers of an int. This means that a VM must return a
49 * standard mask, conformant with the VM spec. I don't know if this is how
50 * Sun does it, but I'm willing to bet money that it is.
52 * @author John Keiser
53 * @author Tom Tromey (tromey@cygnus.com)
54 * @author Eric Blake (ebb9@email.byu.edu)
55 * @see Member#getModifiers()
56 * @see Method#getModifiers()
57 * @see Field#getModifiers()
58 * @see Constructor#getModifiers()
59 * @see Class#getModifiers()
60 * @since 1.1
62 public class Modifier
64 /** <STRONG>This constructor really shouldn't be here ... there are no
65 * instance methods or variables of this class, so instantiation is
66 * worthless. However, this function is in the 1.1 spec, so it is added
67 * for completeness.</STRONG>
69 public Modifier()
73 /**
74 * Public: accessible from any other class.
76 public static final int PUBLIC = 0x0001;
78 /**
79 * Private: accessible only from the same enclosing class.
81 public static final int PRIVATE = 0x0002;
83 /**
84 * Protected: accessible only to subclasses, or within the package.
86 public static final int PROTECTED = 0x0004;
88 /**
89 * Static:<br><ul>
90 * <li>Class: no enclosing instance for nested class.</li>
91 * <li>Field or Method: can be accessed or invoked without an
92 * instance of the declaring class.</li>
93 * </ul>
95 public static final int STATIC = 0x0008;
97 /**
98 * Final:<br><ul>
99 * <li>Class: no subclasses allowed.</li>
100 * <li>Field: cannot be changed.</li>
101 * <li>Method: cannot be overriden.</li>
102 * </ul>
104 public static final int FINAL = 0x0010;
107 * Synchronized: Method: lock the class while calling this method.
109 public static final int SYNCHRONIZED = 0x0020;
112 * Volatile: Field: cannot be cached.
114 public static final int VOLATILE = 0x0040;
117 * Transient: Field: not serialized or deserialized.
119 public static final int TRANSIENT = 0x0080;
122 * Native: Method: use JNI to call this method.
124 public static final int NATIVE = 0x0100;
127 * Interface: Class: is an interface.
129 public static final int INTERFACE = 0x0200;
132 * Abstract:<br><ul>
133 * <li>Class: may not be instantiated.</li>
134 * <li>Method: may not be called.</li>
135 * </ul>
137 public static final int ABSTRACT = 0x0400;
140 * Strictfp: Method: expressions are FP-strict.<p>
141 * Also used as a modifier for classes, to mean that all initializers
142 * and constructors are FP-strict, but does not show up in
143 * Class.getModifiers.
145 public static final int STRICT = 0x0800;
149 * Super - treat invokespecial as polymorphic so that super.foo() works
150 * according to the JLS. This is a reuse of the synchronized constant
151 * to patch a hole in JDK 1.0. *shudder*.
153 static final int SUPER = 0x0020;
156 * All the flags, only used by code in this package.
158 static final int ALL_FLAGS = 0xfff;
161 * GCJ-LOCAL: This access flag is set on methods we declare
162 * internally but which must not be visible to reflection.
164 static final int INVISIBLE = 0x1000;
167 * GCJ-LOCAL: This access flag is set on interpreted classes.
169 static final int INTERPRETED = 0x1000;
172 * Check whether the given modifier is abstract.
173 * @param mod the modifier.
174 * @return <code>true</code> if abstract, <code>false</code> otherwise.
176 public static boolean isAbstract(int mod)
178 return (mod & ABSTRACT) != 0;
182 * Check whether the given modifier is final.
183 * @param mod the modifier.
184 * @return <code>true</code> if final, <code>false</code> otherwise.
186 public static boolean isFinal(int mod)
188 return (mod & FINAL) != 0;
192 * Check whether the given modifier is an interface.
193 * @param mod the modifier.
194 * @return <code>true</code> if an interface, <code>false</code> otherwise.
196 public static boolean isInterface(int mod)
198 return (mod & INTERFACE) != 0;
202 * Check whether the given modifier is native.
203 * @param mod the modifier.
204 * @return <code>true</code> if native, <code>false</code> otherwise.
206 public static boolean isNative(int mod)
208 return (mod & NATIVE) != 0;
212 * Check whether the given modifier is private.
213 * @param mod the modifier.
214 * @return <code>true</code> if private, <code>false</code> otherwise.
216 public static boolean isPrivate(int mod)
218 return (mod & PRIVATE) != 0;
222 * Check whether the given modifier is protected.
223 * @param mod the modifier.
224 * @return <code>true</code> if protected, <code>false</code> otherwise.
226 public static boolean isProtected(int mod)
228 return (mod & PROTECTED) != 0;
232 * Check whether the given modifier is public.
233 * @param mod the modifier.
234 * @return <code>true</code> if public, <code>false</code> otherwise.
236 public static boolean isPublic(int mod)
238 return (mod & PUBLIC) != 0;
242 * Check whether the given modifier is static.
243 * @param mod the modifier.
244 * @return <code>true</code> if static, <code>false</code> otherwise.
246 public static boolean isStatic(int mod)
248 return (mod & STATIC) != 0;
252 * Check whether the given modifier is strictfp.
253 * @param mod the modifier.
254 * @return <code>true</code> if strictfp, <code>false</code> otherwise.
256 public static boolean isStrict(int mod)
258 return (mod & STRICT) != 0;
262 * Check whether the given modifier is synchronized.
263 * @param mod the modifier.
264 * @return <code>true</code> if synchronized, <code>false</code> otherwise.
266 public static boolean isSynchronized(int mod)
268 return (mod & SYNCHRONIZED) != 0;
272 * Check whether the given modifier is transient.
273 * @param mod the modifier.
274 * @return <code>true</code> if transient, <code>false</code> otherwise.
276 public static boolean isTransient(int mod)
278 return (mod & TRANSIENT) != 0;
282 * Check whether the given modifier is volatile.
283 * @param mod the modifier.
284 * @return <code>true</code> if volatile, <code>false</code> otherwise.
286 public static boolean isVolatile(int mod)
288 return (mod & VOLATILE) != 0;
292 * Get a string representation of all the modifiers represented by the
293 * given int. The keywords are printed in this order:
294 * <code>&lt;public|protected|private&gt; abstract static final transient
295 * volatile synchronized native strictfp interface</code>.
297 * @param mod the modifier.
298 * @return the String representing the modifiers.
300 public static String toString(int mod)
302 return toString(mod, new StringBuffer()).toString();
306 * Package helper method that can take a StringBuffer.
307 * @param mod the modifier
308 * @param r the StringBuffer to which the String representation is appended
309 * @return r, with information appended
311 static StringBuffer toString(int mod, StringBuffer r)
313 if (isPublic(mod))
314 r.append("public ");
315 if (isProtected(mod))
316 r.append("protected ");
317 if (isPrivate(mod))
318 r.append("private ");
319 if (isAbstract(mod))
320 r.append("abstract ");
321 if (isStatic(mod))
322 r.append("static ");
323 if (isFinal(mod))
324 r.append("final ");
325 if (isTransient(mod))
326 r.append("transient ");
327 if (isVolatile(mod))
328 r.append("volatile ");
329 if (isSynchronized(mod))
330 r.append("synchronized ");
331 if (isNative(mod))
332 r.append("native ");
333 if (isStrict(mod))
334 r.append("strictfp ");
335 if (isInterface(mod))
336 r.append("interface ");
338 // Trim trailing space.
339 if ((mod & ALL_FLAGS) != 0)
340 r.setLength(r.length() - 1);
341 return r;