1 11 package org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types; 12 13 import java.util.List ; 14 import java.util.Map ; 15 16 import org.eclipse.core.runtime.Assert; 17 18 import org.eclipse.jdt.core.dom.ITypeBinding; 19 20 21 public abstract class TType { 22 23 public static final int NULL_TYPE= 1; 24 public static final int VOID_TYPE= 2; 25 public static final int PRIMITIVE_TYPE= 3; 26 27 public static final int ARRAY_TYPE= 4; 28 29 public static final int STANDARD_TYPE= 5; 30 public static final int GENERIC_TYPE= 6; 31 public static final int PARAMETERIZED_TYPE= 7; 32 public static final int RAW_TYPE= 8; 33 34 public static final int UNBOUND_WILDCARD_TYPE= 9; 35 public static final int SUPER_WILDCARD_TYPE= 10; 36 public static final int EXTENDS_WILDCARD_TYPE= 11; 37 38 public static final int TYPE_VARIABLE= 12; 39 public static final int CAPTURE_TYPE= 13; 40 41 protected static final int WILDCARD_TYPE_SHIFT= 3; 42 protected static final int ARRAY_TYPE_SHIFT= 5; 43 44 private static final int F_IS_CLASS= 1 << 0; 45 private static final int F_IS_INTERFACE= 1 << 1; 46 private static final int F_IS_ENUM= 1 << 2; 47 private static final int F_IS_ANNOTATION= 1 << 3; 48 49 private static final int F_IS_TOP_LEVEL= 1 << 4; 50 private static final int F_IS_NESTED= 1 << 5; 51 private static final int F_IS_MEMBER= 1 << 6; 52 private static final int F_IS_LOCAL= 1 << 7; 53 private static final int F_IS_ANONYMOUS= 1 << 8; 54 55 protected static final TType[] EMPTY_TYPE_ARRAY= new TType[0]; 56 57 private TypeEnvironment fEnvironment; 58 private String fBindingKey; 59 private int fModifiers; 60 private int fFlags; 61 62 65 protected TType(TypeEnvironment environment) { 66 fEnvironment= environment; 67 } 68 69 72 protected TType(TypeEnvironment environment, String key) { 73 this(environment); 74 Assert.isNotNull(key); 75 fBindingKey= key; 76 } 77 78 83 protected void initialize(ITypeBinding binding) { 84 fBindingKey= binding.getKey(); 85 Assert.isNotNull(fBindingKey); 86 fModifiers= binding.getModifiers(); 87 if (binding.isClass()) { 88 fFlags= F_IS_CLASS; 89 } else if (binding.isAnnotation()) { 92 fFlags= F_IS_ANNOTATION | F_IS_INTERFACE; 93 } else if (binding.isInterface()) { 94 fFlags= F_IS_INTERFACE; 95 } else if (binding.isEnum()) { 96 fFlags= F_IS_ENUM; 97 } 98 99 if (binding.isTopLevel()) { 100 fFlags|= F_IS_TOP_LEVEL; 101 } else if (binding.isNested()) { 102 fFlags|= F_IS_NESTED; 103 if (binding.isMember()) { 104 fFlags|= F_IS_MEMBER; 105 } else if (binding.isLocal()) { 106 fFlags|= F_IS_LOCAL; 107 } else if (binding.isAnonymous()) { 108 fFlags|= F_IS_ANONYMOUS; 109 } 110 } 111 } 112 113 118 public TypeEnvironment getEnvironment() { 119 return fEnvironment; 120 } 121 122 128 public String getBindingKey() { 129 return fBindingKey; 130 } 131 132 139 public int getModifiers() { 140 return fModifiers; 141 } 142 143 148 public abstract int getKind(); 149 150 156 public boolean isJavaLangObject() { 157 return false; 158 } 159 160 166 public boolean isJavaLangCloneable() { 167 return false; 168 } 169 170 176 public boolean isJavaIoSerializable() { 177 return false; 178 } 179 180 186 public final boolean isNullType() { 187 return getKind() == NULL_TYPE; 188 } 189 190 196 public final boolean isVoidType() { 197 return getKind() == VOID_TYPE; 198 } 199 200 206 public final boolean isPrimitiveType() { 207 return getKind() == PRIMITIVE_TYPE; 208 } 209 210 216 public final boolean isArrayType() { 217 return getKind() == ARRAY_TYPE; 218 } 219 220 226 public final boolean isHierarchyType() { 227 int elementType= getKind(); 228 return elementType == RAW_TYPE || elementType == PARAMETERIZED_TYPE 229 || elementType == GENERIC_TYPE || elementType == STANDARD_TYPE; 230 } 231 232 238 public final boolean isStandardType() { 239 return getKind() == STANDARD_TYPE; 240 } 241 242 248 public final boolean isRawType() { 249 return getKind() == RAW_TYPE; 250 } 251 252 258 public final boolean isParameterizedType() { 259 return getKind() == PARAMETERIZED_TYPE; 260 } 261 262 268 public final boolean isGenericType() { 269 return getKind() == GENERIC_TYPE; 270 } 271 272 278 public final boolean isTypeVariable() { 279 return getKind() == TYPE_VARIABLE; 280 } 281 282 288 public final boolean isCaptureType() { 289 return getKind() == CAPTURE_TYPE; 290 } 291 292 298 public final boolean isWildcardType() { 299 int elementType= getKind(); 300 return elementType == EXTENDS_WILDCARD_TYPE || elementType == UNBOUND_WILDCARD_TYPE 301 || elementType == SUPER_WILDCARD_TYPE; 302 } 303 304 310 public final boolean isUnboundWildcardType() { 311 return getKind() == UNBOUND_WILDCARD_TYPE; 312 } 313 314 320 public final boolean isExtendsWildcardType() { 321 return getKind() == EXTENDS_WILDCARD_TYPE; 322 } 323 324 330 public final boolean isSuperWildcardType() { 331 return getKind() == SUPER_WILDCARD_TYPE; 332 } 333 334 340 public final boolean isClass() { 341 return (fFlags & F_IS_CLASS) != 0; 342 } 343 344 350 public final boolean isInterface() { 351 return (fFlags & F_IS_INTERFACE) != 0; 352 } 353 354 360 public final boolean isEnum() { 361 return (fFlags & F_IS_ENUM) != 0; 362 } 363 364 370 public final boolean isAnnotation() { 371 return (fFlags & F_IS_ANNOTATION) != 0; 372 } 373 374 380 public final boolean isTopLevel() { 381 return (fFlags & F_IS_TOP_LEVEL) != 0; 382 } 383 384 390 public final boolean isNested() { 391 return (fFlags & F_IS_NESTED) != 0; 392 } 393 394 400 public final boolean isMember() { 401 return (fFlags & F_IS_MEMBER) != 0; 402 } 403 404 410 public final boolean isLocal() { 411 return (fFlags & F_IS_LOCAL) != 0; 412 } 413 414 420 public final boolean isAnonymous() { 421 return (fFlags & F_IS_ANONYMOUS) != 0; 422 } 423 424 429 public TType getSuperclass() { 430 return null; 431 } 432 433 438 public TType[] getInterfaces() { 439 return EMPTY_TYPE_ARRAY; 440 } 441 442 public boolean isEqualTo(ITypeBinding binding) { 443 if (binding == null) 444 return false; 445 return binding.getKey().equals(fBindingKey); 446 } 447 448 451 public final boolean equals(Object other) { 452 if (this == other) 453 return true; 454 if (!(other instanceof TType)) 455 return false; 456 TType otherType= (TType)other; 457 if (getKind() != otherType.getKind()) 458 return false; 459 return doEquals(otherType); 460 } 461 462 468 protected abstract boolean doEquals(TType type); 469 470 475 public TType getErasure() { 476 return this; 477 } 478 479 485 public TType getTypeDeclaration() { 486 return this; 487 } 488 489 494 public TType[] getSubTypes() throws IllegalStateException { 495 Map subTypes= fEnvironment.getSubTypes(); 496 if (subTypes == null) 497 throw new IllegalStateException ("This TypeEnvironment does not remember subtypes"); List subtypes= (List ) subTypes.get(this); 499 if (subtypes == null) 500 return EMPTY_TYPE_ARRAY; 501 else 502 return (TType[]) subtypes.toArray(new TType[subtypes.size()]); 503 } 504 505 512 public final boolean canAssignTo(TType lhs) { 513 if (this.isTypeEquivalentTo(lhs)) 514 return true; 515 return doCanAssignTo(lhs); 516 } 517 518 525 protected boolean isTypeEquivalentTo(TType other) { 526 return this.equals(other); 527 } 528 529 537 protected boolean checkTypeArgument(TType rhs) { 538 return this.equals(rhs); 539 } 540 541 547 protected abstract boolean doCanAssignTo(TType lhs); 548 549 555 public abstract String getName(); 556 557 562 public String getPrettySignature() { 563 return getPlainPrettySignature(); 564 } 565 566 573 protected abstract String getPlainPrettySignature(); 574 575 578 public String toString() { 579 return getPrettySignature(); 580 } 581 } 582 | Popular Tags |