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

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