1 /******************************************************************************* 2 * Copyright (c) 2000, 2018 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 * Red Hat Inc. - use JavaElementLabelsCore 14 *******************************************************************************/ 15 package org.eclipse.jdt.ui; 16 17 import org.eclipse.osgi.util.TextProcessor; 18 19 import org.eclipse.core.runtime.IAdaptable; 20 import org.eclipse.core.runtime.IPath; 21 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.resources.IStorage; 24 25 import org.eclipse.jface.viewers.StyledString; 26 import org.eclipse.jface.viewers.StyledString.Styler; 27 28 import org.eclipse.ui.model.IWorkbenchAdapter; 29 30 import org.eclipse.jdt.core.ClasspathContainerInitializer; 31 import org.eclipse.jdt.core.IClassFile; 32 import org.eclipse.jdt.core.IClasspathContainer; 33 import org.eclipse.jdt.core.ICompilationUnit; 34 import org.eclipse.jdt.core.IField; 35 import org.eclipse.jdt.core.IInitializer; 36 import org.eclipse.jdt.core.IJavaElement; 37 import org.eclipse.jdt.core.IJavaProject; 38 import org.eclipse.jdt.core.ILocalVariable; 39 import org.eclipse.jdt.core.IMethod; 40 import org.eclipse.jdt.core.IPackageFragment; 41 import org.eclipse.jdt.core.IPackageFragmentRoot; 42 import org.eclipse.jdt.core.IType; 43 import org.eclipse.jdt.core.ITypeParameter; 44 import org.eclipse.jdt.core.JavaCore; 45 import org.eclipse.jdt.core.JavaModelException; 46 47 import org.eclipse.jdt.internal.core.manipulation.JavaElementLabelsCore; 48 import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels; 49 import org.eclipse.jdt.internal.corext.util.Strings; 50 51 import org.eclipse.jdt.launching.JavaRuntime; 52 53 import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer; 54 import org.eclipse.jdt.internal.ui.viewsupport.JavaElementLabelComposer; 55 56 57 /** 58 * <code>JavaElementLabels</code> provides helper methods to render names of Java elements. 59 * 60 * @since 3.1 61 * 62 * @noinstantiate This class is not intended to be instantiated by clients. 63 * @noextend This class is not intended to be subclassed by clients. 64 */ 65 public class JavaElementLabels { 66 67 68 /** 69 * Method names contain parameter types. 70 * e.g. <code>foo(int)</code> 71 */ 72 public final static long M_PARAMETER_TYPES= JavaElementLabelsCore.M_PARAMETER_TYPES; 73 74 /** 75 * Method names contain parameter names. 76 * e.g. <code>foo(index)</code> 77 */ 78 public final static long M_PARAMETER_NAMES= JavaElementLabelsCore.M_PARAMETER_NAMES; 79 80 /** 81 * Method labels contain parameter annotations. 82 * E.g. <code>foo(@NonNull int)</code>. 83 * This flag is only valid if {@link #M_PARAMETER_NAMES} or {@link #M_PARAMETER_TYPES} is also set. 84 * @since 3.8 85 */ 86 public final static long M_PARAMETER_ANNOTATIONS= JavaElementLabelsCore.M_PARAMETER_ANNOTATIONS; 87 88 /** 89 * Method names contain type parameters prepended. 90 * e.g. <code><A> foo(A index)</code> 91 */ 92 public final static long M_PRE_TYPE_PARAMETERS= JavaElementLabelsCore.M_PRE_TYPE_PARAMETERS; 93 94 /** 95 * Method names contain type parameters appended. 96 * e.g. <code>foo(A index) <A></code> 97 */ 98 public final static long M_APP_TYPE_PARAMETERS= JavaElementLabelsCore.M_APP_TYPE_PARAMETERS; 99 100 /** 101 * Method names contain thrown exceptions. 102 * e.g. <code>foo throws IOException</code> 103 */ 104 public final static long M_EXCEPTIONS= JavaElementLabelsCore.M_EXCEPTIONS; 105 106 /** 107 * Method names contain return type (appended) 108 * e.g. <code>foo : int</code> 109 */ 110 public final static long M_APP_RETURNTYPE= JavaElementLabelsCore.M_APP_RETURNTYPE; 111 112 /** 113 * Method names contain return type (appended) 114 * e.g. <code>int foo</code> 115 */ 116 public final static long M_PRE_RETURNTYPE= JavaElementLabelsCore.M_PRE_RETURNTYPE; 117 118 /** 119 * Method names are fully qualified. 120 * e.g. <code>java.util.Vector.size</code> 121 */ 122 public final static long M_FULLY_QUALIFIED= JavaElementLabelsCore.M_FULLY_QUALIFIED; 123 124 /** 125 * Method names are post qualified. 126 * e.g. <code>size - java.util.Vector</code> 127 */ 128 public final static long M_POST_QUALIFIED= JavaElementLabelsCore.M_POST_QUALIFIED; 129 130 /** 131 * Initializer names are fully qualified. 132 * e.g. <code>java.util.Vector.{ ... }</code> 133 */ 134 public final static long I_FULLY_QUALIFIED= JavaElementLabelsCore.I_FULLY_QUALIFIED; 135 136 /** 137 * Type names are post qualified. 138 * e.g. <code>{ ... } - java.util.Map</code> 139 */ 140 public final static long I_POST_QUALIFIED= JavaElementLabelsCore.I_POST_QUALIFIED; 141 142 /** 143 * Field names contain the declared type (appended) 144 * e.g. <code>fHello : int</code> 145 */ 146 public final static long F_APP_TYPE_SIGNATURE= JavaElementLabelsCore.F_APP_TYPE_SIGNATURE; 147 148 /** 149 * Field names contain the declared type (prepended) 150 * e.g. <code>int fHello</code> 151 */ 152 public final static long F_PRE_TYPE_SIGNATURE= JavaElementLabelsCore.F_PRE_TYPE_SIGNATURE; 153 154 /** 155 * Fields names are fully qualified. 156 * e.g. <code>java.lang.System.out</code> 157 */ 158 public final static long F_FULLY_QUALIFIED= JavaElementLabelsCore.F_FULLY_QUALIFIED; 159 160 /** 161 * Fields names are post qualified. 162 * e.g. <code>out - java.lang.System</code> 163 */ 164 public final static long F_POST_QUALIFIED= JavaElementLabelsCore.F_POST_QUALIFIED; 165 166 /** 167 * Type names are fully qualified. 168 * e.g. <code>java.util.Map.Entry</code> 169 */ 170 public final static long T_FULLY_QUALIFIED= JavaElementLabelsCore.T_FULLY_QUALIFIED; 171 172 /** 173 * Type names are type container qualified. 174 * e.g. <code>Map.Entry</code> 175 */ 176 public final static long T_CONTAINER_QUALIFIED= JavaElementLabelsCore.T_CONTAINER_QUALIFIED; 177 178 /** 179 * Type names are post qualified. 180 * e.g. <code>Entry - java.util.Map</code> 181 */ 182 public final static long T_POST_QUALIFIED= JavaElementLabelsCore.T_POST_QUALIFIED; 183 184 /** 185 * Type names contain type parameters. 186 * e.g. <code>Map<S, T></code> 187 */ 188 public final static long T_TYPE_PARAMETERS= JavaElementLabelsCore.T_TYPE_PARAMETERS; 189 190 /** 191 * Type parameters are post qualified. 192 * e.g. <code>K - java.util.Map.Entry</code> 193 * 194 * @since 3.5 195 */ 196 public final static long TP_POST_QUALIFIED= JavaElementLabelsCore.TP_POST_QUALIFIED; 197 198 /** 199 * Declarations (import container / declaration, package declaration) are qualified. 200 * e.g. <code>java.util.Vector.class/import container</code> 201 */ 202 public final static long D_QUALIFIED= JavaElementLabelsCore.D_QUALIFIED; 203 204 /** 205 * Declarations (import container / declaration, package declaration) are post qualified. 206 * e.g. <code>import container - java.util.Vector.class</code> 207 */ 208 public final static long D_POST_QUALIFIED= JavaElementLabelsCore.D_POST_QUALIFIED; 209 210 /** 211 * Class file names are fully qualified. 212 * e.g. <code>java.util.Vector.class</code> 213 */ 214 public final static long CF_QUALIFIED= JavaElementLabelsCore.CF_QUALIFIED; 215 216 /** 217 * Class file names are post qualified. 218 * e.g. <code>Vector.class - java.util</code> 219 */ 220 public final static long CF_POST_QUALIFIED= JavaElementLabelsCore.CF_POST_QUALIFIED; 221 222 /** 223 * Compilation unit names are fully qualified. 224 * e.g. <code>java.util.Vector.java</code> 225 */ 226 public final static long CU_QUALIFIED= JavaElementLabelsCore.CU_QUALIFIED; 227 228 /** 229 * Compilation unit names are post qualified. 230 * e.g. <code>Vector.java - java.util</code> 231 */ 232 public final static long CU_POST_QUALIFIED= JavaElementLabelsCore.CU_POST_QUALIFIED; 233 234 /** 235 * Package names are qualified. 236 * e.g. <code>MyProject/src/java.util</code> 237 */ 238 public final static long P_QUALIFIED= JavaElementLabelsCore.P_QUALIFIED; 239 240 /** 241 * Package names are post qualified. 242 * e.g. <code>java.util - MyProject/src</code> 243 */ 244 public final static long P_POST_QUALIFIED= JavaElementLabelsCore.P_POST_QUALIFIED; 245 246 /** 247 * Package names are abbreviated if 248 * {@link PreferenceConstants#APPEARANCE_ABBREVIATE_PACKAGE_NAMES} is <code>true</code> and/or 249 * compressed if {@link PreferenceConstants#APPEARANCE_COMPRESS_PACKAGE_NAMES} is 250 * <code>true</code>. 251 */ 252 public final static long P_COMPRESSED= JavaElementLabelsCore.P_COMPRESSED; 253 254 /** 255 * Package Fragment Roots contain variable name if from a variable. 256 * e.g. <code>JRE_LIB - c:\java\lib\rt.jar</code> 257 */ 258 public final static long ROOT_VARIABLE= JavaElementLabelsCore.ROOT_VARIABLE; 259 260 /** 261 * Package Fragment Roots contain the project name if not an archive (prepended). 262 * e.g. <code>MyProject/src</code> 263 */ 264 public final static long ROOT_QUALIFIED= JavaElementLabelsCore.ROOT_QUALIFIED; 265 266 /** 267 * Package Fragment Roots contain the project name if not an archive (appended). 268 * e.g. <code>src - MyProject</code> 269 */ 270 public final static long ROOT_POST_QUALIFIED= JavaElementLabelsCore.ROOT_POST_QUALIFIED; 271 272 /** 273 * Add root path to all elements except Package Fragment Roots and Java projects. 274 * e.g. <code>java.lang.Vector - C:\java\lib\rt.jar</code> 275 * Option only applies to getElementLabel 276 */ 277 public final static long APPEND_ROOT_PATH= JavaElementLabelsCore.APPEND_ROOT_PATH; 278 279 /** 280 * Add root path to all elements except Package Fragment Roots and Java projects. 281 * e.g. <code>C:\java\lib\rt.jar - java.lang.Vector</code> 282 * Option only applies to getElementLabel 283 */ 284 public final static long PREPEND_ROOT_PATH= JavaElementLabelsCore.PREPEND_ROOT_PATH; 285 286 /** 287 * Post qualify referenced package fragment roots. For example 288 * <code>jdt.jar - org.eclipse.jdt.ui</code> if the jar is referenced 289 * from another project. 290 */ 291 public final static long REFERENCED_ROOT_POST_QUALIFIED= JavaElementLabelsCore.REFERENCED_ROOT_POST_QUALIFIED; 292 293 /** 294 * Specifies to use the resolved information of a IType, IMethod or IField. See {@link IType#isResolved()}. 295 * If resolved information is available, types will be rendered with type parameters of the instantiated type. 296 * Resolved methods render with the parameter types of the method instance. 297 * <code>Vector<String>.get(String)</code> 298 */ 299 public final static long USE_RESOLVED= JavaElementLabelsCore.USE_RESOLVED; 300 301 /** 302 * Specifies to apply color styles to labels. This flag only applies to methods taking or returning a {@link StyledString}. 303 * 304 * @since 3.4 305 */ 306 public final static long COLORIZE= JavaElementLabelsCore.COLORIZE; 307 308 /** 309 * Prepend first category (if any) to field. 310 * @since 3.2 311 */ 312 public final static long F_CATEGORY= JavaElementLabelsCore.F_CATEGORY; 313 /** 314 * Prepend first category (if any) to method. 315 * @since 3.2 316 */ 317 public final static long M_CATEGORY= JavaElementLabelsCore.M_CATEGORY; 318 /** 319 * Prepend first category (if any) to type. 320 * @since 3.2 321 */ 322 public final static long T_CATEGORY= JavaElementLabelsCore.T_CATEGORY; 323 324 /** 325 * Show category for all elements. 326 * @since 3.2 327 */ 328 public final static long ALL_CATEGORY= JavaElementLabelsCore.ALL_CATEGORY; 329 330 /** 331 * Qualify all elements 332 */ 333 public final static long ALL_FULLY_QUALIFIED= JavaElementLabelsCore.ALL_FULLY_QUALIFIED; 334 335 /** 336 * Post qualify all elements 337 */ 338 public final static long ALL_POST_QUALIFIED= JavaElementLabelsCore.ALL_POST_QUALIFIED; 339 340 /** 341 * Default options (M_PARAMETER_TYPES, M_APP_TYPE_PARAMETERS & T_TYPE_PARAMETERS enabled) 342 */ 343 public final static long ALL_DEFAULT= JavaElementLabelsCore.ALL_DEFAULT; 344 345 /** 346 * Default qualify options (All except Root and Package) 347 */ 348 public final static long DEFAULT_QUALIFIED= JavaElementLabelsCore.DEFAULT_QUALIFIED; 349 350 /** 351 * Default post qualify options (All except Root and Package) 352 */ 353 public final static long DEFAULT_POST_QUALIFIED= JavaElementLabelsCore.DEFAULT_POST_QUALIFIED; 354 355 /** 356 * User-readable string for separating post qualified names (e.g. " - "). 357 */ 358 public final static String CONCAT_STRING= JavaElementLabelsCore.CONCAT_STRING; 359 /** 360 * User-readable string for separating list items (e.g. ", "). 361 */ 362 public final static String COMMA_STRING= JavaElementLabelsCore.COMMA_STRING; 363 /** 364 * User-readable string for separating the return type (e.g. " : "). 365 */ 366 public final static String DECL_STRING= JavaElementLabelsCore.DECL_STRING; 367 /** 368 * User-readable string for concatenating categories (e.g. " "). 369 * @since 3.5 370 */ 371 public final static String CATEGORY_SEPARATOR_STRING= JavaElementLabelsCore.CATEGORY_SEPARATOR_STRING; 372 /** 373 * User-readable string for ellipsis ("..."). 374 */ 375 public final static String ELLIPSIS_STRING= JavaElementLabelsCore.ELLIPSIS_STRING; 376 /** 377 * User-readable string for the default package name (e.g. "(default package)"). 378 */ 379 public final static String DEFAULT_PACKAGE= JavaElementLabelsCore.DEFAULT_PACKAGE; 380 381 382 private static final Styler DECORATIONS_STYLE= StyledString.DECORATIONS_STYLER; 383 384 JavaElementLabels()385 private JavaElementLabels() { 386 } 387 388 /** 389 * Returns the label of the given object. The object must be of type {@link IJavaElement} or adapt to {@link IWorkbenchAdapter}. 390 * If the element type is not known, the empty string is returned. 391 * The returned label is BiDi-processed with {@link TextProcessor#process(String, String)}. 392 * 393 * @param obj object to get the label for 394 * @param flags the rendering flags 395 * @return the label or the empty string if the object type is not supported 396 */ getTextLabel(Object obj, long flags)397 public static String getTextLabel(Object obj, long flags) { 398 if (obj instanceof IJavaElement) { 399 return getElementLabel((IJavaElement) obj, flags); 400 401 } else if (obj instanceof IResource) { 402 return BasicElementLabels.getResourceName((IResource) obj); 403 404 } else if (obj instanceof ClassPathContainer) { 405 ClassPathContainer container= (ClassPathContainer) obj; 406 IPath containerPath= container.getClasspathEntry().getPath(); 407 try { 408 return getContainerEntryLabel(containerPath, container.getJavaProject()); 409 } catch (JavaModelException e) { 410 return BasicElementLabels.getPathLabel(containerPath, false); 411 } 412 413 } else if (obj instanceof IStorage) { 414 return BasicElementLabels.getResourceName(((IStorage) obj).getName()); 415 416 } else if (obj instanceof IAdaptable) { 417 IWorkbenchAdapter wbadapter= ((IAdaptable)obj).getAdapter(IWorkbenchAdapter.class); 418 if (wbadapter != null) { 419 return org.eclipse.jdt.internal.core.manipulation.util.Strings.markLTR(wbadapter.getLabel(obj)); 420 } 421 } 422 return ""; //$NON-NLS-1$ 423 } 424 425 /** 426 * Returns the styled label of the given object. The object must be of type {@link IJavaElement} or adapt to {@link IWorkbenchAdapter}. 427 * If the element type is not known, the empty string is returned. 428 * The returned label is BiDi-processed with {@link TextProcessor#process(String, String)}. 429 * 430 * @param obj object to get the label for 431 * @param flags the rendering flags 432 * @return the label or the empty string if the object type is not supported 433 * 434 * @since 3.4 435 */ getStyledTextLabel(Object obj, long flags)436 public static StyledString getStyledTextLabel(Object obj, long flags) { 437 if (obj instanceof IJavaElement) { 438 return getStyledElementLabel((IJavaElement) obj, flags); 439 440 } else if (obj instanceof IResource) { 441 return getStyledResourceLabel((IResource) obj); 442 443 } else if (obj instanceof ClassPathContainer) { 444 ClassPathContainer container= (ClassPathContainer) obj; 445 return getStyledContainerEntryLabel(container.getClasspathEntry().getPath(), container.getJavaProject()); 446 447 } else if (obj instanceof IStorage) { 448 return getStyledStorageLabel((IStorage) obj); 449 450 } else if (obj instanceof IAdaptable) { 451 IWorkbenchAdapter wbadapter= ((IAdaptable)obj).getAdapter(IWorkbenchAdapter.class); 452 if (wbadapter != null) { 453 return Strings.markLTR(new StyledString(wbadapter.getLabel(obj))); 454 } 455 } 456 return new StyledString(); 457 } 458 459 /** 460 * Returns the styled string for the given resource. 461 * The returned label is BiDi-processed with {@link TextProcessor#process(String, String)}. 462 * 463 * @param resource the resource 464 * @return the styled string 465 * @since 3.4 466 */ getStyledResourceLabel(IResource resource)467 private static StyledString getStyledResourceLabel(IResource resource) { 468 StyledString result= new StyledString(resource.getName()); 469 return Strings.markLTR(result); 470 } 471 472 /** 473 * Returns the styled string for the given storage. 474 * The returned label is BiDi-processed with {@link TextProcessor#process(String, String)}. 475 * 476 * @param storage the storage 477 * @return the styled string 478 * @since 3.4 479 */ getStyledStorageLabel(IStorage storage)480 private static StyledString getStyledStorageLabel(IStorage storage) { 481 StyledString result= new StyledString(storage.getName()); 482 return Strings.markLTR(result); 483 } 484 485 486 /** 487 * Returns the label for a Java element with the flags as defined by this class. 488 * 489 * @param element the element to render 490 * @param flags the rendering flags 491 * @return the label of the Java element 492 */ getElementLabel(IJavaElement element, long flags)493 public static String getElementLabel(IJavaElement element, long flags) { 494 StringBuffer result= new StringBuffer(); 495 getElementLabel(element, flags, result); 496 return org.eclipse.jdt.internal.core.manipulation.util.Strings.markJavaElementLabelLTR(result.toString()); 497 } 498 499 /** 500 * Returns the styled label for a Java element with the flags as defined by this class. 501 * 502 * @param element the element to render 503 * @param flags the rendering flags 504 * @return the label of the Java element 505 * 506 * @since 3.4 507 */ getStyledElementLabel(IJavaElement element, long flags)508 public static StyledString getStyledElementLabel(IJavaElement element, long flags) { 509 StyledString result= new StyledString(); 510 getElementLabel(element, flags, result); 511 return Strings.markJavaElementLabelLTR(result); 512 } 513 514 /** 515 * Returns the label for a Java element with the flags as defined by this class. 516 * 517 * @param element the element to render 518 * @param flags the rendering flags 519 * @param buf the buffer to append the resulting label to 520 */ getElementLabel(IJavaElement element, long flags, StringBuffer buf)521 public static void getElementLabel(IJavaElement element, long flags, StringBuffer buf) { 522 new JavaElementLabelComposer(buf).appendElementLabel(element, flags); 523 } 524 525 /** 526 * Returns the styled label for a Java element with the flags as defined by this class. 527 * 528 * @param element the element to render 529 * @param flags the rendering flags 530 * @param result the buffer to append the resulting label to 531 * 532 * @since 3.4 533 */ getElementLabel(IJavaElement element, long flags, StyledString result)534 public static void getElementLabel(IJavaElement element, long flags, StyledString result) { 535 new JavaElementLabelComposer(result).appendElementLabel(element, flags); 536 } 537 538 539 540 /** 541 * Appends the label for a method to a {@link StringBuffer}. Considers the M_* flags. 542 * 543 * @param method the element to render 544 * @param flags the rendering flags. Flags with names starting with 'M_' are considered. 545 * @param buf the buffer to append the resulting label to 546 */ getMethodLabel(IMethod method, long flags, StringBuffer buf)547 public static void getMethodLabel(IMethod method, long flags, StringBuffer buf) { 548 new JavaElementLabelComposer(buf).appendMethodLabel(method, flags); 549 } 550 551 /** 552 * Appends the label for a method to a {@link StyledString}. Considers the M_* flags. 553 * 554 * @param method the element to render 555 * @param flags the rendering flags. Flags with names starting with 'M_' are considered. 556 * @param result the buffer to append the resulting label to 557 * 558 * @since 3.4 559 */ getMethodLabel(IMethod method, long flags, StyledString result)560 public static void getMethodLabel(IMethod method, long flags, StyledString result) { 561 new JavaElementLabelComposer(result).appendMethodLabel(method, flags); 562 } 563 564 /** 565 * Appends the label for a field to a {@link StringBuffer}. Considers the F_* flags. 566 * 567 * @param field the element to render 568 * @param flags the rendering flags. Flags with names starting with 'F_' are considered. 569 * @param buf the buffer to append the resulting label to 570 */ getFieldLabel(IField field, long flags, StringBuffer buf)571 public static void getFieldLabel(IField field, long flags, StringBuffer buf) { 572 new JavaElementLabelComposer(buf).appendFieldLabel(field, flags); 573 } 574 575 /** 576 * Appends the style label for a field to a {@link StyledString}. Considers the F_* flags. 577 * 578 * @param field the element to render 579 * @param flags the rendering flags. Flags with names starting with 'F_' are considered. 580 * @param result the buffer to append the resulting label to 581 * 582 * @since 3.4 583 */ getFieldLabel(IField field, long flags, StyledString result)584 public static void getFieldLabel(IField field, long flags, StyledString result) { 585 new JavaElementLabelComposer(result).appendFieldLabel(field, flags); 586 } 587 588 /** 589 * Appends the label for a local variable to a {@link StringBuffer}. 590 * 591 * @param localVariable the element to render 592 * @param flags the rendering flags. Flags with names starting with 'F_' are considered. 593 * @param buf the buffer to append the resulting label to 594 */ getLocalVariableLabel(ILocalVariable localVariable, long flags, StringBuffer buf)595 public static void getLocalVariableLabel(ILocalVariable localVariable, long flags, StringBuffer buf) { 596 new JavaElementLabelComposer(buf).appendLocalVariableLabel(localVariable, flags); 597 } 598 599 /** 600 * Appends the styled label for a local variable to a {@link StyledString}. 601 * 602 * @param localVariable the element to render 603 * @param flags the rendering flags. Flags with names starting with 'F_' are considered. 604 * @param result the buffer to append the resulting label to 605 * 606 * @since 3.4 607 */ getLocalVariableLabel(ILocalVariable localVariable, long flags, StyledString result)608 public static void getLocalVariableLabel(ILocalVariable localVariable, long flags, StyledString result) { 609 new JavaElementLabelComposer(result).appendLocalVariableLabel(localVariable, flags); 610 } 611 612 613 /** 614 * Appends the label for a initializer to a {@link StringBuffer}. Considers the I_* flags. 615 * 616 * @param initializer the element to render 617 * @param flags the rendering flags. Flags with names starting with 'I_' are considered. 618 * @param buf the buffer to append the resulting label to 619 */ getInitializerLabel(IInitializer initializer, long flags, StringBuffer buf)620 public static void getInitializerLabel(IInitializer initializer, long flags, StringBuffer buf) { 621 new JavaElementLabelComposer(buf).appendInitializerLabel(initializer, flags); 622 } 623 624 /** 625 * Appends the label for a initializer to a {@link StyledString}. Considers the I_* flags. 626 * 627 * @param initializer the element to render 628 * @param flags the rendering flags. Flags with names starting with 'I_' are considered. 629 * @param result the buffer to append the resulting label to 630 * 631 * @since 3.4 632 */ getInitializerLabel(IInitializer initializer, long flags, StyledString result)633 public static void getInitializerLabel(IInitializer initializer, long flags, StyledString result) { 634 new JavaElementLabelComposer(result).appendInitializerLabel(initializer, flags); 635 } 636 637 /** 638 * Appends the label for a type to a {@link StringBuffer}. Considers the T_* flags. 639 * 640 * @param type the element to render 641 * @param flags the rendering flags. Flags with names starting with 'T_' are considered. 642 * @param buf the buffer to append the resulting label to 643 */ getTypeLabel(IType type, long flags, StringBuffer buf)644 public static void getTypeLabel(IType type, long flags, StringBuffer buf) { 645 new JavaElementLabelComposer(buf).appendTypeLabel(type, flags); 646 } 647 648 /** 649 * Appends the label for a type to a {@link StyledString}. Considers the T_* flags. 650 * 651 * @param type the element to render 652 * @param flags the rendering flags. Flags with names starting with 'T_' are considered. 653 * @param result the buffer to append the resulting label to 654 * 655 * @since 3.4 656 */ getTypeLabel(IType type, long flags, StyledString result)657 public static void getTypeLabel(IType type, long flags, StyledString result) { 658 new JavaElementLabelComposer(result).appendTypeLabel(type, flags); 659 } 660 661 662 /** 663 * Appends the label for a type parameter to a {@link StringBuffer}. Considers the TP_* flags. 664 * 665 * @param typeParameter the element to render 666 * @param flags the rendering flags. Flags with names starting with 'TP_' are considered. 667 * @param buf the buffer to append the resulting label to 668 * 669 * @since 3.5 670 */ getTypeParameterLabel(ITypeParameter typeParameter, long flags, StringBuffer buf)671 public static void getTypeParameterLabel(ITypeParameter typeParameter, long flags, StringBuffer buf) { 672 new JavaElementLabelComposer(buf).appendTypeParameterLabel(typeParameter, flags); 673 } 674 675 /** 676 * Appends the label for a type parameter to a {@link StyledString}. Considers the TP_* flags. 677 * 678 * @param typeParameter the element to render 679 * @param flags the rendering flags. Flags with names starting with 'TP_' are considered. 680 * @param result the buffer to append the resulting label to 681 * 682 * @since 3.5 683 */ getTypeParameterLabel(ITypeParameter typeParameter, long flags, StyledString result)684 public static void getTypeParameterLabel(ITypeParameter typeParameter, long flags, StyledString result) { 685 new JavaElementLabelComposer(result).appendTypeParameterLabel(typeParameter, flags); 686 } 687 688 689 /** 690 * Appends the label for a import container, import or package declaration to a {@link StringBuffer}. Considers the D_* flags. 691 * 692 * @param declaration the element to render 693 * @param flags the rendering flags. Flags with names starting with 'D_' are considered. 694 * @param buf the buffer to append the resulting label to 695 */ getDeclarationLabel(IJavaElement declaration, long flags, StringBuffer buf)696 public static void getDeclarationLabel(IJavaElement declaration, long flags, StringBuffer buf) { 697 new JavaElementLabelComposer(buf).appendDeclarationLabel(declaration, flags); 698 } 699 700 /** 701 * Appends the label for a import container, import or package declaration to a {@link StyledString}. Considers the D_* flags. 702 * 703 * @param declaration the element to render 704 * @param flags the rendering flags. Flags with names starting with 'D_' are considered. 705 * @param result the buffer to append the resulting label to 706 * 707 * @since 3.4 708 */ getDeclarationLabel(IJavaElement declaration, long flags, StyledString result)709 public static void getDeclarationLabel(IJavaElement declaration, long flags, StyledString result) { 710 new JavaElementLabelComposer(result).appendDeclarationLabel(declaration, flags); 711 } 712 713 /** 714 * Appends the label for a class file to a {@link StringBuffer}. Considers the CF_* flags. 715 * 716 * @param classFile the element to render 717 * @param flags the rendering flags. Flags with names starting with 'CF_' are considered. 718 * @param buf the buffer to append the resulting label to 719 */ getClassFileLabel(IClassFile classFile, long flags, StringBuffer buf)720 public static void getClassFileLabel(IClassFile classFile, long flags, StringBuffer buf) { 721 new JavaElementLabelComposer(buf).appendClassFileLabel(classFile, flags); 722 } 723 724 /** 725 * Appends the label for a class file to a {@link StyledString}. Considers the CF_* flags. 726 * 727 * @param classFile the element to render 728 * @param flags the rendering flags. Flags with names starting with 'CF_' are considered. 729 * @param result the buffer to append the resulting label to 730 * 731 * @since 3.4 732 */ getClassFileLabel(IClassFile classFile, long flags, StyledString result)733 public static void getClassFileLabel(IClassFile classFile, long flags, StyledString result) { 734 new JavaElementLabelComposer(result).appendClassFileLabel(classFile, flags); 735 } 736 737 /** 738 * Appends the label for a compilation unit to a {@link StringBuffer}. Considers the CU_* flags. 739 * 740 * @param cu the element to render 741 * @param flags the rendering flags. Flags with names starting with 'CU_' are considered. 742 * @param buf the buffer to append the resulting label to 743 */ getCompilationUnitLabel(ICompilationUnit cu, long flags, StringBuffer buf)744 public static void getCompilationUnitLabel(ICompilationUnit cu, long flags, StringBuffer buf) { 745 new JavaElementLabelComposer(buf).appendCompilationUnitLabel(cu, flags); 746 } 747 748 /** 749 * Appends the label for a compilation unit to a {@link StyledString}. Considers the CU_* flags. 750 * 751 * @param cu the element to render 752 * @param flags the rendering flags. Flags with names starting with 'CU_' are considered. 753 * @param result the buffer to append the resulting label to 754 * 755 * @since 3.4 756 */ getCompilationUnitLabel(ICompilationUnit cu, long flags, StyledString result)757 public static void getCompilationUnitLabel(ICompilationUnit cu, long flags, StyledString result) { 758 new JavaElementLabelComposer(result).appendCompilationUnitLabel(cu, flags); 759 } 760 761 /** 762 * Appends the label for a package fragment to a {@link StringBuffer}. Considers the P_* flags. 763 * 764 * @param pack the element to render 765 * @param flags the rendering flags. Flags with names starting with P_' are considered. 766 * @param buf the buffer to append the resulting label to 767 */ getPackageFragmentLabel(IPackageFragment pack, long flags, StringBuffer buf)768 public static void getPackageFragmentLabel(IPackageFragment pack, long flags, StringBuffer buf) { 769 new JavaElementLabelComposer(buf).appendPackageFragmentLabel(pack, flags); 770 } 771 772 /** 773 * Appends the label for a package fragment to a {@link StyledString}. Considers the P_* flags. 774 * 775 * @param pack the element to render 776 * @param flags the rendering flags. Flags with names starting with P_' are considered. 777 * @param result the buffer to append the resulting label to 778 * 779 * @since 3.4 780 */ getPackageFragmentLabel(IPackageFragment pack, long flags, StyledString result)781 public static void getPackageFragmentLabel(IPackageFragment pack, long flags, StyledString result) { 782 new JavaElementLabelComposer(result).appendPackageFragmentLabel(pack, flags); 783 } 784 785 /** 786 * Appends the label for a package fragment root to a {@link StringBuffer}. Considers the ROOT_* flags. 787 * 788 * @param root the element to render 789 * @param flags the rendering flags. Flags with names starting with ROOT_' are considered. 790 * @param buf the buffer to append the resulting label to 791 */ getPackageFragmentRootLabel(IPackageFragmentRoot root, long flags, StringBuffer buf)792 public static void getPackageFragmentRootLabel(IPackageFragmentRoot root, long flags, StringBuffer buf) { 793 new JavaElementLabelComposer(buf).appendPackageFragmentRootLabel(root, flags); 794 } 795 796 /** 797 * Appends the label for a package fragment root to a {@link StyledString}. Considers the ROOT_* flags. 798 * 799 * @param root the element to render 800 * @param flags the rendering flags. Flags with names starting with ROOT_' are considered. 801 * @param result the buffer to append the resulting label to 802 * 803 * @since 3.4 804 */ getPackageFragmentRootLabel(IPackageFragmentRoot root, long flags, StyledString result)805 public static void getPackageFragmentRootLabel(IPackageFragmentRoot root, long flags, StyledString result) { 806 new JavaElementLabelComposer(result).appendPackageFragmentRootLabel(root, flags); 807 } 808 809 /** 810 * Returns the label of a classpath container. 811 * The returned label is BiDi-processed with {@link TextProcessor#process(String, String)}. 812 * 813 * @param containerPath the path of the container 814 * @param project the project the container is resolved in 815 * @return the label of the classpath container 816 * @throws JavaModelException when resolving of the container failed 817 */ getContainerEntryLabel(IPath containerPath, IJavaProject project)818 public static String getContainerEntryLabel(IPath containerPath, IJavaProject project) throws JavaModelException { 819 IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, project); 820 if (container != null) { 821 return org.eclipse.jdt.internal.core.manipulation.util.Strings.markLTR(container.getDescription()); 822 } 823 ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0)); 824 if (initializer != null) { 825 return org.eclipse.jdt.internal.core.manipulation.util.Strings.markLTR(initializer.getDescription(containerPath, project)); 826 } 827 return BasicElementLabels.getPathLabel(containerPath, false); 828 } 829 830 /** 831 * Returns the styled label of a classpath container. 832 * The returned label is BiDi-processed with {@link TextProcessor#process(String, String)}. 833 * 834 * @param containerPath the path of the container 835 * @param project the project the container is resolved in 836 * @return the label of the classpath container 837 * 838 * @since 3.4 839 */ getStyledContainerEntryLabel(IPath containerPath, IJavaProject project)840 public static StyledString getStyledContainerEntryLabel(IPath containerPath, IJavaProject project) { 841 try { 842 IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, project); 843 String description= null; 844 if (container != null) { 845 description= container.getDescription(); 846 } 847 if (description == null) { 848 ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0)); 849 if (initializer != null) { 850 description= initializer.getDescription(containerPath, project); 851 } 852 } 853 if (description != null) { 854 StyledString str= new StyledString(description); 855 if (containerPath.segmentCount() > 0 && JavaRuntime.JRE_CONTAINER.equals(containerPath.segment(0))) { 856 int index= description.indexOf('['); 857 if (index != -1) { 858 str.setStyle(index, description.length() - index, DECORATIONS_STYLE); 859 } 860 } 861 return Strings.markLTR(str); 862 } 863 } catch (JavaModelException e) { 864 // ignore 865 } 866 return new StyledString(BasicElementLabels.getPathLabel(containerPath, false)); 867 } 868 869 } 870