1. /*
  2. * @(#)Modifier.java 1.27 04/02/17
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang.reflect;
  8. import java.security.AccessController;
  9. import sun.reflect.LangReflectAccess;
  10. import sun.reflect.ReflectionFactory;
  11. /**
  12. * The Modifier class provides <code>static</code> methods and
  13. * constants to decode class and member access modifiers. The sets of
  14. * modifiers are represented as integers with distinct bit positions
  15. * representing different modifiers. The values for the constants
  16. * representing the modifiers are taken from <a
  17. * href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/VMSpecTOC.doc.html"><i>The
  18. * Java</i><sup><small>TM</small></sup> <i>Virtual Machine Specification, Second
  19. * edition</i></a> tables
  20. * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75734">4.1</a>,
  21. * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88358">4.4</a>,
  22. * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75568">4.5</a>, and
  23. * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88478">4.7</a>.
  24. *
  25. * @see Class#getModifiers()
  26. * @see Member#getModifiers()
  27. *
  28. * @author Nakul Saraiya
  29. * @author Kenneth Russell
  30. */
  31. public
  32. class Modifier {
  33. /*
  34. * Bootstrapping protocol between java.lang and java.lang.reflect
  35. * packages
  36. */
  37. static {
  38. sun.reflect.ReflectionFactory factory =
  39. (sun.reflect.ReflectionFactory) AccessController.doPrivileged(
  40. new ReflectionFactory.GetReflectionFactoryAction()
  41. );
  42. factory.setLangReflectAccess(new java.lang.reflect.ReflectAccess());
  43. }
  44. /**
  45. * Return <tt>true</tt> if the integer argument includes the
  46. * <tt>public</tt> modifier, <tt>false</tt> otherwise.
  47. *
  48. * @param mod a set of modifiers
  49. * @return <tt>true</tt> if <code>mod</code> includes the
  50. * <tt>public</tt> modifier; <tt>false</tt> otherwise.
  51. */
  52. public static boolean isPublic(int mod) {
  53. return (mod & PUBLIC) != 0;
  54. }
  55. /**
  56. * Return <tt>true</tt> if the integer argument includes the
  57. * <tt>private</tt> modifier, <tt>false</tt> otherwise.
  58. *
  59. * @param mod a set of modifiers
  60. * @return <tt>true</tt> if <code>mod</code> includes the
  61. * <tt>private</tt> modifier; <tt>false</tt> otherwise.
  62. */
  63. public static boolean isPrivate(int mod) {
  64. return (mod & PRIVATE) != 0;
  65. }
  66. /**
  67. * Return <tt>true</tt> if the integer argument includes the
  68. * <tt>protected</tt> modifier, <tt>false</tt> otherwise.
  69. *
  70. * @param mod a set of modifiers
  71. * @return <tt>true</tt> if <code>mod</code> includes the
  72. * <tt>protected</tt> modifier; <tt>false</tt> otherwise.
  73. */
  74. public static boolean isProtected(int mod) {
  75. return (mod & PROTECTED) != 0;
  76. }
  77. /**
  78. * Return <tt>true</tt> if the integer argument includes the
  79. * <tt>static</tt> modifier, <tt>false</tt> otherwise.
  80. *
  81. * @param mod a set of modifiers
  82. * @return <tt>true</tt> if <code>mod</code> includes the
  83. * <tt>static</tt> modifier; <tt>false</tt> otherwise.
  84. */
  85. public static boolean isStatic(int mod) {
  86. return (mod & STATIC) != 0;
  87. }
  88. /**
  89. * Return <tt>true</tt> if the integer argument includes the
  90. * <tt>final</tt> modifier, <tt>false</tt> otherwise.
  91. *
  92. * @param mod a set of modifiers
  93. * @return <tt>true</tt> if <code>mod</code> includes the
  94. * <tt>final</tt> modifier; <tt>false</tt> otherwise.
  95. */
  96. public static boolean isFinal(int mod) {
  97. return (mod & FINAL) != 0;
  98. }
  99. /**
  100. * Return <tt>true</tt> if the integer argument includes the
  101. * <tt>synchronized</tt> modifier, <tt>false</tt> otherwise.
  102. *
  103. * @param mod a set of modifiers
  104. * @return <tt>true</tt> if <code>mod</code> includes the
  105. * <tt>synchronized</tt> modifier; <tt>false</tt> otherwise.
  106. */
  107. public static boolean isSynchronized(int mod) {
  108. return (mod & SYNCHRONIZED) != 0;
  109. }
  110. /**
  111. * Return <tt>true</tt> if the integer argument includes the
  112. * <tt>volatile</tt> modifier, <tt>false</tt> otherwise.
  113. *
  114. * @param mod a set of modifiers
  115. * @return <tt>true</tt> if <code>mod</code> includes the
  116. * <tt>volatile</tt> modifier; <tt>false</tt> otherwise.
  117. */
  118. public static boolean isVolatile(int mod) {
  119. return (mod & VOLATILE) != 0;
  120. }
  121. /**
  122. * Return <tt>true</tt> if the integer argument includes the
  123. * <tt>transient</tt> modifier, <tt>false</tt> otherwise.
  124. *
  125. * @param mod a set of modifiers
  126. * @return <tt>true</tt> if <code>mod</code> includes the
  127. * <tt>transient</tt> modifier; <tt>false</tt> otherwise.
  128. */
  129. public static boolean isTransient(int mod) {
  130. return (mod & TRANSIENT) != 0;
  131. }
  132. /**
  133. * Return <tt>true</tt> if the integer argument includes the
  134. * <tt>native</tt> modifier, <tt>false</tt> otherwise.
  135. *
  136. * @param mod a set of modifiers
  137. * @return <tt>true</tt> if <code>mod</code> includes the
  138. * <tt>native</tt> modifier; <tt>false</tt> otherwise.
  139. */
  140. public static boolean isNative(int mod) {
  141. return (mod & NATIVE) != 0;
  142. }
  143. /**
  144. * Return <tt>true</tt> if the integer argument includes the
  145. * <tt>interface</tt> modifier, <tt>false</tt> otherwise.
  146. *
  147. * @param mod a set of modifiers
  148. * @return <tt>true</tt> if <code>mod</code> includes the
  149. * <tt>interface</tt> modifier; <tt>false</tt> otherwise.
  150. */
  151. public static boolean isInterface(int mod) {
  152. return (mod & INTERFACE) != 0;
  153. }
  154. /**
  155. * Return <tt>true</tt> if the integer argument includes the
  156. * <tt>abstract</tt> modifier, <tt>false</tt> otherwise.
  157. *
  158. * @param mod a set of modifiers
  159. * @return <tt>true</tt> if <code>mod</code> includes the
  160. * <tt>abstract</tt> modifier; <tt>false</tt> otherwise.
  161. */
  162. public static boolean isAbstract(int mod) {
  163. return (mod & ABSTRACT) != 0;
  164. }
  165. /**
  166. * Return <tt>true</tt> if the integer argument includes the
  167. * <tt>strictfp</tt> modifier, <tt>false</tt> otherwise.
  168. *
  169. * @param mod a set of modifiers
  170. * @return <tt>true</tt> if <code>mod</code> includes the
  171. * <tt>strictfp</tt> modifier; <tt>false</tt> otherwise.
  172. */
  173. public static boolean isStrict(int mod) {
  174. return (mod & STRICT) != 0;
  175. }
  176. /**
  177. * Return a string describing the access modifier flags in
  178. * the specified modifier. For example:
  179. * <blockquote><pre>
  180. * public final synchronized strictfp
  181. * </pre></blockquote>
  182. * The modifier names are returned in an order consistent with the
  183. * suggested modifier orderings given in <a
  184. * href="http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html"><em>The
  185. * Java Language Specification, Second Edition</em></a> sections
  186. * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#21613">§8.1.1</a>,
  187. * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78091">§8.3.1</a>,
  188. * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78188">§8.4.3</a>,
  189. * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#42018">§8.8.3</a>, and
  190. * <a href="http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html#235947">§9.1.1</a>.
  191. * The full modifier ordering used by this method is:
  192. * <blockquote> <code>
  193. * public protected private abstract static final transient
  194. * volatile synchronized native strictfp
  195. * interface </code> </blockquote>
  196. * The <code>interface</code> modifier discussed in this class is
  197. * not a true modifier in the Java language and it appears after
  198. * all other modifiers listed by this method. This method may
  199. * return a string of modifiers that are not valid modifiers of a
  200. * Java entity; in other words, no checking is done on the
  201. * possible validity of the combination of modifiers represented
  202. * by the input.
  203. *
  204. * @param mod a set of modifiers
  205. * @return a string representation of the set of modifiers
  206. * represented by <code>mod</code>
  207. */
  208. public static String toString(int mod) {
  209. StringBuffer sb = new StringBuffer();
  210. int len;
  211. if ((mod & PUBLIC) != 0) sb.append("public ");
  212. if ((mod & PROTECTED) != 0) sb.append("protected ");
  213. if ((mod & PRIVATE) != 0) sb.append("private ");
  214. /* Canonical order */
  215. if ((mod & ABSTRACT) != 0) sb.append("abstract ");
  216. if ((mod & STATIC) != 0) sb.append("static ");
  217. if ((mod & FINAL) != 0) sb.append("final ");
  218. if ((mod & TRANSIENT) != 0) sb.append("transient ");
  219. if ((mod & VOLATILE) != 0) sb.append("volatile ");
  220. if ((mod & SYNCHRONIZED) != 0) sb.append("synchronized ");
  221. if ((mod & NATIVE) != 0) sb.append("native ");
  222. if ((mod & STRICT) != 0) sb.append("strictfp ");
  223. if ((mod & INTERFACE) != 0) sb.append("interface ");
  224. if ((len = sb.length()) > 0) /* trim trailing space */
  225. return sb.toString().substring(0, len-1);
  226. return "";
  227. }
  228. /*
  229. * Access modifier flag constants from <em>The Java Virtual
  230. * Machine Specification, Second Edition</em>, tables 4.1, 4.4,
  231. * 4.5, and 4.7.
  232. */
  233. /**
  234. * The <code>int</code> value representing the <code>public</code>
  235. * modifier.
  236. */
  237. public static final int PUBLIC = 0x00000001;
  238. /**
  239. * The <code>int</code> value representing the <code>private</code>
  240. * modifier.
  241. */
  242. public static final int PRIVATE = 0x00000002;
  243. /**
  244. * The <code>int</code> value representing the <code>protected</code>
  245. * modifier.
  246. */
  247. public static final int PROTECTED = 0x00000004;
  248. /**
  249. * The <code>int</code> value representing the <code>static</code>
  250. * modifier.
  251. */
  252. public static final int STATIC = 0x00000008;
  253. /**
  254. * The <code>int</code> value representing the <code>final</code>
  255. * modifier.
  256. */
  257. public static final int FINAL = 0x00000010;
  258. /**
  259. * The <code>int</code> value representing the <code>synchronized</code>
  260. * modifier.
  261. */
  262. public static final int SYNCHRONIZED = 0x00000020;
  263. /**
  264. * The <code>int</code> value representing the <code>volatile</code>
  265. * modifier.
  266. */
  267. public static final int VOLATILE = 0x00000040;
  268. /**
  269. * The <code>int</code> value representing the <code>transient</code>
  270. * modifier.
  271. */
  272. public static final int TRANSIENT = 0x00000080;
  273. /**
  274. * The <code>int</code> value representing the <code>native</code>
  275. * modifier.
  276. */
  277. public static final int NATIVE = 0x00000100;
  278. /**
  279. * The <code>int</code> value representing the <code>interface</code>
  280. * modifier.
  281. */
  282. public static final int INTERFACE = 0x00000200;
  283. /**
  284. * The <code>int</code> value representing the <code>abstract</code>
  285. * modifier.
  286. */
  287. public static final int ABSTRACT = 0x00000400;
  288. /**
  289. * The <code>int</code> value representing the <code>strictfp</code>
  290. * modifier.
  291. */
  292. public static final int STRICT = 0x00000800;
  293. // Bits not (yet) exposed in the public API either because they
  294. // have different meanings for fields and methods and there is no
  295. // way to distinguish between the two in this class, or because
  296. // they are not Java programming language keywords
  297. static final int BRIDGE = 0x00000040;
  298. static final int VARARGS = 0x00000080;
  299. static final int SYNTHETIC = 0x00001000;
  300. static final int ANNOTATION= 0x00002000;
  301. static final int ENUM = 0x00004000;
  302. static boolean isSynthetic(int mod) {
  303. return (mod & SYNTHETIC) != 0;
  304. }
  305. }