1. /*
  2. * @(#)Modifier.java 1.14 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang.reflect;
  8. /**
  9. * The Modifier class provides static methods and constants to decode
  10. * class and member access modifiers.
  11. *
  12. * @see Class#getModifiers()
  13. * @see Member#getModifiers()
  14. *
  15. * @author Nakul Saraiya
  16. */
  17. public
  18. class Modifier {
  19. /**
  20. * Return true if the specified integer includes the <tt>public</tt>
  21. * modifier.
  22. */
  23. public static boolean isPublic(int mod) {
  24. return (mod & PUBLIC) != 0;
  25. }
  26. /**
  27. * Return true if the specifier integer includes the <tt>private</tt>
  28. * modifier.
  29. */
  30. public static boolean isPrivate(int mod) {
  31. return (mod & PRIVATE) != 0;
  32. }
  33. /**
  34. * Return true if the specifier integer includes the <tt>protected</tt>
  35. * modifier.
  36. */
  37. public static boolean isProtected(int mod) {
  38. return (mod & PROTECTED) != 0;
  39. }
  40. /**
  41. * Return true if the specifier integer includes the <tt>static</tt>
  42. * modifier.
  43. */
  44. public static boolean isStatic(int mod) {
  45. return (mod & STATIC) != 0;
  46. }
  47. /**
  48. * Return true if the specified integer includes the <tt>final</tt>
  49. * modifier.
  50. */
  51. public static boolean isFinal(int mod) {
  52. return (mod & FINAL) != 0;
  53. }
  54. /**
  55. * Return true if the specified integer includes the <tt>synchronized</tt>
  56. * modifier.
  57. */
  58. public static boolean isSynchronized(int mod) {
  59. return (mod & SYNCHRONIZED) != 0;
  60. }
  61. /**
  62. * Return true if the specified integer includes the <tt>volatile</tt>
  63. * modifier.
  64. */
  65. public static boolean isVolatile(int mod) {
  66. return (mod & VOLATILE) != 0;
  67. }
  68. /**
  69. * Return true if the specifier integer includes the <tt>transient</tt>
  70. * modifier.
  71. */
  72. public static boolean isTransient(int mod) {
  73. return (mod & TRANSIENT) != 0;
  74. }
  75. /**
  76. * Return true if the specifier integer includes the <tt>native</tt>
  77. * modifier.
  78. */
  79. public static boolean isNative(int mod) {
  80. return (mod & NATIVE) != 0;
  81. }
  82. /**
  83. * Return true if the specifier integer includes the <tt>interface</tt>
  84. * modifier.
  85. */
  86. public static boolean isInterface(int mod) {
  87. return (mod & INTERFACE) != 0;
  88. }
  89. /**
  90. * Return true if the specifier integer includes the <tt>abstract</tt>
  91. * modifier.
  92. */
  93. public static boolean isAbstract(int mod) {
  94. return (mod & ABSTRACT) != 0;
  95. }
  96. /**
  97. * Return true if the specifier integer includes the <tt>strict</tt>
  98. * modifier.
  99. */
  100. public static boolean isStrict(int mod) {
  101. return (mod & STRICT) != 0;
  102. }
  103. /**
  104. * Return a string describing the access modifier flags in
  105. * the specified modifier. For example:
  106. * <blockquote><pre>
  107. * public final synchronized
  108. * private transient volatile
  109. * </pre></blockquote>
  110. * The modifier names are return in canonical order, as
  111. * specified by <em>The Java Language Specification</em>.
  112. */
  113. public static String toString(int mod) {
  114. StringBuffer sb = new StringBuffer();
  115. int len;
  116. if ((mod & PUBLIC) != 0) sb.append("public ");
  117. if ((mod & PRIVATE) != 0) sb.append("private ");
  118. if ((mod & PROTECTED) != 0) sb.append("protected ");
  119. /* Canonical order */
  120. if ((mod & ABSTRACT) != 0) sb.append("abstract ");
  121. if ((mod & STATIC) != 0) sb.append("static ");
  122. if ((mod & FINAL) != 0) sb.append("final ");
  123. if ((mod & TRANSIENT) != 0) sb.append("transient ");
  124. if ((mod & VOLATILE) != 0) sb.append("volatile ");
  125. if ((mod & NATIVE) != 0) sb.append("native ");
  126. if ((mod & SYNCHRONIZED) != 0) sb.append("synchronized ");
  127. if ((mod & INTERFACE) != 0) sb.append("interface ");
  128. if ((mod & STRICT) != 0) sb.append("strict ");
  129. if ((len = sb.length()) > 0) /* trim trailing space */
  130. return sb.toString().substring(0, len-1);
  131. return "";
  132. }
  133. /*
  134. * Access modifier flag constants from <em>The Java Virtual
  135. * Machine Specification</em>, Table 4.1.
  136. */
  137. /**
  138. * The <code>int</code> value representing the <code>public</code>
  139. * modifier.
  140. */
  141. public static final int PUBLIC = 0x00000001;
  142. /**
  143. * The <code>int</code> value representing the <code>private</code>
  144. * modifier.
  145. */
  146. public static final int PRIVATE = 0x00000002;
  147. /**
  148. * The <code>int</code> value representing the <code>protected</code>
  149. * modifier.
  150. */
  151. public static final int PROTECTED = 0x00000004;
  152. /**
  153. * The <code>int</code> value representing the <code>static</code>
  154. * modifier.
  155. */
  156. public static final int STATIC = 0x00000008;
  157. /**
  158. * The <code>int</code> value representing the <code>final</code>
  159. * modifier.
  160. */
  161. public static final int FINAL = 0x00000010;
  162. /**
  163. * The <code>int</code> value representing the <code>synchronized</code>
  164. * modifier.
  165. */
  166. public static final int SYNCHRONIZED = 0x00000020;
  167. /**
  168. * The <code>int</code> value representing the <code>volatile</code>
  169. * modifier.
  170. */
  171. public static final int VOLATILE = 0x00000040;
  172. /**
  173. * The <code>int</code> value representing the <code>transient</code>
  174. * modifier.
  175. */
  176. public static final int TRANSIENT = 0x00000080;
  177. /**
  178. * The <code>int</code> value representing the <code>native</code>
  179. * modifier.
  180. */
  181. public static final int NATIVE = 0x00000100;
  182. /**
  183. * The <code>int</code> value representing the <code>interface</code>
  184. * modifier.
  185. */
  186. public static final int INTERFACE = 0x00000200;
  187. /**
  188. * The <code>int</code> value representing the <code>abstract</code>
  189. * modifier.
  190. */
  191. public static final int ABSTRACT = 0x00000400;
  192. /**
  193. * The <code>int</code> value representing the <code>strict</code>
  194. * modifier.
  195. */
  196. public static final int STRICT = 0x00000800;
  197. }