1 24 25 package org.aspectj.compiler.base.ast; 26 27 29 import org.aspectj.compiler.base.JavaCompiler; 30 import org.aspectj.compiler.base.CodeWriter; 31 import java.io.IOException ; 32 33 34 39 public class Modifiers extends ASTObject { 40 44 45 49 public static final int PUBLIC = 0x00000001; 50 51 55 public static final int PRIVATE = 0x00000002; 56 57 61 public static final int PROTECTED = 0x00000004; 62 63 67 public static final int STATIC = 0x00000008; 68 69 73 public static final int FINAL = 0x00000010; 74 75 79 public static final int SYNCHRONIZED = 0x00000020; 80 81 85 public static final int VOLATILE = 0x00000040; 86 87 91 public static final int TRANSIENT = 0x00000080; 92 93 97 public static final int NATIVE = 0x00000100; 98 99 103 public static final int INTERFACE = 0x00000200; 104 105 109 public static final int ABSTRACT = 0x00000400; 110 111 115 public static final int STRICT = 0x00000800; 116 117 public static final int PRIVILEGED = 0x00010000; 121 122 125 126 127 public static final int DEFAULT_PRINT_MASK = 128 PUBLIC | PRIVATE | PROTECTED | STATIC | FINAL | SYNCHRONIZED | 129 VOLATILE | TRANSIENT | NATIVE | ABSTRACT | STRICT; 130 131 132 public static final int INTERFACE_METHOD_PRINT_MASK = 133 STATIC | FINAL | SYNCHRONIZED | VOLATILE | TRANSIENT | NATIVE; 134 135 136 public static final int CONSTRUCTOR_PRINT_MASK = 137 PUBLIC | PRIVATE | PROTECTED | STATIC | FINAL | SYNCHRONIZED | 138 VOLATILE | TRANSIENT | NATIVE | ABSTRACT; 139 140 141 167 168 public static boolean isPublic(int value) { 169 return (value & PUBLIC) != 0; 170 } 171 public boolean isPublic() { 172 return (value & PUBLIC) != 0; 173 } 174 public static boolean isFinal(int value) { 175 return (value & FINAL) != 0; 176 } 177 public boolean isFinal() { 178 return (value & FINAL) != 0; 179 } 180 public static boolean isPrivate(int value) { 181 return (value & PRIVATE) != 0; 182 } 183 public boolean isPrivate() { 184 return (value & PRIVATE) != 0; 185 } 186 public static boolean isProtected(int value) { 187 return (value & PROTECTED) != 0; 188 } 189 public boolean isProtected() { 190 return (value & PROTECTED) != 0; 191 } 192 193 public static boolean isPackagePrivate(int value) { 194 return (value & (PUBLIC | PROTECTED | PRIVATE)) == 0; 195 } 196 public boolean isPackagePrivate() { 197 return (value & (PUBLIC | PROTECTED | PRIVATE)) == 0; 198 } 199 200 public static boolean isInterface(int value) { 201 return (value & INTERFACE) != 0; 202 } 203 public boolean isInterface() { 204 return (value & INTERFACE) != 0; 205 } 206 207 public static boolean isStatic(int value) { 208 return (value & STATIC) != 0; 209 } 210 public boolean isStatic() { 211 return (value & STATIC) != 0; 212 } 213 public static boolean isAbstract(int value) { 214 return (value & ABSTRACT) != 0; 215 } 216 public boolean isAbstract() { 217 return (value & ABSTRACT) != 0; 218 } 219 public static boolean isStrict(int value) { 220 return (value & STRICT) != 0; 221 } 222 public boolean isStrict() { 223 return (value & STRICT) != 0; 224 } 225 226 public static boolean isPrivileged(int value) { 227 return (value & PRIVILEGED) != 0; 228 } 229 public boolean isPrivileged() { 230 return (value & PRIVILEGED) != 0; 231 } 232 233 public boolean isSynchronized() { 234 return (value & SYNCHRONIZED) != 0; 235 } 236 237 public boolean isNative() { 238 return (value & NATIVE) != 0; 239 } 240 241 public boolean isTransient() { 242 return (value & TRANSIENT) != 0; 243 } 244 245 public boolean isVolatile() { 246 return (value & VOLATILE) != 0; 247 } 248 249 protected void setAttribute(int mask, boolean set) { 250 if (set) value |= mask; 251 else value &= ~mask; 252 } 253 254 public void setPublic(boolean v) { 255 setAttribute(PRIVATE, false); 256 setAttribute(PROTECTED, false); 257 setAttribute(PUBLIC, v); 258 } 259 public void setProtected() { 260 setAttribute(PRIVATE, false); 261 setAttribute(PUBLIC, false); 262 setAttribute(PROTECTED, true); 263 } 264 265 public void setPrivate(boolean v) { 266 setAttribute(PROTECTED, false); 267 setAttribute(PUBLIC, false); 268 setAttribute(PRIVATE, v); 269 } 270 public void setInterface(boolean v) { 271 setAttribute(INTERFACE, v); 272 } 273 public void setFinal(boolean v) { 274 setAttribute(FINAL, v); 275 } 276 public void setAbstract(boolean v) { 277 setAttribute(ABSTRACT, v); 278 } 279 public void setStatic(boolean v) { 280 setAttribute(STATIC, v); 281 } 282 public void setStrict(boolean v) { 283 setAttribute(STRICT, v); 284 } 285 public void setSynchronized(boolean v) { 286 setAttribute(SYNCHRONIZED, v); 287 } 288 289 public boolean isWeakerThan(Modifiers other) { 290 if (isPublic()) return false; 291 if (isProtected()) return other.isPublic(); 292 if (isPackagePrivate()) return other.isPublic() || other.isProtected(); 293 if (isPrivate()) return !other.isPrivate(); 294 295 return false; 297 } 298 299 300 public boolean isMoreAccessibleThan(Modifiers other) { 301 if (isPublic()) return !other.isPublic(); 302 if (isProtected()) return !(other.isPublic() || other.isProtected()); 303 if (isPackagePrivate()) return other.isPrivate(); 304 if (isPrivate()) return false; 305 306 return false; 308 } 309 310 public int getFalseValue() { return 0; } 311 312 317 public int getAcceptableClassValue() { 318 int val = getValue() & (PUBLIC | FINAL | INTERFACE | ABSTRACT); 319 if (isProtected()) val |= PUBLIC; 320 if (isInterface()) { 321 val &= PUBLIC; 322 val |= ABSTRACT | INTERFACE; 323 } else { 324 val |= 0x0020; } 326 return val; 327 } 328 329 333 public int getAcceptableInnerClassValue() { 334 int val = getValue() & 335 (PUBLIC | PRIVATE | PROTECTED | STATIC | FINAL | INTERFACE | ABSTRACT); 336 if (isInterface()) { 337 val |= STATIC | ABSTRACT; 338 } 339 return val; 343 } 344 345 347 public int getAccessValue() { 348 return getValue() & (PUBLIC | PRIVATE | PROTECTED); 349 } 350 351 public String getAccessValueString() { 352 if (isPublic()) return "public"; 353 else if (isPrivate()) return "private"; 354 else if (isProtected()) return "protected"; 355 else return "package"; 356 } 357 358 public void checkIllegalCombination(int c1, int c2) { 359 if ((getValue() & c1) != 0) { 360 if ((getValue() & c2) != 0) { 361 showError("illegal combination of modifiers: " + 362 toString(c1, false) + toString(c2, true)); 363 } 364 } 365 } 366 367 public void checkSpec() { 368 checkIllegalCombination(PUBLIC, PRIVATE); 369 checkIllegalCombination(PUBLIC, PROTECTED); 370 checkIllegalCombination(PROTECTED, PRIVATE); 371 checkIllegalCombination(ABSTRACT, FINAL); 374 checkIllegalCombination(ABSTRACT, NATIVE); 375 checkIllegalCombination(ABSTRACT, SYNCHRONIZED); 377 378 checkIllegalCombination(NATIVE, STRICT); 379 380 checkIllegalCombination(VOLATILE, FINAL); 381 } 382 383 384 public boolean matches(Modifiers otherModifiers) { 386 return (value & otherModifiers.getValue()) == value; 389 } 390 391 public String toShortString() { 392 return toString(); 393 } 394 395 public void unparse(CodeWriter writer) throws IOException { 396 String modText; 399 if (getParent() instanceof ConstructorDec) { 400 modText = toString(CONSTRUCTOR_PRINT_MASK,false); 401 } 402 else if (getParent() instanceof MethodDec && 403 getParent().getParent().getParent() instanceof TypeDec && 404 !((TypeDec)(getParent().getParent().getParent())).isConcrete() 405 ) { 406 modText = toString(INTERFACE_METHOD_PRINT_MASK,false); 407 } 408 else { 409 modText = toString(DEFAULT_PRINT_MASK,false); 410 } 411 if (modText.length() > 0) writer.write(modText); 412 } 413 414 public String toString() { 415 return toString(DEFAULT_PRINT_MASK,true); 416 } 417 418 private String toString(int mask, boolean trim) { 419 int val = value & mask; 420 if (val == 0) return ""; 421 StringBuffer sb = new StringBuffer (); 422 423 if ((val & PUBLIC) != 0) sb.append("public "); 424 else if ((val & PRIVATE) != 0) sb.append("private "); 425 else if ((val & PROTECTED) != 0) sb.append("protected "); 426 427 428 if ((val & STATIC) != 0) sb.append("static "); 429 if ((val & ABSTRACT) != 0) sb.append("abstract "); 430 if ((val & FINAL) != 0) sb.append("final "); 431 if ((val & TRANSIENT) != 0) sb.append("transient "); 432 if ((val & VOLATILE) != 0) sb.append("volatile "); 433 if ((val & NATIVE) != 0) sb.append("native "); 434 if ((val & SYNCHRONIZED) != 0) sb.append("synchronized "); 435 436 if ((val & STRICT) != 0 && (val & (ABSTRACT|NATIVE)) == 0) { 437 sb.append("strictfp "); 438 } 439 440 String s = sb.toString(); 441 if (trim) return s.substring(0, s.length()-1); 442 return s; 443 } 444 445 protected int value; 447 public int getValue() { return value; } 448 public void setValue(int _value) { value = _value; } 449 450 public Modifiers(SourceLocation location, int _value) { 451 super(location); 452 setValue(_value); 453 } 454 protected Modifiers(SourceLocation source) { 455 super(source); 456 } 457 458 public ASTObject copyWalk(CopyWalker walker) { 459 Modifiers ret = new Modifiers(getSourceLocation()); 460 ret.preCopy(walker, this); 461 ret.value = value; 462 return ret; 463 } 464 465 466 public String getDefaultDisplayName() { 467 return "Modifiers(value: "+value+")"; 468 } 469 470 } 472 | Popular Tags |