1 11 package org.eclipse.jdt.internal.core; 12 13 import java.io.InputStream ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.jdt.core.*; 20 import org.eclipse.jdt.core.compiler.*; 21 import org.eclipse.jdt.core.search.SearchEngine; 22 import org.eclipse.jdt.internal.codeassist.CompletionEngine; 23 import org.eclipse.jdt.internal.codeassist.ISelectionRequestor; 24 import org.eclipse.jdt.internal.codeassist.SelectionEngine; 25 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; 26 import org.eclipse.jdt.internal.compiler.env.ISourceType; 27 import org.eclipse.jdt.internal.compiler.lookup.Binding; 28 import org.eclipse.jdt.internal.core.hierarchy.TypeHierarchy; 29 import org.eclipse.jdt.internal.core.util.MementoTokenizer; 30 import org.eclipse.jdt.internal.core.util.Messages; 31 32 39 40 public class SourceType extends NamedMember implements IType { 41 42 protected SourceType(JavaElement parent, String name) { 43 super(parent, name); 44 } 45 protected void closing(Object info) throws JavaModelException { 46 super.closing(info); 47 SourceTypeElementInfo elementInfo = (SourceTypeElementInfo) info; 48 ITypeParameter[] typeParameters = elementInfo.typeParameters; 49 for (int i = 0, length = typeParameters.length; i < length; i++) { 50 ((TypeParameter) typeParameters[i]).close(); 51 } 52 } 53 57 public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,ICompletionRequestor requestor) throws JavaModelException { 58 codeComplete(snippet, insertion, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic, requestor, DefaultWorkingCopyOwner.PRIMARY); 59 } 60 64 public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,ICompletionRequestor requestor, WorkingCopyOwner owner) throws JavaModelException { 65 if (requestor == null) { 66 throw new IllegalArgumentException ("Completion requestor cannot be null"); } 68 codeComplete(snippet, insertion, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic, new org.eclipse.jdt.internal.codeassist.CompletionRequestorWrapper(requestor), owner); 69 70 } 71 74 public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,CompletionRequestor requestor) throws JavaModelException { 75 codeComplete(snippet, insertion, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic, requestor, DefaultWorkingCopyOwner.PRIMARY); 76 } 77 80 public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,CompletionRequestor requestor, WorkingCopyOwner owner) throws JavaModelException { 81 if (requestor == null) { 82 throw new IllegalArgumentException ("Completion requestor cannot be null"); } 84 85 JavaProject project = (JavaProject) getJavaProject(); 86 SearchableEnvironment environment = project.newSearchableNameEnvironment(owner); 87 CompletionEngine engine = new CompletionEngine(environment, requestor, project.getOptions(true), project); 88 89 String source = getCompilationUnit().getSource(); 90 if (source != null && insertion > -1 && insertion < source.length()) { 91 92 char[] prefix = CharOperation.concat(source.substring(0, insertion).toCharArray(), new char[]{'{'}); 93 char[] suffix = CharOperation.concat(new char[]{'}'}, source.substring(insertion).toCharArray()); 94 char[] fakeSource = CharOperation.concat(prefix, snippet, suffix); 95 96 BasicCompilationUnit cu = 97 new BasicCompilationUnit( 98 fakeSource, 99 null, 100 getElementName(), 101 getParent()); 102 103 engine.complete(cu, prefix.length + position, prefix.length); 104 } else { 105 engine.complete(this, snippet, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic); 106 } 107 if (NameLookup.VERBOSE) { 108 System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); } 111 } 112 115 public IField createField(String contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException { 116 CreateFieldOperation op = new CreateFieldOperation(this, contents, force); 117 if (sibling != null) { 118 op.createBefore(sibling); 119 } 120 op.runOperation(monitor); 121 return (IField) op.getResultElements()[0]; 122 } 123 126 public IInitializer createInitializer(String contents, IJavaElement sibling, IProgressMonitor monitor) throws JavaModelException { 127 CreateInitializerOperation op = new CreateInitializerOperation(this, contents); 128 if (sibling != null) { 129 op.createBefore(sibling); 130 } 131 op.runOperation(monitor); 132 return (IInitializer) op.getResultElements()[0]; 133 } 134 137 public IMethod createMethod(String contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException { 138 CreateMethodOperation op = new CreateMethodOperation(this, contents, force); 139 if (sibling != null) { 140 op.createBefore(sibling); 141 } 142 op.runOperation(monitor); 143 return (IMethod) op.getResultElements()[0]; 144 } 145 148 public IType createType(String contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException { 149 CreateTypeOperation op = new CreateTypeOperation(this, contents, force); 150 if (sibling != null) { 151 op.createBefore(sibling); 152 } 153 op.runOperation(monitor); 154 return (IType) op.getResultElements()[0]; 155 } 156 public boolean equals(Object o) { 157 if (!(o instanceof SourceType)) return false; 158 return super.equals(o); 159 } 160 163 public IMethod[] findMethods(IMethod method) { 164 try { 165 return findMethods(method, getMethods()); 166 } catch (JavaModelException e) { 167 return null; 169 } 170 } 171 public IJavaElement[] getChildrenForCategory(String category) throws JavaModelException { 172 IJavaElement[] children = getChildren(); 173 int length = children.length; 174 if (length == 0) return NO_ELEMENTS; 175 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo(); 176 HashMap categories = info.getCategories(); 177 if (categories == null) return NO_ELEMENTS; 178 IJavaElement[] result = new IJavaElement[length]; 179 int index = 0; 180 for (int i = 0; i < length; i++) { 181 IJavaElement child = children[i]; 182 String [] elementCategories = (String []) categories.get(child); 183 if (elementCategories != null) 184 for (int j = 0, length2 = elementCategories.length; j < length2; j++) { 185 if (elementCategories[j].equals(category)) 186 result[index++] = child; 187 } 188 } 189 if (index == 0) return NO_ELEMENTS; 190 if (index < length) 191 System.arraycopy(result, 0, result = new IJavaElement[index], 0, index); 192 return result; 193 } 194 197 public IType getDeclaringType() { 198 IJavaElement parentElement = getParent(); 199 while (parentElement != null) { 200 if (parentElement.getElementType() == IJavaElement.TYPE) { 201 return (IType) parentElement; 202 } else 203 if (parentElement instanceof IMember) { 204 parentElement = parentElement.getParent(); 205 } else { 206 return null; 207 } 208 } 209 return null; 210 } 211 214 public int getElementType() { 215 return TYPE; 216 } 217 220 public IField getField(String fieldName) { 221 return new SourceField(this, fieldName); 222 } 223 226 public IField[] getFields() throws JavaModelException { 227 ArrayList list = getChildrenOfType(FIELD); 228 IField[] array= new IField[list.size()]; 229 list.toArray(array); 230 return array; 231 } 232 235 public String getFullyQualifiedName() { 236 return this.getFullyQualifiedName('$'); 237 } 238 241 public String getFullyQualifiedName(char enclosingTypeSeparator) { 242 try { 243 return getFullyQualifiedName(enclosingTypeSeparator, false); 244 } catch (JavaModelException e) { 245 return null; 247 } 248 } 249 252 public String getFullyQualifiedParameterizedName() throws JavaModelException { 253 return getFullyQualifiedName('.', true); 254 } 255 258 public IJavaElement getHandleFromMemento(String token, MementoTokenizer memento, WorkingCopyOwner workingCopyOwner) { 259 switch (token.charAt(0)) { 260 case JEM_COUNT: 261 return getHandleUpdatingCountFromMemento(memento, workingCopyOwner); 262 case JEM_FIELD: 263 if (!memento.hasMoreTokens()) return this; 264 String fieldName = memento.nextToken(); 265 JavaElement field = (JavaElement)getField(fieldName); 266 return field.getHandleFromMemento(memento, workingCopyOwner); 267 case JEM_INITIALIZER: 268 if (!memento.hasMoreTokens()) return this; 269 String count = memento.nextToken(); 270 JavaElement initializer = (JavaElement)getInitializer(Integer.parseInt(count)); 271 return initializer.getHandleFromMemento(memento, workingCopyOwner); 272 case JEM_METHOD: 273 if (!memento.hasMoreTokens()) return this; 274 String selector = memento.nextToken(); 275 ArrayList params = new ArrayList (); 276 nextParam: while (memento.hasMoreTokens()) { 277 token = memento.nextToken(); 278 switch (token.charAt(0)) { 279 case JEM_TYPE: 280 case JEM_TYPE_PARAMETER: 281 break nextParam; 282 case JEM_METHOD: 283 if (!memento.hasMoreTokens()) return this; 284 String param = memento.nextToken(); 285 StringBuffer buffer = new StringBuffer (); 286 while (param.length() == 1 && Signature.C_ARRAY == param.charAt(0)) { buffer.append(Signature.C_ARRAY); 288 if (!memento.hasMoreTokens()) return this; 289 param = memento.nextToken(); 290 } 291 params.add(buffer.toString() + param); 292 break; 293 default: 294 break nextParam; 295 } 296 } 297 String [] parameters = new String [params.size()]; 298 params.toArray(parameters); 299 JavaElement method = (JavaElement)getMethod(selector, parameters); 300 switch (token.charAt(0)) { 301 case JEM_TYPE: 302 case JEM_TYPE_PARAMETER: 303 case JEM_LOCALVARIABLE: 304 return method.getHandleFromMemento(token, memento, workingCopyOwner); 305 default: 306 return method; 307 } 308 case JEM_TYPE: 309 String typeName; 310 if (memento.hasMoreTokens()) { 311 typeName = memento.nextToken(); 312 char firstChar = typeName.charAt(0); 313 if (firstChar == JEM_FIELD || firstChar == JEM_INITIALIZER || firstChar == JEM_METHOD || firstChar == JEM_TYPE || firstChar == JEM_COUNT) { 314 token = typeName; 315 typeName = ""; } else { 317 token = null; 318 } 319 } else { 320 typeName = ""; token = null; 322 } 323 JavaElement type = (JavaElement)getType(typeName); 324 if (token == null) { 325 return type.getHandleFromMemento(memento, workingCopyOwner); 326 } else { 327 return type.getHandleFromMemento(token, memento, workingCopyOwner); 328 } 329 case JEM_TYPE_PARAMETER: 330 if (!memento.hasMoreTokens()) return this; 331 String typeParameterName = memento.nextToken(); 332 JavaElement typeParameter = new TypeParameter(this, typeParameterName); 333 return typeParameter.getHandleFromMemento(memento, workingCopyOwner); 334 335 } 336 return null; 337 } 338 341 public IInitializer getInitializer(int count) { 342 return new Initializer(this, count); 343 } 344 347 public IInitializer[] getInitializers() throws JavaModelException { 348 ArrayList list = getChildrenOfType(INITIALIZER); 349 IInitializer[] array= new IInitializer[list.size()]; 350 list.toArray(array); 351 return array; 352 } 353 356 public String getKey() { 357 try { 358 return getKey(this, false); 359 } catch (JavaModelException e) { 360 return null; 362 } 363 } 364 367 public IMethod getMethod(String selector, String [] parameterTypeSignatures) { 368 return new SourceMethod(this, selector, parameterTypeSignatures); 369 } 370 373 public IMethod[] getMethods() throws JavaModelException { 374 ArrayList list = getChildrenOfType(METHOD); 375 IMethod[] array= new IMethod[list.size()]; 376 list.toArray(array); 377 return array; 378 } 379 382 public IPackageFragment getPackageFragment() { 383 IJavaElement parentElement = this.parent; 384 while (parentElement != null) { 385 if (parentElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) { 386 return (IPackageFragment)parentElement; 387 } 388 else { 389 parentElement = parentElement.getParent(); 390 } 391 } 392 Assert.isTrue(false); return null; 394 } 395 398 public IJavaElement getPrimaryElement(boolean checkOwner) { 399 if (checkOwner) { 400 CompilationUnit cu = (CompilationUnit)getAncestor(COMPILATION_UNIT); 401 if (cu.isPrimary()) return this; 402 } 403 IJavaElement primaryParent = this.parent.getPrimaryElement(false); 404 switch (primaryParent.getElementType()) { 405 case IJavaElement.COMPILATION_UNIT: 406 return ((ICompilationUnit)primaryParent).getType(this.name); 407 case IJavaElement.TYPE: 408 return ((IType)primaryParent).getType(this.name); 409 case IJavaElement.FIELD: 410 case IJavaElement.INITIALIZER: 411 case IJavaElement.METHOD: 412 return ((IMember)primaryParent).getType(this.name, this.occurrenceCount); 413 } 414 return this; 415 } 416 419 public String getSuperclassName() throws JavaModelException { 420 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo(); 421 char[] superclassName= info.getSuperclassName(); 422 if (superclassName == null) { 423 return null; 424 } 425 return new String (superclassName); 426 } 427 428 432 public String getSuperclassTypeSignature() throws JavaModelException { 433 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo(); 434 char[] superclassName= info.getSuperclassName(); 435 if (superclassName == null) { 436 return null; 437 } 438 return new String (Signature.createTypeSignature(superclassName, false)); 439 } 440 441 444 public String [] getSuperInterfaceNames() throws JavaModelException { 445 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo(); 446 char[][] names= info.getInterfaceNames(); 447 return CharOperation.toStrings(names); 448 } 449 450 454 public String [] getSuperInterfaceTypeSignatures() throws JavaModelException { 455 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo(); 456 char[][] names= info.getInterfaceNames(); 457 if (names == null) { 458 return CharOperation.NO_STRINGS; 459 } 460 String [] strings= new String [names.length]; 461 for (int i= 0; i < names.length; i++) { 462 strings[i]= new String (Signature.createTypeSignature(names[i], false)); 463 } 464 return strings; 465 } 466 467 public ITypeParameter[] getTypeParameters() throws JavaModelException { 468 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo(); 469 return info.typeParameters; 470 } 471 472 476 public String [] getTypeParameterSignatures() throws JavaModelException { 477 ITypeParameter[] typeParameters = getTypeParameters(); 478 int length = typeParameters.length; 479 String [] typeParameterSignatures = new String [length]; 480 for (int i = 0; i < length; i++) { 481 TypeParameter typeParameter = (TypeParameter) typeParameters[i]; 482 TypeParameterElementInfo info = (TypeParameterElementInfo) typeParameter.getElementInfo(); 483 char[][] bounds = info.bounds; 484 if (bounds == null) { 485 typeParameterSignatures[i] = Signature.createTypeParameterSignature(typeParameter.getElementName(), CharOperation.NO_STRINGS); 486 } else { 487 int boundsLength = bounds.length; 488 char[][] boundSignatures = new char[boundsLength][]; 489 for (int j = 0; j < boundsLength; j++) { 490 boundSignatures[j] = Signature.createCharArrayTypeSignature(bounds[j], false); 491 } 492 typeParameterSignatures[i] = new String (Signature.createTypeParameterSignature(typeParameter.getElementName().toCharArray(), boundSignatures)); 493 } 494 } 495 return typeParameterSignatures; 496 } 497 498 501 public IType getType(String typeName) { 502 return new SourceType(this, typeName); 503 } 504 public ITypeParameter getTypeParameter(String typeParameterName) { 505 return new TypeParameter(this, typeParameterName); 506 } 507 510 public String getTypeQualifiedName() { 511 return this.getTypeQualifiedName('$'); 512 } 513 516 public String getTypeQualifiedName(char enclosingTypeSeparator) { 517 try { 518 return getTypeQualifiedName(enclosingTypeSeparator, false); 519 } catch (JavaModelException e) { 520 return null; 522 } 523 } 524 525 528 public IType[] getTypes() throws JavaModelException { 529 ArrayList list= getChildrenOfType(TYPE); 530 IType[] array= new IType[list.size()]; 531 list.toArray(array); 532 return array; 533 } 534 537 public boolean isAnonymous() { 538 return this.name.length() == 0; 539 } 540 541 544 public boolean isClass() throws JavaModelException { 545 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo(); 546 return TypeDeclaration.kind(info.getModifiers()) == TypeDeclaration.CLASS_DECL; 547 } 548 549 553 public boolean isEnum() throws JavaModelException { 554 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo(); 555 return TypeDeclaration.kind(info.getModifiers()) == TypeDeclaration.ENUM_DECL; 556 } 557 558 561 public boolean isInterface() throws JavaModelException { 562 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo(); 563 switch (TypeDeclaration.kind(info.getModifiers())) { 564 case TypeDeclaration.INTERFACE_DECL: 565 case TypeDeclaration.ANNOTATION_TYPE_DECL: return true; 567 } 568 return false; 569 } 570 571 575 public boolean isAnnotation() throws JavaModelException { 576 SourceTypeElementInfo info = (SourceTypeElementInfo) getElementInfo(); 577 return TypeDeclaration.kind(info.getModifiers()) == TypeDeclaration.ANNOTATION_TYPE_DECL; 578 } 579 580 583 public boolean isLocal() { 584 switch (this.parent.getElementType()) { 585 case IJavaElement.METHOD: 586 case IJavaElement.INITIALIZER: 587 case IJavaElement.FIELD: 588 return true; 589 default: 590 return false; 591 } 592 } 593 596 public boolean isMember() { 597 return getDeclaringType() != null; 598 } 599 602 public boolean isResolved() { 603 return false; 604 } 605 608 public ITypeHierarchy loadTypeHierachy(InputStream input, IProgressMonitor monitor) throws JavaModelException { 609 return loadTypeHierachy(input, DefaultWorkingCopyOwner.PRIMARY, monitor); 610 } 611 641 public ITypeHierarchy loadTypeHierachy(InputStream input, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException { 642 return TypeHierarchy.load(this, input, owner); 644 } 645 648 public ITypeHierarchy newSupertypeHierarchy(IProgressMonitor monitor) throws JavaModelException { 649 return this.newSupertypeHierarchy(DefaultWorkingCopyOwner.PRIMARY, monitor); 650 } 651 654 public ITypeHierarchy newSupertypeHierarchy( 655 ICompilationUnit[] workingCopies, 656 IProgressMonitor monitor) 657 throws JavaModelException { 658 659 CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), false); 660 op.runOperation(monitor); 661 return op.getResult(); 662 } 663 673 public ITypeHierarchy newSupertypeHierarchy( 674 IWorkingCopy[] workingCopies, 675 IProgressMonitor monitor) 676 throws JavaModelException { 677 678 ICompilationUnit[] copies; 679 if (workingCopies == null) { 680 copies = null; 681 } else { 682 int length = workingCopies.length; 683 System.arraycopy(workingCopies, 0, copies = new ICompilationUnit[length], 0, length); 684 } 685 return newSupertypeHierarchy(copies, monitor); 686 } 687 690 public ITypeHierarchy newSupertypeHierarchy( 691 WorkingCopyOwner owner, 692 IProgressMonitor monitor) 693 throws JavaModelException { 694 695 ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true); 696 CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), false); 697 op.runOperation(monitor); 698 return op.getResult(); 699 } 700 703 public ITypeHierarchy newTypeHierarchy(IJavaProject project, IProgressMonitor monitor) throws JavaModelException { 704 return newTypeHierarchy(project, DefaultWorkingCopyOwner.PRIMARY, monitor); 705 } 706 709 public ITypeHierarchy newTypeHierarchy(IJavaProject project, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException { 710 if (project == null) { 711 throw new IllegalArgumentException (Messages.hierarchy_nullProject); 712 } 713 ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true); 714 ICompilationUnit[] projectWCs = null; 715 if (workingCopies != null) { 716 int length = workingCopies.length; 717 projectWCs = new ICompilationUnit[length]; 718 int index = 0; 719 for (int i = 0; i < length; i++) { 720 ICompilationUnit wc = workingCopies[i]; 721 if (project.equals(wc.getJavaProject())) { 722 projectWCs[index++] = wc; 723 } 724 } 725 if (index != length) { 726 System.arraycopy(projectWCs, 0, projectWCs = new ICompilationUnit[index], 0, index); 727 } 728 } 729 CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation( 730 this, 731 projectWCs, 732 project, 733 true); 734 op.runOperation(monitor); 735 return op.getResult(); 736 } 737 740 public ITypeHierarchy newTypeHierarchy(IProgressMonitor monitor) throws JavaModelException { 741 CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, null, SearchEngine.createWorkspaceScope(), true); 742 op.runOperation(monitor); 743 return op.getResult(); 744 } 745 748 public ITypeHierarchy newTypeHierarchy( 749 ICompilationUnit[] workingCopies, 750 IProgressMonitor monitor) 751 throws JavaModelException { 752 753 CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), true); 754 op.runOperation(monitor); 755 return op.getResult(); 756 } 757 761 public ITypeHierarchy newTypeHierarchy( 762 IWorkingCopy[] workingCopies, 763 IProgressMonitor monitor) 764 throws JavaModelException { 765 766 ICompilationUnit[] copies; 767 if (workingCopies == null) { 768 copies = null; 769 } else { 770 int length = workingCopies.length; 771 System.arraycopy(workingCopies, 0, copies = new ICompilationUnit[length], 0, length); 772 } 773 return newTypeHierarchy(copies, monitor); 774 } 775 778 public ITypeHierarchy newTypeHierarchy( 779 WorkingCopyOwner owner, 780 IProgressMonitor monitor) 781 throws JavaModelException { 782 783 ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true); 784 CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), true); 785 op.runOperation(monitor); 786 return op.getResult(); 787 } 788 public JavaElement resolved(Binding binding) { 789 SourceRefElement resolvedHandle = new ResolvedSourceType(this.parent, this.name, new String (binding.computeUniqueKey())); 790 resolvedHandle.occurrenceCount = this.occurrenceCount; 791 return resolvedHandle; 792 } 793 796 public String [][] resolveType(String typeName) throws JavaModelException { 797 return resolveType(typeName, DefaultWorkingCopyOwner.PRIMARY); 798 } 799 802 public String [][] resolveType(String typeName, WorkingCopyOwner owner) throws JavaModelException { 803 ISourceType info = (ISourceType) getElementInfo(); 804 JavaProject project = (JavaProject) getJavaProject(); 805 SearchableEnvironment environment = project.newSearchableNameEnvironment(owner); 806 807 class TypeResolveRequestor implements ISelectionRequestor { 808 String [][] answers = null; 809 public void acceptType(char[] packageName, char[] tName, int modifiers, boolean isDeclaration, char[] uniqueKey, int start, int end) { 810 String [] answer = new String [] {new String (packageName), new String (tName) }; 811 if (this.answers == null) { 812 this.answers = new String [][]{ answer }; 813 } else { 814 int length = this.answers.length; 816 System.arraycopy(this.answers, 0, this.answers = new String [length+1][], 0, length); 817 this.answers[length] = answer; 818 } 819 } 820 public void acceptError(CategorizedProblem error) { 821 } 823 public void acceptField(char[] declaringTypePackageName, char[] declaringTypeName, char[] fieldName, boolean isDeclaration, char[] uniqueKey, int start, int end) { 824 } 826 public void acceptMethod(char[] declaringTypePackageName, char[] declaringTypeName, String enclosingDeclaringTypeSignature, char[] selector, char[][] parameterPackageNames, char[][] parameterTypeNames, String [] parameterSignatures, char[][] typeParameterNames, char[][][] typeParameterBoundNames, boolean isConstructor, boolean isDeclaration, char[] uniqueKey, int start, int end) { 827 } 829 public void acceptPackage(char[] packageName){ 830 } 832 public void acceptTypeParameter(char[] declaringTypePackageName, char[] declaringTypeName, char[] typeParameterName, boolean isDeclaration, int start, int end) { 833 } 835 public void acceptMethodTypeParameter(char[] declaringTypePackageName, char[] declaringTypeName, char[] selector, int selectorStart, int selcetorEnd, char[] typeParameterName, boolean isDeclaration, int start, int end) { 836 } 838 839 } 840 TypeResolveRequestor requestor = new TypeResolveRequestor(); 841 SelectionEngine engine = 842 new SelectionEngine(environment, requestor, project.getOptions(true)); 843 844 IType[] topLevelTypes = getCompilationUnit().getTypes(); 845 int length = topLevelTypes.length; 846 SourceTypeElementInfo[] topLevelInfos = new SourceTypeElementInfo[length]; 847 for (int i = 0; i < length; i++) { 848 topLevelInfos[i] = (SourceTypeElementInfo) ((SourceType)topLevelTypes[i]).getElementInfo(); 849 } 850 851 engine.selectType(info, typeName.toCharArray(), topLevelInfos, false); 852 if (NameLookup.VERBOSE) { 853 System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); } 856 return requestor.answers; 857 } 858 861 protected void toStringInfo(int tab, StringBuffer buffer, Object info, boolean showResolvedInfo) { 862 buffer.append(tabString(tab)); 863 if (info == null) { 864 String elementName = getElementName(); 865 if (elementName.length() == 0) { 866 buffer.append("<anonymous #"); buffer.append(this.occurrenceCount); 868 buffer.append(">"); } else { 870 toStringName(buffer); 871 } 872 buffer.append(" (not open)"); } else if (info == NO_INFO) { 874 String elementName = getElementName(); 875 if (elementName.length() == 0) { 876 buffer.append("<anonymous #"); buffer.append(this.occurrenceCount); 878 buffer.append(">"); } else { 880 toStringName(buffer); 881 } 882 } else { 883 try { 884 if (this.isEnum()) { 885 buffer.append("enum "); } else if (this.isAnnotation()) { 887 buffer.append("@interface "); } else if (this.isInterface()) { 889 buffer.append("interface "); } else { 891 buffer.append("class "); } 893 String elementName = getElementName(); 894 if (elementName.length() == 0) { 895 buffer.append("<anonymous #"); buffer.append(this.occurrenceCount); 897 buffer.append(">"); } else { 899 toStringName(buffer); 900 } 901 } catch (JavaModelException e) { 902 buffer.append("<JavaModelException in toString of " + getElementName()); } 904 } 905 } 906 } 907 | Popular Tags |