Modifier.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* java.lang.reflect.Modifier
  2. Copyright (C) 1998, 1999, 2001, 2002, 2005, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.lang.reflect;
  32. /**
  33. * Modifier is a helper class with static methods to determine whether an
  34. * int returned from getModifiers() represents static, public, protected,
  35. * native, final, etc... and provides an additional method to print
  36. * out all of the modifiers in an int in order.
  37. * <p>
  38. * The methods in this class use the bitmask values in the VM spec to
  39. * determine the modifiers of an int. This means that a VM must return a
  40. * standard mask, conformant with the VM spec. I don't know if this is how
  41. * Sun does it, but I'm willing to bet money that it is.
  42. *
  43. * @author John Keiser
  44. * @author Tom Tromey (tromey@cygnus.com)
  45. * @author Eric Blake (ebb9@email.byu.edu)
  46. * @see Member#getModifiers()
  47. * @see Method#getModifiers()
  48. * @see Field#getModifiers()
  49. * @see Constructor#getModifiers()
  50. * @see Class#getModifiers()
  51. * @since 1.1
  52. */
  53. public class Modifier
  54. {
  55. /** <STRONG>This constructor really shouldn't be here ... there are no
  56. * instance methods or variables of this class, so instantiation is
  57. * worthless. However, this function is in the 1.1 spec, so it is added
  58. * for completeness.</STRONG>
  59. */
  60. public Modifier()
  61. {
  62. }
  63. /**
  64. * Public: accessible from any other class.
  65. */
  66. public static final int PUBLIC = 0x0001;
  67. /**
  68. * Private: accessible only from the same enclosing class.
  69. */
  70. public static final int PRIVATE = 0x0002;
  71. /**
  72. * Protected: accessible only to subclasses, or within the package.
  73. */
  74. public static final int PROTECTED = 0x0004;
  75. /**
  76. * Static:<br><ul>
  77. * <li>Class: no enclosing instance for nested class.</li>
  78. * <li>Field or Method: can be accessed or invoked without an
  79. * instance of the declaring class.</li>
  80. * </ul>
  81. */
  82. public static final int STATIC = 0x0008;
  83. /**
  84. * Final:<br><ul>
  85. * <li>Class: no subclasses allowed.</li>
  86. * <li>Field: cannot be changed.</li>
  87. * <li>Method: cannot be overriden.</li>
  88. * </ul>
  89. */
  90. public static final int FINAL = 0x0010;
  91. /**
  92. * Synchronized: Method: lock the class while calling this method.
  93. */
  94. public static final int SYNCHRONIZED = 0x0020;
  95. /**
  96. * Volatile: Field: cannot be cached.
  97. */
  98. public static final int VOLATILE = 0x0040;
  99. /**
  100. * Transient: Field: not serialized or deserialized.
  101. */
  102. public static final int TRANSIENT = 0x0080;
  103. /**
  104. * Native: Method: use JNI to call this method.
  105. */
  106. public static final int NATIVE = 0x0100;
  107. /**
  108. * Interface: Class: is an interface.
  109. */
  110. public static final int INTERFACE = 0x0200;
  111. /**
  112. * Abstract:<br><ul>
  113. * <li>Class: may not be instantiated.</li>
  114. * <li>Method: may not be called.</li>
  115. * </ul>
  116. */
  117. public static final int ABSTRACT = 0x0400;
  118. /**
  119. * Strictfp: Method: expressions are FP-strict.<p>
  120. * Also used as a modifier for classes, to mean that all initializers
  121. * and constructors are FP-strict, but does not show up in
  122. * Class.getModifiers.
  123. */
  124. public static final int STRICT = 0x0800;
  125. /**
  126. * Super - treat invokespecial as polymorphic so that super.foo() works
  127. * according to the JLS. This is a reuse of the synchronized constant
  128. * to patch a hole in JDK 1.0. *shudder*.
  129. */
  130. static final int SUPER = 0x0020;
  131. /**
  132. * All the flags, only used by code in this package.
  133. */
  134. static final int ALL_FLAGS = 0xfff;
  135. /**
  136. * Flag indicating a bridge method.
  137. */
  138. static final int BRIDGE = 0x40;
  139. /**
  140. * Flag indicating a varargs method.
  141. */
  142. static final int VARARGS = 0x80;
  143. /**
  144. * Flag indicating a synthetic member.
  145. */
  146. static final int SYNTHETIC = 0x1000;
  147. /**
  148. * Flag indicating an enum constant or an enum class.
  149. */
  150. static final int ENUM = 0x4000;
  151. /**
  152. * GCJ-LOCAL: This access flag is set on methods we declare
  153. * internally but which must not be visible to reflection.
  154. */
  155. static final int INVISIBLE = 0x8000;
  156. /**
  157. * GCJ-LOCAL: This access flag is set on interpreted classes.
  158. */
  159. static final int INTERPRETED = 0x1000;
  160. /**
  161. * Check whether the given modifier is abstract.
  162. * @param mod the modifier.
  163. * @return <code>true</code> if abstract, <code>false</code> otherwise.
  164. */
  165. public static boolean isAbstract(int mod)
  166. {
  167. return (mod & ABSTRACT) != 0;
  168. }
  169. /**
  170. * Check whether the given modifier is final.
  171. * @param mod the modifier.
  172. * @return <code>true</code> if final, <code>false</code> otherwise.
  173. */
  174. public static boolean isFinal(int mod)
  175. {
  176. return (mod & FINAL) != 0;
  177. }
  178. /**
  179. * Check whether the given modifier is an interface.
  180. * @param mod the modifier.
  181. * @return <code>true</code> if an interface, <code>false</code> otherwise.
  182. */
  183. public static boolean isInterface(int mod)
  184. {
  185. return (mod & INTERFACE) != 0;
  186. }
  187. /**
  188. * Check whether the given modifier is native.
  189. * @param mod the modifier.
  190. * @return <code>true</code> if native, <code>false</code> otherwise.
  191. */
  192. public static boolean isNative(int mod)
  193. {
  194. return (mod & NATIVE) != 0;
  195. }
  196. /**
  197. * Check whether the given modifier is private.
  198. * @param mod the modifier.
  199. * @return <code>true</code> if private, <code>false</code> otherwise.
  200. */
  201. public static boolean isPrivate(int mod)
  202. {
  203. return (mod & PRIVATE) != 0;
  204. }
  205. /**
  206. * Check whether the given modifier is protected.
  207. * @param mod the modifier.
  208. * @return <code>true</code> if protected, <code>false</code> otherwise.
  209. */
  210. public static boolean isProtected(int mod)
  211. {
  212. return (mod & PROTECTED) != 0;
  213. }
  214. /**
  215. * Check whether the given modifier is public.
  216. * @param mod the modifier.
  217. * @return <code>true</code> if public, <code>false</code> otherwise.
  218. */
  219. public static boolean isPublic(int mod)
  220. {
  221. return (mod & PUBLIC) != 0;
  222. }
  223. /**
  224. * Check whether the given modifier is static.
  225. * @param mod the modifier.
  226. * @return <code>true</code> if static, <code>false</code> otherwise.
  227. */
  228. public static boolean isStatic(int mod)
  229. {
  230. return (mod & STATIC) != 0;
  231. }
  232. /**
  233. * Check whether the given modifier is strictfp.
  234. * @param mod the modifier.
  235. * @return <code>true</code> if strictfp, <code>false</code> otherwise.
  236. */
  237. public static boolean isStrict(int mod)
  238. {
  239. return (mod & STRICT) != 0;
  240. }
  241. /**
  242. * Check whether the given modifier is synchronized.
  243. * @param mod the modifier.
  244. * @return <code>true</code> if synchronized, <code>false</code> otherwise.
  245. */
  246. public static boolean isSynchronized(int mod)
  247. {
  248. return (mod & SYNCHRONIZED) != 0;
  249. }
  250. /**
  251. * Check whether the given modifier is transient.
  252. * @param mod the modifier.
  253. * @return <code>true</code> if transient, <code>false</code> otherwise.
  254. */
  255. public static boolean isTransient(int mod)
  256. {
  257. return (mod & TRANSIENT) != 0;
  258. }
  259. /**
  260. * Check whether the given modifier is volatile.
  261. * @param mod the modifier.
  262. * @return <code>true</code> if volatile, <code>false</code> otherwise.
  263. */
  264. public static boolean isVolatile(int mod)
  265. {
  266. return (mod & VOLATILE) != 0;
  267. }
  268. /**
  269. * Get a string representation of all the modifiers represented by the
  270. * given int. The keywords are printed in this order:
  271. * <code>&lt;public|protected|private&gt; abstract static final transient
  272. * volatile synchronized native strictfp interface</code>.
  273. *
  274. * @param mod the modifier.
  275. * @return the String representing the modifiers.
  276. */
  277. public static String toString(int mod)
  278. {
  279. return toString(mod, new StringBuffer()).toString();
  280. }
  281. /**
  282. * Package helper method that can take a StringBuilder.
  283. * @param mod the modifier
  284. * @param r the StringBuilder to which the String representation is appended
  285. * @return r, with information appended
  286. */
  287. static StringBuilder toString(int mod, StringBuilder r)
  288. {
  289. r.append(toString(mod, new StringBuffer()));
  290. return r;
  291. }
  292. /**
  293. * Package helper method that can take a StringBuffer.
  294. * @param mod the modifier
  295. * @param r the StringBuffer to which the String representation is appended
  296. * @return r, with information appended
  297. */
  298. static StringBuffer toString(int mod, StringBuffer r)
  299. {
  300. if (isPublic(mod))
  301. r.append("public ");
  302. if (isProtected(mod))
  303. r.append("protected ");
  304. if (isPrivate(mod))
  305. r.append("private ");
  306. if (isAbstract(mod))
  307. r.append("abstract ");
  308. if (isStatic(mod))
  309. r.append("static ");
  310. if (isFinal(mod))
  311. r.append("final ");
  312. if (isTransient(mod))
  313. r.append("transient ");
  314. if (isVolatile(mod))
  315. r.append("volatile ");
  316. if (isSynchronized(mod))
  317. r.append("synchronized ");
  318. if (isNative(mod))
  319. r.append("native ");
  320. if (isStrict(mod))
  321. r.append("strictfp ");
  322. if (isInterface(mod))
  323. r.append("interface ");
  324. // Trim trailing space.
  325. if ((mod & ALL_FLAGS) != 0)
  326. r.setLength(r.length() - 1);
  327. return r;
  328. }
  329. }