001/* java.lang.reflect.Modifier 002 Copyright (C) 1998, 1999, 2001, 2002, 2005, 2008, 2012 Free Software Foundation, Inc. 003 004This file is part of GNU Classpath. 005 006GNU Classpath is free software; you can redistribute it and/or modify 007it under the terms of the GNU General Public License as published by 008the Free Software Foundation; either version 2, or (at your option) 009any later version. 010 011GNU Classpath is distributed in the hope that it will be useful, but 012WITHOUT ANY WARRANTY; without even the implied warranty of 013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014General Public License for more details. 015 016You should have received a copy of the GNU General Public License 017along with GNU Classpath; see the file COPYING. If not, write to the 018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 01902110-1301 USA. 020 021Linking this library statically or dynamically with other modules is 022making a combined work based on this library. Thus, the terms and 023conditions of the GNU General Public License cover the whole 024combination. 025 026As a special exception, the copyright holders of this library give you 027permission to link this library with independent modules to produce an 028executable, regardless of the license terms of these independent 029modules, and to copy and distribute the resulting executable under 030terms of your choice, provided that you also meet, for each linked 031independent module, the terms and conditions of the license of that 032module. An independent module is a module which is not derived from 033or based on this library. If you modify this library, you may extend 034this exception to your version of the library, but you are not 035obligated to do so. If you do not wish to do so, delete this 036exception statement from your version. */ 037 038 039package java.lang.reflect; 040 041import gnu.java.lang.CPStringBuilder; 042 043/** 044 * Modifier is a helper class with static methods to determine whether an 045 * int returned from getModifiers() represents static, public, protected, 046 * native, final, etc... and provides an additional method to print 047 * out all of the modifiers in an int in order. 048 * <p> 049 * The methods in this class use the bitmask values in the VM spec to 050 * determine the modifiers of an int. This means that a VM must return a 051 * standard mask, conformant with the VM spec. I don't know if this is how 052 * Sun does it, but I'm willing to bet money that it is. 053 * 054 * @author John Keiser 055 * @author Tom Tromey (tromey@cygnus.com) 056 * @author Eric Blake (ebb9@email.byu.edu) 057 * @see Member#getModifiers() 058 * @see Method#getModifiers() 059 * @see Field#getModifiers() 060 * @see Constructor#getModifiers() 061 * @see Class#getModifiers() 062 * @since 1.1 063 */ 064public class Modifier 065{ 066 /** <STRONG>This constructor really shouldn't be here ... there are no 067 * instance methods or variables of this class, so instantiation is 068 * worthless. However, this function is in the 1.1 spec, so it is added 069 * for completeness.</STRONG> 070 */ 071 public Modifier() 072 { 073 } 074 075 /** 076 * Public: accessible from any other class. 077 */ 078 public static final int PUBLIC = 0x0001; 079 080 /** 081 * Private: accessible only from the same enclosing class. 082 */ 083 public static final int PRIVATE = 0x0002; 084 085 /** 086 * Protected: accessible only to subclasses, or within the package. 087 */ 088 public static final int PROTECTED = 0x0004; 089 090 /** 091 * Static:<br><ul> 092 * <li>Class: no enclosing instance for nested class.</li> 093 * <li>Field or Method: can be accessed or invoked without an 094 * instance of the declaring class.</li> 095 * </ul> 096 */ 097 public static final int STATIC = 0x0008; 098 099 /** 100 * Final:<br><ul> 101 * <li>Class: no subclasses allowed.</li> 102 * <li>Field: cannot be changed.</li> 103 * <li>Method: cannot be overriden.</li> 104 * </ul> 105 */ 106 public static final int FINAL = 0x0010; 107 108 /** 109 * Synchronized: Method: lock the class while calling this method. 110 */ 111 public static final int SYNCHRONIZED = 0x0020; 112 113 /** 114 * Volatile: Field: cannot be cached. 115 */ 116 public static final int VOLATILE = 0x0040; 117 118 /** 119 * Transient: Field: not serialized or deserialized. 120 */ 121 public static final int TRANSIENT = 0x0080; 122 123 /** 124 * Native: Method: use JNI to call this method. 125 */ 126 public static final int NATIVE = 0x0100; 127 128 /** 129 * Interface: Class: is an interface. 130 */ 131 public static final int INTERFACE = 0x0200; 132 133 /** 134 * Abstract:<br><ul> 135 * <li>Class: may not be instantiated.</li> 136 * <li>Method: may not be called.</li> 137 * </ul> 138 */ 139 public static final int ABSTRACT = 0x0400; 140 141 /** 142 * Strictfp: Method: expressions are FP-strict.<p> 143 * Also used as a modifier for classes, to mean that all initializers 144 * and constructors are FP-strict, but does not show up in 145 * Class.getModifiers. 146 */ 147 public static final int STRICT = 0x0800; 148 149 150 /** 151 * Super - treat invokespecial as polymorphic so that super.foo() works 152 * according to the JLS. This is a reuse of the synchronized constant 153 * to patch a hole in JDK 1.0. *shudder*. 154 */ 155 static final int SUPER = 0x0020; 156 157 /** 158 * All the flags, only used by code in this package. 159 */ 160 static final int ALL_FLAGS = 0xfff; 161 162 /** 163 * Flag indicating a bridge method. 164 */ 165 static final int BRIDGE = 0x40; 166 167 /** 168 * Flag indicating a varargs method. 169 */ 170 static final int VARARGS = 0x80; 171 172 /** 173 * Flag indicating a synthetic member. 174 */ 175 static final int SYNTHETIC = 0x1000; 176 177 /** 178 * Flag indicating an enum constant or an enum class. 179 */ 180 static final int ENUM = 0x4000; 181 182 /** 183 * Check whether the given modifier is abstract. 184 * @param mod the modifier. 185 * @return <code>true</code> if abstract, <code>false</code> otherwise. 186 */ 187 public static boolean isAbstract(int mod) 188 { 189 return (mod & ABSTRACT) != 0; 190 } 191 192 /** 193 * Check whether the given modifier is final. 194 * @param mod the modifier. 195 * @return <code>true</code> if final, <code>false</code> otherwise. 196 */ 197 public static boolean isFinal(int mod) 198 { 199 return (mod & FINAL) != 0; 200 } 201 202 /** 203 * Check whether the given modifier is an interface. 204 * @param mod the modifier. 205 * @return <code>true</code> if an interface, <code>false</code> otherwise. 206 */ 207 public static boolean isInterface(int mod) 208 { 209 return (mod & INTERFACE) != 0; 210 } 211 212 /** 213 * Check whether the given modifier is native. 214 * @param mod the modifier. 215 * @return <code>true</code> if native, <code>false</code> otherwise. 216 */ 217 public static boolean isNative(int mod) 218 { 219 return (mod & NATIVE) != 0; 220 } 221 222 /** 223 * Check whether the given modifier is private. 224 * @param mod the modifier. 225 * @return <code>true</code> if private, <code>false</code> otherwise. 226 */ 227 public static boolean isPrivate(int mod) 228 { 229 return (mod & PRIVATE) != 0; 230 } 231 232 /** 233 * Check whether the given modifier is protected. 234 * @param mod the modifier. 235 * @return <code>true</code> if protected, <code>false</code> otherwise. 236 */ 237 public static boolean isProtected(int mod) 238 { 239 return (mod & PROTECTED) != 0; 240 } 241 242 /** 243 * Check whether the given modifier is public. 244 * @param mod the modifier. 245 * @return <code>true</code> if public, <code>false</code> otherwise. 246 */ 247 public static boolean isPublic(int mod) 248 { 249 return (mod & PUBLIC) != 0; 250 } 251 252 /** 253 * Check whether the given modifier is static. 254 * @param mod the modifier. 255 * @return <code>true</code> if static, <code>false</code> otherwise. 256 */ 257 public static boolean isStatic(int mod) 258 { 259 return (mod & STATIC) != 0; 260 } 261 262 /** 263 * Check whether the given modifier is strictfp. 264 * @param mod the modifier. 265 * @return <code>true</code> if strictfp, <code>false</code> otherwise. 266 */ 267 public static boolean isStrict(int mod) 268 { 269 return (mod & STRICT) != 0; 270 } 271 272 /** 273 * Check whether the given modifier is synchronized. 274 * @param mod the modifier. 275 * @return <code>true</code> if synchronized, <code>false</code> otherwise. 276 */ 277 public static boolean isSynchronized(int mod) 278 { 279 return (mod & SYNCHRONIZED) != 0; 280 } 281 282 /** 283 * Check whether the given modifier is transient. 284 * @param mod the modifier. 285 * @return <code>true</code> if transient, <code>false</code> otherwise. 286 */ 287 public static boolean isTransient(int mod) 288 { 289 return (mod & TRANSIENT) != 0; 290 } 291 292 /** 293 * Check whether the given modifier is volatile. 294 * @param mod the modifier. 295 * @return <code>true</code> if volatile, <code>false</code> otherwise. 296 */ 297 public static boolean isVolatile(int mod) 298 { 299 return (mod & VOLATILE) != 0; 300 } 301 302 /** 303 * @since 1.7 304 */ 305 public static int classModifiers() 306 { 307 return PUBLIC | PROTECTED | PRIVATE | STATIC | ABSTRACT | FINAL | STRICT; 308 } 309 310 /** 311 * @since 1.7 312 */ 313 public static int interfaceModifiers() 314 { 315 return PUBLIC | PROTECTED | PRIVATE | STATIC | ABSTRACT | STRICT; 316 } 317 318 /** 319 * @since 1.7 320 */ 321 public static int constructorModifiers() 322 { 323 return PUBLIC | PROTECTED | PRIVATE; 324 } 325 326 /** 327 * @since 1.7 328 */ 329 public static int methodModifiers() 330 { 331 return PUBLIC | PROTECTED | PRIVATE | STATIC | ABSTRACT | FINAL | STRICT | SYNCHRONIZED | NATIVE; 332 } 333 334 /** 335 * @since 1.7 336 */ 337 public static int fieldModifiers() 338 { 339 return PUBLIC | PROTECTED | PRIVATE | STATIC | FINAL | TRANSIENT | VOLATILE; 340 } 341 342 /** 343 * Get a string representation of all the modifiers represented by the 344 * given int. The keywords are printed in this order: 345 * <code><public|protected|private> abstract static final transient 346 * volatile synchronized native strictfp interface</code>. 347 * 348 * @param mod the modifier. 349 * @return the String representing the modifiers. 350 */ 351 public static String toString(int mod) 352 { 353 return toString(mod, new CPStringBuilder()).toString(); 354 } 355 356 /** 357 * Package helper method that can take a CPStringBuilder. 358 * @param mod the modifier 359 * @param r the CPStringBuilder to which the String representation is appended 360 * @return r, with information appended 361 */ 362 static CPStringBuilder toString(int mod, CPStringBuilder r) 363 { 364 if (isPublic(mod)) 365 r.append("public "); 366 if (isProtected(mod)) 367 r.append("protected "); 368 if (isPrivate(mod)) 369 r.append("private "); 370 if (isAbstract(mod)) 371 r.append("abstract "); 372 if (isStatic(mod)) 373 r.append("static "); 374 if (isFinal(mod)) 375 r.append("final "); 376 if (isTransient(mod)) 377 r.append("transient "); 378 if (isVolatile(mod)) 379 r.append("volatile "); 380 if (isSynchronized(mod)) 381 r.append("synchronized "); 382 if (isNative(mod)) 383 r.append("native "); 384 if (isStrict(mod)) 385 r.append("strictfp "); 386 if (isInterface(mod)) 387 r.append("interface "); 388 389 // Trim trailing space. 390 if ((mod & ALL_FLAGS) != 0) 391 r.setLength(r.length() - 1); 392 return r; 393 } 394}