1 11 package org.eclipse.jdt.ui; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 import org.eclipse.core.runtime.IPath; 15 16 import org.eclipse.core.resources.IProject; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.resources.IStorage; 19 20 import org.eclipse.jface.preference.IPreferenceStore; 21 22 import org.eclipse.ui.model.IWorkbenchAdapter; 23 24 import org.eclipse.jdt.core.BindingKey; 25 import org.eclipse.jdt.core.ClasspathContainerInitializer; 26 import org.eclipse.jdt.core.Flags; 27 import org.eclipse.jdt.core.IClassFile; 28 import org.eclipse.jdt.core.IClasspathContainer; 29 import org.eclipse.jdt.core.IClasspathEntry; 30 import org.eclipse.jdt.core.ICompilationUnit; 31 import org.eclipse.jdt.core.IField; 32 import org.eclipse.jdt.core.IInitializer; 33 import org.eclipse.jdt.core.IJavaElement; 34 import org.eclipse.jdt.core.IJavaProject; 35 import org.eclipse.jdt.core.ILocalVariable; 36 import org.eclipse.jdt.core.IMember; 37 import org.eclipse.jdt.core.IMethod; 38 import org.eclipse.jdt.core.IPackageFragment; 39 import org.eclipse.jdt.core.IPackageFragmentRoot; 40 import org.eclipse.jdt.core.IType; 41 import org.eclipse.jdt.core.ITypeParameter; 42 import org.eclipse.jdt.core.JavaCore; 43 import org.eclipse.jdt.core.JavaModelException; 44 import org.eclipse.jdt.core.Signature; 45 46 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 47 import org.eclipse.jdt.internal.corext.util.Messages; 48 49 import org.eclipse.jdt.internal.ui.JavaPlugin; 50 import org.eclipse.jdt.internal.ui.JavaUIMessages; 51 import org.eclipse.jdt.internal.ui.viewsupport.StorageLabelProvider; 52 53 58 public class JavaElementLabels { 59 60 61 65 public final static long M_PARAMETER_TYPES= 1L << 0; 66 67 71 public final static long M_PARAMETER_NAMES= 1L << 1; 72 73 77 public final static long M_PRE_TYPE_PARAMETERS= 1L << 2; 78 79 83 public final static long M_APP_TYPE_PARAMETERS= 1L << 3; 84 85 89 public final static long M_EXCEPTIONS= 1L << 4; 90 91 95 public final static long M_APP_RETURNTYPE= 1L << 5; 96 97 101 public final static long M_PRE_RETURNTYPE= 1L << 6; 102 103 107 public final static long M_FULLY_QUALIFIED= 1L << 7; 108 109 113 public final static long M_POST_QUALIFIED= 1L << 8; 114 115 119 public final static long I_FULLY_QUALIFIED= 1L << 10; 120 121 125 public final static long I_POST_QUALIFIED= 1L << 11; 126 127 131 public final static long F_APP_TYPE_SIGNATURE= 1L << 14; 132 133 137 public final static long F_PRE_TYPE_SIGNATURE= 1L << 15; 138 139 143 public final static long F_FULLY_QUALIFIED= 1L << 16; 144 145 149 public final static long F_POST_QUALIFIED= 1L << 17; 150 151 155 public final static long T_FULLY_QUALIFIED= 1L << 18; 156 157 161 public final static long T_CONTAINER_QUALIFIED= 1L << 19; 162 163 167 public final static long T_POST_QUALIFIED= 1L << 20; 168 169 173 public final static long T_TYPE_PARAMETERS= 1L << 21; 174 175 179 public final static long D_QUALIFIED= 1L << 24; 180 181 185 public final static long D_POST_QUALIFIED= 1L << 25; 186 187 191 public final static long CF_QUALIFIED= 1L << 27; 192 193 197 public final static long CF_POST_QUALIFIED= 1L << 28; 198 199 203 public final static long CU_QUALIFIED= 1L << 31; 204 205 209 public final static long CU_POST_QUALIFIED= 1L << 32; 210 211 215 public final static long P_QUALIFIED= 1L << 35; 216 217 221 public final static long P_POST_QUALIFIED= 1L << 36; 222 223 227 public final static long P_COMPRESSED= 1L << 37; 228 229 233 public final static long ROOT_VARIABLE= 1L << 40; 234 235 239 public final static long ROOT_QUALIFIED= 1L << 41; 240 241 245 public final static long ROOT_POST_QUALIFIED= 1L << 42; 246 247 252 public final static long APPEND_ROOT_PATH= 1L << 43; 253 254 259 public final static long PREPEND_ROOT_PATH= 1L << 44; 260 261 266 public final static long REFERENCED_ROOT_POST_QUALIFIED= 1L << 45; 267 268 274 public final static long USE_RESOLVED= 1L << 48; 275 276 280 public final static long F_CATEGORY= 1L << 49; 281 285 public final static long M_CATEGORY= 1L << 50; 286 290 public final static long T_CATEGORY= 1L << 51; 291 292 296 public final static long ALL_CATEGORY= new Long (JavaElementLabels.F_CATEGORY | JavaElementLabels.M_CATEGORY | JavaElementLabels.T_CATEGORY).longValue(); 297 298 301 public final static long ALL_FULLY_QUALIFIED= new Long (F_FULLY_QUALIFIED | M_FULLY_QUALIFIED | I_FULLY_QUALIFIED | T_FULLY_QUALIFIED | D_QUALIFIED | CF_QUALIFIED | CU_QUALIFIED | P_QUALIFIED | ROOT_QUALIFIED).longValue(); 302 303 304 307 public final static long ALL_POST_QUALIFIED= new Long (F_POST_QUALIFIED | M_POST_QUALIFIED | I_POST_QUALIFIED | T_POST_QUALIFIED | D_POST_QUALIFIED | CF_POST_QUALIFIED | CU_POST_QUALIFIED | P_POST_QUALIFIED | ROOT_POST_QUALIFIED).longValue(); 308 309 312 public final static long ALL_DEFAULT= new Long (M_PARAMETER_TYPES | M_APP_TYPE_PARAMETERS | T_TYPE_PARAMETERS).longValue(); 313 314 317 public final static long DEFAULT_QUALIFIED= new Long (F_FULLY_QUALIFIED | M_FULLY_QUALIFIED | I_FULLY_QUALIFIED | T_FULLY_QUALIFIED | D_QUALIFIED | CF_QUALIFIED | CU_QUALIFIED).longValue(); 318 319 322 public final static long DEFAULT_POST_QUALIFIED= new Long (F_POST_QUALIFIED | M_POST_QUALIFIED | I_POST_QUALIFIED | T_POST_QUALIFIED | D_POST_QUALIFIED | CF_POST_QUALIFIED | CU_POST_QUALIFIED).longValue(); 323 324 327 public final static String CONCAT_STRING= JavaUIMessages.JavaElementLabels_concat_string; 328 331 public final static String COMMA_STRING= JavaUIMessages.JavaElementLabels_comma_string; 332 335 public final static String DECL_STRING= JavaUIMessages.JavaElementLabels_declseparator_string; 336 341 private final static String CATEGORY_SEPARATOR_STRING= JavaUIMessages.JavaElementLabels_category_separator_string; 342 345 public final static String ELLIPSIS_STRING= "..."; 349 public final static String DEFAULT_PACKAGE= JavaUIMessages.JavaElementLabels_default_package; 350 351 352 private final static long QUALIFIER_FLAGS= P_COMPRESSED | USE_RESOLVED; 353 354 357 private static String fgPkgNamePattern= ""; private static String fgPkgNamePrefix; 359 private static String fgPkgNamePostfix; 360 private static int fgPkgNameChars; 361 private static int fgPkgNameLength= -1; 362 363 private JavaElementLabels() { 364 } 365 366 private static final boolean getFlag(long flags, long flag) { 367 return (flags & flag) != 0; 368 } 369 370 377 public static String getTextLabel(Object obj, long flags) { 378 if (obj instanceof IJavaElement) { 379 return getElementLabel((IJavaElement) obj, flags); 380 } else if (obj instanceof IResource) { 381 return ((IResource) obj).getName(); 382 } else if (obj instanceof IStorage) { 383 StorageLabelProvider storageLabelProvider= new StorageLabelProvider(); 384 String label= storageLabelProvider.getText(obj); 385 storageLabelProvider.dispose(); 386 return label; 387 } else if (obj instanceof IAdaptable) { 388 IWorkbenchAdapter wbadapter= (IWorkbenchAdapter) ((IAdaptable)obj).getAdapter(IWorkbenchAdapter.class); 389 if (wbadapter != null) { 390 return wbadapter.getLabel(obj); 391 } 392 } 393 return ""; } 395 396 402 public static String getElementLabel(IJavaElement element, long flags) { 403 StringBuffer buf= new StringBuffer (60); 404 getElementLabel(element, flags, buf); 405 return buf.toString(); 406 } 407 408 414 public static void getElementLabel(IJavaElement element, long flags, StringBuffer buf) { 415 int type= element.getElementType(); 416 IPackageFragmentRoot root= null; 417 418 if (type != IJavaElement.JAVA_MODEL && type != IJavaElement.JAVA_PROJECT && type != IJavaElement.PACKAGE_FRAGMENT_ROOT) 419 root= JavaModelUtil.getPackageFragmentRoot(element); 420 if (root != null && getFlag(flags, PREPEND_ROOT_PATH)) { 421 getPackageFragmentRootLabel(root, ROOT_QUALIFIED, buf); 422 buf.append(CONCAT_STRING); 423 } 424 425 switch (type) { 426 case IJavaElement.METHOD: 427 getMethodLabel((IMethod) element, flags, buf); 428 break; 429 case IJavaElement.FIELD: 430 getFieldLabel((IField) element, flags, buf); 431 break; 432 case IJavaElement.LOCAL_VARIABLE: 433 getLocalVariableLabel((ILocalVariable) element, flags, buf); 434 break; 435 case IJavaElement.INITIALIZER: 436 getInitializerLabel((IInitializer) element, flags, buf); 437 break; 438 case IJavaElement.TYPE: 439 getTypeLabel((IType) element, flags, buf); 440 break; 441 case IJavaElement.CLASS_FILE: 442 getClassFileLabel((IClassFile) element, flags, buf); 443 break; 444 case IJavaElement.COMPILATION_UNIT: 445 getCompilationUnitLabel((ICompilationUnit) element, flags, buf); 446 break; 447 case IJavaElement.PACKAGE_FRAGMENT: 448 getPackageFragmentLabel((IPackageFragment) element, flags, buf); 449 break; 450 case IJavaElement.PACKAGE_FRAGMENT_ROOT: 451 getPackageFragmentRootLabel((IPackageFragmentRoot) element, flags, buf); 452 break; 453 case IJavaElement.IMPORT_CONTAINER: 454 case IJavaElement.IMPORT_DECLARATION: 455 case IJavaElement.PACKAGE_DECLARATION: 456 getDeclarationLabel(element, flags, buf); 457 break; 458 case IJavaElement.JAVA_PROJECT: 459 case IJavaElement.JAVA_MODEL: 460 buf.append(element.getElementName()); 461 break; 462 default: 463 buf.append(element.getElementName()); 464 } 465 466 if (root != null && getFlag(flags, APPEND_ROOT_PATH)) { 467 buf.append(CONCAT_STRING); 468 getPackageFragmentRootLabel(root, ROOT_QUALIFIED, buf); 469 } 470 } 471 472 478 public static void getMethodLabel(IMethod method, long flags, StringBuffer buf) { 479 try { 480 BindingKey resolvedKey= getFlag(flags, USE_RESOLVED) && method.isResolved() ? new BindingKey(method.getKey()) : null; 481 String resolvedSig= (resolvedKey != null) ? resolvedKey.toSignature() : null; 482 483 if (getFlag(flags, M_PRE_TYPE_PARAMETERS)) { 485 if (resolvedKey != null) { 486 if (resolvedKey.isParameterizedMethod()) { 487 String [] typeArgRefs= resolvedKey.getTypeArguments(); 488 if (typeArgRefs.length > 0) { 489 getTypeArgumentSignaturesLabel(typeArgRefs, flags, buf); 490 buf.append(' '); 491 } 492 } else { 493 String [] typeParameterSigs= Signature.getTypeParameters(resolvedSig); 494 if (typeParameterSigs.length > 0) { 495 getTypeParameterSignaturesLabel(typeParameterSigs, flags, buf); 496 buf.append(' '); 497 } 498 } 499 } else if (method.exists()) { 500 ITypeParameter[] typeParameters= method.getTypeParameters(); 501 if (typeParameters.length > 0) { 502 getTypeParametersLabel(typeParameters, flags, buf); 503 buf.append(' '); 504 } 505 } 506 } 507 508 if (getFlag(flags, M_PRE_RETURNTYPE) && method.exists() && !method.isConstructor()) { 510 String returnTypeSig= resolvedSig != null ? Signature.getReturnType(resolvedSig) : method.getReturnType(); 511 getTypeSignatureLabel(returnTypeSig, flags, buf); 512 buf.append(' '); 513 } 514 515 if (getFlag(flags, M_FULLY_QUALIFIED)) { 517 getTypeLabel(method.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); 518 buf.append('.'); 519 } 520 521 buf.append(method.getElementName()); 522 523 buf.append('('); 525 if (getFlag(flags, M_PARAMETER_TYPES | M_PARAMETER_NAMES)) { 526 String [] types= null; 527 int nParams= 0; 528 boolean renderVarargs= false; 529 if (getFlag(flags, M_PARAMETER_TYPES)) { 530 if (resolvedSig != null) { 531 types= Signature.getParameterTypes(resolvedSig); 532 } else { 533 types= method.getParameterTypes(); 534 } 535 nParams= types.length; 536 renderVarargs= method.exists() && Flags.isVarargs(method.getFlags()); 537 } 538 String [] names= null; 539 if (getFlag(flags, M_PARAMETER_NAMES) && method.exists()) { 540 names= method.getParameterNames(); 541 if (types == null) { 542 nParams= names.length; 543 } else { if (nParams != names.length) { 545 if (resolvedSig != null && types.length > names.length) { 546 nParams= names.length; 548 String [] typesWithoutSyntheticParams= new String [nParams]; 549 System.arraycopy(types, types.length - nParams, typesWithoutSyntheticParams, 0, nParams); 550 types= typesWithoutSyntheticParams; 551 } else { 552 names= null; } 556 } 557 } 558 } 559 560 for (int i= 0; i < nParams; i++) { 561 if (i > 0) { 562 buf.append(COMMA_STRING); 563 } 564 if (types != null) { 565 String paramSig= types[i]; 566 if (renderVarargs && (i == nParams - 1)) { 567 int newDim= Signature.getArrayCount(paramSig) - 1; 568 getTypeSignatureLabel(Signature.getElementType(paramSig), flags, buf); 569 for (int k= 0; k < newDim; k++) { 570 buf.append('[').append(']'); 571 } 572 buf.append(ELLIPSIS_STRING); 573 } else { 574 getTypeSignatureLabel(paramSig, flags, buf); 575 } 576 } 577 if (names != null) { 578 if (types != null) { 579 buf.append(' '); 580 } 581 buf.append(names[i]); 582 } 583 } 584 } else { 585 if (method.getParameterTypes().length > 0) { 586 buf.append(ELLIPSIS_STRING); 587 } 588 } 589 buf.append(')'); 590 591 if (getFlag(flags, M_EXCEPTIONS)) { 592 String [] types; 593 if (resolvedKey != null) { 594 types= resolvedKey.getThrownExceptions(); 595 } else { 596 types= method.exists() ? method.getExceptionTypes() : new String [0]; 597 } 598 if (types.length > 0) { 599 buf.append(" throws "); for (int i= 0; i < types.length; i++) { 601 if (i > 0) { 602 buf.append(COMMA_STRING); 603 } 604 getTypeSignatureLabel(types[i], flags, buf); 605 } 606 } 607 } 608 609 if (getFlag(flags, M_APP_TYPE_PARAMETERS)) { 610 if (resolvedKey != null) { 611 if (resolvedKey.isParameterizedMethod()) { 612 String [] typeArgRefs= resolvedKey.getTypeArguments(); 613 if (typeArgRefs.length > 0) { 614 buf.append(' '); 615 getTypeArgumentSignaturesLabel(typeArgRefs, flags, buf); 616 } 617 } else { 618 String [] typeParameterSigs= Signature.getTypeParameters(resolvedSig); 619 if (typeParameterSigs.length > 0) { 620 buf.append(' '); 621 getTypeParameterSignaturesLabel(typeParameterSigs, flags, buf); 622 } 623 } 624 } else if (method.exists()) { 625 ITypeParameter[] typeParameters= method.getTypeParameters(); 626 if (typeParameters.length > 0) { 627 buf.append(' '); 628 getTypeParametersLabel(typeParameters, flags, buf); 629 } 630 } 631 } 632 633 if (getFlag(flags, M_APP_RETURNTYPE) && method.exists() && !method.isConstructor()) { 634 buf.append(DECL_STRING); 635 String returnTypeSig= resolvedSig != null ? Signature.getReturnType(resolvedSig) : method.getReturnType(); 636 getTypeSignatureLabel(returnTypeSig, flags, buf); 637 } 638 639 if (getFlag(flags, M_CATEGORY) && method.exists()) 641 getCategoryLabel(method, buf); 642 643 if (getFlag(flags, M_POST_QUALIFIED)) { 645 buf.append(CONCAT_STRING); 646 getTypeLabel(method.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); 647 } 648 649 } catch (JavaModelException e) { 650 JavaPlugin.log(e); } 652 } 653 654 private static void getCategoryLabel(IMember member, StringBuffer buf) throws JavaModelException { 655 String [] categories= member.getCategories(); 656 if (categories.length > 0) { 657 StringBuffer categoriesBuf= new StringBuffer (30); 658 for (int i= 0; i < categories.length; i++) { 659 if (i > 0) 660 categoriesBuf.append(CATEGORY_SEPARATOR_STRING); 661 categoriesBuf.append(categories[i]); 662 } 663 buf.append(CONCAT_STRING); 664 buf.append(Messages.format(JavaUIMessages.JavaElementLabels_category , categoriesBuf.toString())); 665 } 666 } 667 668 private static void getTypeParametersLabel(ITypeParameter[] typeParameters, long flags, StringBuffer buf) { 669 if (typeParameters.length > 0) { 670 buf.append('<'); 671 for (int i = 0; i < typeParameters.length; i++) { 672 if (i > 0) { 673 buf.append(COMMA_STRING); 674 } 675 buf.append(typeParameters[i].getElementName()); 676 } 677 buf.append('>'); 678 } 679 } 680 681 687 public static void getFieldLabel(IField field, long flags, StringBuffer buf) { 688 try { 689 690 if (getFlag(flags, F_PRE_TYPE_SIGNATURE) && field.exists() && !Flags.isEnum(field.getFlags())) { 691 if (getFlag(flags, USE_RESOLVED) && field.isResolved()) { 692 getTypeSignatureLabel(new BindingKey(field.getKey()).toSignature(), flags, buf); 693 } else { 694 getTypeSignatureLabel(field.getTypeSignature(), flags, buf); 695 } 696 buf.append(' '); 697 } 698 699 if (getFlag(flags, F_FULLY_QUALIFIED)) { 701 getTypeLabel(field.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); 702 buf.append('.'); 703 } 704 buf.append(field.getElementName()); 705 706 if (getFlag(flags, F_APP_TYPE_SIGNATURE) && field.exists() && !Flags.isEnum(field.getFlags())) { 707 buf.append(DECL_STRING); 708 if (getFlag(flags, USE_RESOLVED) && field.isResolved()) { 709 getTypeSignatureLabel(new BindingKey(field.getKey()).toSignature(), flags, buf); 710 } else { 711 getTypeSignatureLabel(field.getTypeSignature(), flags, buf); 712 } 713 } 714 715 if (getFlag(flags, F_CATEGORY) && field.exists()) 717 getCategoryLabel(field, buf); 718 719 if (getFlag(flags, F_POST_QUALIFIED)) { 721 buf.append(CONCAT_STRING); 722 getTypeLabel(field.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); 723 } 724 725 } catch (JavaModelException e) { 726 JavaPlugin.log(e); } 728 } 729 730 736 public static void getLocalVariableLabel(ILocalVariable localVariable, long flags, StringBuffer buf) { 737 if (getFlag(flags, F_PRE_TYPE_SIGNATURE)) { 738 getTypeSignatureLabel(localVariable.getTypeSignature(), flags, buf); 739 buf.append(' '); 740 } 741 742 if (getFlag(flags, F_FULLY_QUALIFIED)) { 743 getElementLabel(localVariable.getParent(), M_PARAMETER_TYPES | M_FULLY_QUALIFIED | T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); 744 buf.append('.'); 745 } 746 747 buf.append(localVariable.getElementName()); 748 749 if (getFlag(flags, F_APP_TYPE_SIGNATURE)) { 750 buf.append(DECL_STRING); 751 getTypeSignatureLabel(localVariable.getTypeSignature(), flags, buf); 752 } 753 754 if (getFlag(flags, F_POST_QUALIFIED)) { 756 buf.append(CONCAT_STRING); 757 getElementLabel(localVariable.getParent(), M_PARAMETER_TYPES | M_FULLY_QUALIFIED | T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); 758 } 759 } 760 761 767 public static void getInitializerLabel(IInitializer initializer, long flags, StringBuffer buf) { 768 if (getFlag(flags, I_FULLY_QUALIFIED)) { 770 getTypeLabel(initializer.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); 771 buf.append('.'); 772 } 773 buf.append(JavaUIMessages.JavaElementLabels_initializer); 774 775 if (getFlag(flags, I_POST_QUALIFIED)) { 777 buf.append(CONCAT_STRING); 778 getTypeLabel(initializer.getDeclaringType(), T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); 779 } 780 } 781 782 private static void getTypeSignatureLabel(String typeSig, long flags, StringBuffer buf) { 783 int sigKind= Signature.getTypeSignatureKind(typeSig); 784 switch (sigKind) { 785 case Signature.BASE_TYPE_SIGNATURE: 786 buf.append(Signature.toString(typeSig)); 787 break; 788 case Signature.ARRAY_TYPE_SIGNATURE: 789 getTypeSignatureLabel(Signature.getElementType(typeSig), flags, buf); 790 for (int dim= Signature.getArrayCount(typeSig); dim > 0; dim--) { 791 buf.append('[').append(']'); 792 } 793 break; 794 case Signature.CLASS_TYPE_SIGNATURE: 795 String baseType= Signature.toString(Signature.getTypeErasure(typeSig)); 796 buf.append(Signature.getSimpleName(baseType)); 797 798 String [] typeArguments= Signature.getTypeArguments(typeSig); 799 getTypeArgumentSignaturesLabel(typeArguments, flags, buf); 800 break; 801 case Signature.TYPE_VARIABLE_SIGNATURE: 802 buf.append(Signature.toString(typeSig)); 803 break; 804 case Signature.WILDCARD_TYPE_SIGNATURE: 805 char ch= typeSig.charAt(0); 806 if (ch == Signature.C_STAR) { buf.append('?'); 808 } else { 809 if (ch == Signature.C_EXTENDS) { 810 buf.append("? extends "); getTypeSignatureLabel(typeSig.substring(1), flags, buf); 812 } else if (ch == Signature.C_SUPER) { 813 buf.append("? super "); getTypeSignatureLabel(typeSig.substring(1), flags, buf); 815 } 816 } 817 break; 818 case Signature.CAPTURE_TYPE_SIGNATURE: 819 getTypeSignatureLabel(typeSig.substring(1), flags, buf); 820 break; 821 default: 822 } 824 } 825 826 private static void getTypeArgumentSignaturesLabel(String [] typeArgsSig, long flags, StringBuffer buf) { 827 if (typeArgsSig.length > 0) { 828 buf.append('<'); 829 for (int i = 0; i < typeArgsSig.length; i++) { 830 if (i > 0) { 831 buf.append(COMMA_STRING); 832 } 833 getTypeSignatureLabel(typeArgsSig[i], flags, buf); 834 } 835 buf.append('>'); 836 } 837 } 838 839 private static void getTypeParameterSignaturesLabel(String [] typeParamSigs, long flags, StringBuffer buf) { 840 if (typeParamSigs.length > 0) { 841 buf.append('<'); 842 for (int i = 0; i < typeParamSigs.length; i++) { 843 if (i > 0) { 844 buf.append(COMMA_STRING); 845 } 846 buf.append(Signature.getTypeVariable(typeParamSigs[i])); 847 } 848 buf.append('>'); 849 } 850 } 851 852 853 859 public static void getTypeLabel(IType type, long flags, StringBuffer buf) { 860 861 if (getFlag(flags, T_FULLY_QUALIFIED)) { 862 IPackageFragment pack= type.getPackageFragment(); 863 if (!pack.isDefaultPackage()) { 864 getPackageFragmentLabel(pack, (flags & QUALIFIER_FLAGS), buf); 865 buf.append('.'); 866 } 867 } 868 if (getFlag(flags, T_FULLY_QUALIFIED | T_CONTAINER_QUALIFIED)) { 869 IType declaringType= type.getDeclaringType(); 870 if (declaringType != null) { 871 getTypeLabel(declaringType, T_CONTAINER_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); 872 buf.append('.'); 873 } 874 int parentType= type.getParent().getElementType(); 875 if (parentType == IJavaElement.METHOD || parentType == IJavaElement.FIELD || parentType == IJavaElement.INITIALIZER) { getElementLabel(type.getParent(), 0, buf); 877 buf.append('.'); 878 } 879 } 880 881 String typeName= type.getElementName(); 882 if (typeName.length() == 0) { try { 884 if (type.getParent() instanceof IField && type.isEnum()) { 885 typeName= '{' + ELLIPSIS_STRING + '}'; 886 } else { 887 String supertypeName; 888 String [] superInterfaceNames= type.getSuperInterfaceNames(); 889 if (superInterfaceNames.length > 0) { 890 supertypeName= Signature.getSimpleName(superInterfaceNames[0]); 891 } else { 892 supertypeName= Signature.getSimpleName(type.getSuperclassName()); 893 } 894 typeName= Messages.format(JavaUIMessages.JavaElementLabels_anonym_type , supertypeName); 895 } 896 } catch (JavaModelException e) { 897 typeName= JavaUIMessages.JavaElementLabels_anonym; 899 } 900 } 901 buf.append(typeName); 902 if (getFlag(flags, T_TYPE_PARAMETERS)) { 903 if (getFlag(flags, USE_RESOLVED) && type.isResolved()) { 904 BindingKey key= new BindingKey(type.getKey()); 905 if (key.isParameterizedType()) { 906 String [] typeArguments= key.getTypeArguments(); 907 getTypeArgumentSignaturesLabel(typeArguments, flags, buf); 908 } else { 909 String [] typeParameters= Signature.getTypeParameters(key.toSignature()); 910 getTypeParameterSignaturesLabel(typeParameters, flags, buf); 911 } 912 } else if (type.exists()) { 913 try { 914 getTypeParametersLabel(type.getTypeParameters(), flags, buf); 915 } catch (JavaModelException e) { 916 } 918 } 919 } 920 921 if (getFlag(flags, T_CATEGORY) && type.exists()) { 923 try { 924 getCategoryLabel(type, buf); 925 } catch (JavaModelException e) { 926 } 928 } 929 930 if (getFlag(flags, T_POST_QUALIFIED)) { 932 buf.append(CONCAT_STRING); 933 IType declaringType= type.getDeclaringType(); 934 if (declaringType != null) { 935 getTypeLabel(declaringType, T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS), buf); 936 int parentType= type.getParent().getElementType(); 937 if (parentType == IJavaElement.METHOD || parentType == IJavaElement.FIELD || parentType == IJavaElement.INITIALIZER) { buf.append('.'); 939 getElementLabel(type.getParent(), 0, buf); 940 } 941 } else { 942 getPackageFragmentLabel(type.getPackageFragment(), flags & QUALIFIER_FLAGS, buf); 943 } 944 } 945 } 946 947 953 public static void getDeclarationLabel(IJavaElement declaration, long flags, StringBuffer buf) { 954 if (getFlag(flags, D_QUALIFIED)) { 955 IJavaElement openable= (IJavaElement) declaration.getOpenable(); 956 if (openable != null) { 957 buf.append(getElementLabel(openable, CF_QUALIFIED | CU_QUALIFIED | (flags & QUALIFIER_FLAGS))); 958 buf.append('/'); 959 } 960 } 961 if (declaration.getElementType() == IJavaElement.IMPORT_CONTAINER) { 962 buf.append(JavaUIMessages.JavaElementLabels_import_container); 963 } else { 964 buf.append(declaration.getElementName()); 965 } 966 if (getFlag(flags, D_POST_QUALIFIED)) { 968 IJavaElement openable= (IJavaElement) declaration.getOpenable(); 969 if (openable != null) { 970 buf.append(CONCAT_STRING); 971 buf.append(getElementLabel(openable, CF_QUALIFIED | CU_QUALIFIED | (flags & QUALIFIER_FLAGS))); 972 } 973 } 974 } 975 976 982 public static void getClassFileLabel(IClassFile classFile, long flags, StringBuffer buf) { 983 if (getFlag(flags, CF_QUALIFIED)) { 984 IPackageFragment pack= (IPackageFragment) classFile.getParent(); 985 if (!pack.isDefaultPackage()) { 986 getPackageFragmentLabel(pack, (flags & QUALIFIER_FLAGS), buf); 987 buf.append('.'); 988 } 989 } 990 buf.append(classFile.getElementName()); 991 992 if (getFlag(flags, CF_POST_QUALIFIED)) { 993 buf.append(CONCAT_STRING); 994 getPackageFragmentLabel((IPackageFragment) classFile.getParent(), flags & QUALIFIER_FLAGS, buf); 995 } 996 } 997 998 1004 public static void getCompilationUnitLabel(ICompilationUnit cu, long flags, StringBuffer buf) { 1005 if (getFlag(flags, CU_QUALIFIED)) { 1006 IPackageFragment pack= (IPackageFragment) cu.getParent(); 1007 if (!pack.isDefaultPackage()) { 1008 getPackageFragmentLabel(pack, (flags & QUALIFIER_FLAGS), buf); 1009 buf.append('.'); 1010 } 1011 } 1012 buf.append(cu.getElementName()); 1013 1014 if (getFlag(flags, CU_POST_QUALIFIED)) { 1015 buf.append(CONCAT_STRING); 1016 getPackageFragmentLabel((IPackageFragment) cu.getParent(), flags & QUALIFIER_FLAGS, buf); 1017 } 1018 } 1019 1020 1026 public static void getPackageFragmentLabel(IPackageFragment pack, long flags, StringBuffer buf) { 1027 if (getFlag(flags, P_QUALIFIED)) { 1028 getPackageFragmentRootLabel((IPackageFragmentRoot) pack.getParent(), ROOT_QUALIFIED, buf); 1029 buf.append('/'); 1030 } 1031 refreshPackageNamePattern(); 1032 if (pack.isDefaultPackage()) { 1033 buf.append(DEFAULT_PACKAGE); 1034 } else if (getFlag(flags, P_COMPRESSED) && fgPkgNameLength >= 0) { 1035 String name= pack.getElementName(); 1036 int start= 0; 1037 int dot= name.indexOf('.', start); 1038 while (dot > 0) { 1039 if (dot - start > fgPkgNameLength-1) { 1040 buf.append(fgPkgNamePrefix); 1041 if (fgPkgNameChars > 0) 1042 buf.append(name.substring(start, Math.min(start+ fgPkgNameChars, dot))); 1043 buf.append(fgPkgNamePostfix); 1044 } else 1045 buf.append(name.substring(start, dot + 1)); 1046 start= dot + 1; 1047 dot= name.indexOf('.', start); 1048 } 1049 buf.append(name.substring(start)); 1050 } else { 1051 buf.append(pack.getElementName()); 1052 } 1053 if (getFlag(flags, P_POST_QUALIFIED)) { 1054 buf.append(CONCAT_STRING); 1055 getPackageFragmentRootLabel((IPackageFragmentRoot) pack.getParent(), ROOT_QUALIFIED, buf); 1056 } 1057 } 1058 1059 1065 public static void getPackageFragmentRootLabel(IPackageFragmentRoot root, long flags, StringBuffer buf) { 1066 if (root.isArchive()) 1067 getArchiveLabel(root, flags, buf); 1068 else 1069 getFolderLabel(root, flags, buf); 1070 } 1071 1072 private static void getArchiveLabel(IPackageFragmentRoot root, long flags, StringBuffer buf) { 1073 if (getFlag(flags, ROOT_VARIABLE) && getVariableLabel(root, flags, buf)) 1075 return; 1076 boolean external= root.isExternal(); 1077 if (external) 1078 getExternalArchiveLabel(root, flags, buf); 1079 else 1080 getInternalArchiveLabel(root, flags, buf); 1081 } 1082 1083 private static boolean getVariableLabel(IPackageFragmentRoot root, long flags, StringBuffer buf) { 1084 try { 1085 IClasspathEntry rawEntry= root.getRawClasspathEntry(); 1086 if (rawEntry != null && rawEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) { 1087 IPath path= rawEntry.getPath().makeRelative(); 1088 if (getFlag(flags, REFERENCED_ROOT_POST_QUALIFIED)) { 1089 int segements= path.segmentCount(); 1090 if (segements > 0) { 1091 buf.append(path.segment(segements - 1)); 1092 if (segements > 1) { 1093 buf.append(CONCAT_STRING); 1094 buf.append(path.removeLastSegments(1).toOSString()); 1095 } 1096 } else { 1097 buf.append(path.toString()); 1098 } 1099 } else { 1100 buf.append(path.toString()); 1101 } 1102 buf.append(CONCAT_STRING); 1103 if (root.isExternal()) 1104 buf.append(root.getPath().toOSString()); 1105 else 1106 buf.append(root.getPath().makeRelative().toString()); 1107 return true; 1108 } 1109 } catch (JavaModelException e) { 1110 JavaPlugin.log(e); } 1112 return false; 1113 } 1114 1115 private static void getExternalArchiveLabel(IPackageFragmentRoot root, long flags, StringBuffer buf) { 1116 IPath path= root.getPath(); 1117 if (getFlag(flags, REFERENCED_ROOT_POST_QUALIFIED)) { 1118 int segements= path.segmentCount(); 1119 if (segements > 0) { 1120 buf.append(path.segment(segements - 1)); 1121 if (segements > 1 || path.getDevice() != null) { 1122 buf.append(CONCAT_STRING); 1123 buf.append(path.removeLastSegments(1).toOSString()); 1124 } 1125 } else { 1126 buf.append(path.toOSString()); 1127 } 1128 } else { 1129 buf.append(path.toOSString()); 1130 } 1131 } 1132 1133 private static void getInternalArchiveLabel(IPackageFragmentRoot root, long flags, StringBuffer buf) { 1134 IResource resource= root.getResource(); 1135 boolean rootQualified= getFlag(flags, ROOT_QUALIFIED); 1136 boolean referencedQualified= getFlag(flags, REFERENCED_ROOT_POST_QUALIFIED) && isReferenced(root); 1137 if (rootQualified) { 1138 buf.append(root.getPath().makeRelative().toString()); 1139 } else { 1140 buf.append(root.getElementName()); 1141 if (referencedQualified) { 1142 buf.append(CONCAT_STRING); 1143 buf.append(resource.getParent().getFullPath().makeRelative().toString()); 1144 } else if (getFlag(flags, ROOT_POST_QUALIFIED)) { 1145 buf.append(CONCAT_STRING); 1146 buf.append(root.getParent().getPath().makeRelative().toString()); 1147 } 1148 } 1149 } 1150 1151 private static void getFolderLabel(IPackageFragmentRoot root, long flags, StringBuffer buf) { 1152 IResource resource= root.getResource(); 1153 boolean rootQualified= getFlag(flags, ROOT_QUALIFIED); 1154 boolean referencedQualified= getFlag(flags, REFERENCED_ROOT_POST_QUALIFIED) && isReferenced(root); 1155 if (rootQualified) { 1156 buf.append(root.getPath().makeRelative().toString()); 1157 } else { 1158 if (resource != null) { 1159 IPath projectRelativePath= resource.getProjectRelativePath(); 1160 if (projectRelativePath.segmentCount() == 0) { 1161 buf.append(resource.getName()); 1162 referencedQualified= false; 1163 } else { 1164 buf.append(projectRelativePath.toString()); 1165 } 1166 } else 1167 buf.append(root.getElementName()); 1168 if (referencedQualified) { 1169 buf.append(CONCAT_STRING); 1170 buf.append(resource.getProject().getName()); 1171 } else if (getFlag(flags, ROOT_POST_QUALIFIED)) { 1172 buf.append(CONCAT_STRING); 1173 buf.append(root.getParent().getElementName()); 1174 } 1175 } 1176 } 1177 1178 1186 private static boolean isReferenced(IPackageFragmentRoot root) { 1187 IResource resource= root.getResource(); 1188 if (resource != null) { 1189 IProject jarProject= resource.getProject(); 1190 IProject container= root.getJavaProject().getProject(); 1191 return !container.equals(jarProject); 1192 } 1193 return false; 1194 } 1195 1196 private static void refreshPackageNamePattern() { 1197 String pattern= getPkgNamePatternForPackagesView(); 1198 final String EMPTY_STRING= ""; if (pattern.equals(fgPkgNamePattern)) 1200 return; 1201 else if (pattern.length() == 0) { 1202 fgPkgNamePattern= EMPTY_STRING; 1203 fgPkgNameLength= -1; 1204 return; 1205 } 1206 fgPkgNamePattern= pattern; 1207 int i= 0; 1208 fgPkgNameChars= 0; 1209 fgPkgNamePrefix= EMPTY_STRING; 1210 fgPkgNamePostfix= EMPTY_STRING; 1211 while (i < pattern.length()) { 1212 char ch= pattern.charAt(i); 1213 if (Character.isDigit(ch)) { 1214 fgPkgNameChars= ch-48; 1215 if (i > 0) 1216 fgPkgNamePrefix= pattern.substring(0, i); 1217 if (i >= 0) 1218 fgPkgNamePostfix= pattern.substring(i+1); 1219 fgPkgNameLength= fgPkgNamePrefix.length() + fgPkgNameChars + fgPkgNamePostfix.length(); 1220 return; 1221 } 1222 i++; 1223 } 1224 fgPkgNamePrefix= pattern; 1225 fgPkgNameLength= pattern.length(); 1226 } 1227 1228 private static String getPkgNamePatternForPackagesView() { 1229 IPreferenceStore store= PreferenceConstants.getPreferenceStore(); 1230 if (!store.getBoolean(PreferenceConstants.APPEARANCE_COMPRESS_PACKAGE_NAMES)) 1231 return ""; return store.getString(PreferenceConstants.APPEARANCE_PKG_NAME_PATTERN_FOR_PKG_VIEW); 1233 } 1234 1235 1242 public static String getContainerEntryLabel(IPath containerPath, IJavaProject project) throws JavaModelException { 1243 IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, project); 1244 if (container != null) { 1245 return container.getDescription(); 1246 } 1247 ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0)); 1248 if (initializer != null) { 1249 return initializer.getDescription(containerPath, project); 1250 } 1251 return containerPath.toString(); 1252 } 1253 1254} 1255 | Popular Tags |