1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.jdt.core.Flags; 16 import org.eclipse.jdt.core.ICompilationUnit; 17 import org.eclipse.jdt.core.IField; 18 import org.eclipse.jdt.core.IJavaElement; 19 import org.eclipse.jdt.core.IMethod; 20 import org.eclipse.jdt.core.IPackageFragment; 21 import org.eclipse.jdt.core.ISourceRange; 22 import org.eclipse.jdt.core.IType; 23 import org.eclipse.jdt.core.ITypeParameter; 24 import org.eclipse.jdt.core.JavaModelException; 25 import org.eclipse.jdt.core.Signature; 26 import org.eclipse.jdt.core.compiler.*; 27 import org.eclipse.jdt.internal.codeassist.ISelectionRequestor; 28 import org.eclipse.jdt.internal.codeassist.SelectionEngine; 29 import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration; 30 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; 31 import org.eclipse.jdt.internal.compiler.lookup.FieldBinding; 32 import org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding; 33 import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding; 34 import org.eclipse.jdt.internal.compiler.lookup.MethodBinding; 35 import org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding; 36 import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding; 37 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; 38 import org.eclipse.jdt.internal.compiler.lookup.TypeConstants; 39 import org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding; 40 import org.eclipse.jdt.internal.core.util.HandleFactory; 41 import org.eclipse.jdt.internal.core.util.Util; 42 43 47 public class SelectionRequestor implements ISelectionRequestor { 48 51 protected NameLookup nameLookup; 52 53 56 protected Openable openable; 57 58 61 protected IJavaElement[] elements = JavaElement.NO_ELEMENTS; 62 protected int elementIndex = -1; 63 64 protected HandleFactory handleFactory = new HandleFactory(); 65 66 72 public SelectionRequestor(NameLookup nameLookup, Openable openable) { 73 super(); 74 this.nameLookup = nameLookup; 75 this.openable = openable; 76 } 77 private void acceptBinaryMethod( 78 IType type, 79 IMethod method, 80 char[] uniqueKey, 81 boolean isConstructor) { 82 try { 83 if(!isConstructor || ((JavaElement)method).getSourceMapper() == null) { 84 if (uniqueKey != null) { 85 ResolvedBinaryMethod resolvedMethod = new ResolvedBinaryMethod( 86 (JavaElement)method.getParent(), 87 method.getElementName(), 88 method.getParameterTypes(), 89 new String (uniqueKey)); 90 resolvedMethod.occurrenceCount = method.getOccurrenceCount(); 91 method = resolvedMethod; 92 } 93 94 addElement(method); 95 if(SelectionEngine.DEBUG){ 96 System.out.print("SELECTION - accept method("); System.out.print(method.toString()); 98 System.out.println(")"); } 100 } else { 101 ISourceRange range = method.getSourceRange(); 102 if (range.getOffset() != -1 && range.getLength() != 0 ) { 103 if (uniqueKey != null) { 104 ResolvedBinaryMethod resolvedMethod = new ResolvedBinaryMethod( 105 (JavaElement)method.getParent(), 106 method.getElementName(), 107 method.getParameterTypes(), 108 new String (uniqueKey)); 109 resolvedMethod.occurrenceCount = method.getOccurrenceCount(); 110 method = resolvedMethod; 111 } 112 addElement(method); 113 if(SelectionEngine.DEBUG){ 114 System.out.print("SELECTION - accept method("); System.out.print(method.toString()); 116 System.out.println(")"); } 118 } else { 119 addElement(type); 121 if(SelectionEngine.DEBUG){ 122 System.out.print("SELECTION - accept type("); System.out.print(type.toString()); 124 System.out.println(")"); } 126 } 127 } 128 } catch (JavaModelException e) { 129 } 131 } 132 137 protected void acceptBinaryMethod( 138 IType type, 139 char[] selector, 140 char[][] parameterPackageNames, 141 char[][] parameterTypeNames, 142 String [] parameterSignatures, 143 char[][] typeParameterNames, 144 char[][][] typeParameterBoundNames, 145 char[] uniqueKey, 146 boolean isConstructor) { 147 IMethod method= type.getMethod(new String (selector), parameterSignatures); 148 149 if (method.exists()) { 150 if (typeParameterNames != null && typeParameterNames.length != 0) { 151 IMethod[] methods = type.findMethods(method); 152 if (methods.length > 1) { 153 for (int i = 0; i < methods.length; i++) { 154 if (areTypeParametersCompatible(methods[i], typeParameterNames, typeParameterBoundNames)) { 155 acceptBinaryMethod(type, method, uniqueKey, isConstructor); 156 } 157 } 158 return; 159 } 160 } 161 acceptBinaryMethod(type, method, uniqueKey, isConstructor); 162 } 163 } 164 167 public void acceptType(char[] packageName, char[] typeName, int modifiers, boolean isDeclaration, char[] uniqueKey, int start, int end) { 168 int acceptFlags = 0; 169 int kind = modifiers & (ClassFileConstants.AccInterface|ClassFileConstants.AccEnum|ClassFileConstants.AccAnnotation); 170 switch (kind) { 171 case ClassFileConstants.AccAnnotation: 172 case ClassFileConstants.AccAnnotation|ClassFileConstants.AccInterface: 173 acceptFlags = NameLookup.ACCEPT_ANNOTATIONS; 174 break; 175 case ClassFileConstants.AccEnum: 176 acceptFlags = NameLookup.ACCEPT_ENUMS; 177 break; 178 case ClassFileConstants.AccInterface: 179 acceptFlags = NameLookup.ACCEPT_INTERFACES; 180 break; 181 default: 182 acceptFlags = NameLookup.ACCEPT_CLASSES; 183 break; 184 } 185 IType type = null; 186 if(isDeclaration) { 187 type = resolveTypeByLocation(packageName, typeName, acceptFlags, start, end); 188 } else { 189 type = resolveType(packageName, typeName, acceptFlags); 190 if(type != null ) { 191 String key = uniqueKey == null ? type.getKey() : new String (uniqueKey); 192 if(type.isBinary()) { 193 ResolvedBinaryType resolvedType = new ResolvedBinaryType((JavaElement)type.getParent(), type.getElementName(), key); 194 resolvedType.occurrenceCount = type.getOccurrenceCount(); 195 type = resolvedType; 196 } else { 197 ResolvedSourceType resolvedType = new ResolvedSourceType((JavaElement)type.getParent(), type.getElementName(), key); 198 resolvedType.occurrenceCount = type.getOccurrenceCount(); 199 type = resolvedType; 200 } 201 } 202 } 203 204 if (type != null) { 205 addElement(type); 206 if(SelectionEngine.DEBUG){ 207 System.out.print("SELECTION - accept type("); System.out.print(type.toString()); 209 System.out.println(")"); } 211 } 212 } 213 216 public void acceptError(CategorizedProblem error) { 217 } 219 222 public void acceptField(char[] declaringTypePackageName, char[] declaringTypeName, char[] name, boolean isDeclaration, char[] uniqueKey, int start, int end) { 223 if(isDeclaration) { 224 IType type= resolveTypeByLocation(declaringTypePackageName, declaringTypeName, 225 NameLookup.ACCEPT_ALL, 226 start, end); 227 if(type != null) { 228 try { 229 IField[] fields = type.getFields(); 230 for (int i = 0; i < fields.length; i++) { 231 IField field = fields[i]; 232 ISourceRange range = field.getNameRange(); 233 if(range.getOffset() <= start 234 && range.getOffset() + range.getLength() >= end 235 && field.getElementName().equals(new String (name))) { 236 addElement(fields[i]); 237 if(SelectionEngine.DEBUG){ 238 System.out.print("SELECTION - accept field("); System.out.print(field.toString()); 240 System.out.println(")"); } 242 return; } 244 } 245 } catch (JavaModelException e) { 246 return; 247 } 248 } 249 } else { 250 IType type= resolveType(declaringTypePackageName, declaringTypeName, NameLookup.ACCEPT_ALL); 251 if (type != null) { 252 IField field= type.getField(new String (name)); 253 if (field.exists()) { 254 if (uniqueKey != null) { 255 if(field.isBinary()) { 256 ResolvedBinaryField resolvedField = new ResolvedBinaryField( 257 (JavaElement)field.getParent(), 258 field.getElementName(), 259 new String (uniqueKey)); 260 resolvedField.occurrenceCount = field.getOccurrenceCount(); 261 field = resolvedField; 262 } else { 263 ResolvedSourceField resolvedField = new ResolvedSourceField( 264 (JavaElement)field.getParent(), 265 field.getElementName(), 266 new String (uniqueKey)); 267 resolvedField.occurrenceCount = field.getOccurrenceCount(); 268 field = resolvedField; 269 } 270 } 271 addElement(field); 272 if(SelectionEngine.DEBUG){ 273 System.out.print("SELECTION - accept field("); System.out.print(field.toString()); 275 System.out.println(")"); } 277 } 278 } 279 } 280 } 281 public void acceptLocalField(FieldBinding fieldBinding) { 282 IJavaElement res; 283 if(fieldBinding.declaringClass instanceof ParameterizedTypeBinding) { 284 LocalTypeBinding localTypeBinding = (LocalTypeBinding)((ParameterizedTypeBinding)fieldBinding.declaringClass).genericType(); 285 res = findLocalElement(localTypeBinding.sourceStart()); 286 } else { 287 SourceTypeBinding typeBinding = (SourceTypeBinding)fieldBinding.declaringClass; 288 res = findLocalElement(typeBinding.sourceStart()); 289 } 290 if (res != null && res.getElementType() == IJavaElement.TYPE) { 291 IType type = (IType) res; 292 IField field= type.getField(new String (fieldBinding.name)); 293 if (field.exists()) { 294 char[] uniqueKey = fieldBinding.computeUniqueKey(); 295 if(field.isBinary()) { 296 ResolvedBinaryField resolvedField = new ResolvedBinaryField( 297 (JavaElement)field.getParent(), 298 field.getElementName(), 299 new String (uniqueKey)); 300 resolvedField.occurrenceCount = field.getOccurrenceCount(); 301 field = resolvedField; 302 } else { 303 ResolvedSourceField resolvedField = new ResolvedSourceField( 304 (JavaElement)field.getParent(), 305 field.getElementName(), 306 new String (uniqueKey)); 307 resolvedField.occurrenceCount = field.getOccurrenceCount(); 308 field = resolvedField; 309 } 310 addElement(field); 311 if(SelectionEngine.DEBUG){ 312 System.out.print("SELECTION - accept field("); System.out.print(field.toString()); 314 System.out.println(")"); } 316 } 317 } 318 } 319 public void acceptLocalMethod(MethodBinding methodBinding) { 320 IJavaElement res = findLocalElement(methodBinding.sourceStart()); 321 if(res != null) { 322 if(res.getElementType() == IJavaElement.METHOD) { 323 IMethod method = (IMethod) res; 324 325 char[] uniqueKey = methodBinding.computeUniqueKey(); 326 if(method.isBinary()) { 327 ResolvedBinaryMethod resolvedRes = new ResolvedBinaryMethod( 328 (JavaElement)res.getParent(), 329 method.getElementName(), 330 method.getParameterTypes(), 331 new String (uniqueKey)); 332 resolvedRes.occurrenceCount = method.getOccurrenceCount(); 333 res = resolvedRes; 334 } else { 335 ResolvedSourceMethod resolvedRes = new ResolvedSourceMethod( 336 (JavaElement)res.getParent(), 337 method.getElementName(), 338 method.getParameterTypes(), 339 new String (uniqueKey)); 340 resolvedRes.occurrenceCount = method.getOccurrenceCount(); 341 res = resolvedRes; 342 } 343 addElement(res); 344 if(SelectionEngine.DEBUG){ 345 System.out.print("SELECTION - accept method("); System.out.print(res.toString()); 347 System.out.println(")"); } 349 } else if(methodBinding.selector == TypeConstants.INIT && res.getElementType() == IJavaElement.TYPE) { 350 res = ((JavaElement)res).resolved(methodBinding.declaringClass); 352 addElement(res); 353 if(SelectionEngine.DEBUG){ 354 System.out.print("SELECTION - accept type("); System.out.print(res.toString()); 356 System.out.println(")"); } 358 } 359 } 360 } 361 public void acceptLocalType(TypeBinding typeBinding) { 362 IJavaElement res = null; 363 if(typeBinding instanceof ParameterizedTypeBinding) { 364 LocalTypeBinding localTypeBinding = (LocalTypeBinding)((ParameterizedTypeBinding)typeBinding).genericType(); 365 res = findLocalElement(localTypeBinding.sourceStart()); 366 } else if(typeBinding instanceof SourceTypeBinding) { 367 res = findLocalElement(((SourceTypeBinding)typeBinding).sourceStart()); 368 } 369 if(res != null && res.getElementType() == IJavaElement.TYPE) { 370 res = ((JavaElement)res).resolved(typeBinding); 371 addElement(res); 372 if(SelectionEngine.DEBUG){ 373 System.out.print("SELECTION - accept type("); System.out.print(res.toString()); 375 System.out.println(")"); } 377 } 378 } 379 public void acceptLocalTypeParameter(TypeVariableBinding typeVariableBinding) { 380 IJavaElement res; 381 if(typeVariableBinding.declaringElement instanceof ParameterizedTypeBinding) { 382 LocalTypeBinding localTypeBinding = (LocalTypeBinding)((ParameterizedTypeBinding)typeVariableBinding.declaringElement).genericType(); 383 res = findLocalElement(localTypeBinding.sourceStart()); 384 } else { 385 SourceTypeBinding typeBinding = (SourceTypeBinding)typeVariableBinding.declaringElement; 386 res = findLocalElement(typeBinding.sourceStart()); 387 } 388 if (res != null && res.getElementType() == IJavaElement.TYPE) { 389 IType type = (IType) res; 390 ITypeParameter typeParameter = type.getTypeParameter(new String (typeVariableBinding.sourceName)); 391 if (typeParameter.exists()) { 392 addElement(typeParameter); 393 if(SelectionEngine.DEBUG){ 394 System.out.print("SELECTION - accept type parameter("); System.out.print(typeParameter.toString()); 396 System.out.println(")"); } 398 } 399 } 400 } 401 public void acceptLocalMethodTypeParameter(TypeVariableBinding typeVariableBinding) { 402 MethodBinding methodBinding = (MethodBinding)typeVariableBinding.declaringElement; 403 IJavaElement res = findLocalElement(methodBinding.sourceStart()); 404 if(res != null && res.getElementType() == IJavaElement.METHOD) { 405 IMethod method = (IMethod) res; 406 407 ITypeParameter typeParameter = method.getTypeParameter(new String (typeVariableBinding.sourceName)); 408 if (typeParameter.exists()) { 409 addElement(typeParameter); 410 if(SelectionEngine.DEBUG){ 411 System.out.print("SELECTION - accept type parameter("); System.out.print(typeParameter.toString()); 413 System.out.println(")"); } 415 } 416 } 417 } 418 public void acceptLocalVariable(LocalVariableBinding binding) { 419 LocalDeclaration local = binding.declaration; 420 IJavaElement parent = findLocalElement(local.sourceStart); IJavaElement localVar = null; 422 if(parent != null) { 423 localVar = new LocalVariable( 424 (JavaElement)parent, 425 new String (local.name), 426 local.declarationSourceStart, 427 local.declarationSourceEnd, 428 local.sourceStart, 429 local.sourceEnd, 430 Util.typeSignature(local.type)); 431 } 432 if (localVar != null) { 433 addElement(localVar); 434 if(SelectionEngine.DEBUG){ 435 System.out.print("SELECTION - accept local variable("); System.out.print(localVar.toString()); 437 System.out.println(")"); } 439 } 440 } 441 444 public void acceptMethod( 445 char[] declaringTypePackageName, 446 char[] declaringTypeName, 447 String enclosingDeclaringTypeSignature, 448 char[] selector, 449 char[][] parameterPackageNames, 450 char[][] parameterTypeNames, 451 String [] parameterSignatures, 452 char[][] typeParameterNames, 453 char[][][] typeParameterBoundNames, 454 boolean isConstructor, 455 boolean isDeclaration, 456 char[] uniqueKey, 457 int start, 458 int end) { 459 IJavaElement[] previousElement = this.elements; 460 int previousElementIndex = this.elementIndex; 461 this.elements = JavaElement.NO_ELEMENTS; 462 this.elementIndex = -1; 463 464 if(isDeclaration) { 465 IType type = resolveTypeByLocation(declaringTypePackageName, declaringTypeName, 466 NameLookup.ACCEPT_ALL, 467 start, end); 468 469 if(type != null) { 470 this.acceptMethodDeclaration(type, selector, start, end); 471 } 472 } else { 473 IType type = resolveType(declaringTypePackageName, declaringTypeName, 474 NameLookup.ACCEPT_ALL); 475 if (type != null) { 477 if (type.isBinary()) { 478 479 IType declaringDeclaringType = type.getDeclaringType(); 481 482 boolean isStatic = false; 483 try { 484 isStatic = Flags.isStatic(type.getFlags()); 485 } catch (JavaModelException e) { 486 } 488 489 if(declaringDeclaringType != null && isConstructor && !isStatic) { 490 int length = parameterPackageNames.length; 491 System.arraycopy(parameterPackageNames, 0, parameterPackageNames = new char[length+1][], 1, length); 492 System.arraycopy(parameterTypeNames, 0, parameterTypeNames = new char[length+1][], 1, length); 493 System.arraycopy(parameterSignatures, 0, parameterSignatures = new String [length+1], 1, length); 494 495 parameterPackageNames[0] = declaringDeclaringType.getPackageFragment().getElementName().toCharArray(); 496 parameterTypeNames[0] = declaringDeclaringType.getTypeQualifiedName().toCharArray(); 497 parameterSignatures[0] = Signature.getTypeErasure(enclosingDeclaringTypeSignature); 498 } 499 500 acceptBinaryMethod(type, selector, parameterPackageNames, parameterTypeNames, parameterSignatures, typeParameterNames, typeParameterBoundNames, uniqueKey, isConstructor); 501 } else { 502 acceptSourceMethod(type, selector, parameterPackageNames, parameterTypeNames, parameterSignatures, typeParameterNames, typeParameterBoundNames, uniqueKey); 503 } 504 } 505 } 506 507 if(previousElementIndex > -1) { 508 int elementsLength = this.elementIndex + previousElementIndex + 2; 509 if(elementsLength > this.elements.length) { 510 System.arraycopy(this.elements, 0, this.elements = new IJavaElement[elementsLength * 2 + 1], 0, this.elementIndex + 1); 511 } 512 System.arraycopy(previousElement, 0, this.elements, this.elementIndex + 1, previousElementIndex + 1); 513 this.elementIndex += previousElementIndex + 1; 514 } 515 } 516 519 public void acceptPackage(char[] packageName) { 520 IPackageFragment[] pkgs = this.nameLookup.findPackageFragments(new String (packageName), false); 521 if (pkgs != null) { 522 for (int i = 0, length = pkgs.length; i < length; i++) { 523 addElement(pkgs[i]); 524 if(SelectionEngine.DEBUG){ 525 System.out.print("SELECTION - accept package("); System.out.print(pkgs[i].toString()); 527 System.out.println(")"); } 529 } 530 } 531 } 532 537 protected void acceptSourceMethod( 538 IType type, 539 char[] selector, 540 char[][] parameterPackageNames, 541 char[][] parameterTypeNames, 542 String [] parameterSignatures, 543 char[][] typeParameterNames, 544 char[][][] typeParameterBoundNames, 545 char[] uniqueKey) { 546 547 String name = new String (selector); 548 IMethod[] methods = null; 549 try { 550 methods = type.getMethods(); 551 for (int i = 0; i < methods.length; i++) { 552 if (methods[i].getElementName().equals(name) 553 && methods[i].getParameterTypes().length == parameterTypeNames.length) { 554 IMethod method = methods[i]; 555 if (uniqueKey != null) { 556 ResolvedSourceMethod resolvedMethod = new ResolvedSourceMethod( 557 (JavaElement)method.getParent(), 558 method.getElementName(), 559 method.getParameterTypes(), 560 new String (uniqueKey)); 561 resolvedMethod.occurrenceCount = method.getOccurrenceCount(); 562 method = resolvedMethod; 563 } 564 addElement(method); 565 } 566 } 567 } catch (JavaModelException e) { 568 return; 569 } 570 571 if (this.elementIndex == -1) { 573 addElement(type); 575 if(SelectionEngine.DEBUG){ 576 System.out.print("SELECTION - accept type("); System.out.print(type.toString()); 578 System.out.println(")"); } 580 return; 581 } 582 583 if (this.elementIndex == 0) { 585 if(SelectionEngine.DEBUG){ 586 System.out.print("SELECTION - accept method("); System.out.print(this.elements[0].toString()); 588 System.out.println(")"); } 590 return; 591 } 592 593 IJavaElement[] matches = this.elements; 595 int matchesIndex = this.elementIndex; 596 this.elements = JavaElement.NO_ELEMENTS; 597 this.elementIndex = -1; 598 for (int i = 0; i <= matchesIndex; i++) { 599 IMethod method= (IMethod)matches[i]; 600 String [] signatures = method.getParameterTypes(); 601 boolean match= true; 602 for (int p = 0; p < signatures.length; p++) { 603 String simpleName= Signature.getSimpleName(Signature.toString(Signature.getTypeErasure(signatures[p]))); 604 char[] simpleParameterName = CharOperation.lastSegment(parameterTypeNames[p], '.'); 605 if (!simpleName.equals(new String (simpleParameterName))) { 606 match = false; 607 break; 608 } 609 } 610 611 if (match && !areTypeParametersCompatible(method, typeParameterNames, typeParameterBoundNames)) { 612 match = false; 613 } 614 615 if (match) { 616 addElement(method); 617 if(SelectionEngine.DEBUG){ 618 System.out.print("SELECTION - accept method("); System.out.print(method.toString()); 620 System.out.println(")"); } 622 } 623 } 624 625 } 626 protected void acceptMethodDeclaration(IType type, char[] selector, int start, int end) { 627 String name = new String (selector); 628 IMethod[] methods = null; 629 try { 630 methods = type.getMethods(); 631 for (int i = 0; i < methods.length; i++) { 632 ISourceRange range = methods[i].getNameRange(); 633 if(range.getOffset() <= start 634 && range.getOffset() + range.getLength() >= end 635 && methods[i].getElementName().equals(name)) { 636 addElement(methods[i]); 637 if(SelectionEngine.DEBUG){ 638 System.out.print("SELECTION - accept method("); System.out.print(this.elements[0].toString()); 640 System.out.println(")"); } 642 return; } 644 } 645 } catch (JavaModelException e) { 646 return; 647 } 648 649 addElement(type); 651 if(SelectionEngine.DEBUG){ 652 System.out.print("SELECTION - accept type("); System.out.print(type.toString()); 654 System.out.println(")"); } 656 return; 657 } 658 public void acceptTypeParameter(char[] declaringTypePackageName, char[] declaringTypeName, char[] typeParameterName, boolean isDeclaration, int start, int end) { 659 IType type; 660 if(isDeclaration) { 661 type = resolveTypeByLocation(declaringTypePackageName, declaringTypeName, 662 NameLookup.ACCEPT_ALL, 663 start, end); 664 } else { 665 type = resolveType(declaringTypePackageName, declaringTypeName, 666 NameLookup.ACCEPT_ALL); 667 } 668 669 if(type != null) { 670 ITypeParameter typeParameter = type.getTypeParameter(new String (typeParameterName)); 671 if(typeParameter == null) { 672 addElement(type); 673 if(SelectionEngine.DEBUG){ 674 System.out.print("SELECTION - accept type("); System.out.print(type.toString()); 676 System.out.println(")"); } 678 } else { 679 addElement(typeParameter); 680 if(SelectionEngine.DEBUG){ 681 System.out.print("SELECTION - accept type parameter("); System.out.print(typeParameter.toString()); 683 System.out.println(")"); } 685 } 686 } 687 } 688 public void acceptMethodTypeParameter(char[] declaringTypePackageName, char[] declaringTypeName, char[] selector,int selectorStart, int selectorEnd, char[] typeParameterName, boolean isDeclaration, int start, int end) { 689 IType type = resolveTypeByLocation(declaringTypePackageName, declaringTypeName, 690 NameLookup.ACCEPT_ALL, 691 selectorStart, selectorEnd); 692 693 if(type != null) { 694 IMethod method = null; 695 696 String name = new String (selector); 697 IMethod[] methods = null; 698 699 try { 700 methods = type.getMethods(); 701 done : for (int i = 0; i < methods.length; i++) { 702 ISourceRange range = methods[i].getNameRange(); 703 if(range.getOffset() >= selectorStart 704 && range.getOffset() + range.getLength() <= selectorEnd 705 && methods[i].getElementName().equals(name)) { 706 method = methods[i]; 707 break done; 708 } 709 } 710 } catch (JavaModelException e) { 711 } 713 714 if(method == null) { 715 addElement(type); 716 if(SelectionEngine.DEBUG){ 717 System.out.print("SELECTION - accept type("); System.out.print(type.toString()); 719 System.out.println(")"); } 721 } else { 722 ITypeParameter typeParameter = method.getTypeParameter(new String (typeParameterName)); 723 if(typeParameter == null) { 724 addElement(method); 725 if(SelectionEngine.DEBUG){ 726 System.out.print("SELECTION - accept method("); System.out.print(method.toString()); 728 System.out.println(")"); } 730 } else { 731 addElement(typeParameter); 732 if(SelectionEngine.DEBUG){ 733 System.out.print("SELECTION - accept method type parameter("); System.out.print(typeParameter.toString()); 735 System.out.println(")"); } 737 } 738 } 739 } 740 } 741 744 protected void addElement(IJavaElement element) { 745 int elementLength = this.elementIndex + 1; 746 if (elementLength == this.elements.length) { 747 System.arraycopy(this.elements, 0, this.elements = new IJavaElement[(elementLength*2) + 1], 0, elementLength); 748 } 749 this.elements[++this.elementIndex] = element; 750 } 751 private boolean areTypeParametersCompatible(IMethod method, char[][] typeParameterNames, char[][][] typeParameterBoundNames) { 752 try { 753 ITypeParameter[] typeParameters = method.getTypeParameters(); 754 int length1 = typeParameters == null ? 0 : typeParameters.length; 755 int length2 = typeParameterNames == null ? 0 : typeParameterNames.length; 756 if (length1 != length2) { 757 return false; 758 } else { 759 for (int j = 0; j < length1; j++) { 760 ITypeParameter typeParameter = typeParameters[j]; 761 String typeParameterName = typeParameter.getElementName(); 762 if (!typeParameterName.equals(new String (typeParameterNames[j]))) { 763 return false; 764 } 765 766 String [] bounds = typeParameter.getBounds(); 767 int boundCount = typeParameterBoundNames[j] == null ? 0 : typeParameterBoundNames[j].length; 768 769 if (bounds.length != boundCount) { 770 return false; 771 } else { 772 for (int k = 0; k < boundCount; k++) { 773 String simpleName = Signature.getSimpleName(bounds[k]); 774 int index = simpleName.indexOf('<'); 775 if (index != -1) { 776 simpleName = simpleName.substring(0, index); 777 } 778 if (!simpleName.equals(new String (typeParameterBoundNames[j][k]))) { 779 return false; 780 } 781 } 782 } 783 } 784 } 785 } catch (JavaModelException e) { 786 return false; 787 } 788 return true; 789 } 790 793 protected IJavaElement findLocalElement(int pos) { 794 IJavaElement res = null; 795 if(this.openable instanceof ICompilationUnit) { 796 ICompilationUnit cu = (ICompilationUnit) this.openable; 797 try { 798 res = cu.getElementAt(pos); 799 } catch (JavaModelException e) { 800 } 802 } else if (this.openable instanceof ClassFile) { 803 ClassFile cf = (ClassFile) this.openable; 804 try { 805 res = cf.getElementAtConsideringSibling(pos); 806 } catch (JavaModelException e) { 807 } 809 } 810 return res; 811 } 812 815 public IJavaElement[] getElements() { 816 int elementLength = this.elementIndex + 1; 817 if (this.elements.length != elementLength) { 818 System.arraycopy(this.elements, 0, this.elements = new IJavaElement[elementLength], 0, elementLength); 819 } 820 return this.elements; 821 } 822 825 protected IType resolveType(char[] packageName, char[] typeName, int acceptFlags) { 826 827 IType type= null; 828 829 if (this.openable instanceof CompilationUnit && ((CompilationUnit)this.openable).isWorkingCopy()) { 830 CompilationUnit wc = (CompilationUnit) this.openable; 831 try { 832 if(((packageName == null || packageName.length == 0) && wc.getPackageDeclarations().length == 0) || 833 (!(packageName == null || packageName.length == 0) && wc.getPackageDeclaration(new String (packageName)).exists())) { 834 835 char[][] compoundName = CharOperation.splitOn('.', typeName); 836 if(compoundName.length > 0) { 837 type = wc.getType(new String (compoundName[0])); 838 for (int i = 1, length = compoundName.length; i < length; i++) { 839 type = type.getType(new String (compoundName[i])); 840 } 841 } 842 843 if(type != null && !type.exists()) { 844 type = null; 845 } 846 } 847 }catch (JavaModelException e) { 848 } 850 } 851 852 if(type == null) { 853 IPackageFragment[] pkgs = this.nameLookup.findPackageFragments( 854 (packageName == null || packageName.length == 0) ? IPackageFragment.DEFAULT_PACKAGE_NAME : new String (packageName), 855 false); 856 for (int i = 0, length = pkgs == null ? 0 : pkgs.length; i < length; i++) { 858 type= this.nameLookup.findType(new String (typeName), pkgs[i], false, acceptFlags, true); 859 if (type != null) break; 860 } 861 if (type == null) { 862 String pName= IPackageFragment.DEFAULT_PACKAGE_NAME; 863 if (packageName != null) { 864 pName = new String (packageName); 865 } 866 if (this.openable != null && this.openable.getParent().getElementName().equals(pName)) { 867 String tName= new String (typeName); 869 tName = tName.replace('.','$'); 870 IType[] allTypes= null; 871 try { 872 ArrayList list = this.openable.getChildrenOfType(IJavaElement.TYPE); 873 allTypes = new IType[list.size()]; 874 list.toArray(allTypes); 875 } catch (JavaModelException e) { 876 return null; 877 } 878 for (int i= 0; i < allTypes.length; i++) { 879 if (allTypes[i].getTypeQualifiedName().equals(tName)) { 880 return allTypes[i]; 881 } 882 } 883 } 884 } 885 } 886 return type; 887 } 888 protected IType resolveTypeByLocation(char[] packageName, char[] typeName, int acceptFlags, int start, int end) { 889 890 IType type= null; 891 892 if (this.openable instanceof CompilationUnit && ((CompilationUnit)this.openable).isOpen()) { 894 CompilationUnit wc = (CompilationUnit) this.openable; 895 try { 896 if(((packageName == null || packageName.length == 0) && wc.getPackageDeclarations().length == 0) || 897 (!(packageName == null || packageName.length == 0) && wc.getPackageDeclaration(new String (packageName)).exists())) { 898 899 char[][] compoundName = CharOperation.splitOn('.', typeName); 900 if(compoundName.length > 0) { 901 902 IType[] tTypes = wc.getTypes(); 903 int i = 0; 904 int depth = 0; 905 done : while(i < tTypes.length) { 906 ISourceRange range = tTypes[i].getSourceRange(); 907 if(range.getOffset() <= start 908 && range.getOffset() + range.getLength() >= end 909 && tTypes[i].getElementName().equals(new String (compoundName[depth]))) { 910 if(depth == compoundName.length - 1) { 911 type = tTypes[i]; 912 break done; 913 } 914 tTypes = tTypes[i].getTypes(); 915 i = 0; 916 depth++; 917 continue done; 918 } 919 i++; 920 } 921 } 922 923 if(type != null && !type.exists()) { 924 type = null; 925 } 926 } 927 }catch (JavaModelException e) { 928 } 930 } 931 932 if(type == null) { 933 IPackageFragment[] pkgs = this.nameLookup.findPackageFragments( 934 (packageName == null || packageName.length == 0) ? IPackageFragment.DEFAULT_PACKAGE_NAME : new String (packageName), 935 false); 936 for (int i = 0, length = pkgs == null ? 0 : pkgs.length; i < length; i++) { 938 type= this.nameLookup.findType(new String (typeName), pkgs[i], false, acceptFlags, true); 939 if (type != null) break; 940 } 941 if (type == null) { 942 String pName= IPackageFragment.DEFAULT_PACKAGE_NAME; 943 if (packageName != null) { 944 pName = new String (packageName); 945 } 946 if (this.openable != null && this.openable.getParent().getElementName().equals(pName)) { 947 String tName= new String (typeName); 949 tName = tName.replace('.','$'); 950 IType[] allTypes= null; 951 try { 952 ArrayList list = this.openable.getChildrenOfType(IJavaElement.TYPE); 953 allTypes = new IType[list.size()]; 954 list.toArray(allTypes); 955 } catch (JavaModelException e) { 956 return null; 957 } 958 for (int i= 0; i < allTypes.length; i++) { 959 if (allTypes[i].getTypeQualifiedName().equals(tName)) { 960 return allTypes[i]; 961 } 962 } 963 } 964 } 965 } 966 return type; 967 } 968 } 969 | Popular Tags |