1 /* java.lang.reflect.Modifier
2 Copyright (C) 1998, 1999, 2001, 2002, 2005, 2008 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)
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., 51 Franklin Street, Fifth Floor, Boston, MA
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
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 import gnu.java.lang.CPStringBuilder;
44 * Modifier is a helper class with static methods to determine whether an
45 * int returned from getModifiers() represents static, public, protected,
46 * native, final, etc... and provides an additional method to print
47 * out all of the modifiers in an int in order.
49 * The methods in this class use the bitmask values in the VM spec to
50 * determine the modifiers of an int. This means that a VM must return a
51 * standard mask, conformant with the VM spec. I don't know if this is how
52 * Sun does it, but I'm willing to bet money that it is.
55 * @author Tom Tromey (tromey@cygnus.com)
56 * @author Eric Blake (ebb9@email.byu.edu)
57 * @see Member#getModifiers()
58 * @see Method#getModifiers()
59 * @see Field#getModifiers()
60 * @see Constructor#getModifiers()
61 * @see Class#getModifiers()
66 /** <STRONG>This constructor really shouldn't be here ... there are no
67 * instance methods or variables of this class, so instantiation is
68 * worthless. However, this function is in the 1.1 spec, so it is added
69 * for completeness.</STRONG>
76 * Public: accessible from any other class.
78 public static final int PUBLIC = 0x0001;
81 * Private: accessible only from the same enclosing class.
83 public static final int PRIVATE = 0x0002;
86 * Protected: accessible only to subclasses, or within the package.
88 public static final int PROTECTED = 0x0004;
92 * <li>Class: no enclosing instance for nested class.</li>
93 * <li>Field or Method: can be accessed or invoked without an
94 * instance of the declaring class.</li>
97 public static final int STATIC = 0x0008;
101 * <li>Class: no subclasses allowed.</li>
102 * <li>Field: cannot be changed.</li>
103 * <li>Method: cannot be overriden.</li>
106 public static final int FINAL = 0x0010;
109 * Synchronized: Method: lock the class while calling this method.
111 public static final int SYNCHRONIZED = 0x0020;
114 * Volatile: Field: cannot be cached.
116 public static final int VOLATILE = 0x0040;
119 * Transient: Field: not serialized or deserialized.
121 public static final int TRANSIENT = 0x0080;
124 * Native: Method: use JNI to call this method.
126 public static final int NATIVE = 0x0100;
129 * Interface: Class: is an interface.
131 public static final int INTERFACE = 0x0200;
135 * <li>Class: may not be instantiated.</li>
136 * <li>Method: may not be called.</li>
139 public static final int ABSTRACT = 0x0400;
142 * Strictfp: Method: expressions are FP-strict.<p>
143 * Also used as a modifier for classes, to mean that all initializers
144 * and constructors are FP-strict, but does not show up in
145 * Class.getModifiers.
147 public static final int STRICT = 0x0800;
151 * Super - treat invokespecial as polymorphic so that super.foo() works
152 * according to the JLS. This is a reuse of the synchronized constant
153 * to patch a hole in JDK 1.0. *shudder*.
155 static final int SUPER = 0x0020;
158 * All the flags, only used by code in this package.
160 static final int ALL_FLAGS = 0xfff;
163 * Flag indicating a bridge method.
165 static final int BRIDGE = 0x40;
168 * Flag indicating a varargs method.
170 static final int VARARGS = 0x80;
173 * Flag indicating a synthetic member.
175 static final int SYNTHETIC = 0x1000;
178 * Flag indicating an enum constant or an enum class.
180 static final int ENUM = 0x4000;
183 * Check whether the given modifier is abstract.
184 * @param mod the modifier.
185 * @return <code>true</code> if abstract, <code>false</code> otherwise.
187 public static boolean isAbstract(int mod)
189 return (mod & ABSTRACT) != 0;
193 * Check whether the given modifier is final.
194 * @param mod the modifier.
195 * @return <code>true</code> if final, <code>false</code> otherwise.
197 public static boolean isFinal(int mod)
199 return (mod & FINAL) != 0;
203 * Check whether the given modifier is an interface.
204 * @param mod the modifier.
205 * @return <code>true</code> if an interface, <code>false</code> otherwise.
207 public static boolean isInterface(int mod)
209 return (mod & INTERFACE) != 0;
213 * Check whether the given modifier is native.
214 * @param mod the modifier.
215 * @return <code>true</code> if native, <code>false</code> otherwise.
217 public static boolean isNative(int mod)
219 return (mod & NATIVE) != 0;
223 * Check whether the given modifier is private.
224 * @param mod the modifier.
225 * @return <code>true</code> if private, <code>false</code> otherwise.
227 public static boolean isPrivate(int mod)
229 return (mod & PRIVATE) != 0;
233 * Check whether the given modifier is protected.
234 * @param mod the modifier.
235 * @return <code>true</code> if protected, <code>false</code> otherwise.
237 public static boolean isProtected(int mod)
239 return (mod & PROTECTED) != 0;
243 * Check whether the given modifier is public.
244 * @param mod the modifier.
245 * @return <code>true</code> if public, <code>false</code> otherwise.
247 public static boolean isPublic(int mod)
249 return (mod & PUBLIC) != 0;
253 * Check whether the given modifier is static.
254 * @param mod the modifier.
255 * @return <code>true</code> if static, <code>false</code> otherwise.
257 public static boolean isStatic(int mod)
259 return (mod & STATIC) != 0;
263 * Check whether the given modifier is strictfp.
264 * @param mod the modifier.
265 * @return <code>true</code> if strictfp, <code>false</code> otherwise.
267 public static boolean isStrict(int mod)
269 return (mod & STRICT) != 0;
273 * Check whether the given modifier is synchronized.
274 * @param mod the modifier.
275 * @return <code>true</code> if synchronized, <code>false</code> otherwise.
277 public static boolean isSynchronized(int mod)
279 return (mod & SYNCHRONIZED) != 0;
283 * Check whether the given modifier is transient.
284 * @param mod the modifier.
285 * @return <code>true</code> if transient, <code>false</code> otherwise.
287 public static boolean isTransient(int mod)
289 return (mod & TRANSIENT) != 0;
293 * Check whether the given modifier is volatile.
294 * @param mod the modifier.
295 * @return <code>true</code> if volatile, <code>false</code> otherwise.
297 public static boolean isVolatile(int mod)
299 return (mod & VOLATILE) != 0;
303 * Get a string representation of all the modifiers represented by the
304 * given int. The keywords are printed in this order:
305 * <code><public|protected|private> abstract static final transient
306 * volatile synchronized native strictfp interface</code>.
308 * @param mod the modifier.
309 * @return the String representing the modifiers.
311 public static String toString(int mod)
313 return toString(mod, new CPStringBuilder()).toString();
317 * Package helper method that can take a CPStringBuilder.
318 * @param mod the modifier
319 * @param r the CPStringBuilder to which the String representation is appended
320 * @return r, with information appended
322 static CPStringBuilder toString(int mod, CPStringBuilder r)
326 if (isProtected(mod))
327 r.append("protected ");
329 r.append("private ");
331 r.append("abstract ");
336 if (isTransient(mod))
337 r.append("transient ");
339 r.append("volatile ");
340 if (isSynchronized(mod))
341 r.append("synchronized ");
345 r.append("strictfp ");
346 if (isInterface(mod))
347 r.append("interface ");
349 // Trim trailing space.
350 if ((mod & ALL_FLAGS) != 0)
351 r.setLength(r.length() - 1);