1 12 package org.eclipse.jdt.internal.corext.util; 13 14 import java.util.Arrays ; 15 import java.util.HashSet ; 16 import java.util.Map ; 17 18 import org.eclipse.text.edits.MalformedTreeException; 19 import org.eclipse.text.edits.TextEdit; 20 21 import org.eclipse.core.runtime.Assert; 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IPath; 24 import org.eclipse.core.runtime.IProgressMonitor; 25 import org.eclipse.core.runtime.IStatus; 26 import org.eclipse.core.runtime.NullProgressMonitor; 27 import org.eclipse.core.runtime.Path; 28 import org.eclipse.core.runtime.SubProgressMonitor; 29 30 import org.eclipse.core.filebuffers.FileBuffers; 31 import org.eclipse.core.filebuffers.ITextFileBufferManager; 32 import org.eclipse.core.filebuffers.LocationKind; 33 34 import org.eclipse.core.resources.IFile; 35 import org.eclipse.core.resources.IResource; 36 import org.eclipse.core.resources.IStorage; 37 38 import org.eclipse.jface.text.BadLocationException; 39 import org.eclipse.jface.text.Document; 40 import org.eclipse.jface.text.IDocument; 41 import org.eclipse.jface.text.RewriteSessionEditProcessor; 42 43 import org.eclipse.jdt.core.ClasspathContainerInitializer; 44 import org.eclipse.jdt.core.Flags; 45 import org.eclipse.jdt.core.IClasspathContainer; 46 import org.eclipse.jdt.core.IClasspathEntry; 47 import org.eclipse.jdt.core.ICompilationUnit; 48 import org.eclipse.jdt.core.IField; 49 import org.eclipse.jdt.core.IJarEntryResource; 50 import org.eclipse.jdt.core.IJavaElement; 51 import org.eclipse.jdt.core.IJavaProject; 52 import org.eclipse.jdt.core.ILocalVariable; 53 import org.eclipse.jdt.core.IMember; 54 import org.eclipse.jdt.core.IMethod; 55 import org.eclipse.jdt.core.IPackageFragment; 56 import org.eclipse.jdt.core.IPackageFragmentRoot; 57 import org.eclipse.jdt.core.IType; 58 import org.eclipse.jdt.core.ITypeHierarchy; 59 import org.eclipse.jdt.core.JavaCore; 60 import org.eclipse.jdt.core.JavaModelException; 61 import org.eclipse.jdt.core.Signature; 62 import org.eclipse.jdt.core.WorkingCopyOwner; 63 import org.eclipse.jdt.core.compiler.CharOperation; 64 65 import org.eclipse.jdt.internal.corext.CorextMessages; 66 import org.eclipse.jdt.internal.corext.ValidateEditException; 67 68 import org.eclipse.jdt.launching.IVMInstall; 69 import org.eclipse.jdt.launching.IVMInstall2; 70 import org.eclipse.jdt.launching.JavaRuntime; 71 import org.eclipse.jdt.launching.environments.IExecutionEnvironment; 72 73 import org.eclipse.jdt.internal.ui.JavaUIStatus; 74 75 78 public final class JavaModelUtil { 79 80 93 public static final String DEFAULT_CU_SUFFIX= ".java"; 95 101 public static IType findType(IJavaProject jproject, String fullyQualifiedName) throws JavaModelException { 102 IType type= jproject.findType(fullyQualifiedName); 104 if (type != null) 105 return type; 106 IPackageFragmentRoot[] roots= jproject.getPackageFragmentRoots(); 107 for (int i= 0; i < roots.length; i++) { 108 IPackageFragmentRoot root= roots[i]; 109 type= findType(root, fullyQualifiedName); 110 if (type != null && type.exists()) 111 return type; 112 } 113 return null; 114 } 115 116 123 public static IType findType(IJavaProject jproject, String fullyQualifiedName, WorkingCopyOwner owner) throws JavaModelException { 124 IType type= jproject.findType(fullyQualifiedName, owner); 126 if (type != null) 127 return type; 128 IPackageFragmentRoot[] roots= jproject.getPackageFragmentRoots(); 129 for (int i= 0; i < roots.length; i++) { 130 IPackageFragmentRoot root= roots[i]; 131 type= findType(root, fullyQualifiedName); 132 if (type != null && type.exists()) 133 return type; 134 } 135 return null; 136 } 137 138 139 140 private static IType findType(IPackageFragmentRoot root, String fullyQualifiedName) throws JavaModelException{ 141 IJavaElement[] children= root.getChildren(); 142 for (int i= 0; i < children.length; i++) { 143 IJavaElement element= children[i]; 144 if (element.getElementType() == IJavaElement.PACKAGE_FRAGMENT){ 145 IPackageFragment pack= (IPackageFragment)element; 146 if (! fullyQualifiedName.startsWith(pack.getElementName())) 147 continue; 148 IType type= findType(pack, fullyQualifiedName); 149 if (type != null && type.exists()) 150 return type; 151 } 152 } 153 return null; 154 } 155 156 private static IType findType(IPackageFragment pack, String fullyQualifiedName) throws JavaModelException{ 157 ICompilationUnit[] cus= pack.getCompilationUnits(); 158 for (int i= 0; i < cus.length; i++) { 159 ICompilationUnit unit= cus[i]; 160 IType type= findType(unit, fullyQualifiedName); 161 if (type != null && type.exists()) 162 return type; 163 } 164 return null; 165 } 166 167 private static IType findType(ICompilationUnit cu, String fullyQualifiedName) throws JavaModelException{ 168 IType[] types= cu.getAllTypes(); 169 for (int i= 0; i < types.length; i++) { 170 IType type= types[i]; 171 if (getFullyQualifiedName(type).equals(fullyQualifiedName)) 172 return type; 173 } 174 return null; 175 } 176 177 185 public static IJavaElement findTypeContainer(IJavaProject jproject, String typeContainerName) throws JavaModelException { 186 IJavaElement result= jproject.findType(typeContainerName); 188 if (result == null) { 189 IPath path= new Path(typeContainerName.replace('.', '/')); 191 result= jproject.findElement(path); 192 if (!(result instanceof IPackageFragment)) { 193 result= null; 194 } 195 196 } 197 return result; 198 } 199 200 207 public static IType findTypeInCompilationUnit(ICompilationUnit cu, String typeQualifiedName) throws JavaModelException { 208 IType[] types= cu.getAllTypes(); 209 for (int i= 0; i < types.length; i++) { 210 String currName= getTypeQualifiedName(types[i]); 211 if (typeQualifiedName.equals(currName)) { 212 return types[i]; 213 } 214 } 215 return null; 216 } 217 218 227 public static IJavaElement findInCompilationUnit(ICompilationUnit cu, IJavaElement element) { 228 IJavaElement[] elements= cu.findElements(element); 229 if (elements != null && elements.length > 0) { 230 return elements[0]; 231 } 232 return null; 233 } 234 235 241 public static String getTypeQualifiedName(IType type) { 242 try { 243 if (type.isBinary() && !type.isAnonymous()) { 244 IType declaringType= type.getDeclaringType(); 245 if (declaringType != null) { 246 return getTypeQualifiedName(declaringType) + '.' + type.getElementName(); 247 } 248 } 249 } catch (JavaModelException e) { 250 } 252 return type.getTypeQualifiedName('.'); 253 } 254 255 261 public static String getFullyQualifiedName(IType type) { 262 try { 263 if (type.isBinary() && !type.isAnonymous()) { 264 IType declaringType= type.getDeclaringType(); 265 if (declaringType != null) { 266 return getFullyQualifiedName(declaringType) + '.' + type.getElementName(); 267 } 268 } 269 } catch (JavaModelException e) { 270 } 272 return type.getFullyQualifiedName('.'); 273 } 274 275 278 public static String getTypeContainerName(IType type) { 279 IType outerType= type.getDeclaringType(); 280 if (outerType != null) { 281 return getFullyQualifiedName(outerType); 282 } else { 283 return type.getPackageFragment().getElementName(); 284 } 285 } 286 287 288 292 public static String concatenateName(String name1, String name2) { 293 StringBuffer buf= new StringBuffer (); 294 if (name1 != null && name1.length() > 0) { 295 buf.append(name1); 296 } 297 if (name2 != null && name2.length() > 0) { 298 if (buf.length() > 0) { 299 buf.append('.'); 300 } 301 buf.append(name2); 302 } 303 return buf.toString(); 304 } 305 306 310 public static String concatenateName(char[] name1, char[] name2) { 311 StringBuffer buf= new StringBuffer (); 312 if (name1 != null && name1.length > 0) { 313 buf.append(name1); 314 } 315 if (name2 != null && name2.length > 0) { 316 if (buf.length() > 0) { 317 buf.append('.'); 318 } 319 buf.append(name2); 320 } 321 return buf.toString(); 322 } 323 324 330 public static boolean isVisible(IMember member, IPackageFragment pack) throws JavaModelException { 331 332 int type= member.getElementType(); 333 if (type == IJavaElement.INITIALIZER || (type == IJavaElement.METHOD && member.getElementName().startsWith("<"))) { return false; 335 } 336 337 int otherflags= member.getFlags(); 338 IType declaringType= member.getDeclaringType(); 339 if (Flags.isPublic(otherflags) || (declaringType != null && isInterfaceOrAnnotation(declaringType))) { 340 return true; 341 } else if (Flags.isPrivate(otherflags)) { 342 return false; 343 } 344 345 IPackageFragment otherpack= (IPackageFragment) member.getAncestor(IJavaElement.PACKAGE_FRAGMENT); 346 return (pack != null && otherpack != null && isSamePackage(pack, otherpack)); 347 } 348 349 355 public static boolean isVisibleInHierarchy(IMember member, IPackageFragment pack) throws JavaModelException { 356 int type= member.getElementType(); 357 if (type == IJavaElement.INITIALIZER || (type == IJavaElement.METHOD && member.getElementName().startsWith("<"))) { return false; 359 } 360 361 int otherflags= member.getFlags(); 362 363 IType declaringType= member.getDeclaringType(); 364 if (Flags.isPublic(otherflags) || Flags.isProtected(otherflags) || (declaringType != null && isInterfaceOrAnnotation(declaringType))) { 365 return true; 366 } else if (Flags.isPrivate(otherflags)) { 367 return false; 368 } 369 370 IPackageFragment otherpack= (IPackageFragment) member.getAncestor(IJavaElement.PACKAGE_FRAGMENT); 371 return (pack != null && pack.equals(otherpack)); 372 } 373 374 375 379 public static IPackageFragmentRoot getPackageFragmentRoot(IJavaElement element) { 380 return (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); 381 } 382 383 393 public static IMethod findMethod(String name, String [] paramTypes, boolean isConstructor, IType type) throws JavaModelException { 394 IMethod[] methods= type.getMethods(); 395 for (int i= 0; i < methods.length; i++) { 396 if (isSameMethodSignature(name, paramTypes, isConstructor, methods[i])) { 397 return methods[i]; 398 } 399 } 400 return null; 401 } 402 403 416 public static IMethod findMethodInHierarchy(ITypeHierarchy hierarchy, IType type, String name, String [] paramTypes, boolean isConstructor) throws JavaModelException { 417 IMethod method= findMethod(name, paramTypes, isConstructor, type); 418 if (method != null) { 419 return method; 420 } 421 IType superClass= hierarchy.getSuperclass(type); 422 if (superClass != null) { 423 IMethod res= findMethodInHierarchy(hierarchy, superClass, name, paramTypes, isConstructor); 424 if (res != null) { 425 return res; 426 } 427 } 428 if (!isConstructor) { 429 IType[] superInterfaces= hierarchy.getSuperInterfaces(type); 430 for (int i= 0; i < superInterfaces.length; i++) { 431 IMethod res= findMethodInHierarchy(hierarchy, superInterfaces[i], name, paramTypes, false); 432 if (res != null) { 433 return res; 434 } 435 } 436 } 437 return method; 438 } 439 440 441 451 public static boolean isSameMethodSignature(String name, String [] paramTypes, boolean isConstructor, IMethod curr) throws JavaModelException { 452 if (isConstructor || name.equals(curr.getElementName())) { 453 if (isConstructor == curr.isConstructor()) { 454 String [] currParamTypes= curr.getParameterTypes(); 455 if (paramTypes.length == currParamTypes.length) { 456 for (int i= 0; i < paramTypes.length; i++) { 457 String t1= Signature.getSimpleName(Signature.toString(paramTypes[i])); 458 String t2= Signature.getSimpleName(Signature.toString(currParamTypes[i])); 459 if (!t1.equals(t2)) { 460 return false; 461 } 462 } 463 return true; 464 } 465 } 466 } 467 return false; 468 } 469 470 474 public static boolean isSamePackage(IPackageFragment pack1, IPackageFragment pack2) { 475 return pack1.getElementName().equals(pack2.getElementName()); 476 } 477 478 481 public static boolean hasMainMethod(IType type) throws JavaModelException { 482 IMethod[] methods= type.getMethods(); 483 for (int i= 0; i < methods.length; i++) { 484 if (methods[i].isMainMethod()) { 485 return true; 486 } 487 } 488 return false; 489 } 490 491 494 public static boolean isBoolean(IField field) throws JavaModelException{ 495 return field.getTypeSignature().equals(Signature.SIG_BOOLEAN); 496 } 497 498 501 public static boolean isInterfaceOrAnnotation(IType type) throws JavaModelException { 502 return type.isInterface(); 503 } 504 505 512 public static String getResolvedTypeName(String refTypeSig, IType declaringType) throws JavaModelException { 513 int arrayCount= Signature.getArrayCount(refTypeSig); 514 char type= refTypeSig.charAt(arrayCount); 515 if (type == Signature.C_UNRESOLVED) { 516 String name= ""; int bracket= refTypeSig.indexOf(Signature.C_GENERIC_START, arrayCount + 1); 518 if (bracket > 0) 519 name= refTypeSig.substring(arrayCount + 1, bracket); 520 else { 521 int semi= refTypeSig.indexOf(Signature.C_SEMICOLON, arrayCount + 1); 522 if (semi == -1) { 523 throw new IllegalArgumentException (); 524 } 525 name= refTypeSig.substring(arrayCount + 1, semi); 526 } 527 String [][] resolvedNames= declaringType.resolveType(name); 528 if (resolvedNames != null && resolvedNames.length > 0) { 529 return JavaModelUtil.concatenateName(resolvedNames[0][0], resolvedNames[0][1]); 530 } 531 return null; 532 } else { 533 return Signature.toString(refTypeSig.substring(arrayCount)); 534 } 535 } 536 537 540 public static boolean isEditable(ICompilationUnit cu) { 541 Assert.isNotNull(cu); 542 IResource resource= cu.getPrimary().getResource(); 543 return (resource.exists() && !resource.getResourceAttributes().isReadOnly()); 544 } 545 546 553 public static IMember toOriginal(IMember member) { 554 if (member instanceof IMethod) 555 return toOriginalMethod((IMethod)member); 556 557 559 return (IMember) member.getPrimaryElement(); 560 564 } 565 566 572 private static IMethod toOriginalMethod(IMethod method) { 573 ICompilationUnit cu= method.getCompilationUnit(); 574 if (cu == null || isPrimary(cu)) { 575 return method; 576 } 577 try{ 578 if (! method.getElementName().equals(method.getDeclaringType().getElementName())) 580 return (IMethod) method.getPrimaryElement(); 581 582 IType originalType = (IType) toOriginal(method.getDeclaringType()); 583 IMethod[] methods = originalType.findMethods(method); 584 boolean isConstructor = method.isConstructor(); 585 for (int i=0; i < methods.length; i++) { 586 if (methods[i].isConstructor() == isConstructor) 587 return methods[i]; 588 } 589 return null; 590 } catch (JavaModelException e){ 591 return null; 592 } 593 } 594 595 598 public static boolean isPrimary(ICompilationUnit cu) { 599 return cu.getOwner() == null; 600 } 601 602 609 public static boolean isExceptionToBeLogged(CoreException exception) { 610 if (!(exception instanceof JavaModelException)) 611 return true; 612 JavaModelException je= (JavaModelException)exception; 613 if (!je.isDoesNotExist()) 614 return true; 615 IJavaElement[] elements= je.getJavaModelStatus().getElements(); 616 for (int i= 0; i < elements.length; i++) { 617 IJavaElement element= elements[i]; 618 if (element.getElementType() == IJavaElement.COMPILATION_UNIT) 623 continue; 624 ICompilationUnit unit= (ICompilationUnit)element.getAncestor(IJavaElement.COMPILATION_UNIT); 625 if (unit == null) 626 return true; 627 if (!unit.isWorkingCopy()) 628 return true; 629 } 630 return false; 631 } 632 633 public static IType[] getAllSuperTypes(IType type, IProgressMonitor pm) throws JavaModelException { 634 IType[] superTypes= SuperTypeHierarchyCache.getTypeHierarchy(type).getAllSupertypes(type); 636 if (type.isInterface()) { 637 IType objekt= type.getJavaProject().findType("java.lang.Object"); if (objekt != null) { 639 IType[] superInterfacesAndObject= new IType[superTypes.length + 1]; 640 System.arraycopy(superTypes, 0, superInterfacesAndObject, 0, superTypes.length); 641 superInterfacesAndObject[superTypes.length]= objekt; 642 return superInterfacesAndObject; 643 } 644 } 645 return superTypes; 646 } 647 648 public static boolean isSuperType(ITypeHierarchy hierarchy, IType possibleSuperType, IType type) { 649 IType superClass= hierarchy.getSuperclass(type); 651 if (superClass != null && (possibleSuperType.equals(superClass) || isSuperType(hierarchy, possibleSuperType, superClass))) { 652 return true; 653 } 654 if (Flags.isInterface(hierarchy.getCachedFlags(possibleSuperType))) { 655 IType[] superInterfaces= hierarchy.getSuperInterfaces(type); 656 for (int i= 0; i < superInterfaces.length; i++) { 657 IType curr= superInterfaces[i]; 658 if (possibleSuperType.equals(curr) || isSuperType(hierarchy, possibleSuperType, curr)) { 659 return true; 660 } 661 } 662 } 663 return false; 664 } 665 666 public static boolean isExcludedPath(IPath resourcePath, IPath[] exclusionPatterns) { 667 char[] path = resourcePath.toString().toCharArray(); 668 for (int i = 0, length = exclusionPatterns.length; i < length; i++) { 669 char[] pattern= exclusionPatterns[i].toString().toCharArray(); 670 if (CharOperation.pathMatch(pattern, path, true, '/')) { 671 return true; 672 } 673 } 674 return false; 675 } 676 677 678 684 public final static boolean isExcluded(IPath resourcePath, char[][] exclusionPatterns) { 685 if (exclusionPatterns == null) return false; 686 char[] path = resourcePath.toString().toCharArray(); 687 for (int i = 0, length = exclusionPatterns.length; i < length; i++) 688 if (CharOperation.pathMatch(exclusionPatterns[i], path, true, '/')) 689 return true; 690 return false; 691 } 692 693 694 698 public static void reconcile(ICompilationUnit unit) throws JavaModelException { 699 unit.reconcile( 700 ICompilationUnit.NO_AST, 701 false , 702 null , 703 null ); 704 } 705 706 718 public static IClasspathEntry getClasspathEntryToEdit(IJavaProject jproject, IPath containerPath, IPath libPath) throws JavaModelException { 719 IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, jproject); 720 ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0)); 721 if (container != null && initializer != null && initializer.canUpdateClasspathContainer(containerPath, jproject)) { 722 return findEntryInContainer(container, libPath); 723 } 724 return null; } 726 727 734 public static IClasspathEntry findEntryInContainer(IClasspathContainer container, IPath libPath) { 735 IClasspathEntry[] entries= container.getClasspathEntries(); 736 for (int i= 0; i < entries.length; i++) { 737 IClasspathEntry curr= entries[i]; 738 IClasspathEntry resolved= JavaCore.getResolvedClasspathEntry(curr); 739 if (resolved != null && libPath.equals(resolved.getPath())) { 740 return curr; } 742 } 743 return null; } 745 746 752 public static ICompilationUnit[] getAllCompilationUnits(IJavaElement[] javaElements) throws JavaModelException { 753 HashSet result= new HashSet (); 754 for (int i= 0; i < javaElements.length; i++) { 755 addAllCus(result, javaElements[i]); 756 } 757 return (ICompilationUnit[]) result.toArray(new ICompilationUnit[result.size()]); 758 } 759 760 private static void addAllCus(HashSet collector, IJavaElement javaElement) throws JavaModelException { 761 switch (javaElement.getElementType()) { 762 case IJavaElement.JAVA_PROJECT: 763 IJavaProject javaProject= (IJavaProject) javaElement; 764 IPackageFragmentRoot[] packageFragmentRoots= javaProject.getPackageFragmentRoots(); 765 for (int i= 0; i < packageFragmentRoots.length; i++) 766 addAllCus(collector, packageFragmentRoots[i]); 767 return; 768 769 case IJavaElement.PACKAGE_FRAGMENT_ROOT: 770 IPackageFragmentRoot packageFragmentRoot= (IPackageFragmentRoot) javaElement; 771 if (packageFragmentRoot.getKind() != IPackageFragmentRoot.K_SOURCE) 772 return; 773 IJavaElement[] packageFragments= packageFragmentRoot.getChildren(); 774 for (int j= 0; j < packageFragments.length; j++) 775 addAllCus(collector, packageFragments[j]); 776 return; 777 778 case IJavaElement.PACKAGE_FRAGMENT: 779 IPackageFragment packageFragment= (IPackageFragment) javaElement; 780 collector.addAll(Arrays.asList(packageFragment.getCompilationUnits())); 781 return; 782 783 case IJavaElement.COMPILATION_UNIT: 784 collector.add(javaElement); 785 return; 786 787 default: 788 IJavaElement cu= javaElement.getAncestor(IJavaElement.COMPILATION_UNIT); 789 if (cu != null) 790 collector.add(cu); 791 } 792 } 793 794 795 798 public static void set50CompilanceOptions(Map map) { 799 setCompilanceOptions(map, JavaCore.VERSION_1_5); 800 } 801 802 public static void setCompilanceOptions(Map map, String compliance) { 803 if (JavaCore.VERSION_1_6.equals(compliance)) { 804 map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_6); 805 map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6); 806 map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_6); 807 map.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR); 808 map.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR); 809 } else if (JavaCore.VERSION_1_5.equals(compliance)) { 810 map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5); 811 map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5); 812 map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5); 813 map.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.ERROR); 814 map.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.ERROR); 815 } else if (JavaCore.VERSION_1_4.equals(compliance)) { 816 map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_4); 817 map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_3); 818 map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_2); 819 map.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.WARNING); 820 map.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.WARNING); 821 } else if (JavaCore.VERSION_1_3.equals(compliance)) { 822 map.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_3); 823 map.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_3); 824 map.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_1); 825 map.put(JavaCore.COMPILER_PB_ASSERT_IDENTIFIER, JavaCore.IGNORE); 826 map.put(JavaCore.COMPILER_PB_ENUM_IDENTIFIER, JavaCore.IGNORE); 827 } else { 828 throw new IllegalArgumentException ("Unsupported compliance: " + compliance); } 830 } 831 832 public static void setDefaultClassfileOptions(Map map, String compliance) { 833 map.put(JavaCore.COMPILER_CODEGEN_INLINE_JSR_BYTECODE, is50OrHigher(compliance) ? JavaCore.ENABLED : JavaCore.DISABLED); 834 map.put(JavaCore.COMPILER_LOCAL_VARIABLE_ATTR, JavaCore.GENERATE); 835 map.put(JavaCore.COMPILER_LINE_NUMBER_ATTR, JavaCore.GENERATE); 836 map.put(JavaCore.COMPILER_SOURCE_FILE_ATTR, JavaCore.GENERATE); 837 map.put(JavaCore.COMPILER_CODEGEN_UNUSED_LOCAL, JavaCore.PRESERVE); 838 } 839 840 843 public static boolean isVersionLessThan(String version1, String version2) { 844 return version1.compareTo(version2) < 0; 845 } 846 847 public static boolean is50OrHigher(String compliance) { 848 return !isVersionLessThan(compliance, JavaCore.VERSION_1_5); 849 } 850 851 public static boolean is50OrHigher(IJavaProject project) { 852 return is50OrHigher(project.getOption(JavaCore.COMPILER_COMPLIANCE, true)); 853 } 854 855 public static boolean is50OrHigherJRE(IJavaProject project) throws CoreException { 856 IVMInstall vmInstall= JavaRuntime.getVMInstall(project); 857 if (!(vmInstall instanceof IVMInstall2)) 858 return true; 860 String compliance= getCompilerCompliance((IVMInstall2) vmInstall, null); 861 if (compliance == null) 862 return true; return compliance.startsWith(JavaCore.VERSION_1_5) || compliance.startsWith(JavaCore.VERSION_1_6); 864 } 865 866 public static String getCompilerCompliance(IVMInstall2 vMInstall, String defaultCompliance) { 867 String version= vMInstall.getJavaVersion(); 868 if (version == null) { 869 return defaultCompliance; 870 } else if (version.startsWith(JavaCore.VERSION_1_6)) { 871 return JavaCore.VERSION_1_6; 872 } else if (version.startsWith(JavaCore.VERSION_1_5)) { 873 return JavaCore.VERSION_1_5; 874 } else if (version.startsWith(JavaCore.VERSION_1_4)) { 875 return JavaCore.VERSION_1_4; 876 } else if (version.startsWith(JavaCore.VERSION_1_3)) { 877 return JavaCore.VERSION_1_3; 878 } else if (version.startsWith(JavaCore.VERSION_1_2)) { 879 return JavaCore.VERSION_1_3; 880 } else if (version.startsWith(JavaCore.VERSION_1_1)) { 881 return JavaCore.VERSION_1_3; 882 } 883 return defaultCompliance; 884 } 885 886 public static String getExecutionEnvironmentCompliance(IExecutionEnvironment executionEnvironment) { 887 String desc= executionEnvironment.getId(); 888 if (desc.indexOf("1.6") != -1) { return JavaCore.VERSION_1_6; 890 } else if (desc.indexOf("1.5") != -1) { return JavaCore.VERSION_1_5; 892 } else if (desc.indexOf("1.4") != -1) { return JavaCore.VERSION_1_4; 894 } 895 return JavaCore.VERSION_1_3; 896 } 897 898 906 public static String getRenamedCUName(ICompilationUnit cu, String newMainName) { 907 String oldName = cu.getElementName(); 908 int i = oldName.lastIndexOf('.'); 909 if (i != -1) { 910 return newMainName + oldName.substring(i); 911 } else { 912 return newMainName; 913 } 914 } 915 916 925 public static void applyEdit(ICompilationUnit cu, TextEdit edit, boolean save, IProgressMonitor monitor) throws CoreException, ValidateEditException { 926 if (monitor == null) { 927 monitor= new NullProgressMonitor(); 928 } 929 monitor.beginTask(CorextMessages.JavaModelUtil_applyedit_operation, 3); 930 931 try { 932 IDocument document= null; 933 try { 934 document= aquireDocument(cu, new SubProgressMonitor(monitor, 1)); 935 if (save) { 936 commitDocument(cu, document, edit, new SubProgressMonitor(monitor, 1)); 937 } else { 938 new RewriteSessionEditProcessor(document, edit, TextEdit.UPDATE_REGIONS).performEdits(); 939 } 940 } catch (BadLocationException e) { 941 throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e)); 942 } finally { 943 releaseDocument(cu, document, new SubProgressMonitor(monitor, 1)); 944 } 945 } finally { 946 monitor.done(); 947 } 948 } 949 950 private static IDocument aquireDocument(ICompilationUnit cu, IProgressMonitor monitor) throws CoreException { 951 if (JavaModelUtil.isPrimary(cu)) { 952 IFile file= (IFile) cu.getResource(); 953 if (file.exists()) { 954 ITextFileBufferManager bufferManager= FileBuffers.getTextFileBufferManager(); 955 IPath path= cu.getPath(); 956 bufferManager.connect(path, LocationKind.IFILE, monitor); 957 return bufferManager.getTextFileBuffer(path, LocationKind.IFILE).getDocument(); 958 } 959 } 960 monitor.done(); 961 return new Document(cu.getSource()); 962 } 963 964 private static void commitDocument(ICompilationUnit cu, IDocument document, TextEdit edit, IProgressMonitor monitor) throws CoreException, MalformedTreeException, BadLocationException { 965 if (JavaModelUtil.isPrimary(cu)) { 966 IFile file= (IFile) cu.getResource(); 967 if (file.exists()) { 968 IStatus status= Resources.makeCommittable(file, null); 969 if (!status.isOK()) { 970 throw new ValidateEditException(status); 971 } 972 new RewriteSessionEditProcessor(document, edit, TextEdit.UPDATE_REGIONS).performEdits(); 974 ITextFileBufferManager bufferManager= FileBuffers.getTextFileBufferManager(); 975 bufferManager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE).commit(monitor, true); 976 return; 977 } 978 } 979 new RewriteSessionEditProcessor(document, edit, TextEdit.UPDATE_REGIONS).performEdits(); 981 } 982 983 984 private static void releaseDocument(ICompilationUnit cu, IDocument document, IProgressMonitor monitor) throws CoreException { 985 if (JavaModelUtil.isPrimary(cu)) { 986 IFile file= (IFile) cu.getResource(); 987 if (file.exists()) { 988 ITextFileBufferManager bufferManager= FileBuffers.getTextFileBufferManager(); 989 bufferManager.disconnect(file.getFullPath(), LocationKind.IFILE, monitor); 990 return; 991 } 992 } 993 cu.getBuffer().setContents(document.get()); 994 monitor.done(); 995 } 996 997 public static boolean isImplicitImport(String qualifier, ICompilationUnit cu) { 998 if ("java.lang".equals(qualifier)) { return true; 1000 } 1001 String packageName= cu.getParent().getElementName(); 1002 if (qualifier.equals(packageName)) { 1003 return true; 1004 } 1005 String typeName= JavaCore.removeJavaLikeExtension(cu.getElementName()); 1006 String mainTypeName= JavaModelUtil.concatenateName(packageName, typeName); 1007 return qualifier.equals(mainTypeName); 1008 } 1009 1010 public static boolean isOpenableStorage(Object storage) { 1011 if (storage instanceof IJarEntryResource) { 1012 return ((IJarEntryResource) storage).isFile(); 1013 } else { 1014 return storage instanceof IStorage; 1015 } 1016 } 1017 1018 1029 public static boolean isParameter(ILocalVariable currentLocal) throws JavaModelException { 1030 1031 final IJavaElement parent= currentLocal.getParent(); 1032 if (parent instanceof IMethod) { 1033 final String [] params= ((IMethod) parent).getParameterNames(); 1034 for (int i= 0; i < params.length; i++) { 1035 if (params[i].equals(currentLocal.getElementName())) 1036 return true; 1037 } 1038 } 1039 return false; 1040 } 1041 1042} 1043 | Popular Tags |