Modifier.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* java.lang.reflect.Modifier
  2. Copyright (C) 1998, 1999, 2001, 2002, 2005, 2008, 2012 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. import gnu.java.lang.CPStringBuilder;
  33. /**
  34. * Modifier is a helper class with static methods to determine whether an
  35. * int returned from getModifiers() represents static, public, protected,
  36. * native, final, etc... and provides an additional method to print
  37. * out all of the modifiers in an int in order.
  38. * <p>
  39. * The methods in this class use the bitmask values in the VM spec to
  40. * determine the modifiers of an int. This means that a VM must return a
  41. * standard mask, conformant with the VM spec. I don't know if this is how
  42. * Sun does it, but I'm willing to bet money that it is.
  43. *
  44. * @author John Keiser
  45. * @author Tom Tromey (tromey@cygnus.com)
  46. * @author Eric Blake (ebb9@email.byu.edu)
  47. * @see Member#getModifiers()
  48. * @see Method#getModifiers()
  49. * @see Field#getModifiers()
  50. * @see Constructor#getModifiers()
  51. * @see Class#getModifiers()
  52. * @since 1.1
  53. */
  54. public class Modifier
  55. {
  56. /** <STRONG>This constructor really shouldn't be here ... there are no
  57. * instance methods or variables of this class, so instantiation is
  58. * worthless. However, this function is in the 1.1 spec, so it is added
  59. * for completeness.</STRONG>
  60. */
  61. public Modifier()
  62. {
  63. }
  64. /**
  65. * Public: accessible from any other class.
  66. */
  67. public static final int PUBLIC = 0x0001;
  68. /**
  69. * Private: accessible only from the same enclosing class.
  70. */
  71. public static final int PRIVATE = 0x0002;
  72. /**
  73. * Protected: accessible only to subclasses, or within the package.
  74. */
  75. public static final int PROTECTED = 0x0004;
  76. /**
  77. * Static:<br><ul>
  78. * <li>Class: no enclosing instance for nested class.</li>
  79. * <li>Field or Method: can be accessed or invoked without an
  80. * instance of the declaring class.</li>
  81. * </ul>
  82. */
  83. public static final int STATIC = 0x0008;
  84. /**
  85. * Final:<br><ul>
  86. * <li>Class: no subclasses allowed.</li>
  87. * <li>Field: cannot be changed.</li>
  88. * <li>Method: cannot be overriden.</li>
  89. * </ul>
  90. */
  91. public static final int FINAL = 0x0010;
  92. /**
  93. * Synchronized: Method: lock the class while calling this method.
  94. */
  95. public static final int SYNCHRONIZED = 0x0020;
  96. /**
  97. * Volatile: Field: cannot be cached.
  98. */
  99. public static final int VOLATILE = 0x0040;
  100. /**
  101. * Transient: Field: not serialized or deserialized.
  102. */
  103. public static final int TRANSIENT = 0x0080;
  104. /**
  105. * Native: Method: use JNI to call this method.
  106. */
  107. public static final int NATIVE = 0x0100;
  108. /**
  109. * Interface: Class: is an interface.
  110. */
  111. public static final int INTERFACE = 0x0200;
  112. /**
  113. * Abstract:<br><ul>
  114. * <li>Class: may not be instantiated.</li>
  115. * <li>Method: may not be called.</li>
  116. * </ul>
  117. */
  118. public static final int ABSTRACT = 0x0400;
  119. /**
  120. * Strictfp: Method: expressions are FP-strict.<p>
  121. * Also used as a modifier for classes, to mean that all initializers
  122. * and constructors are FP-strict, but does not show up in
  123. * Class.getModifiers.
  124. */
  125. public static final int STRICT = 0x0800;
  126. /**
  127. * Super - treat invokespecial as polymorphic so that super.foo() works
  128. * according to the JLS. This is a reuse of the synchronized constant
  129. * to patch a hole in JDK 1.0. *shudder*.
  130. */
  131. static final int SUPER = 0x0020;
  132. /**
  133. * All the flags, only used by code in this package.
  134. */
  135. static final int ALL_FLAGS = 0xfff;
  136. /**
  137. * Flag indicating a bridge method.
  138. */
  139. static final int BRIDGE = 0x40;
  140. /**
  141. * Flag indicating a varargs method.
  142. */
  143. static final int VARARGS = 0x80;
  144. /**
  145. * Flag indicating a synthetic member.
  146. */
  147. static final int SYNTHETIC = 0x1000;
  148. /**
  149. * Flag indicating an enum constant or an enum class.
  150. */
  151. static final int ENUM = 0x4000;
  152. /**
  153. * Check whether the given modifier is abstract.
  154. * @param mod the modifier.
  155. * @return <code>true</code> if abstract, <code>false</code> otherwise.
  156. */
  157. public static boolean isAbstract(int mod)
  158. {
  159. return (mod & ABSTRACT) != 0;
  160. }
  161. /**
  162. * Check whether the given modifier is final.
  163. * @param mod the modifier.
  164. * @return <code>true</code> if final, <code>false</code> otherwise.
  165. */
  166. public static boolean isFinal(int mod)
  167. {
  168. return (mod & FINAL) != 0;
  169. }
  170. /**
  171. * Check whether the given modifier is an interface.
  172. * @param mod the modifier.
  173. * @return <code>true</code> if an interface, <code>false</code> otherwise.
  174. */
  175. public static boolean isInterface(int mod)
  176. {
  177. return (mod & INTERFACE) != 0;
  178. }
  179. /**
  180. * Check whether the given modifier is native.
  181. * @param mod the modifier.
  182. * @return <code>true</code> if native, <code>false</code> otherwise.
  183. */
  184. public static boolean isNative(int mod)
  185. {
  186. return (mod & NATIVE) != 0;
  187. }
  188. /**
  189. * Check whether the given modifier is private.
  190. * @param mod the modifier.
  191. * @return <code>true</code> if private, <code>false</code> otherwise.
  192. */
  193. public static boolean isPrivate(int mod)
  194. {
  195. return (mod & PRIVATE) != 0;
  196. }
  197. /**
  198. * Check whether the given modifier is protected.
  199. * @param mod the modifier.
  200. * @return <code>true</code> if protected, <code>false</code> otherwise.
  201. */
  202. public static boolean isProtected(int mod)
  203. {
  204. return (mod & PROTECTED) != 0;
  205. }
  206. /**
  207. * Check whether the given modifier is public.
  208. * @param mod the modifier.
  209. * @return <code>true</code> if public, <code>false</code> otherwise.
  210. */
  211. public static boolean isPublic(int mod)
  212. {
  213. return (mod & PUBLIC) != 0;
  214. }
  215. /**
  216. * Check whether the given modifier is static.
  217. * @param mod the modifier.
  218. * @return <code>true</code> if static, <code>false</code> otherwise.
  219. */
  220. public static boolean isStatic(int mod)
  221. {
  222. return (mod & STATIC) != 0;
  223. }
  224. /**
  225. * Check whether the given modifier is strictfp.
  226. * @param mod the modifier.
  227. * @return <code>true</code> if strictfp, <code>false</code> otherwise.
  228. */
  229. public static boolean isStrict(int mod)
  230. {
  231. return (mod & STRICT) != 0;
  232. }
  233. /**
  234. * Check whether the given modifier is synchronized.
  235. * @param mod the modifier.
  236. * @return <code>true</code> if synchronized, <code>false</code> otherwise.
  237. */
  238. public static boolean isSynchronized(int mod)
  239. {
  240. return (mod & SYNCHRONIZED) != 0;
  241. }
  242. /**
  243. * Check whether the given modifier is transient.
  244. * @param mod the modifier.
  245. * @return <code>true</code> if transient, <code>false</code> otherwise.
  246. */
  247. public static boolean isTransient(int mod)
  248. {
  249. return (mod & TRANSIENT) != 0;
  250. }
  251. /**
  252. * Check whether the given modifier is volatile.
  253. * @param mod the modifier.
  254. * @return <code>true</code> if volatile, <code>false</code> otherwise.
  255. */
  256. public static boolean isVolatile(int mod)
  257. {
  258. return (mod & VOLATILE) != 0;
  259. }
  260. /**
  261. * @since 1.7
  262. */
  263. public static int classModifiers()
  264. {
  265. return PUBLIC | PROTECTED | PRIVATE | STATIC | ABSTRACT | FINAL | STRICT;
  266. }
  267. /**
  268. * @since 1.7
  269. */
  270. public static int interfaceModifiers()
  271. {
  272. return PUBLIC | PROTECTED | PRIVATE | STATIC | ABSTRACT | STRICT;
  273. }
  274. /**
  275. * @since 1.7
  276. */
  277. public static int constructorModifiers()
  278. {
  279. return PUBLIC | PROTECTED | PRIVATE;
  280. }
  281. /**
  282. * @since 1.7
  283. */
  284. public static int methodModifiers()
  285. {
  286. return PUBLIC | PROTECTED | PRIVATE | STATIC | ABSTRACT | FINAL | STRICT | SYNCHRONIZED | NATIVE;
  287. }
  288. /**
  289. * @since 1.7
  290. */
  291. public static int fieldModifiers()
  292. {
  293. return PUBLIC | PROTECTED | PRIVATE | STATIC | FINAL | TRANSIENT | VOLATILE;
  294. }
  295. /**
  296. * Get a string representation of all the modifiers represented by the
  297. * given int. The keywords are printed in this order:
  298. * <code>&lt;public|protected|private&gt; abstract static final transient
  299. * volatile synchronized native strictfp interface</code>.
  300. *
  301. * @param mod the modifier.
  302. * @return the String representing the modifiers.
  303. */
  304. public static String toString(int mod)
  305. {
  306. return toString(mod, new CPStringBuilder()).toString();
  307. }
  308. /**
  309. * Package helper method that can take a CPStringBuilder.
  310. * @param mod the modifier
  311. * @param r the CPStringBuilder to which the String representation is appended
  312. * @return r, with information appended
  313. */
  314. static CPStringBuilder toString(int mod, CPStringBuilder r)
  315. {
  316. if (isPublic(mod))
  317. r.append("public ");
  318. if (isProtected(mod))
  319. r.append("protected ");
  320. if (isPrivate(mod))
  321. r.append("private ");
  322. if (isAbstract(mod))
  323. r.append("abstract ");
  324. if (isStatic(mod))
  325. r.append("static ");
  326. if (isFinal(mod))
  327. r.append("final ");
  328. if (isTransient(mod))
  329. r.append("transient ");
  330. if (isVolatile(mod))
  331. r.append("volatile ");
  332. if (isSynchronized(mod))
  333. r.append("synchronized ");
  334. if (isNative(mod))
  335. r.append("native ");
  336. if (isStrict(mod))
  337. r.append("strictfp ");
  338. if (isInterface(mod))
  339. r.append("interface ");
  340. // Trim trailing space.
  341. if ((mod & ALL_FLAGS) != 0)
  342. r.setLength(r.length() - 1);
  343. return r;
  344. }
  345. }