You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Modifier.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later.
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist;
  16. import javassist.bytecode.AccessFlag;
  17. /**
  18. * The Modifier class provides static methods and constants to decode
  19. * class and member access modifiers. The constant values are equivalent
  20. * to the corresponding values in <code>javassist.bytecode.AccessFlag</code>.
  21. *
  22. * <p>All the methods/constants in this class are compatible with
  23. * ones in <code>java.lang.reflect.Modifier</code>.
  24. *
  25. * @see CtClass#getModifiers()
  26. */
  27. public class Modifier {
  28. public static final int PUBLIC = AccessFlag.PUBLIC;
  29. public static final int PRIVATE = AccessFlag.PRIVATE;
  30. public static final int PROTECTED = AccessFlag.PROTECTED;
  31. public static final int STATIC = AccessFlag.STATIC;
  32. public static final int FINAL = AccessFlag.FINAL;
  33. public static final int SYNCHRONIZED = AccessFlag.SYNCHRONIZED;
  34. public static final int VOLATILE = AccessFlag.VOLATILE;
  35. public static final int TRANSIENT = AccessFlag.TRANSIENT;
  36. public static final int NATIVE = AccessFlag.NATIVE;
  37. public static final int INTERFACE = AccessFlag.INTERFACE;
  38. public static final int ABSTRACT = AccessFlag.ABSTRACT;
  39. public static final int STRICT = AccessFlag.STRICT;
  40. public static final int ANNOTATION = AccessFlag.ANNOTATION;
  41. public static final int ENUM = AccessFlag.ENUM;
  42. /**
  43. * Returns true if the modifiers include the <tt>public</tt>
  44. * modifier.
  45. */
  46. public static boolean isPublic(int mod) {
  47. return (mod & PUBLIC) != 0;
  48. }
  49. /**
  50. * Returns true if the modifiers include the <tt>private</tt>
  51. * modifier.
  52. */
  53. public static boolean isPrivate(int mod) {
  54. return (mod & PRIVATE) != 0;
  55. }
  56. /**
  57. * Returns true if the modifiers include the <tt>protected</tt>
  58. * modifier.
  59. */
  60. public static boolean isProtected(int mod) {
  61. return (mod & PROTECTED) != 0;
  62. }
  63. /**
  64. * Returns true if the modifiers do not include either
  65. * <tt>public</tt>, <tt>protected</tt>, or <tt>private</tt>.
  66. */
  67. public static boolean isPackage(int mod) {
  68. return (mod & (PUBLIC | PRIVATE | PROTECTED)) == 0;
  69. }
  70. /**
  71. * Returns true if the modifiers include the <tt>static</tt>
  72. * modifier.
  73. */
  74. public static boolean isStatic(int mod) {
  75. return (mod & STATIC) != 0;
  76. }
  77. /**
  78. * Returns true if the modifiers include the <tt>final</tt>
  79. * modifier.
  80. */
  81. public static boolean isFinal(int mod) {
  82. return (mod & FINAL) != 0;
  83. }
  84. /**
  85. * Returns true if the modifiers include the <tt>synchronized</tt>
  86. * modifier.
  87. */
  88. public static boolean isSynchronized(int mod) {
  89. return (mod & SYNCHRONIZED) != 0;
  90. }
  91. /**
  92. * Returns true if the modifiers include the <tt>volatile</tt>
  93. * modifier.
  94. */
  95. public static boolean isVolatile(int mod) {
  96. return (mod & VOLATILE) != 0;
  97. }
  98. /**
  99. * Returns true if the modifiers include the <tt>transient</tt>
  100. * modifier.
  101. */
  102. public static boolean isTransient(int mod) {
  103. return (mod & TRANSIENT) != 0;
  104. }
  105. /**
  106. * Returns true if the modifiers include the <tt>native</tt>
  107. * modifier.
  108. */
  109. public static boolean isNative(int mod) {
  110. return (mod & NATIVE) != 0;
  111. }
  112. /**
  113. * Returns true if the modifiers include the <tt>interface</tt>
  114. * modifier.
  115. */
  116. public static boolean isInterface(int mod) {
  117. return (mod & INTERFACE) != 0;
  118. }
  119. /**
  120. * Returns true if the modifiers include the <tt>annotation</tt>
  121. * modifier.
  122. *
  123. * @since 3.2
  124. */
  125. public static boolean isAnnotation(int mod) {
  126. return (mod & ANNOTATION) != 0;
  127. }
  128. /**
  129. * Returns true if the modifiers include the <tt>enum</tt>
  130. * modifier.
  131. *
  132. * @since 3.2
  133. */
  134. public static boolean isEnum(int mod) {
  135. return (mod & ENUM) != 0;
  136. }
  137. /**
  138. * Returns true if the modifiers include the <tt>abstract</tt>
  139. * modifier.
  140. */
  141. public static boolean isAbstract(int mod) {
  142. return (mod & ABSTRACT) != 0;
  143. }
  144. /**
  145. * Returns true if the modifiers include the <tt>strictfp</tt>
  146. * modifier.
  147. */
  148. public static boolean isStrict(int mod) {
  149. return (mod & STRICT) != 0;
  150. }
  151. /**
  152. * Truns the public bit on. The protected and private bits are
  153. * cleared.
  154. */
  155. public static int setPublic(int mod) {
  156. return (mod & ~(PRIVATE | PROTECTED)) | PUBLIC;
  157. }
  158. /**
  159. * Truns the protected bit on. The protected and public bits are
  160. * cleared.
  161. */
  162. public static int setProtected(int mod) {
  163. return (mod & ~(PRIVATE | PUBLIC)) | PROTECTED;
  164. }
  165. /**
  166. * Truns the private bit on. The protected and private bits are
  167. * cleared.
  168. */
  169. public static int setPrivate(int mod) {
  170. return (mod & ~(PROTECTED | PUBLIC)) | PRIVATE;
  171. }
  172. /**
  173. * Clears the public, protected, and private bits.
  174. */
  175. public static int setPackage(int mod) {
  176. return (mod & ~(PROTECTED | PUBLIC | PRIVATE));
  177. }
  178. /**
  179. * Clears a specified bit in <code>mod</code>.
  180. */
  181. public static int clear(int mod, int clearBit) {
  182. return mod & ~clearBit;
  183. }
  184. /**
  185. * Return a string describing the access modifier flags in
  186. * the specified modifier.
  187. *
  188. * @param mod modifier flags.
  189. */
  190. public static String toString(int mod) {
  191. return java.lang.reflect.Modifier.toString(mod);
  192. }
  193. }