001/* java.lang.reflect.Modifier
002   Copyright (C) 1998, 1999, 2001, 2002, 2005, 2006  Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010 
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package java.lang.reflect;
040
041/**
042 * Modifier is a helper class with static methods to determine whether an
043 * int returned from getModifiers() represents static, public, protected,
044 * native, final, etc... and provides an additional method to print
045 * out all of the modifiers in an int in order.
046 * <p>
047 * The methods in this class use the bitmask values in the VM spec to
048 * determine the modifiers of an int. This means that a VM must return a
049 * standard mask, conformant with the VM spec.  I don't know if this is how
050 * Sun does it, but I'm willing to bet money that it is.
051 *
052 * @author John Keiser
053 * @author Tom Tromey (tromey@cygnus.com)
054 * @author Eric Blake (ebb9@email.byu.edu)
055 * @see Member#getModifiers()
056 * @see Method#getModifiers()
057 * @see Field#getModifiers()
058 * @see Constructor#getModifiers()
059 * @see Class#getModifiers()
060 * @since 1.1
061 */
062public class Modifier
063{
064  /** <STRONG>This constructor really shouldn't be here ... there are no
065   * instance methods or variables of this class, so instantiation is
066   * worthless.  However, this function is in the 1.1 spec, so it is added
067   * for completeness.</STRONG>
068   */
069  public Modifier()
070  {
071  }
072
073  /**
074   * Public: accessible from any other class.
075   */
076  public static final int PUBLIC = 0x0001;
077
078  /**
079   * Private: accessible only from the same enclosing class.
080   */
081  public static final int PRIVATE = 0x0002;
082
083  /**
084   * Protected: accessible only to subclasses, or within the package.
085   */
086  public static final int PROTECTED = 0x0004;
087
088  /**
089   * Static:<br><ul>
090   * <li>Class: no enclosing instance for nested class.</li>
091   * <li>Field or Method: can be accessed or invoked without an
092   *     instance of the declaring class.</li>
093   * </ul>
094   */
095  public static final int STATIC = 0x0008;
096
097  /**
098   * Final:<br><ul>
099   * <li>Class: no subclasses allowed.</li>
100   * <li>Field: cannot be changed.</li>
101   * <li>Method: cannot be overriden.</li>
102   * </ul>
103   */
104  public static final int FINAL = 0x0010;
105
106  /**
107   * Synchronized: Method: lock the class while calling this method.
108   */
109  public static final int SYNCHRONIZED = 0x0020;
110
111  /**
112   * Volatile: Field: cannot be cached.
113   */
114  public static final int VOLATILE = 0x0040;
115
116  /**
117   * Transient: Field: not serialized or deserialized.
118   */
119  public static final int TRANSIENT = 0x0080;
120
121  /**
122   * Native: Method: use JNI to call this method.
123   */
124  public static final int NATIVE = 0x0100;
125
126  /**
127   * Interface: Class: is an interface.
128   */
129  public static final int INTERFACE = 0x0200;
130
131  /**
132   * Abstract:<br><ul>
133   * <li>Class: may not be instantiated.</li>
134   * <li>Method: may not be called.</li>
135   * </ul>
136   */
137  public static final int ABSTRACT = 0x0400;
138
139  /**
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.
144   */
145  public static final int STRICT = 0x0800;
146
147
148  /**
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*.
152   */
153  static final int SUPER = 0x0020;
154
155  /**
156   * All the flags, only used by code in this package.
157   */
158  static final int ALL_FLAGS = 0xfff;
159
160  /**
161   * Flag indicating a bridge method.
162   */
163  static final int BRIDGE = 0x40;
164
165  /**
166   * Flag indicating a varargs method.
167   */
168  static final int VARARGS = 0x80;
169
170  /**
171   * Flag indicating a synthetic member.
172   */
173  static final int SYNTHETIC = 0x1000;
174
175  /**
176   * Flag indicating an enum constant or an enum class.
177   */
178  static final int ENUM = 0x4000;
179
180  /**
181   * GCJ-LOCAL: This access flag is set on methods we declare
182   * internally but which must not be visible to reflection.
183   */
184  static final int INVISIBLE = 0x8000;
185
186  /**
187   * GCJ-LOCAL: This access flag is set on interpreted classes.
188   */
189  static final int INTERPRETED = 0x1000;
190
191  /**
192   * Check whether the given modifier is abstract.
193   * @param mod the modifier.
194   * @return <code>true</code> if abstract, <code>false</code> otherwise.
195   */
196  public static boolean isAbstract(int mod)
197  {
198    return (mod & ABSTRACT) != 0;
199  }
200
201  /**
202   * Check whether the given modifier is final.
203   * @param mod the modifier.
204   * @return <code>true</code> if final, <code>false</code> otherwise.
205   */
206  public static boolean isFinal(int mod)
207  {
208    return (mod & FINAL) != 0;
209  }
210
211  /**
212   * Check whether the given modifier is an interface.
213   * @param mod the modifier.
214   * @return <code>true</code> if an interface, <code>false</code> otherwise.
215   */
216  public static boolean isInterface(int mod)
217  {
218    return (mod & INTERFACE) != 0;
219  }
220
221  /**
222   * Check whether the given modifier is native.
223   * @param mod the modifier.
224   * @return <code>true</code> if native, <code>false</code> otherwise.
225   */
226  public static boolean isNative(int mod)
227  {
228    return (mod & NATIVE) != 0;
229  }
230
231  /**
232   * Check whether the given modifier is private.
233   * @param mod the modifier.
234   * @return <code>true</code> if private, <code>false</code> otherwise.
235   */
236  public static boolean isPrivate(int mod)
237  {
238    return (mod & PRIVATE) != 0;
239  }
240
241  /**
242   * Check whether the given modifier is protected.
243   * @param mod the modifier.
244   * @return <code>true</code> if protected, <code>false</code> otherwise.
245   */
246  public static boolean isProtected(int mod)
247  {
248    return (mod & PROTECTED) != 0;
249  }
250
251  /**
252   * Check whether the given modifier is public.
253   * @param mod the modifier.
254   * @return <code>true</code> if public, <code>false</code> otherwise.
255   */
256  public static boolean isPublic(int mod)
257  {
258    return (mod & PUBLIC) != 0;
259  }
260
261  /**
262   * Check whether the given modifier is static.
263   * @param mod the modifier.
264   * @return <code>true</code> if static, <code>false</code> otherwise.
265   */
266  public static boolean isStatic(int mod)
267  {
268    return (mod & STATIC) != 0;
269  }
270
271  /**
272   * Check whether the given modifier is strictfp.
273   * @param mod the modifier.
274   * @return <code>true</code> if strictfp, <code>false</code> otherwise.
275   */
276  public static boolean isStrict(int mod)
277  {
278    return (mod & STRICT) != 0;
279  }
280
281  /**
282   * Check whether the given modifier is synchronized.
283   * @param mod the modifier.
284   * @return <code>true</code> if synchronized, <code>false</code> otherwise.
285   */
286  public static boolean isSynchronized(int mod)
287  {
288    return (mod & SYNCHRONIZED) != 0;
289  }
290
291  /**
292   * Check whether the given modifier is transient.
293   * @param mod the modifier.
294   * @return <code>true</code> if transient, <code>false</code> otherwise.
295   */
296  public static boolean isTransient(int mod)
297  {
298    return (mod & TRANSIENT) != 0;
299  }
300
301  /**
302   * Check whether the given modifier is volatile.
303   * @param mod the modifier.
304   * @return <code>true</code> if volatile, <code>false</code> otherwise.
305   */
306  public static boolean isVolatile(int mod)
307  {
308    return (mod & VOLATILE) != 0;
309  }
310
311  /**
312   * Get a string representation of all the modifiers represented by the
313   * given int. The keywords are printed in this order:
314   * <code>&lt;public|protected|private&gt; abstract static final transient
315   * volatile synchronized native strictfp interface</code>.
316   *
317   * @param mod the modifier.
318   * @return the String representing the modifiers.
319   */
320  public static String toString(int mod)
321  {
322    return toString(mod, new StringBuffer()).toString();
323  }
324
325  /**
326   * Package helper method that can take a StringBuilder.
327   * @param mod the modifier
328   * @param r the StringBuilder to which the String representation is appended
329   * @return r, with information appended
330   */
331  static StringBuilder toString(int mod, StringBuilder r)
332  {
333    r.append(toString(mod, new StringBuffer()));
334    return r;
335  }
336
337  /**
338   * Package helper method that can take a StringBuffer.
339   * @param mod the modifier
340   * @param r the StringBuffer to which the String representation is appended
341   * @return r, with information appended
342   */
343  static StringBuffer toString(int mod, StringBuffer r)
344  {
345    if (isPublic(mod))
346      r.append("public ");
347    if (isProtected(mod))
348      r.append("protected ");
349    if (isPrivate(mod))
350      r.append("private ");
351    if (isAbstract(mod))
352      r.append("abstract ");
353    if (isStatic(mod))
354      r.append("static ");
355    if (isFinal(mod))
356      r.append("final ");
357    if (isTransient(mod))
358      r.append("transient ");
359    if (isVolatile(mod))
360      r.append("volatile ");
361    if (isSynchronized(mod))
362      r.append("synchronized ");
363    if (isNative(mod))
364      r.append("native ");
365    if (isStrict(mod))
366      r.append("strictfp ");
367    if (isInterface(mod))
368      r.append("interface ");
369    
370    // Trim trailing space.
371    if ((mod & ALL_FLAGS) != 0)
372      r.setLength(r.length() - 1);
373    return r;
374  }
375}