1 11 12 package org.eclipse.jdt.core.dom; 13 14 import java.util.List ; 15 16 import org.eclipse.jdt.core.IJavaElement; 17 import org.eclipse.jdt.core.JavaModelException; 18 import org.eclipse.jdt.internal.compiler.lookup.ArrayBinding; 19 import org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope; 20 import org.eclipse.jdt.internal.compiler.util.Util; 21 import org.eclipse.jdt.internal.core.CompilationUnit; 22 import org.eclipse.jdt.internal.core.PackageFragment; 23 24 27 class RecoveredTypeBinding implements ITypeBinding { 28 29 private VariableDeclaration variableDeclaration; 30 private Type currentType; 31 private BindingResolver resolver; 32 private int dimensions; 33 private RecoveredTypeBinding innerTypeBinding; 34 private ITypeBinding[] typeArguments; 35 private org.eclipse.jdt.internal.compiler.lookup.TypeBinding referenceBinding; 36 37 RecoveredTypeBinding(BindingResolver resolver, VariableDeclaration variableDeclaration) { 38 this.variableDeclaration = variableDeclaration; 39 this.resolver = resolver; 40 this.currentType = getType(); 41 this.dimensions = variableDeclaration.getExtraDimensions(); 42 if (this.currentType.isArrayType()) { 43 this.dimensions += ((ArrayType) this.currentType).getDimensions(); 44 } 45 } 46 47 RecoveredTypeBinding(BindingResolver resolver, org.eclipse.jdt.internal.compiler.lookup.TypeBinding referenceBinding) { 48 this.resolver = resolver; 49 this.dimensions = referenceBinding.dimensions(); 50 this.referenceBinding = referenceBinding; 51 } 52 53 RecoveredTypeBinding(BindingResolver resolver, Type type) { 54 this.currentType = type; 55 this.resolver = resolver; 56 this.dimensions = 0; 57 if (type.isArrayType()) { 58 this.dimensions += ((ArrayType) type).getDimensions(); 59 } 60 } 61 62 RecoveredTypeBinding(BindingResolver resolver, RecoveredTypeBinding typeBinding, int dimensions) { 63 this.innerTypeBinding = typeBinding; 64 this.dimensions = typeBinding.getDimensions() + dimensions; 65 this.resolver = resolver; 66 } 67 68 71 public ITypeBinding createArrayType(int dims) { 72 return this.resolver.getTypeBinding(this, dims); 73 } 74 75 78 public String getBinaryName() { 79 return null; 80 } 81 82 85 public ITypeBinding getBound() { 86 return null; 87 } 88 89 92 public ITypeBinding getComponentType() { 93 if (this.dimensions == 0) return null; 94 return this.resolver.getTypeBinding(this, -1); 95 } 96 97 100 public IVariableBinding[] getDeclaredFields() { 101 return TypeBinding.NO_VARIABLE_BINDINGS; 102 } 103 104 107 public IMethodBinding[] getDeclaredMethods() { 108 return TypeBinding.NO_METHOD_BINDINGS; 109 } 110 111 114 public int getDeclaredModifiers() { 115 return 0; 116 } 117 118 121 public ITypeBinding[] getDeclaredTypes() { 122 return TypeBinding.NO_TYPE_BINDINGS; 123 } 124 125 128 public ITypeBinding getDeclaringClass() { 129 return null; 130 } 131 132 135 public IMethodBinding getDeclaringMethod() { 136 return null; 137 } 138 139 142 public int getDimensions() { 143 return this.dimensions; 144 } 145 146 149 public ITypeBinding getElementType() { 150 if (this.referenceBinding != null) { 151 if (this.referenceBinding.isArrayType()) { 152 ArrayBinding arrayBinding = (ArrayBinding) this.referenceBinding; 153 return new RecoveredTypeBinding(this.resolver, arrayBinding.leafComponentType); 154 } else { 155 return new RecoveredTypeBinding(this.resolver, this.referenceBinding); 156 } 157 } 158 if (this.innerTypeBinding != null) { 159 return this.innerTypeBinding.getElementType(); 160 } 161 if (this.currentType!= null && this.currentType.isArrayType()) { 162 return this.resolver.getTypeBinding(((ArrayType) this.currentType).getElementType()); 163 } 164 if (this.variableDeclaration != null && this.variableDeclaration.getExtraDimensions() != 0) { 165 return this.resolver.getTypeBinding(getType()); 166 } 167 return null; 168 } 169 170 173 public ITypeBinding getErasure() { 174 return this; 175 } 176 177 180 public ITypeBinding[] getInterfaces() { 181 return TypeBinding.NO_TYPE_BINDINGS; 182 } 183 184 187 public int getModifiers() { 188 return Modifier.NONE; 189 } 190 191 194 public String getName() { 195 char[] brackets = new char[this.dimensions * 2]; 196 for (int i = this.dimensions * 2 - 1; i >= 0; i -= 2) { 197 brackets[i] = ']'; 198 brackets[i - 1] = '['; 199 } 200 StringBuffer buffer = new StringBuffer (this.getInternalName()); 201 buffer.append(brackets); 202 return String.valueOf(buffer); 203 } 204 205 private String getInternalName() { 206 if (this.innerTypeBinding != null) { 207 return this.innerTypeBinding.getInternalName(); 208 } else if (this.referenceBinding != null) { 209 org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding typeBinding = null; 210 if (this.referenceBinding.isArrayType()) { 211 ArrayBinding arrayBinding = (ArrayBinding) this.referenceBinding; 212 if (arrayBinding.leafComponentType instanceof org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) { 213 typeBinding = (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) arrayBinding.leafComponentType; 214 } 215 } else if (this.referenceBinding instanceof org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) { 216 typeBinding = (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) this.referenceBinding; 217 } 218 return new String (typeBinding.compoundName[typeBinding.compoundName.length - 1]); 219 } 220 return this.getTypeNameFrom(getType()); 221 } 222 223 226 public IPackageBinding getPackage() { 227 CompilationUnitScope scope = this.resolver.scope(); 228 if (scope != null) { 229 return this.resolver.getPackageBinding(scope.getCurrentPackage()); 230 } 231 return null; 232 } 233 234 237 public String getQualifiedName() { 238 return this.getName(); 239 } 240 241 244 public ITypeBinding getSuperclass() { 245 return this.resolver.resolveWellKnownType("java.lang.Object"); } 247 248 251 public ITypeBinding[] getTypeArguments() { 252 if (this.referenceBinding != null) { 253 return this.typeArguments = TypeBinding.NO_TYPE_BINDINGS; 254 } 255 if (this.typeArguments != null) { 256 return typeArguments; 257 } 258 259 if (this.innerTypeBinding != null) { 260 return this.innerTypeBinding.getTypeArguments(); 261 } 262 263 if (this.currentType.isParameterizedType()) { 264 ParameterizedType parameterizedType = (ParameterizedType) this.currentType; 265 List typeArgumentsList = parameterizedType.typeArguments(); 266 int size = typeArgumentsList.size(); 267 ITypeBinding[] temp = new ITypeBinding[size]; 268 for (int i = 0; i < size; i++) { 269 ITypeBinding currentTypeBinding = ((Type) typeArgumentsList.get(i)).resolveBinding(); 270 if (currentTypeBinding == null) { 271 return this.typeArguments = TypeBinding.NO_TYPE_BINDINGS; 272 } 273 temp[i] = currentTypeBinding; 274 } 275 return this.typeArguments = temp; 276 } 277 return this.typeArguments = TypeBinding.NO_TYPE_BINDINGS; 278 } 279 280 283 public ITypeBinding[] getTypeBounds() { 284 return TypeBinding.NO_TYPE_BINDINGS; 285 } 286 287 290 public ITypeBinding getTypeDeclaration() { 291 return this; 292 } 293 294 297 public ITypeBinding[] getTypeParameters() { 298 return TypeBinding.NO_TYPE_BINDINGS; 299 } 300 301 304 public ITypeBinding getWildcard() { 305 return null; 306 } 307 308 311 public boolean isAnnotation() { 312 return false; 313 } 314 315 318 public boolean isAnonymous() { 319 return false; 320 } 321 322 325 public boolean isArray() { 326 return false; 327 } 328 329 332 public boolean isAssignmentCompatible(ITypeBinding typeBinding) { 333 if ("java.lang.Object".equals(typeBinding.getQualifiedName())) { return true; 335 } 336 return this.isEqualTo(typeBinding); 338 } 339 340 343 public boolean isCapture() { 344 return false; 345 } 346 347 350 public boolean isCastCompatible(ITypeBinding typeBinding) { 351 if ("java.lang.Object".equals(typeBinding.getQualifiedName())) { return true; 353 } 354 return this.isEqualTo(typeBinding); 356 } 357 358 361 public boolean isClass() { 362 return true; 363 } 364 365 368 public boolean isEnum() { 369 return false; 370 } 371 372 375 public boolean isFromSource() { 376 return false; 377 } 378 379 382 public boolean isGenericType() { 383 return false; 384 } 385 386 389 public boolean isInterface() { 390 return false; 391 } 392 393 396 public boolean isLocal() { 397 return false; 398 } 399 400 403 public boolean isMember() { 404 return false; 405 } 406 407 410 public boolean isNested() { 411 return false; 412 } 413 414 417 public boolean isNullType() { 418 return false; 419 } 420 421 424 public boolean isParameterizedType() { 425 if (this.innerTypeBinding != null) { 426 return this.innerTypeBinding.isParameterizedType(); 427 } 428 if (this.currentType != null) { 429 return this.currentType.isParameterizedType(); 430 } 431 return false; 432 } 433 434 437 public boolean isPrimitive() { 438 return false; 439 } 440 441 444 public boolean isRawType() { 445 return false; 446 } 447 448 451 public boolean isSubTypeCompatible(ITypeBinding typeBinding) { 452 if ("java.lang.Object".equals(typeBinding.getQualifiedName())) { return true; 454 } 455 return this.isEqualTo(typeBinding); 457 } 458 459 462 public boolean isTopLevel() { 463 return true; 464 } 465 466 469 public boolean isTypeVariable() { 470 return false; 471 } 472 473 476 public boolean isUpperbound() { 477 return false; 478 } 479 480 483 public boolean isWildcardType() { 484 return false; 485 } 486 487 490 public IAnnotationBinding[] getAnnotations() { 491 return AnnotationBinding.NoAnnotations; 492 } 493 494 497 public IJavaElement getJavaElement() { 498 try { 499 IPackageBinding packageBinding = getPackage(); 500 if (packageBinding != null) { 501 final IJavaElement javaElement = packageBinding.getJavaElement(); 502 if (javaElement!= null && javaElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) { 503 return new CompilationUnit((PackageFragment) javaElement, this.getInternalName(), this.resolver.getWorkingCopyOwner()).getWorkingCopy(this.resolver.getWorkingCopyOwner(), null); 504 } 505 } 506 } catch (JavaModelException e) { 507 } 509 return null; 510 } 511 512 515 public String getKey() { 516 StringBuffer buffer = new StringBuffer (); 517 buffer.append("Recovered#"); if (this.innerTypeBinding != null) { 519 buffer.append("innerTypeBinding") .append(this.innerTypeBinding.getKey()); 521 } else if (this.currentType != null) { 522 buffer.append("currentType") .append(this.currentType.toString()); 524 } else if (this.referenceBinding != null) { 525 buffer.append("referenceBinding") .append(this.referenceBinding.computeUniqueKey()); 527 } else if (variableDeclaration != null) { 528 buffer 529 .append("variableDeclaration") .append(this.variableDeclaration.getClass()) 531 .append(this.variableDeclaration.getName().getIdentifier()) 532 .append(this.variableDeclaration.getExtraDimensions()); 533 } 534 buffer.append(this.getDimensions()); 535 if (this.typeArguments != null) { 536 buffer.append('<'); 537 for (int i = 0, max = this.typeArguments.length; i < max; i++) { 538 if (i != 0) { 539 buffer.append(','); 540 } 541 buffer.append(this.typeArguments[i].getKey()); 542 } 543 buffer.append('>'); 544 } 545 return String.valueOf(buffer); 546 } 547 548 551 public int getKind() { 552 return IBinding.TYPE; 553 } 554 555 558 public boolean isDeprecated() { 559 return false; 560 } 561 562 565 public boolean isEqualTo(IBinding other) { 566 if (!other.isRecovered() || other.getKind() != IBinding.TYPE) return false; 567 return this.getKey().equals(other.getKey()); 568 } 569 570 573 public boolean isRecovered() { 574 return true; 575 } 576 577 580 public boolean isSynthetic() { 581 return false; 582 } 583 584 private String getTypeNameFrom(Type type) { 585 if (type == null) return Util.EMPTY_STRING; 586 switch(type.getNodeType0()) { 587 case ASTNode.ARRAY_TYPE : 588 ArrayType arrayType = (ArrayType) type; 589 type = arrayType.getElementType(); 590 return getTypeNameFrom(type); 591 case ASTNode.PARAMETERIZED_TYPE : 592 ParameterizedType parameterizedType = (ParameterizedType) type; 593 StringBuffer buffer = new StringBuffer (getTypeNameFrom(parameterizedType.getType())); 594 ITypeBinding[] tArguments = getTypeArguments(); 595 final int typeArgumentsLength = tArguments.length; 596 if (typeArgumentsLength != 0) { 597 buffer.append('<'); 598 for (int i = 0; i < typeArgumentsLength; i++) { 599 if (i > 0) { 600 buffer.append(','); 601 } 602 buffer.append(tArguments[i].getName()); 603 } 604 buffer.append('>'); 605 } 606 return String.valueOf(buffer); 607 case ASTNode.PRIMITIVE_TYPE : 608 PrimitiveType primitiveType = (PrimitiveType) type; 609 return primitiveType.getPrimitiveTypeCode().toString(); 610 case ASTNode.QUALIFIED_TYPE : 611 QualifiedType qualifiedType = (QualifiedType) type; 612 return qualifiedType.getName().getIdentifier(); 613 case ASTNode.SIMPLE_TYPE : 614 SimpleType simpleType = (SimpleType) type; 615 Name name = simpleType.getName(); 616 if (name.isQualifiedName()) { 617 QualifiedName qualifiedName = (QualifiedName) name; 618 return qualifiedName.getName().getIdentifier(); 619 } 620 return ((SimpleName) name).getIdentifier(); 621 } 622 return Util.EMPTY_STRING; 623 } 624 625 private Type getType() { 626 if (this.currentType != null) { 627 return this.currentType; 628 } 629 if (this.variableDeclaration == null) return null; 630 switch(this.variableDeclaration.getNodeType()) { 631 case ASTNode.SINGLE_VARIABLE_DECLARATION : 632 SingleVariableDeclaration singleVariableDeclaration = (SingleVariableDeclaration) this.variableDeclaration; 633 return singleVariableDeclaration.getType(); 634 default : 635 ASTNode parent = this.variableDeclaration.getParent(); 637 switch(parent.getNodeType()) { 638 case ASTNode.VARIABLE_DECLARATION_EXPRESSION : 639 VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression) parent; 640 return variableDeclarationExpression.getType(); 641 case ASTNode.VARIABLE_DECLARATION_STATEMENT : 642 VariableDeclarationStatement statement = (VariableDeclarationStatement) parent; 643 return statement.getType(); 644 case ASTNode.FIELD_DECLARATION : 645 FieldDeclaration fieldDeclaration = (FieldDeclaration) parent; 646 return fieldDeclaration.getType(); 647 } 648 } 649 return null; } 651 } 652 | Popular Tags |