Source Home >> Java Source 1.6.0 >> java.lang.reflect.Modifier V 0.09
  • 001/*
  • 002 * @(#)Modifier.java 1.28 05/11/17
  • 003 *
  • 004 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
  • 005 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  • 006 */
  • 007
  • 008package java.lang.reflect;
  • 009
  • 010import java.security.AccessController;
  • 011import sun.reflect.LangReflectAccess;
  • 012import sun.reflect.ReflectionFactory;
  • 013
  • 014/**
  • 015 * The Modifier class provides <code>static</code> methods and
  • 016 * constants to decode class and member access modifiers. The sets of
  • 017 * modifiers are represented as integers with distinct bit positions
  • 018 * representing different modifiers. The values for the constants
  • 019 * representing the modifiers are taken from <a
  • 020 * href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/VMSpecTOC.doc.html"><i>The
  • 021 * Java</i><sup><small>TM</small></sup> <i>Virtual Machine Specification, Second
  • 022 * edition</i></a> tables
  • 023 * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75734">4.1</a>,
  • 024 * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88358">4.4</a>,
  • 025 * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75568">4.5</a>, and
  • 026 * <a href="http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#88478">4.7</a>.
  • 027 *
  • 028 * @see Class#getModifiers()
  • 029 * @see Member#getModifiers()
  • 030 *
  • 031 * @author Nakul Saraiya
  • 032 * @author Kenneth Russell
  • 033 */
  • 034public
  • 035class Modifier {
  • 036
  • 037 /*
  • 038 * Bootstrapping protocol between java.lang and java.lang.reflect
  • 039 * packages
  • 040 */
  • 041 static {
  • 042 sun.reflect.ReflectionFactory factory =
  • 043 (sun.reflect.ReflectionFactory) AccessController.doPrivileged(
  • 044 new ReflectionFactory.GetReflectionFactoryAction()
  • 045 );
  • 046 factory.setLangReflectAccess(new java.lang.reflect.ReflectAccess());
  • 047 }
  • 048
  • 049 /**
  • 050 * Return <tt>true</tt> if the integer argument includes the
  • 051 * <tt>public</tt> modifier, <tt>false</tt> otherwise.
  • 052 *
  • 053 * @param mod a set of modifiers
  • 054 * @return <tt>true</tt> if <code>mod</code> includes the
  • 055 * <tt>public</tt> modifier; <tt>false</tt> otherwise.
  • 056 */
  • 057 public static boolean isPublic(int mod) {
  • 058 return (mod & PUBLIC) != 0;
  • 059 }
  • 060
  • 061 /**
  • 062 * Return <tt>true</tt> if the integer argument includes the
  • 063 * <tt>private</tt> modifier, <tt>false</tt> otherwise.
  • 064 *
  • 065 * @param mod a set of modifiers
  • 066 * @return <tt>true</tt> if <code>mod</code> includes the
  • 067 * <tt>private</tt> modifier; <tt>false</tt> otherwise.
  • 068 */
  • 069 public static boolean isPrivate(int mod) {
  • 070 return (mod & PRIVATE) != 0;
  • 071 }
  • 072
  • 073 /**
  • 074 * Return <tt>true</tt> if the integer argument includes the
  • 075 * <tt>protected</tt> modifier, <tt>false</tt> otherwise.
  • 076 *
  • 077 * @param mod a set of modifiers
  • 078 * @return <tt>true</tt> if <code>mod</code> includes the
  • 079 * <tt>protected</tt> modifier; <tt>false</tt> otherwise.
  • 080 */
  • 081 public static boolean isProtected(int mod) {
  • 082 return (mod & PROTECTED) != 0;
  • 083 }
  • 084
  • 085 /**
  • 086 * Return <tt>true</tt> if the integer argument includes the
  • 087 * <tt>static</tt> modifier, <tt>false</tt> otherwise.
  • 088 *
  • 089 * @param mod a set of modifiers
  • 090 * @return <tt>true</tt> if <code>mod</code> includes the
  • 091 * <tt>static</tt> modifier; <tt>false</tt> otherwise.
  • 092 */
  • 093 public static boolean isStatic(int mod) {
  • 094 return (mod & STATIC) != 0;
  • 095 }
  • 096
  • 097 /**
  • 098 * Return <tt>true</tt> if the integer argument includes the
  • 099 * <tt>final</tt> modifier, <tt>false</tt> otherwise.
  • 100 *
  • 101 * @param mod a set of modifiers
  • 102 * @return <tt>true</tt> if <code>mod</code> includes the
  • 103 * <tt>final</tt> modifier; <tt>false</tt> otherwise.
  • 104 */
  • 105 public static boolean isFinal(int mod) {
  • 106 return (mod & FINAL) != 0;
  • 107 }
  • 108
  • 109 /**
  • 110 * Return <tt>true</tt> if the integer argument includes the
  • 111 * <tt>synchronized</tt> modifier, <tt>false</tt> otherwise.
  • 112 *
  • 113 * @param mod a set of modifiers
  • 114 * @return <tt>true</tt> if <code>mod</code> includes the
  • 115 * <tt>synchronized</tt> modifier; <tt>false</tt> otherwise.
  • 116 */
  • 117 public static boolean isSynchronized(int mod) {
  • 118 return (mod & SYNCHRONIZED) != 0;
  • 119 }
  • 120
  • 121 /**
  • 122 * Return <tt>true</tt> if the integer argument includes the
  • 123 * <tt>volatile</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>volatile</tt> modifier; <tt>false</tt> otherwise.
  • 128 */
  • 129 public static boolean isVolatile(int mod) {
  • 130 return (mod & VOLATILE) != 0;
  • 131 }
  • 132
  • 133 /**
  • 134 * Return <tt>true</tt> if the integer argument includes the
  • 135 * <tt>transient</tt> modifier, <tt>false</tt> otherwise.
  • 136 *
  • 137 * @param mod a set of modifiers
  • 138 * @return <tt>true</tt> if <code>mod</code> includes the
  • 139 * <tt>transient</tt> modifier; <tt>false</tt> otherwise.
  • 140 */
  • 141 public static boolean isTransient(int mod) {
  • 142 return (mod & TRANSIENT) != 0;
  • 143 }
  • 144
  • 145 /**
  • 146 * Return <tt>true</tt> if the integer argument includes the
  • 147 * <tt>native</tt> modifier, <tt>false</tt> otherwise.
  • 148 *
  • 149 * @param mod a set of modifiers
  • 150 * @return <tt>true</tt> if <code>mod</code> includes the
  • 151 * <tt>native</tt> modifier; <tt>false</tt> otherwise.
  • 152 */
  • 153 public static boolean isNative(int mod) {
  • 154 return (mod & NATIVE) != 0;
  • 155 }
  • 156
  • 157 /**
  • 158 * Return <tt>true</tt> if the integer argument includes the
  • 159 * <tt>interface</tt> modifier, <tt>false</tt> otherwise.
  • 160 *
  • 161 * @param mod a set of modifiers
  • 162 * @return <tt>true</tt> if <code>mod</code> includes the
  • 163 * <tt>interface</tt> modifier; <tt>false</tt> otherwise.
  • 164 */
  • 165 public static boolean isInterface(int mod) {
  • 166 return (mod & INTERFACE) != 0;
  • 167 }
  • 168
  • 169 /**
  • 170 * Return <tt>true</tt> if the integer argument includes the
  • 171 * <tt>abstract</tt> modifier, <tt>false</tt> otherwise.
  • 172 *
  • 173 * @param mod a set of modifiers
  • 174 * @return <tt>true</tt> if <code>mod</code> includes the
  • 175 * <tt>abstract</tt> modifier; <tt>false</tt> otherwise.
  • 176 */
  • 177 public static boolean isAbstract(int mod) {
  • 178 return (mod & ABSTRACT) != 0;
  • 179 }
  • 180
  • 181 /**
  • 182 * Return <tt>true</tt> if the integer argument includes the
  • 183 * <tt>strictfp</tt> modifier, <tt>false</tt> otherwise.
  • 184 *
  • 185 * @param mod a set of modifiers
  • 186 * @return <tt>true</tt> if <code>mod</code> includes the
  • 187 * <tt>strictfp</tt> modifier; <tt>false</tt> otherwise.
  • 188 */
  • 189 public static boolean isStrict(int mod) {
  • 190 return (mod & STRICT) != 0;
  • 191 }
  • 192
  • 193 /**
  • 194 * Return a string describing the access modifier flags in
  • 195 * the specified modifier. For example:
  • 196 * <blockquote><pre>
  • 197 * public final synchronized strictfp
  • 198 * </pre></blockquote>
  • 199 * The modifier names are returned in an order consistent with the
  • 200 * suggested modifier orderings given in <a
  • 201 * href="http://java.sun.com/docs/books/jls/second_edition/html/j.title.doc.html"><em>The
  • 202 * Java Language Specification, Second Edition</em></a> sections
  • 203 * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#21613">§8.1.1</a>,
  • 204 * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78091">§8.3.1</a>,
  • 205 * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78188">§8.4.3</a>,
  • 206 * <a href="http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#42018">§8.8.3</a>, and
  • 207 * <a href="http://java.sun.com/docs/books/jls/second_edition/html/interfaces.doc.html#235947">§9.1.1</a>.
  • 208 * The full modifier ordering used by this method is:
  • 209 * <blockquote> <code>
  • 210 * public protected private abstract static final transient
  • 211 * volatile synchronized native strictfp
  • 212 * interface </code> </blockquote>
  • 213 * The <code>interface</code> modifier discussed in this class is
  • 214 * not a true modifier in the Java language and it appears after
  • 215 * all other modifiers listed by this method. This method may
  • 216 * return a string of modifiers that are not valid modifiers of a
  • 217 * Java entity; in other words, no checking is done on the
  • 218 * possible validity of the combination of modifiers represented
  • 219 * by the input.
  • 220 *
  • 221 * @param mod a set of modifiers
  • 222 * @return a string representation of the set of modifiers
  • 223 * represented by <code>mod</code>
  • 224 */
  • 225 public static String toString(int mod) {
  • 226 StringBuffer sb = new StringBuffer();
  • 227 int len;
  • 228
  • 229 if ((mod & PUBLIC) != 0) sb.append("public ");
  • 230 if ((mod & PROTECTED) != 0) sb.append("protected ");
  • 231 if ((mod & PRIVATE) != 0) sb.append("private ");
  • 232
  • 233 /* Canonical order */
  • 234 if ((mod & ABSTRACT) != 0) sb.append("abstract ");
  • 235 if ((mod & STATIC) != 0) sb.append("static ");
  • 236 if ((mod & FINAL) != 0) sb.append("final ");
  • 237 if ((mod & TRANSIENT) != 0) sb.append("transient ");
  • 238 if ((mod & VOLATILE) != 0) sb.append("volatile ");
  • 239 if ((mod & SYNCHRONIZED) != 0) sb.append("synchronized ");
  • 240 if ((mod & NATIVE) != 0) sb.append("native ");
  • 241 if ((mod & STRICT) != 0) sb.append("strictfp ");
  • 242 if ((mod & INTERFACE) != 0) sb.append("interface ");
  • 243
  • 244 if ((len = sb.length()) > 0) /* trim trailing space */
  • 245 return sb.toString().substring(0, len-1);
  • 246 return "";
  • 247 }
  • 248
  • 249 /*
  • 250 * Access modifier flag constants from <em>The Java Virtual
  • 251 * Machine Specification, Second Edition</em>, tables 4.1, 4.4,
  • 252 * 4.5, and 4.7.
  • 253 */
  • 254
  • 255 /**
  • 256 * The <code>int</code> value representing the <code>public</code>
  • 257 * modifier.
  • 258 */
  • 259 public static final int PUBLIC = 0x00000001;
  • 260
  • 261 /**
  • 262 * The <code>int</code> value representing the <code>private</code>
  • 263 * modifier.
  • 264 */
  • 265 public static final int PRIVATE = 0x00000002;
  • 266
  • 267 /**
  • 268 * The <code>int</code> value representing the <code>protected</code>
  • 269 * modifier.
  • 270 */
  • 271 public static final int PROTECTED = 0x00000004;
  • 272
  • 273 /**
  • 274 * The <code>int</code> value representing the <code>static</code>
  • 275 * modifier.
  • 276 */
  • 277 public static final int STATIC = 0x00000008;
  • 278
  • 279 /**
  • 280 * The <code>int</code> value representing the <code>final</code>
  • 281 * modifier.
  • 282 */
  • 283 public static final int FINAL = 0x00000010;
  • 284
  • 285 /**
  • 286 * The <code>int</code> value representing the <code>synchronized</code>
  • 287 * modifier.
  • 288 */
  • 289 public static final int SYNCHRONIZED = 0x00000020;
  • 290
  • 291 /**
  • 292 * The <code>int</code> value representing the <code>volatile</code>
  • 293 * modifier.
  • 294 */
  • 295 public static final int VOLATILE = 0x00000040;
  • 296
  • 297 /**
  • 298 * The <code>int</code> value representing the <code>transient</code>
  • 299 * modifier.
  • 300 */
  • 301 public static final int TRANSIENT = 0x00000080;
  • 302
  • 303 /**
  • 304 * The <code>int</code> value representing the <code>native</code>
  • 305 * modifier.
  • 306 */
  • 307 public static final int NATIVE = 0x00000100;
  • 308
  • 309 /**
  • 310 * The <code>int</code> value representing the <code>interface</code>
  • 311 * modifier.
  • 312 */
  • 313 public static final int INTERFACE = 0x00000200;
  • 314
  • 315 /**
  • 316 * The <code>int</code> value representing the <code>abstract</code>
  • 317 * modifier.
  • 318 */
  • 319 public static final int ABSTRACT = 0x00000400;
  • 320
  • 321 /**
  • 322 * The <code>int</code> value representing the <code>strictfp</code>
  • 323 * modifier.
  • 324 */
  • 325 public static final int STRICT = 0x00000800;
  • 326
  • 327 // Bits not (yet) exposed in the public API either because they
  • 328 // have different meanings for fields and methods and there is no
  • 329 // way to distinguish between the two in this class, or because
  • 330 // they are not Java programming language keywords
  • 331 static final int BRIDGE = 0x00000040;
  • 332 static final int VARARGS = 0x00000080;
  • 333 static final int SYNTHETIC = 0x00001000;
  • 334 static final int ANNOTATION= 0x00002000;
  • 335 static final int ENUM = 0x00004000;
  • 336 static boolean isSynthetic(int mod) {
  • 337 return (mod & SYNTHETIC) != 0;
  • 338 }
  • 339}

文件:Modifier.java
包名:java.lang.reflect
类名:Modifier
继承:
接口: