1 11 package org.eclipse.jdt.internal.core; 12 13 import java.io.InputStream ; 14 import java.net.URL ; 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.OperationCanceledException; 21 import org.eclipse.jdt.core.*; 22 import org.eclipse.jdt.core.compiler.CharOperation; 23 import org.eclipse.jdt.core.search.SearchEngine; 24 import org.eclipse.jdt.internal.codeassist.CompletionEngine; 25 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; 26 import org.eclipse.jdt.internal.compiler.env.IBinaryType; 27 import org.eclipse.jdt.internal.compiler.lookup.Binding; 28 import org.eclipse.jdt.internal.compiler.util.SuffixConstants; 29 import org.eclipse.jdt.internal.core.JavaModelManager.PerProjectInfo; 30 import org.eclipse.jdt.internal.core.hierarchy.TypeHierarchy; 31 import org.eclipse.jdt.internal.core.util.MementoTokenizer; 32 import org.eclipse.jdt.internal.core.util.Messages; 33 import org.eclipse.jdt.internal.core.util.Util; 34 35 40 41 public class BinaryType extends BinaryMember implements IType, SuffixConstants { 42 43 private static final IField[] NO_FIELDS = new IField[0]; 44 private static final IMethod[] NO_METHODS = new IMethod[0]; 45 private static final IType[] NO_TYPES = new IType[0]; 46 private static final IInitializer[] NO_INITIALIZERS = new IInitializer[0]; 47 public static final String EMPTY_JAVADOC = org.eclipse.jdt.internal.compiler.util.Util.EMPTY_STRING; 48 49 protected BinaryType(JavaElement parent, String name) { 50 super(parent, name); 51 } 52 55 protected void closing(Object info) throws JavaModelException { 56 ClassFileInfo cfi = getClassFileInfo(); 57 cfi.removeBinaryChildren(); 58 } 59 60 64 public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,ICompletionRequestor requestor) throws JavaModelException { 65 codeComplete(snippet, insertion, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic, requestor, DefaultWorkingCopyOwner.PRIMARY); 66 } 67 68 72 public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,ICompletionRequestor requestor, WorkingCopyOwner owner) throws JavaModelException { 73 if (requestor == null) { 74 throw new IllegalArgumentException ("Completion requestor cannot be null"); } 76 codeComplete(snippet, insertion, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic, new org.eclipse.jdt.internal.codeassist.CompletionRequestorWrapper(requestor), owner); 77 } 78 81 public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,CompletionRequestor requestor) throws JavaModelException { 82 codeComplete(snippet, insertion, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic, requestor, DefaultWorkingCopyOwner.PRIMARY); 83 } 84 85 88 public void codeComplete(char[] snippet,int insertion,int position,char[][] localVariableTypeNames,char[][] localVariableNames,int[] localVariableModifiers,boolean isStatic,CompletionRequestor requestor, WorkingCopyOwner owner) throws JavaModelException { 89 if (requestor == null) { 90 throw new IllegalArgumentException ("Completion requestor cannot be null"); } 92 JavaProject project = (JavaProject) getJavaProject(); 93 SearchableEnvironment environment = project.newSearchableNameEnvironment(owner); 94 CompletionEngine engine = new CompletionEngine(environment, requestor, project.getOptions(true), project); 95 96 String source = getClassFile().getSource(); 97 if (source != null && insertion > -1 && insertion < source.length()) { 98 100 char[] prefix = CharOperation.concat(source.substring(0, insertion).toCharArray(), new char[]{'{'}); 101 char[] suffix = CharOperation.concat(new char[]{'}'}, source.substring(insertion).toCharArray()); 102 char[] fakeSource = CharOperation.concat(prefix, snippet, suffix); 103 104 BasicCompilationUnit cu = 105 new BasicCompilationUnit( 106 fakeSource, 107 null, 108 getElementName(), 109 project); 111 engine.complete(cu, prefix.length + position, prefix.length); 112 } else { 113 engine.complete(this, snippet, position, localVariableTypeNames, localVariableNames, localVariableModifiers, isStatic); 114 } 115 if (NameLookup.VERBOSE) { 116 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"); } 119 } 120 121 124 public IField createField(String contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException { 125 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.READ_ONLY, this)); 126 } 127 130 public IInitializer createInitializer(String contents, IJavaElement sibling, IProgressMonitor monitor) throws JavaModelException { 131 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.READ_ONLY, this)); 132 } 133 136 public IMethod createMethod(String contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException { 137 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.READ_ONLY, this)); 138 } 139 142 public IType createType(String contents, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws JavaModelException { 143 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.READ_ONLY, this)); 144 } 145 public boolean equals(Object o) { 146 if (!(o instanceof BinaryType)) return false; 147 return super.equals(o); 148 } 149 152 public IMethod[] findMethods(IMethod method) { 153 try { 154 return findMethods(method, getMethods()); 155 } catch (JavaModelException e) { 156 return null; 158 } 159 } 160 163 public IJavaElement[] getChildren() throws JavaModelException { 164 ClassFileInfo cfi = getClassFileInfo(); 165 return cfi.binaryChildren; 166 } 167 public IJavaElement[] getChildrenForCategory(String category) throws JavaModelException { 168 IJavaElement[] children = getChildren(); 169 int length = children.length; 170 if (length == 0) return children; 171 SourceMapper mapper= getSourceMapper(); 172 if (mapper != null) { 173 ((ClassFile)getClassFile()).getBuffer(); 175 176 HashMap categories = mapper.categories; 177 IJavaElement[] result = new IJavaElement[length]; 178 int index = 0; 179 if (categories != null) { 180 for (int i = 0; i < length; i++) { 181 IJavaElement child = children[i]; 182 String [] cats = (String []) categories.get(child); 183 if (cats != null) { 184 for (int j = 0, length2 = cats.length; j < length2; j++) { 185 if (cats[j].equals(category)) { 186 result[index++] = child; 187 break; 188 } 189 } 190 } 191 } 192 } 193 if (index < length) 194 System.arraycopy(result, 0, result = new IJavaElement[index], 0, index); 195 return result; 196 } 197 return NO_ELEMENTS; 198 } 199 protected ClassFileInfo getClassFileInfo() throws JavaModelException { 200 ClassFile cf = (ClassFile)this.parent; 201 return (ClassFileInfo) cf.getElementInfo(); 202 } 203 206 public IType getDeclaringType() { 207 IClassFile classFile = this.getClassFile(); 208 if (classFile.isOpen()) { 209 try { 210 char[] enclosingTypeName = ((IBinaryType) getElementInfo()).getEnclosingTypeName(); 211 if (enclosingTypeName == null) { 212 return null; 213 } 214 enclosingTypeName = ClassFile.unqualifiedName(enclosingTypeName); 215 216 if (classFile.getElementName().length() > enclosingTypeName.length+1 219 && Character.isDigit(classFile.getElementName().charAt(enclosingTypeName.length+1))) { 220 return null; 221 } 222 223 return getPackageFragment().getClassFile(new String (enclosingTypeName) + SUFFIX_STRING_class).getType(); 224 } catch (JavaModelException npe) { 225 return null; 226 } 227 } else { 228 String classFileName = classFile.getElementName(); 232 int lastDollar = -1; 233 for (int i = 0, length = classFileName.length(); i < length; i++) { 234 char c = classFileName.charAt(i); 235 if (Character.isDigit(c) && lastDollar == i-1) { 236 return null; 238 } else if (c == '$') { 239 lastDollar = i; 240 } 241 } 242 if (lastDollar == -1) { 243 return null; 244 } else { 245 String enclosingName = classFileName.substring(0, lastDollar); 246 String enclosingClassFileName = enclosingName + SUFFIX_STRING_class; 247 return 248 new BinaryType( 249 (JavaElement)this.getPackageFragment().getClassFile(enclosingClassFileName), 250 Util.localTypeName(enclosingName, enclosingName.lastIndexOf('$'), enclosingName.length())); 251 } 252 } 253 } 254 public Object getElementInfo(IProgressMonitor monitor) throws JavaModelException { 255 JavaModelManager manager = JavaModelManager.getJavaModelManager(); 256 Object info = manager.getInfo(this); 257 if (info != null && info != JavaModelCache.NON_EXISTING_JAR_TYPE_INFO) return info; 258 return openWhenClosed(createElementInfo(), monitor); 259 } 260 263 public int getElementType() { 264 return TYPE; 265 } 266 269 public IField getField(String fieldName) { 270 return new BinaryField(this, fieldName); 271 } 272 275 public IField[] getFields() throws JavaModelException { 276 ArrayList list = getChildrenOfType(FIELD); 277 int size; 278 if ((size = list.size()) == 0) { 279 return NO_FIELDS; 280 } else { 281 IField[] array= new IField[size]; 282 list.toArray(array); 283 return array; 284 } 285 } 286 289 public int getFlags() throws JavaModelException { 290 IBinaryType info = (IBinaryType) getElementInfo(); 291 return info.getModifiers(); 292 } 293 296 public String getFullyQualifiedName() { 297 return this.getFullyQualifiedName('$'); 298 } 299 302 public String getFullyQualifiedName(char enclosingTypeSeparator) { 303 try { 304 return getFullyQualifiedName(enclosingTypeSeparator, false); 305 } catch (JavaModelException e) { 306 return null; 308 } 309 } 310 311 314 public String getFullyQualifiedParameterizedName() throws JavaModelException { 315 return getFullyQualifiedName('.', true); 316 } 317 318 321 public IJavaElement getHandleFromMemento(String token, MementoTokenizer memento, WorkingCopyOwner workingCopyOwner) { 322 switch (token.charAt(0)) { 323 case JEM_COUNT: 324 return getHandleUpdatingCountFromMemento(memento, workingCopyOwner); 325 case JEM_FIELD: 326 if (!memento.hasMoreTokens()) return this; 327 String fieldName = memento.nextToken(); 328 JavaElement field = (JavaElement)getField(fieldName); 329 return field.getHandleFromMemento(memento, workingCopyOwner); 330 case JEM_INITIALIZER: 331 if (!memento.hasMoreTokens()) return this; 332 String count = memento.nextToken(); 333 JavaElement initializer = (JavaElement)getInitializer(Integer.parseInt(count)); 334 return initializer.getHandleFromMemento(memento, workingCopyOwner); 335 case JEM_METHOD: 336 if (!memento.hasMoreTokens()) return this; 337 String selector = memento.nextToken(); 338 ArrayList params = new ArrayList (); 339 nextParam: while (memento.hasMoreTokens()) { 340 token = memento.nextToken(); 341 switch (token.charAt(0)) { 342 case JEM_TYPE: 343 case JEM_TYPE_PARAMETER: 344 break nextParam; 345 case JEM_METHOD: 346 if (!memento.hasMoreTokens()) return this; 347 String param = memento.nextToken(); 348 StringBuffer buffer = new StringBuffer (); 349 while (param.length() == 1 && Signature.C_ARRAY == param.charAt(0)) { buffer.append(Signature.C_ARRAY); 351 if (!memento.hasMoreTokens()) return this; 352 param = memento.nextToken(); 353 } 354 params.add(buffer.toString() + param); 355 break; 356 default: 357 break nextParam; 358 } 359 } 360 String [] parameters = new String [params.size()]; 361 params.toArray(parameters); 362 JavaElement method = (JavaElement)getMethod(selector, parameters); 363 switch (token.charAt(0)) { 364 case JEM_TYPE: 365 case JEM_TYPE_PARAMETER: 366 case JEM_LOCALVARIABLE: 367 return method.getHandleFromMemento(token, memento, workingCopyOwner); 368 default: 369 return method; 370 } 371 case JEM_TYPE: 372 String typeName; 373 if (memento.hasMoreTokens()) { 374 typeName = memento.nextToken(); 375 char firstChar = typeName.charAt(0); 376 if (firstChar == JEM_FIELD || firstChar == JEM_INITIALIZER || firstChar == JEM_METHOD || firstChar == JEM_TYPE || firstChar == JEM_COUNT) { 377 token = typeName; 378 typeName = ""; } else { 380 token = null; 381 } 382 } else { 383 typeName = ""; token = null; 385 } 386 JavaElement type = (JavaElement)getType(typeName); 387 if (token == null) { 388 return type.getHandleFromMemento(memento, workingCopyOwner); 389 } else { 390 return type.getHandleFromMemento(token, memento, workingCopyOwner); 391 } 392 case JEM_TYPE_PARAMETER: 393 if (!memento.hasMoreTokens()) return this; 394 String typeParameterName = memento.nextToken(); 395 JavaElement typeParameter = new TypeParameter(this, typeParameterName); 396 return typeParameter.getHandleFromMemento(memento, workingCopyOwner); 397 } 398 return null; 399 } 400 403 public IInitializer getInitializer(int count) { 404 return new Initializer(this, count); 405 } 406 409 public IInitializer[] getInitializers() { 410 return NO_INITIALIZERS; 411 } 412 public String getKey(boolean forceOpen) throws JavaModelException { 413 return getKey(this, forceOpen); 414 } 415 418 public IMethod getMethod(String selector, String [] parameterTypeSignatures) { 419 return new BinaryMethod(this, selector, parameterTypeSignatures); 420 } 421 424 public IMethod[] getMethods() throws JavaModelException { 425 ArrayList list = getChildrenOfType(METHOD); 426 int size; 427 if ((size = list.size()) == 0) { 428 return NO_METHODS; 429 } else { 430 IMethod[] array= new IMethod[size]; 431 list.toArray(array); 432 return array; 433 } 434 } 435 438 public IPackageFragment getPackageFragment() { 439 IJavaElement parentElement = this.parent; 440 while (parentElement != null) { 441 if (parentElement.getElementType() == IJavaElement.PACKAGE_FRAGMENT) { 442 return (IPackageFragment)parentElement; 443 } 444 else { 445 parentElement = parentElement.getParent(); 446 } 447 } 448 Assert.isTrue(false); return null; 450 } 451 452 456 public String getSuperclassTypeSignature() throws JavaModelException { 457 IBinaryType info = (IBinaryType) getElementInfo(); 458 char[] genericSignature = info.getGenericSignature(); 459 if (genericSignature != null) { 460 int signatureLength = genericSignature.length; 461 int index = 0; 463 if (genericSignature[0] == '<') { 464 int count = 1; 465 while (count > 0 && ++index < signatureLength) { 466 switch (genericSignature[index]) { 467 case '<': 468 count++; 469 break; 470 case '>': 471 count--; 472 break; 473 } 474 } 475 index++; 476 } 477 int start = index; 478 index = Util.scanClassTypeSignature(genericSignature, start) + 1; 479 char[] superclassSig = CharOperation.subarray(genericSignature, start, index); 480 return new String (ClassFile.translatedName(superclassSig)); 481 } else { 482 char[] superclassName = info.getSuperclassName(); 483 if (superclassName == null) { 484 return null; 485 } 486 return new String (Signature.createTypeSignature(ClassFile.translatedName(superclassName), true)); 487 } 488 } 489 490 public String getSourceFileName(IBinaryType info) { 491 if (info == null) { 492 try { 493 info = (IBinaryType) getElementInfo(); 494 } catch (JavaModelException e) { 495 IType type = this; 497 IType enclosingType = getDeclaringType(); 498 while (enclosingType != null) { 499 type = enclosingType; 500 enclosingType = type.getDeclaringType(); 501 } 502 return type.getElementName() + Util.defaultJavaExtension(); 503 } 504 } 505 return sourceFileName(info); 506 } 507 508 511 public String getSuperclassName() throws JavaModelException { 512 IBinaryType info = (IBinaryType) getElementInfo(); 513 char[] superclassName = info.getSuperclassName(); 514 if (superclassName == null) { 515 return null; 516 } 517 return new String (ClassFile.translatedName(superclassName)); 518 } 519 522 public String [] getSuperInterfaceNames() throws JavaModelException { 523 IBinaryType info = (IBinaryType) getElementInfo(); 524 char[][] names= info.getInterfaceNames(); 525 int length; 526 if (names == null || (length = names.length) == 0) { 527 return CharOperation.NO_STRINGS; 528 } 529 names= ClassFile.translatedNames(names); 530 String [] strings= new String [length]; 531 for (int i= 0; i < length; i++) { 532 strings[i]= new String (names[i]); 533 } 534 return strings; 535 } 536 537 541 public String [] getSuperInterfaceTypeSignatures() throws JavaModelException { 542 IBinaryType info = (IBinaryType) getElementInfo(); 543 char[] genericSignature = info.getGenericSignature(); 544 if (genericSignature != null) { 545 ArrayList interfaces = new ArrayList (); 546 int signatureLength = genericSignature.length; 547 int index = 0; 549 if (genericSignature[0] == '<') { 550 int count = 1; 551 while (count > 0 && ++index < signatureLength) { 552 switch (genericSignature[index]) { 553 case '<': 554 count++; 555 break; 556 case '>': 557 count--; 558 break; 559 } 560 } 561 index++; 562 } 563 index = Util.scanClassTypeSignature(genericSignature, index) + 1; 565 while (index < signatureLength) { 566 int start = index; 567 index = Util.scanClassTypeSignature(genericSignature, start) + 1; 568 char[] interfaceSig = CharOperation.subarray(genericSignature, start, index); 569 interfaces.add(new String (ClassFile.translatedName(interfaceSig))); 570 } 571 int size = interfaces.size(); 572 String [] result = new String [size]; 573 interfaces.toArray(result); 574 return result; 575 } else { 576 char[][] names= info.getInterfaceNames(); 577 int length; 578 if (names == null || (length = names.length) == 0) { 579 return CharOperation.NO_STRINGS; 580 } 581 names= ClassFile.translatedNames(names); 582 String [] strings= new String [length]; 583 for (int i= 0; i < length; i++) { 584 strings[i]= new String (Signature.createTypeSignature(names[i], true)); 585 } 586 return strings; 587 } 588 } 589 590 public ITypeParameter[] getTypeParameters() throws JavaModelException { 591 String [] typeParameterSignatures = getTypeParameterSignatures(); 592 int length = typeParameterSignatures.length; 593 if (length == 0) return TypeParameter.NO_TYPE_PARAMETERS; 594 ITypeParameter[] typeParameters = new ITypeParameter[length]; 595 for (int i = 0; i < typeParameterSignatures.length; i++) { 596 String typeParameterName = Signature.getTypeVariable(typeParameterSignatures[i]); 597 typeParameters[i] = new TypeParameter(this, typeParameterName); 598 } 599 return typeParameters; 600 } 601 602 606 public String [] getTypeParameterSignatures() throws JavaModelException { 607 IBinaryType info = (IBinaryType) getElementInfo(); 608 char[] genericSignature = info.getGenericSignature(); 609 if (genericSignature == null) 610 return CharOperation.NO_STRINGS; 611 612 char[] dotBaseSignature = CharOperation.replaceOnCopy(genericSignature, '/', '.'); 613 char[][] typeParams = Signature.getTypeParameters(dotBaseSignature); 614 return CharOperation.toStrings(typeParams); 615 } 616 617 620 public IType getType(String typeName) { 621 IClassFile classFile= getPackageFragment().getClassFile(getTypeQualifiedName() + "$" + typeName + SUFFIX_STRING_class); return new BinaryType((JavaElement)classFile, typeName); 623 } 624 public ITypeParameter getTypeParameter(String typeParameterName) { 625 return new TypeParameter(this, typeParameterName); 626 } 627 630 public String getTypeQualifiedName() { 631 return this.getTypeQualifiedName('$'); 632 } 633 636 public String getTypeQualifiedName(char enclosingTypeSeparator) { 637 try { 638 return getTypeQualifiedName(enclosingTypeSeparator, false); 639 } catch (JavaModelException e) { 640 return null; 642 } 643 } 644 647 public IType[] getTypes() throws JavaModelException { 648 ArrayList list = getChildrenOfType(TYPE); 649 int size; 650 if ((size = list.size()) == 0) { 651 return NO_TYPES; 652 } else { 653 IType[] array= new IType[size]; 654 list.toArray(array); 655 return array; 656 } 657 } 658 659 662 public boolean isAnonymous() throws JavaModelException { 663 IBinaryType info = (IBinaryType) getElementInfo(); 664 return info.isAnonymous(); 665 } 666 669 public boolean isClass() throws JavaModelException { 670 IBinaryType info = (IBinaryType) getElementInfo(); 671 return TypeDeclaration.kind(info.getModifiers()) == TypeDeclaration.CLASS_DECL; 672 673 } 674 675 679 public boolean isEnum() throws JavaModelException { 680 IBinaryType info = (IBinaryType) getElementInfo(); 681 return TypeDeclaration.kind(info.getModifiers()) == TypeDeclaration.ENUM_DECL; 682 } 683 684 687 public boolean isInterface() throws JavaModelException { 688 IBinaryType info = (IBinaryType) getElementInfo(); 689 switch (TypeDeclaration.kind(info.getModifiers())) { 690 case TypeDeclaration.INTERFACE_DECL: 691 case TypeDeclaration.ANNOTATION_TYPE_DECL: return true; 693 } 694 return false; 695 } 696 700 public boolean isAnnotation() throws JavaModelException { 701 IBinaryType info = (IBinaryType) getElementInfo(); 702 return TypeDeclaration.kind(info.getModifiers()) == TypeDeclaration.ANNOTATION_TYPE_DECL; 703 } 704 705 708 public boolean isLocal() throws JavaModelException { 709 IBinaryType info = (IBinaryType) getElementInfo(); 710 return info.isLocal(); 711 } 712 715 public boolean isMember() throws JavaModelException { 716 IBinaryType info = (IBinaryType) getElementInfo(); 717 return info.isMember(); 718 } 719 722 public boolean isResolved() { 723 return false; 724 } 725 728 public ITypeHierarchy loadTypeHierachy(InputStream input, IProgressMonitor monitor) throws JavaModelException { 729 return loadTypeHierachy(input, DefaultWorkingCopyOwner.PRIMARY, monitor); 730 } 731 734 public ITypeHierarchy loadTypeHierachy(InputStream input, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException { 735 return TypeHierarchy.load(this, input, owner); 736 } 737 740 public ITypeHierarchy newSupertypeHierarchy(IProgressMonitor monitor) throws JavaModelException { 741 return this.newSupertypeHierarchy(DefaultWorkingCopyOwner.PRIMARY, monitor); 742 } 743 746 public ITypeHierarchy newSupertypeHierarchy( 747 ICompilationUnit[] workingCopies, 748 IProgressMonitor monitor) 749 throws JavaModelException { 750 751 CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), false); 752 op.runOperation(monitor); 753 return op.getResult(); 754 } 755 765 public ITypeHierarchy newSupertypeHierarchy( 766 IWorkingCopy[] workingCopies, 767 IProgressMonitor monitor) 768 throws JavaModelException { 769 770 ICompilationUnit[] copies; 771 if (workingCopies == null) { 772 copies = null; 773 } else { 774 int length = workingCopies.length; 775 System.arraycopy(workingCopies, 0, copies = new ICompilationUnit[length], 0, length); 776 } 777 return newSupertypeHierarchy(copies, monitor); 778 } 779 782 public ITypeHierarchy newSupertypeHierarchy( 783 WorkingCopyOwner owner, 784 IProgressMonitor monitor) 785 throws JavaModelException { 786 787 ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true); 788 CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), false); 789 op.runOperation(monitor); 790 return op.getResult(); 791 } 792 795 public ITypeHierarchy newTypeHierarchy(IJavaProject project, IProgressMonitor monitor) throws JavaModelException { 796 return newTypeHierarchy(project, DefaultWorkingCopyOwner.PRIMARY, monitor); 797 } 798 801 public ITypeHierarchy newTypeHierarchy(IJavaProject project, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException { 802 if (project == null) { 803 throw new IllegalArgumentException (Messages.hierarchy_nullProject); 804 } 805 ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true); 806 ICompilationUnit[] projectWCs = null; 807 if (workingCopies != null) { 808 int length = workingCopies.length; 809 projectWCs = new ICompilationUnit[length]; 810 int index = 0; 811 for (int i = 0; i < length; i++) { 812 ICompilationUnit wc = workingCopies[i]; 813 if (project.equals(wc.getJavaProject())) { 814 projectWCs[index++] = wc; 815 } 816 } 817 if (index != length) { 818 System.arraycopy(projectWCs, 0, projectWCs = new ICompilationUnit[index], 0, index); 819 } 820 } 821 CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation( 822 this, 823 projectWCs, 824 project, 825 true); 826 op.runOperation(monitor); 827 return op.getResult(); 828 } 829 838 public ITypeHierarchy newTypeHierarchy(IProgressMonitor monitor) throws JavaModelException { 839 return newTypeHierarchy((IWorkingCopy[])null, monitor); 840 } 841 844 public ITypeHierarchy newTypeHierarchy( 845 ICompilationUnit[] workingCopies, 846 IProgressMonitor monitor) 847 throws JavaModelException { 848 849 CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), true); 850 op.runOperation(monitor); 851 return op.getResult(); 852 } 853 857 public ITypeHierarchy newTypeHierarchy( 858 IWorkingCopy[] workingCopies, 859 IProgressMonitor monitor) 860 throws JavaModelException { 861 862 ICompilationUnit[] copies; 863 if (workingCopies == null) { 864 copies = null; 865 } else { 866 int length = workingCopies.length; 867 System.arraycopy(workingCopies, 0, copies = new ICompilationUnit[length], 0, length); 868 } 869 return newTypeHierarchy(copies, monitor); 870 } 871 874 public ITypeHierarchy newTypeHierarchy( 875 WorkingCopyOwner owner, 876 IProgressMonitor monitor) 877 throws JavaModelException { 878 879 ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true); 880 CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), true); 881 op.runOperation(monitor); 882 return op.getResult(); 883 } 884 public JavaElement resolved(Binding binding) { 885 SourceRefElement resolvedHandle = new ResolvedBinaryType(this.parent, this.name, new String (binding.computeUniqueKey())); 886 resolvedHandle.occurrenceCount = this.occurrenceCount; 887 return resolvedHandle; 888 } 889 892 public String [][] resolveType(String typeName) { 893 return null; 895 } 896 899 public String [][] resolveType(String typeName, WorkingCopyOwner owner) { 900 return null; 902 } 903 907 public String sourceFileName(IBinaryType info) { 908 char[] sourceFileName = info.sourceFileName(); 909 if (sourceFileName == null) { 910 915 if (info.isMember()) { 916 IType enclosingType = getDeclaringType(); 917 if (enclosingType == null) return null; while (enclosingType.getDeclaringType() != null) { 919 enclosingType = enclosingType.getDeclaringType(); 920 } 921 return enclosingType.getElementName() + Util.defaultJavaExtension(); 922 } else if (info.isLocal() || info.isAnonymous()){ 923 String typeQualifiedName = getTypeQualifiedName(); 924 int dollar = typeQualifiedName.indexOf('$'); 925 if (dollar == -1) { 926 return getElementName() + Util.defaultJavaExtension(); 928 } 929 return typeQualifiedName.substring(0, dollar) + Util.defaultJavaExtension(); 930 } else { 931 return getElementName() + Util.defaultJavaExtension(); 932 } 933 } else { 934 int index = CharOperation.lastIndexOf('/', sourceFileName); 935 return new String (sourceFileName, index + 1, sourceFileName.length - index - 1); 936 } 937 } 938 941 protected void toStringInfo(int tab, StringBuffer buffer, Object info, boolean showResolvedInfo) { 942 buffer.append(this.tabString(tab)); 943 if (info == null) { 944 toStringName(buffer); 945 buffer.append(" (not open)"); } else if (info == NO_INFO) { 947 toStringName(buffer); 948 } else { 949 try { 950 if (this.isAnnotation()) { 951 buffer.append("@interface "); } else if (this.isEnum()) { 953 buffer.append("enum "); } else if (this.isInterface()) { 955 buffer.append("interface "); } else { 957 buffer.append("class "); } 959 toStringName(buffer); 960 } catch (JavaModelException e) { 961 buffer.append("<JavaModelException in toString of " + getElementName()); } 963 } 964 } 965 protected void toStringName(StringBuffer buffer) { 966 if (getElementName().length() > 0) 967 super.toStringName(buffer); 968 else 969 buffer.append("<anonymous>"); } 971 public String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException { 972 final String contents = getJavadocContents(monitor); 973 if (contents == null) return null; 974 final int indexOfStartOfClassData = contents.indexOf(JavadocConstants.START_OF_CLASS_DATA); 975 if (indexOfStartOfClassData == -1) throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.UNKNOWN_JAVADOC_FORMAT, this)); 976 int indexOfNextSummary = contents.indexOf(JavadocConstants.NESTED_CLASS_SUMMARY); 977 if (this.isEnum() && indexOfNextSummary == -1) { 978 indexOfNextSummary = contents.indexOf(JavadocConstants.ENUM_CONSTANT_SUMMARY); 980 } 981 if (this.isAnnotation() && indexOfNextSummary == -1) { 982 indexOfNextSummary = contents.indexOf(JavadocConstants.ANNOTATION_TYPE_REQUIRED_MEMBER_SUMMARY); 984 if (indexOfNextSummary == -1) { 985 indexOfNextSummary = contents.indexOf(JavadocConstants.ANNOTATION_TYPE_OPTIONAL_MEMBER_SUMMARY); 987 } 988 } 989 if (indexOfNextSummary == -1) { 990 indexOfNextSummary = contents.indexOf(JavadocConstants.FIELD_SUMMARY); 992 } 993 if (indexOfNextSummary == -1) { 994 indexOfNextSummary = contents.indexOf(JavadocConstants.CONSTRUCTOR_SUMMARY); 996 } 997 if (indexOfNextSummary == -1) { 998 indexOfNextSummary = contents.indexOf(JavadocConstants.METHOD_SUMMARY); 1000 } 1001 if (indexOfNextSummary == -1) { 1002 indexOfNextSummary = contents.indexOf(JavadocConstants.END_OF_CLASS_DATA); 1004 } 1005 if (indexOfNextSummary == -1) { 1006 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.UNKNOWN_JAVADOC_FORMAT, this)); 1007 } 1008 1012 int start = indexOfStartOfClassData + JavadocConstants.START_OF_CLASS_DATA_LENGTH; 1013 int indexOfFirstParagraph = contents.indexOf("<P>", start); if (indexOfFirstParagraph == -1) { 1015 indexOfFirstParagraph = contents.indexOf("<p>", start); } 1017 if (indexOfFirstParagraph != -1 && indexOfFirstParagraph < indexOfNextSummary) { 1018 start = indexOfFirstParagraph; 1019 } 1020 return contents.substring(start, indexOfNextSummary); 1021} 1022public String getJavadocContents(IProgressMonitor monitor) throws JavaModelException { 1023 PerProjectInfo projectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(this.getJavaProject().getProject()); 1024 String cachedJavadoc = null; 1025 synchronized (projectInfo.javadocCache) { 1026 cachedJavadoc = (String ) projectInfo.javadocCache.get(this); 1027 } 1028 if (cachedJavadoc != null && cachedJavadoc != EMPTY_JAVADOC) { 1029 return cachedJavadoc; 1030 } 1031 URL baseLocation= getJavadocBaseLocation(); 1032 if (baseLocation == null) { 1033 return null; 1034 } 1035 StringBuffer pathBuffer = new StringBuffer (baseLocation.toExternalForm()); 1036 1037 if (!(pathBuffer.charAt(pathBuffer.length() - 1) == '/')) { 1038 pathBuffer.append('/'); 1039 } 1040 IPackageFragment pack= this.getPackageFragment(); 1041 String typeQualifiedName = null; 1042 if (this.isMember()) { 1043 IType currentType = this; 1044 StringBuffer typeName = new StringBuffer (); 1045 while (currentType != null) { 1046 typeName.insert(0, currentType.getElementName()); 1047 currentType = currentType.getDeclaringType(); 1048 if (currentType != null) { 1049 typeName.insert(0, '.'); 1050 } 1051 } 1052 typeQualifiedName = new String (typeName.toString()); 1053 } else { 1054 typeQualifiedName = this.getElementName(); 1055 } 1056 1057 pathBuffer.append(pack.getElementName().replace('.', '/')).append('/').append(typeQualifiedName).append(JavadocConstants.HTML_EXTENSION); 1058 1059 if (monitor != null && monitor.isCanceled()) throw new OperationCanceledException(); 1060 final String contents = getURLContents(String.valueOf(pathBuffer)); 1061 synchronized (projectInfo.javadocCache) { 1062 projectInfo.javadocCache.put(this, contents); 1063 } 1064 return contents; 1065} 1066} 1067 | Popular Tags |