1 11 package org.eclipse.jdt.internal.core; 12 13 import java.io.*; 14 import java.net.URI ; 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Hashtable ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import javax.xml.parsers.DocumentBuilder ; 23 import javax.xml.parsers.DocumentBuilderFactory ; 24 import javax.xml.parsers.ParserConfigurationException ; 25 import org.eclipse.core.resources.ICommand; 26 import org.eclipse.core.resources.IFile; 27 import org.eclipse.core.resources.IFolder; 28 import org.eclipse.core.resources.IMarker; 29 import org.eclipse.core.resources.IProject; 30 import org.eclipse.core.resources.IProjectDescription; 31 import org.eclipse.core.resources.IProjectNature; 32 import org.eclipse.core.resources.IResource; 33 import org.eclipse.core.resources.IWorkspace; 34 import org.eclipse.core.resources.IWorkspaceRoot; 35 import org.eclipse.core.resources.ProjectScope; 36 import org.eclipse.core.resources.ResourcesPlugin; 37 import org.eclipse.core.runtime.AssertionFailedException; 38 import org.eclipse.core.runtime.CoreException; 39 import org.eclipse.core.runtime.IPath; 40 import org.eclipse.core.runtime.IProgressMonitor; 41 import org.eclipse.core.runtime.IStatus; 42 import org.eclipse.core.runtime.Path; 43 import org.eclipse.core.runtime.Preferences; 44 import org.eclipse.core.runtime.QualifiedName; 45 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 46 import org.eclipse.core.runtime.preferences.IScopeContext; 47 import org.eclipse.jdt.core.IClasspathContainer; 48 import org.eclipse.jdt.core.IClasspathEntry; 49 import org.eclipse.jdt.core.ICompilationUnit; 50 import org.eclipse.jdt.core.IJavaElement; 51 import org.eclipse.jdt.core.IJavaModelMarker; 52 import org.eclipse.jdt.core.IJavaModelStatus; 53 import org.eclipse.jdt.core.IJavaModelStatusConstants; 54 import org.eclipse.jdt.core.IJavaProject; 55 import org.eclipse.jdt.core.IPackageFragment; 56 import org.eclipse.jdt.core.IPackageFragmentRoot; 57 import org.eclipse.jdt.core.IRegion; 58 import org.eclipse.jdt.core.IType; 59 import org.eclipse.jdt.core.ITypeHierarchy; 60 import org.eclipse.jdt.core.JavaCore; 61 import org.eclipse.jdt.core.JavaModelException; 62 import org.eclipse.jdt.core.WorkingCopyOwner; 63 import org.eclipse.jdt.core.compiler.CategorizedProblem; 64 import org.eclipse.jdt.core.compiler.CharOperation; 65 import org.eclipse.jdt.core.eval.IEvaluationContext; 66 import org.eclipse.jdt.internal.compiler.util.ObjectVector; 67 import org.eclipse.jdt.internal.compiler.util.SuffixConstants; 68 import org.eclipse.jdt.internal.core.JavaModelManager.PerProjectInfo; 69 import org.eclipse.jdt.internal.core.JavaProjectElementInfo.ProjectCache; 70 import org.eclipse.jdt.internal.core.builder.JavaBuilder; 71 import org.eclipse.jdt.internal.core.eval.EvaluationContextWrapper; 72 import org.eclipse.jdt.internal.core.util.MementoTokenizer; 73 import org.eclipse.jdt.internal.core.util.Messages; 74 import org.eclipse.jdt.internal.core.util.Util; 75 import org.eclipse.jdt.internal.eval.EvaluationContext; 76 import org.osgi.service.prefs.BackingStoreException; 77 import org.w3c.dom.Element ; 78 import org.w3c.dom.Node ; 79 import org.w3c.dom.NodeList ; 80 import org.xml.sax.InputSource ; 81 import org.xml.sax.SAXException ; 82 83 101 public class JavaProject 102 extends Openable 103 implements IJavaProject, IProjectNature, SuffixConstants { 104 105 108 public static final String CLASSPATH_FILENAME = ".classpath"; 110 113 public static final IClasspathEntry[] INVALID_CLASSPATH = new IClasspathEntry[0]; 114 115 118 protected static final boolean IS_CASE_SENSITIVE = !new File("Temp").equals(new File("temp")); 120 123 protected static final String [] NO_PREREQUISITES = CharOperation.NO_STRINGS; 124 125 129 private static final String PREF_FILENAME = ".jprefs"; 131 134 public static final String DEFAULT_PREFERENCES_DIRNAME = ".settings"; 136 139 public static final String JAVA_CORE_PREFS_FILE = JavaCore.PLUGIN_ID+".prefs"; 141 144 private static final IClasspathEntry[] RESOLUTION_IN_PROGRESS = new IClasspathEntry[0]; 145 146 149 protected IProject project; 150 151 156 public JavaProject() { 157 super(null); 158 } 159 160 public JavaProject(IProject project, JavaElement parent) { 161 super(parent); 162 this.project = project; 163 } 164 165 public static boolean areClasspathsEqual( 166 IClasspathEntry[] firstClasspath, IClasspathEntry[] secondClasspath, 167 IPath firstOutputLocation, IPath secondOutputLocation) { 168 int length = firstClasspath.length; 169 if (length != secondClasspath.length) return false; 170 for (int i = 0; i < length; i++) { 171 if (!firstClasspath[i].equals(secondClasspath[i])) 172 return false; 173 } 174 if (firstOutputLocation == null) 175 return secondOutputLocation == null; 176 return firstOutputLocation.equals(secondOutputLocation); 177 } 178 179 187 private static boolean areClasspathsEqual(IClasspathEntry[] newClasspath, IPath newOutputLocation, IClasspathEntry[] otherClasspathWithOutput) { 188 189 if (otherClasspathWithOutput == null || otherClasspathWithOutput.length == 0) 190 return false; 191 192 int length = otherClasspathWithOutput.length; 193 if (length != newClasspath.length + 1) 194 return false; 196 197 198 for (int i = 0; i < length - 1; i++) { 200 if (!otherClasspathWithOutput[i].equals(newClasspath[i])) 201 return false; 202 } 203 IClasspathEntry output = otherClasspathWithOutput[length - 1]; 205 if (output.getContentKind() != ClasspathEntry.K_OUTPUT 206 || !output.getPath().equals(newOutputLocation)) 207 return false; 208 return true; 209 } 210 211 219 public static IPath canonicalizedPath(IPath externalPath) { 220 221 if (externalPath == null) 222 return null; 223 224 228 if (IS_CASE_SENSITIVE) { 229 return externalPath; 233 } 234 235 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 237 if (workspace == null) return externalPath; if (workspace.getRoot().findMember(externalPath) != null) { 239 return externalPath; 243 } 244 245 IPath canonicalPath = null; 246 try { 247 canonicalPath = 248 new Path(new File(externalPath.toOSString()).getCanonicalPath()); 249 } catch (IOException e) { 250 return externalPath; 255 } 256 257 IPath result; 258 int canonicalLength = canonicalPath.segmentCount(); 259 if (canonicalLength == 0) { 260 return externalPath; 265 } else if (externalPath.isAbsolute()) { 266 result = canonicalPath; 267 } else { 268 int externalLength = externalPath.segmentCount(); 271 if (canonicalLength >= externalLength) { 272 result = canonicalPath.removeFirstSegments(canonicalLength - externalLength); 273 } else { 274 return externalPath; 278 } 279 } 280 281 if (externalPath.getDevice() == null) { 283 result = result.setDevice(null); 284 } 285 return result; 289 } 290 291 297 public static boolean hasJavaNature(IProject project) { 298 try { 299 return project.hasNature(JavaCore.NATURE_ID); 300 } catch (CoreException e) { 301 if (ExternalJavaProject.EXTERNAL_PROJECT_NAME.equals(project.getName())) 302 return true; 303 } 305 return false; 306 } 307 308 314 public static void validateCycles(Map preferredClasspaths) throws JavaModelException { 315 316 318 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); 319 IProject[] rscProjects = workspaceRoot.getProjects(); 320 int length = rscProjects.length; 321 JavaProject[] projects = new JavaProject[length]; 322 323 HashSet cycleParticipants = new HashSet (); 324 HashSet traversed = new HashSet (); 325 326 ArrayList prereqChain = new ArrayList (); 328 for (int i = 0; i < length; i++){ 329 if (hasJavaNature(rscProjects[i])) { 330 JavaProject project = (projects[i] = (JavaProject)JavaCore.create(rscProjects[i])); 331 if (!traversed.contains(project.getPath())){ 332 prereqChain.clear(); 333 project.updateCycleParticipants(prereqChain, cycleParticipants, workspaceRoot, traversed, preferredClasspaths); 334 } 335 } 336 } 337 339 for (int i = 0; i < length; i++){ 340 JavaProject project = projects[i]; 341 if (project != null) { 342 if (cycleParticipants.contains(project.getPath())){ 343 IMarker cycleMarker = project.getCycleMarker(); 344 String circularCPOption = project.getOption(JavaCore.CORE_CIRCULAR_CLASSPATH, true); 345 int circularCPSeverity = JavaCore.ERROR.equals(circularCPOption) ? IMarker.SEVERITY_ERROR : IMarker.SEVERITY_WARNING; 346 if (cycleMarker != null) { 347 try { 349 int existingSeverity = ((Integer )cycleMarker.getAttribute(IMarker.SEVERITY)).intValue(); 350 if (existingSeverity != circularCPSeverity) { 351 cycleMarker.setAttribute(IMarker.SEVERITY, circularCPSeverity); 352 } 353 } catch (CoreException e) { 354 throw new JavaModelException(e); 355 } 356 } else { 357 project.createClasspathProblemMarker( 359 new JavaModelStatus(IJavaModelStatusConstants.CLASSPATH_CYCLE, project)); 360 } 361 } else { 362 project.flushClasspathProblemMarkers(true, false); 363 } 364 } 365 } 366 } 367 368 371 protected void addToBuildSpec(String builderID) throws CoreException { 372 373 IProjectDescription description = this.project.getDescription(); 374 int javaCommandIndex = getJavaCommandIndex(description.getBuildSpec()); 375 376 if (javaCommandIndex == -1) { 377 378 ICommand command = description.newCommand(); 380 command.setBuilderName(builderID); 381 setJavaCommand(description, command); 382 } 383 } 384 387 protected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException { 388 389 if (!hasJavaNature((IProject) underlyingResource)) { 391 throw newNotPresentException(); 392 } 393 394 IClasspathEntry[] resolvedClasspath = getResolvedClasspath(); 396 397 info.setChildren(computePackageFragmentRoots(resolvedClasspath, false, null )); 399 400 getPerProjectInfo().rememberExternalLibTimestamps(); 402 403 return true; 404 } 405 406 412 public void computeChildren(JavaProjectElementInfo info) throws JavaModelException { 413 IClasspathEntry[] classpath = getResolvedClasspath(); 414 JavaProjectElementInfo.ProjectCache projectCache = info.projectCache; 415 if (projectCache != null) { 416 IPackageFragmentRoot[] newRoots = computePackageFragmentRoots(classpath, true, null ); 417 checkIdentical: { IPackageFragmentRoot[] oldRoots = projectCache.allPkgFragmentRootsCache; 419 if (oldRoots.length == newRoots.length){ 420 for (int i = 0, length = oldRoots.length; i < length; i++){ 421 if (!oldRoots[i].equals(newRoots[i])){ 422 break checkIdentical; 423 } 424 } 425 return; } 427 } 428 } 429 info.setNonJavaResources(null); 430 info.setChildren( 431 computePackageFragmentRoots(classpath, false, null )); 432 } 433 434 438 private void computeExpandedClasspath( 439 ClasspathEntry referringEntry, 440 HashSet rootIDs, 441 ObjectVector accumulatedEntries) throws JavaModelException { 442 443 String projectRootId = this.rootID(); 444 if (rootIDs.contains(projectRootId)){ 445 return; } 447 rootIDs.add(projectRootId); 448 449 IClasspathEntry[] resolvedClasspath = getResolvedClasspath(); 450 451 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); 452 boolean isInitialProject = referringEntry == null; 453 for (int i = 0, length = resolvedClasspath.length; i < length; i++){ 454 ClasspathEntry entry = (ClasspathEntry) resolvedClasspath[i]; 455 if (isInitialProject || entry.isExported()){ 456 String rootID = entry.rootID(); 457 if (rootIDs.contains(rootID)) { 458 continue; 459 } 460 ClasspathEntry combinedEntry = entry.combineWith(referringEntry); 462 accumulatedEntries.add(combinedEntry); 463 464 if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) { 466 IResource member = workspaceRoot.findMember(entry.getPath()); 467 if (member != null && member.getType() == IResource.PROJECT){ IProject projRsc = (IProject) member; 469 if (JavaProject.hasJavaNature(projRsc)) { 470 JavaProject javaProject = (JavaProject) JavaCore.create(projRsc); 471 javaProject.computeExpandedClasspath( 472 combinedEntry, 473 rootIDs, 474 accumulatedEntries); 475 } 476 } 477 } else { 478 rootIDs.add(rootID); 479 } 480 } 481 } 482 } 483 484 490 public IPackageFragmentRoot[] computePackageFragmentRoots(IClasspathEntry resolvedEntry) { 491 try { 492 return 493 computePackageFragmentRoots( 494 new IClasspathEntry[]{ resolvedEntry }, 495 false, null 497 ); 498 } catch (JavaModelException e) { 499 return new IPackageFragmentRoot[] {}; 500 } 501 } 502 503 515 public void computePackageFragmentRoots( 516 IClasspathEntry resolvedEntry, 517 ObjectVector accumulatedRoots, 518 HashSet rootIDs, 519 IClasspathEntry referringEntry, 520 boolean checkExistency, 521 boolean retrieveExportedRoots, 522 Map rootToResolvedEntries) throws JavaModelException { 523 524 String rootID = ((ClasspathEntry)resolvedEntry).rootID(); 525 if (rootIDs.contains(rootID)) return; 526 527 IPath projectPath = this.project.getFullPath(); 528 IPath entryPath = resolvedEntry.getPath(); 529 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); 530 IPackageFragmentRoot root = null; 531 532 switch(resolvedEntry.getEntryKind()){ 533 534 case IClasspathEntry.CPE_SOURCE : 536 537 if (projectPath.isPrefixOf(entryPath)){ 538 if (checkExistency) { 539 Object target = JavaModel.getTarget(workspaceRoot, entryPath, checkExistency); 540 if (target == null) return; 541 542 if (target instanceof IFolder || target instanceof IProject){ 543 root = getPackageFragmentRoot((IResource)target); 544 } 545 } else { 546 root = getFolderPackageFragmentRoot(entryPath); 547 } 548 } 549 break; 550 551 case IClasspathEntry.CPE_LIBRARY : 553 554 if (referringEntry != null && !resolvedEntry.isExported()) return; 555 556 if (checkExistency) { 557 Object target = JavaModel.getTarget(workspaceRoot, entryPath, checkExistency); 558 if (target == null) return; 559 560 if (target instanceof IResource){ 561 root = getPackageFragmentRoot((IResource) target); 563 } else { 564 if (JavaModel.isFile(target) && (org.eclipse.jdt.internal.compiler.util.Util.isArchiveFileName(entryPath.lastSegment()))) { 566 root = new JarPackageFragmentRoot(entryPath, this); 567 } 568 } 569 } else { 570 root = getPackageFragmentRoot(entryPath); 571 } 572 break; 573 574 case IClasspathEntry.CPE_PROJECT : 576 577 if (!retrieveExportedRoots) return; 578 if (referringEntry != null && !resolvedEntry.isExported()) return; 579 580 IResource member = workspaceRoot.findMember(entryPath); 581 if (member != null && member.getType() == IResource.PROJECT){ IProject requiredProjectRsc = (IProject) member; 583 if (JavaProject.hasJavaNature(requiredProjectRsc)){ rootIDs.add(rootID); 585 JavaProject requiredProject = (JavaProject)JavaCore.create(requiredProjectRsc); 586 requiredProject.computePackageFragmentRoots( 587 requiredProject.getResolvedClasspath(), 588 accumulatedRoots, 589 rootIDs, 590 rootToResolvedEntries == null ? resolvedEntry : ((ClasspathEntry)resolvedEntry).combineWith((ClasspathEntry) referringEntry), checkExistency, 592 retrieveExportedRoots, 593 rootToResolvedEntries); 594 } 595 break; 596 } 597 } 598 if (root != null) { 599 accumulatedRoots.add(root); 600 rootIDs.add(rootID); 601 if (rootToResolvedEntries != null) rootToResolvedEntries.put(root, ((ClasspathEntry)resolvedEntry).combineWith((ClasspathEntry) referringEntry)); 602 } 603 } 604 605 615 public IPackageFragmentRoot[] computePackageFragmentRoots( 616 IClasspathEntry[] resolvedClasspath, 617 boolean retrieveExportedRoots, 618 Map rootToResolvedEntries) throws JavaModelException { 619 620 ObjectVector accumulatedRoots = new ObjectVector(); 621 computePackageFragmentRoots( 622 resolvedClasspath, 623 accumulatedRoots, 624 new HashSet (5), null, true, retrieveExportedRoots, 628 rootToResolvedEntries); 629 IPackageFragmentRoot[] rootArray = new IPackageFragmentRoot[accumulatedRoots.size()]; 630 accumulatedRoots.copyInto(rootArray); 631 return rootArray; 632 } 633 634 647 public void computePackageFragmentRoots( 648 IClasspathEntry[] resolvedClasspath, 649 ObjectVector accumulatedRoots, 650 HashSet rootIDs, 651 IClasspathEntry referringEntry, 652 boolean checkExistency, 653 boolean retrieveExportedRoots, 654 Map rootToResolvedEntries) throws JavaModelException { 655 656 if (referringEntry == null){ 657 rootIDs.add(rootID()); 658 } 659 for (int i = 0, length = resolvedClasspath.length; i < length; i++){ 660 computePackageFragmentRoots( 661 resolvedClasspath[i], 662 accumulatedRoots, 663 rootIDs, 664 referringEntry, 665 checkExistency, 666 retrieveExportedRoots, 667 rootToResolvedEntries); 668 } 669 } 670 675 public String computeSharedPropertyFileName(QualifiedName qName) { 676 677 return '.' + qName.getLocalName(); 678 } 679 680 683 public void configure() throws CoreException { 684 685 addToBuildSpec(JavaCore.BUILDER_ID); 687 } 688 689 694 public boolean contains(IResource resource) { 695 696 IClasspathEntry[] classpath; 697 IPath output; 698 try { 699 classpath = getResolvedClasspath(); 700 output = getOutputLocation(); 701 } catch (JavaModelException e) { 702 return false; 703 } 704 705 IPath fullPath = resource.getFullPath(); 706 IPath innerMostOutput = output.isPrefixOf(fullPath) ? output : null; 707 IClasspathEntry innerMostEntry = null; 708 for (int j = 0, cpLength = classpath.length; j < cpLength; j++) { 709 IClasspathEntry entry = classpath[j]; 710 711 IPath entryPath = entry.getPath(); 712 if ((innerMostEntry == null || innerMostEntry.getPath().isPrefixOf(entryPath)) 713 && entryPath.isPrefixOf(fullPath)) { 714 innerMostEntry = entry; 715 } 716 IPath entryOutput = classpath[j].getOutputLocation(); 717 if (entryOutput != null && entryOutput.isPrefixOf(fullPath)) { 718 innerMostOutput = entryOutput; 719 } 720 } 721 if (innerMostEntry != null) { 722 if (innerMostOutput != null && innerMostOutput.segmentCount() > 1 && innerMostEntry.getPath().segmentCount() == 1) { return false; 726 } 727 if (resource instanceof IFolder) { 728 return true; 730 } 731 switch (innerMostEntry.getEntryKind()) { 732 case IClasspathEntry.CPE_SOURCE: 733 return !org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(fullPath.lastSegment()); 735 case IClasspathEntry.CPE_LIBRARY: 736 return !org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(fullPath.lastSegment()); 738 } 739 } 740 if (innerMostOutput != null) { 741 return false; 742 } 743 return true; 744 } 745 746 749 public void createClasspathProblemMarker(IJavaModelStatus status) { 750 751 IMarker marker = null; 752 int severity; 753 String [] arguments = CharOperation.NO_STRINGS; 754 boolean isCycleProblem = false, isClasspathFileFormatProblem = false; 755 switch (status.getCode()) { 756 757 case IJavaModelStatusConstants.CLASSPATH_CYCLE : 758 isCycleProblem = true; 759 if (JavaCore.ERROR.equals(getOption(JavaCore.CORE_CIRCULAR_CLASSPATH, true))) { 760 severity = IMarker.SEVERITY_ERROR; 761 } else { 762 severity = IMarker.SEVERITY_WARNING; 763 } 764 break; 765 766 case IJavaModelStatusConstants.INVALID_CLASSPATH_FILE_FORMAT : 767 isClasspathFileFormatProblem = true; 768 severity = IMarker.SEVERITY_ERROR; 769 break; 770 771 case IJavaModelStatusConstants.INCOMPATIBLE_JDK_LEVEL : 772 String setting = getOption(JavaCore.CORE_INCOMPATIBLE_JDK_LEVEL, true); 773 if (JavaCore.ERROR.equals(setting)) { 774 severity = IMarker.SEVERITY_ERROR; 775 } else if (JavaCore.WARNING.equals(setting)) { 776 severity = IMarker.SEVERITY_WARNING; 777 } else { 778 return; } 780 break; 781 782 default: 783 IPath path = status.getPath(); 784 if (path != null) arguments = new String [] { path.toString() }; 785 if (JavaCore.ERROR.equals(getOption(JavaCore.CORE_INCOMPLETE_CLASSPATH, true)) && 786 status.getSeverity() != IStatus.WARNING) { 787 severity = IMarker.SEVERITY_ERROR; 788 } else { 789 severity = IMarker.SEVERITY_WARNING; 790 } 791 break; 792 } 793 794 try { 795 marker = this.project.createMarker(IJavaModelMarker.BUILDPATH_PROBLEM_MARKER); 796 marker.setAttributes( 797 new String [] { 798 IMarker.MESSAGE, 799 IMarker.SEVERITY, 800 IMarker.LOCATION, 801 IJavaModelMarker.CYCLE_DETECTED, 802 IJavaModelMarker.CLASSPATH_FILE_FORMAT, 803 IJavaModelMarker.ID, 804 IJavaModelMarker.ARGUMENTS , 805 IJavaModelMarker.CATEGORY_ID, 806 IMarker.SOURCE_ID, 807 }, 808 new Object [] { 809 status.getMessage(), 810 new Integer (severity), 811 Messages.classpath_buildPath, 812 isCycleProblem ? "true" : "false", isClasspathFileFormatProblem ? "true" : "false", new Integer (status.getCode()), 815 Util.getProblemArgumentsForMarker(arguments) , 816 new Integer (CategorizedProblem.CAT_BUILDPATH), 817 JavaBuilder.SOURCE_ID, 818 } 819 ); 820 } catch (CoreException e) { 821 if (JavaModelManager.VERBOSE) { 823 e.printStackTrace(); 824 } 825 } 826 } 827 828 831 protected Object createElementInfo() { 832 return new JavaProjectElementInfo(); 833 } 834 835 838 public IClasspathEntry[] decodeClasspath(String xmlClasspath, Map unknownElements) throws IOException, AssertionFailedException { 839 840 ArrayList paths = new ArrayList (); 841 IClasspathEntry defaultOutput = null; 842 StringReader reader = new StringReader(xmlClasspath); 843 Element cpElement; 844 try { 845 DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 846 cpElement = parser.parse(new InputSource (reader)).getDocumentElement(); 847 } catch (SAXException e) { 848 throw new IOException(Messages.file_badFormat); 849 } catch (ParserConfigurationException e) { 850 throw new IOException(Messages.file_badFormat); 851 } finally { 852 reader.close(); 853 } 854 855 if (!cpElement.getNodeName().equalsIgnoreCase("classpath")) { throw new IOException(Messages.file_badFormat); 857 } 858 NodeList list = cpElement.getElementsByTagName("classpathentry"); int length = list.getLength(); 860 861 for (int i = 0; i < length; ++i) { 862 Node node = list.item(i); 863 if (node.getNodeType() == Node.ELEMENT_NODE) { 864 IClasspathEntry entry = ClasspathEntry.elementDecode((Element)node, this, unknownElements); 865 if (entry != null){ 866 if (entry.getContentKind() == ClasspathEntry.K_OUTPUT) { 867 defaultOutput = entry; } else { 869 paths.add(entry); 870 } 871 } 872 } 873 } 874 int pathSize = paths.size(); 876 IClasspathEntry[] entries = new IClasspathEntry[pathSize + (defaultOutput == null ? 0 : 1)]; 877 paths.toArray(entries); 878 if (defaultOutput != null) entries[pathSize] = defaultOutput; return entries; 880 } 881 882 public IClasspathEntry decodeClasspathEntry(String encodedEntry) { 883 884 try { 885 if (encodedEntry == null) return null; 886 StringReader reader = new StringReader(encodedEntry); 887 Element node; 888 889 try { 890 DocumentBuilder parser = 891 DocumentBuilderFactory.newInstance().newDocumentBuilder(); 892 node = parser.parse(new InputSource (reader)).getDocumentElement(); 893 } catch (SAXException e) { 894 return null; 895 } catch (ParserConfigurationException e) { 896 return null; 897 } finally { 898 reader.close(); 899 } 900 901 if (!node.getNodeName().equalsIgnoreCase("classpathentry") || node.getNodeType() != Node.ELEMENT_NODE) { 903 return null; 904 } 905 return ClasspathEntry.elementDecode(node, this, null); 906 } catch (IOException e) { 907 return null; 909 } 910 } 911 912 916 public void deconfigure() throws CoreException { 917 918 removeFromBuildSpec(JavaCore.BUILDER_ID); 920 921 } 924 925 929 protected IClasspathEntry[] defaultClasspath() { 930 931 return new IClasspathEntry[] { 932 JavaCore.newSourceEntry(this.project.getFullPath())}; 933 } 934 935 939 protected IPath defaultOutputLocation() { 940 return this.project.getFullPath().append("bin"); } 942 943 946 protected String encodeClasspath(IClasspathEntry[] classpath, IPath outputLocation, boolean indent, Map unknownElements) throws JavaModelException { 947 try { 948 ByteArrayOutputStream s = new ByteArrayOutputStream(); 949 OutputStreamWriter writer = new OutputStreamWriter(s, "UTF8"); XMLWriter xmlWriter = new XMLWriter(writer, this, true); 951 952 xmlWriter.startTag(ClasspathEntry.TAG_CLASSPATH, indent); 953 for (int i = 0; i < classpath.length; ++i) { 954 ((ClasspathEntry)classpath[i]).elementEncode(xmlWriter, this.project.getFullPath(), indent, true, unknownElements); 955 } 956 957 if (outputLocation != null) { 958 outputLocation = outputLocation.removeFirstSegments(1); 959 outputLocation = outputLocation.makeRelative(); 960 HashMap parameters = new HashMap (); 961 parameters.put(ClasspathEntry.TAG_KIND, ClasspathEntry.kindToString(ClasspathEntry.K_OUTPUT)); 962 parameters.put(ClasspathEntry.TAG_PATH, String.valueOf(outputLocation)); 963 xmlWriter.printTag(ClasspathEntry.TAG_CLASSPATHENTRY, parameters, indent, true, true); 964 } 965 966 xmlWriter.endTag(ClasspathEntry.TAG_CLASSPATH, indent, true); 967 writer.flush(); 968 writer.close(); 969 return s.toString("UTF8"); } catch (IOException e) { 971 throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION); 972 } 973 } 974 975 public String encodeClasspathEntry(IClasspathEntry classpathEntry) { 976 try { 977 ByteArrayOutputStream s = new ByteArrayOutputStream(); 978 OutputStreamWriter writer = new OutputStreamWriter(s, "UTF8"); XMLWriter xmlWriter = new XMLWriter(writer, this, false); 980 981 ((ClasspathEntry)classpathEntry).elementEncode(xmlWriter, this.project.getFullPath(), true, true, null); 982 983 writer.flush(); 984 writer.close(); 985 return s.toString("UTF8"); } catch (IOException e) { 987 return null; } 989 } 990 991 999 public boolean equals(Object o) { 1000 1001 if (this == o) 1002 return true; 1003 1004 if (!(o instanceof JavaProject)) 1005 return false; 1006 1007 JavaProject other = (JavaProject) o; 1008 return this.project.equals(other.getProject()); 1009 } 1010 1011 public boolean exists() { 1012 try { 1013 return this.project.hasNature(JavaCore.NATURE_ID); 1014 } catch (CoreException e) { 1015 } 1017 return false; 1018 } 1019 1020 1023 public IJavaElement findElement(IPath path) throws JavaModelException { 1024 return findElement(path, DefaultWorkingCopyOwner.PRIMARY); 1025 } 1026 1027 1030 public IJavaElement findElement(IPath path, WorkingCopyOwner owner) throws JavaModelException { 1031 1032 if (path == null || path.isAbsolute()) { 1033 throw new JavaModelException( 1034 new JavaModelStatus(IJavaModelStatusConstants.INVALID_PATH, path)); 1035 } 1036 try { 1037 1038 String extension = path.getFileExtension(); 1039 if (extension == null) { 1040 String packageName = path.toString().replace(IPath.SEPARATOR, '.'); 1041 1042 NameLookup lookup = newNameLookup((WorkingCopyOwner)null); 1043 IPackageFragment[] pkgFragments = lookup.findPackageFragments(packageName, false); 1044 if (pkgFragments == null) { 1045 return null; 1046 1047 } else { 1048 for (int i = 0, length = pkgFragments.length; i < length; i++) { 1050 1051 IPackageFragment pkgFragment = pkgFragments[i]; 1052 if (this.equals(pkgFragment.getParent().getParent())) { 1053 return pkgFragment; 1054 } 1055 } 1056 return pkgFragments[0]; 1058 } 1059 } else if (Util.isJavaLikeFileName(path.lastSegment()) 1060 || extension.equalsIgnoreCase(EXTENSION_class)) { 1061 IPath packagePath = path.removeLastSegments(1); 1062 String packageName = packagePath.toString().replace(IPath.SEPARATOR, '.'); 1063 String typeName = path.lastSegment(); 1064 typeName = typeName.substring(0, typeName.length() - extension.length() - 1); 1065 String qualifiedName = null; 1066 if (packageName.length() > 0) { 1067 qualifiedName = packageName + "." + typeName; } else { 1069 qualifiedName = typeName; 1070 } 1071 1072 NameLookup lookup = newNameLookup(owner); 1074 NameLookup.Answer answer = lookup.findType( 1075 qualifiedName, 1076 false, 1077 NameLookup.ACCEPT_ALL, 1078 true, 1079 false, 1080 false, 1081 null); 1082 1083 if (answer != null) { 1084 return answer.type.getParent(); 1085 } else { 1086 return null; 1087 } 1088 } else { 1089 return null; 1091 } 1092 } catch (JavaModelException e) { 1093 if (e.getStatus().getCode() 1094 == IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST) { 1095 return null; 1096 } else { 1097 throw e; 1098 } 1099 } 1100 } 1101 1102 1105 public IPackageFragment findPackageFragment(IPath path) 1106 throws JavaModelException { 1107 1108 return findPackageFragment0(JavaProject.canonicalizedPath(path)); 1109 } 1110 1113 private IPackageFragment findPackageFragment0(IPath path) 1114 throws JavaModelException { 1115 1116 NameLookup lookup = newNameLookup((WorkingCopyOwner)null); 1117 return lookup.findPackageFragment(path); 1118 } 1119 1120 1123 public IPackageFragmentRoot findPackageFragmentRoot(IPath path) 1124 throws JavaModelException { 1125 1126 return findPackageFragmentRoot0(JavaProject.canonicalizedPath(path)); 1127 } 1128 1131 public IPackageFragmentRoot findPackageFragmentRoot0(IPath path) 1132 throws JavaModelException { 1133 1134 IPackageFragmentRoot[] allRoots = this.getAllPackageFragmentRoots(); 1135 if (!path.isAbsolute()) { 1136 throw new IllegalArgumentException (Messages.path_mustBeAbsolute); 1137 } 1138 for (int i= 0; i < allRoots.length; i++) { 1139 IPackageFragmentRoot classpathRoot= allRoots[i]; 1140 if (classpathRoot.getPath().equals(path)) { 1141 return classpathRoot; 1142 } 1143 } 1144 return null; 1145 } 1146 1149 public IPackageFragmentRoot[] findPackageFragmentRoots(IClasspathEntry entry) { 1150 try { 1151 IClasspathEntry[] classpath = this.getRawClasspath(); 1152 for (int i = 0, length = classpath.length; i < length; i++) { 1153 if (classpath[i].equals(entry)) { return 1155 computePackageFragmentRoots( 1156 resolveClasspath(new IClasspathEntry[] {entry}), 1157 false, null); 1159 } 1160 } 1161 } catch (JavaModelException e) { 1162 } 1164 return new IPackageFragmentRoot[] {}; 1165 } 1166 1169 public IType findType(String fullyQualifiedName) throws JavaModelException { 1170 return findType(fullyQualifiedName, DefaultWorkingCopyOwner.PRIMARY); 1171 } 1172 1175 public IType findType(String fullyQualifiedName, IProgressMonitor progressMonitor) throws JavaModelException { 1176 return findType(fullyQualifiedName, DefaultWorkingCopyOwner.PRIMARY, progressMonitor); 1177 } 1178 1179 1182 IType findType(String fullyQualifiedName, NameLookup lookup, boolean considerSecondaryTypes, IProgressMonitor progressMonitor) throws JavaModelException { 1183 NameLookup.Answer answer = lookup.findType( 1184 fullyQualifiedName, 1185 false, 1186 NameLookup.ACCEPT_ALL, 1187 considerSecondaryTypes, 1188 true, 1189 false, 1190 progressMonitor); 1191 if (answer == null) { 1192 int lastDot = fullyQualifiedName.lastIndexOf('.'); 1194 if (lastDot == -1) return null; 1195 IType type = findType(fullyQualifiedName.substring(0, lastDot), lookup, considerSecondaryTypes, progressMonitor); 1196 if (type != null) { 1197 type = type.getType(fullyQualifiedName.substring(lastDot+1)); 1198 if (!type.exists()) { 1199 return null; 1200 } 1201 } 1202 return type; 1203 } 1204 return answer.type; 1205 } 1206 1209 public IType findType(String packageName, String typeQualifiedName) throws JavaModelException { 1210 return findType(packageName, typeQualifiedName, DefaultWorkingCopyOwner.PRIMARY); 1211 } 1212 1215 public IType findType(String packageName, String typeQualifiedName, IProgressMonitor progressMonitor) throws JavaModelException { 1216 return findType(packageName, typeQualifiedName, DefaultWorkingCopyOwner.PRIMARY, progressMonitor); 1217 } 1218 1221 IType findType(String packageName, String typeQualifiedName, NameLookup lookup, boolean considerSecondaryTypes, IProgressMonitor progressMonitor) throws JavaModelException { 1222 NameLookup.Answer answer = lookup.findType( 1223 typeQualifiedName, 1224 packageName, 1225 false, 1226 NameLookup.ACCEPT_ALL, 1227 considerSecondaryTypes, 1228 true, false, 1230 progressMonitor); 1231 return answer == null ? null : answer.type; 1232 } 1233 1236 public IType findType(String packageName, String typeQualifiedName, WorkingCopyOwner owner) throws JavaModelException { 1237 NameLookup lookup = newNameLookup(owner); 1238 return findType( 1239 packageName, 1240 typeQualifiedName, 1241 lookup, 1242 false, null); 1244 } 1245 1246 1249 public IType findType(String packageName, String typeQualifiedName, WorkingCopyOwner owner, IProgressMonitor progressMonitor) throws JavaModelException { 1250 NameLookup lookup = newNameLookup(owner); 1251 return findType( 1252 packageName, 1253 typeQualifiedName, 1254 lookup, 1255 true, progressMonitor); 1257 } 1258 1259 1262 public IType findType(String fullyQualifiedName, WorkingCopyOwner owner) throws JavaModelException { 1263 NameLookup lookup = newNameLookup(owner); 1264 return findType(fullyQualifiedName, lookup, false, null); 1265 } 1266 1267 1270 public IType findType(String fullyQualifiedName, WorkingCopyOwner owner, IProgressMonitor progressMonitor) throws JavaModelException { 1271 NameLookup lookup = newNameLookup(owner); 1272 return findType(fullyQualifiedName, lookup, true, progressMonitor); 1273 } 1274 1275 protected void flushClasspathProblemMarkers(boolean flushCycleMarkers, boolean flushClasspathFormatMarkers) { 1279 try { 1280 if (this.project.isAccessible()) { 1281 IMarker[] markers = this.project.findMarkers(IJavaModelMarker.BUILDPATH_PROBLEM_MARKER, false, IResource.DEPTH_ZERO); 1282 for (int i = 0, length = markers.length; i < length; i++) { 1283 IMarker marker = markers[i]; 1284 if (flushCycleMarkers && flushClasspathFormatMarkers) { 1285 marker.delete(); 1286 } else { 1287 String cycleAttr = (String )marker.getAttribute(IJavaModelMarker.CYCLE_DETECTED); 1288 String classpathFileFormatAttr = (String )marker.getAttribute(IJavaModelMarker.CLASSPATH_FILE_FORMAT); 1289 if ((flushCycleMarkers == (cycleAttr != null && cycleAttr.equals("true"))) && (flushClasspathFormatMarkers == (classpathFileFormatAttr != null && classpathFileFormatAttr.equals("true")))){ marker.delete(); 1292 } 1293 } 1294 } 1295 } 1296 } catch (CoreException e) { 1297 if (JavaModelManager.VERBOSE) { 1299 e.printStackTrace(); 1300 } 1301 } 1302 } 1303 1304 1308 public IPath[] getAccessRestrictions(String optionName) { 1309 String sequence = getOption(optionName, true); if (sequence == null || sequence.length() == 0) return null; 1311 IPath[] rules = null; 1312 char[][] patterns = CharOperation.splitOn('|', sequence.toCharArray()); 1313 int patternCount; 1314 if ((patternCount = patterns.length) > 0) { 1315 rules = new IPath[patternCount]; 1316 for (int j = 0; j < patterns.length; j++){ 1317 rules[j] = new Path(new String (patterns[j])); 1318 } 1319 } 1320 return rules; 1321 } 1322 1323 1326 public IPackageFragmentRoot[] getAllPackageFragmentRoots() 1327 throws JavaModelException { 1328 1329 return getAllPackageFragmentRoots(null ); 1330 } 1331 1332 public IPackageFragmentRoot[] getAllPackageFragmentRoots(Map rootToResolvedEntries) throws JavaModelException { 1333 1334 return computePackageFragmentRoots(getResolvedClasspath(), true, rootToResolvedEntries); 1335 } 1336 1337 1344 public IClasspathEntry getClasspathEntryFor(IPath path) throws JavaModelException { 1345 getResolvedClasspath(); PerProjectInfo perProjectInfo = getPerProjectInfo(); 1347 if (perProjectInfo == null) 1348 return null; 1349 Map rootPathToResolvedEntries = perProjectInfo.rootPathToResolvedEntries; 1350 if (rootPathToResolvedEntries == null) 1351 return null; 1352 return (IClasspathEntry) rootPathToResolvedEntries.get(path); 1353 } 1354 1355 1358 public IMarker getCycleMarker(){ 1359 try { 1360 if (this.project.isAccessible()) { 1361 IMarker[] markers = this.project.findMarkers(IJavaModelMarker.BUILDPATH_PROBLEM_MARKER, false, IResource.DEPTH_ZERO); 1362 for (int i = 0, length = markers.length; i < length; i++) { 1363 IMarker marker = markers[i]; 1364 String cycleAttr = (String )marker.getAttribute(IJavaModelMarker.CYCLE_DETECTED); 1365 if (cycleAttr != null && cycleAttr.equals("true")){ return marker; 1367 } 1368 } 1369 } 1370 } catch (CoreException e) { 1371 } 1373 return null; 1374 } 1375 1376 1381 public IEclipsePreferences getEclipsePreferences(){ 1382 if (!JavaProject.hasJavaNature(this.project)) return null; 1383 JavaModelManager.PerProjectInfo perProjectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfo(this.project, true); 1385 if (perProjectInfo.preferences != null) return perProjectInfo.preferences; 1386 IScopeContext context = new ProjectScope(getProject()); 1388 final IEclipsePreferences eclipsePreferences = context.getNode(JavaCore.PLUGIN_ID); 1389 updatePreferences(eclipsePreferences); 1390 perProjectInfo.preferences = eclipsePreferences; 1391 1392 IEclipsePreferences.INodeChangeListener nodeListener = new IEclipsePreferences.INodeChangeListener() { 1394 public void added(IEclipsePreferences.NodeChangeEvent event) { 1395 } 1397 public void removed(IEclipsePreferences.NodeChangeEvent event) { 1398 if (event.getChild() == eclipsePreferences) { 1399 JavaModelManager.getJavaModelManager().resetProjectPreferences(JavaProject.this); 1400 } 1401 } 1402 }; 1403 ((IEclipsePreferences) eclipsePreferences.parent()).addNodeChangeListener(nodeListener); 1404 1405 IEclipsePreferences.IPreferenceChangeListener preferenceListener = new IEclipsePreferences.IPreferenceChangeListener() { 1407 public void preferenceChange(IEclipsePreferences.PreferenceChangeEvent event) { 1408 String propertyName = event.getKey(); 1409 JavaModelManager manager = JavaModelManager.getJavaModelManager(); 1410 if (propertyName.startsWith(JavaCore.PLUGIN_ID)) { 1411 if (propertyName.equals(JavaCore.CORE_JAVA_BUILD_CLEAN_OUTPUT_FOLDER) || 1412 propertyName.equals(JavaCore.CORE_JAVA_BUILD_RESOURCE_COPY_FILTER) || 1413 propertyName.equals(JavaCore.CORE_JAVA_BUILD_DUPLICATE_RESOURCE) || 1414 propertyName.equals(JavaCore.CORE_JAVA_BUILD_RECREATE_MODIFIED_CLASS_FILES_IN_OUTPUT_FOLDER) || 1415 propertyName.equals(JavaCore.CORE_JAVA_BUILD_INVALID_CLASSPATH) || 1416 propertyName.equals(JavaCore.CORE_ENABLE_CLASSPATH_EXCLUSION_PATTERNS) || 1417 propertyName.equals(JavaCore.CORE_ENABLE_CLASSPATH_MULTIPLE_OUTPUT_LOCATIONS) || 1418 propertyName.equals(JavaCore.CORE_INCOMPLETE_CLASSPATH) || 1419 propertyName.equals(JavaCore.CORE_CIRCULAR_CLASSPATH) || 1420 propertyName.equals(JavaCore.CORE_INCOMPATIBLE_JDK_LEVEL)) 1421 { 1422 manager.deltaState.addClasspathValidation(JavaProject.this); 1423 } 1424 manager.resetProjectOptions(JavaProject.this); 1425 } 1426 } 1427 }; 1428 eclipsePreferences.addPreferenceChangeListener(preferenceListener); 1429 return eclipsePreferences; 1430 } 1431 1432 public String getElementName() { 1433 return this.project.getName(); 1434 } 1435 1436 1439 public int getElementType() { 1440 return JAVA_PROJECT; 1441 } 1442 1443 1450 public IClasspathEntry[] getExpandedClasspath() throws JavaModelException { 1451 1452 ObjectVector accumulatedEntries = new ObjectVector(); 1453 computeExpandedClasspath(null, new HashSet (5), accumulatedEntries); 1454 1455 IClasspathEntry[] expandedPath = new IClasspathEntry[accumulatedEntries.size()]; 1456 accumulatedEntries.copyInto(expandedPath); 1457 1458 return expandedPath; 1459 } 1460 1461 1466 public IPackageFragmentRoot getFolderPackageFragmentRoot(IPath path) { 1467 if (path.segmentCount() == 1) { return getPackageFragmentRoot(this.project); 1469 } 1470 return getPackageFragmentRoot(this.project.getWorkspace().getRoot().getFolder(path)); 1471 } 1472 1473 1476 public IJavaElement getHandleFromMemento(String token, MementoTokenizer memento, WorkingCopyOwner owner) { 1477 switch (token.charAt(0)) { 1478 case JEM_PACKAGEFRAGMENTROOT: 1479 String rootPath = IPackageFragmentRoot.DEFAULT_PACKAGEROOT_PATH; 1480 token = null; 1481 while (memento.hasMoreTokens()) { 1482 token = memento.nextToken(); 1483 char firstChar = token.charAt(0); 1484 if (firstChar != JEM_PACKAGEFRAGMENT && firstChar != JEM_COUNT) { 1485 rootPath += token; 1486 } else { 1487 break; 1488 } 1489 } 1490 JavaElement root = (JavaElement)getPackageFragmentRoot(new Path(rootPath)); 1491 if (token != null && token.charAt(0) == JEM_PACKAGEFRAGMENT) { 1492 return root.getHandleFromMemento(token, memento, owner); 1493 } else { 1494 return root.getHandleFromMemento(memento, owner); 1495 } 1496 } 1497 return null; 1498 } 1499 1500 1504 protected char getHandleMementoDelimiter() { 1505 1506 return JEM_JAVAPROJECT; 1507 } 1508 1509 1513 private int getJavaCommandIndex(ICommand[] buildSpec) { 1514 1515 for (int i = 0; i < buildSpec.length; ++i) { 1516 if (buildSpec[i].getBuilderName().equals(JavaCore.BUILDER_ID)) { 1517 return i; 1518 } 1519 } 1520 return -1; 1521 } 1522 1523 1526 protected JavaProjectElementInfo getJavaProjectElementInfo() 1527 throws JavaModelException { 1528 1529 return (JavaProjectElementInfo) getElementInfo(); 1530 } 1531 1532 1535 public Object [] getNonJavaResources() throws JavaModelException { 1536 1537 return ((JavaProjectElementInfo) getElementInfo()).getNonJavaResources(this); 1538 } 1539 1540 1543 public String getOption(String optionName, boolean inheritJavaCoreOptions) { 1544 1545 String propertyName = optionName; 1546 if (JavaModelManager.getJavaModelManager().optionNames.contains(propertyName)){ 1547 IEclipsePreferences projectPreferences = getEclipsePreferences(); 1548 String javaCoreDefault = inheritJavaCoreOptions ? JavaCore.getOption(propertyName) : null; 1549 if (projectPreferences == null) return javaCoreDefault; 1550 String value = projectPreferences.get(propertyName, javaCoreDefault); 1551 return value == null ? null : value.trim(); 1552 } 1553 return null; 1554 } 1555 1556 1559 public Map getOptions(boolean inheritJavaCoreOptions) { 1560 1561 Map options = inheritJavaCoreOptions ? JavaCore.getOptions() : new Hashtable (5); 1563 1564 JavaModelManager.PerProjectInfo perProjectInfo = null; 1566 Hashtable projectOptions = null; 1567 HashSet optionNames = JavaModelManager.getJavaModelManager().optionNames; 1568 try { 1569 perProjectInfo = getPerProjectInfo(); 1570 projectOptions = perProjectInfo.options; 1571 if (projectOptions == null) { 1572 IEclipsePreferences projectPreferences= getEclipsePreferences(); 1574 if (projectPreferences == null) return options; String [] propertyNames = projectPreferences.keys(); 1577 projectOptions = new Hashtable (propertyNames.length); 1578 for (int i = 0; i < propertyNames.length; i++){ 1579 String propertyName = propertyNames[i]; 1580 String value = projectPreferences.get(propertyName, null); 1581 if (value != null && optionNames.contains(propertyName)){ 1582 projectOptions.put(propertyName, value.trim()); 1583 } 1584 } 1585 perProjectInfo.options = projectOptions; 1587 } 1588 } catch (JavaModelException jme) { 1589 projectOptions = new Hashtable (); 1590 } catch (BackingStoreException e) { 1591 projectOptions = new Hashtable (); 1592 } 1593 1594 if (inheritJavaCoreOptions) { 1596 Iterator propertyNames = projectOptions.entrySet().iterator(); 1597 while (propertyNames.hasNext()) { 1598 Map.Entry entry = (Map.Entry ) propertyNames.next(); 1599 String propertyName = (String ) entry.getKey(); 1600 String propertyValue = (String ) entry.getValue(); 1601 if (propertyValue != null && optionNames.contains(propertyName)){ 1602 options.put(propertyName, propertyValue.trim()); 1603 } 1604 } 1605 return options; 1606 } 1607 return projectOptions; 1608 } 1609 1610 1613 public IPath getOutputLocation() throws JavaModelException { 1614 JavaModelManager.PerProjectInfo perProjectInfo = this.getPerProjectInfo(); 1616 IPath outputLocation = perProjectInfo.outputLocation; 1617 if (outputLocation != null) return outputLocation; 1618 1619 getRawClasspath(); 1621 1622 outputLocation = perProjectInfo.outputLocation; 1623 if (outputLocation == null) { 1624 return this.defaultOutputLocation(); 1625 } 1626 return outputLocation; 1627 } 1628 1629 1637 public IPackageFragmentRoot getPackageFragmentRoot(IPath path) { 1638 if (!path.isAbsolute()) { 1639 path = getPath().append(path); 1640 } 1641 int segmentCount = path.segmentCount(); 1642 switch (segmentCount) { 1643 case 0: 1644 return null; 1645 case 1: 1646 if (path.equals(getPath())) { return getPackageFragmentRoot(this.project); 1649 } 1650 default: 1651 if (org.eclipse.jdt.internal.compiler.util.Util.isArchiveFileName(path.lastSegment())) { 1654 IResource resource = this.project.getWorkspace().getRoot().findMember(path); 1655 if (resource != null && resource.getType() == IResource.FOLDER){ 1656 return getPackageFragmentRoot(resource); 1657 } 1658 return getPackageFragmentRoot0(path); 1659 } else if (segmentCount == 1) { 1660 return getPackageFragmentRoot(this.project.getWorkspace().getRoot().getProject(path.lastSegment())); 1662 } else { 1663 return getPackageFragmentRoot(this.project.getWorkspace().getRoot().getFolder(path)); 1665 } 1666 } 1667 } 1668 1669 1672 public IPackageFragmentRoot getPackageFragmentRoot(IResource resource) { 1673 1674 switch (resource.getType()) { 1675 case IResource.FILE: 1676 if (org.eclipse.jdt.internal.compiler.util.Util.isArchiveFileName(resource.getName())) { 1677 return new JarPackageFragmentRoot(resource, this); 1678 } else { 1679 return null; 1680 } 1681 case IResource.FOLDER: 1682 return new PackageFragmentRoot(resource, this); 1683 case IResource.PROJECT: 1684 return new PackageFragmentRoot(resource, this); 1685 default: 1686 return null; 1687 } 1688 } 1689 1690 1693 public IPackageFragmentRoot getPackageFragmentRoot(String jarPath) { 1694 1695 return getPackageFragmentRoot0(JavaProject.canonicalizedPath(new Path(jarPath))); 1696 } 1697 1698 1701 public IPackageFragmentRoot getPackageFragmentRoot0(IPath jarPath) { 1702 1703 return new JarPackageFragmentRoot(jarPath, this); 1704 } 1705 1706 1709 public IPackageFragmentRoot[] getPackageFragmentRoots() 1710 throws JavaModelException { 1711 1712 Object [] children; 1713 int length; 1714 IPackageFragmentRoot[] roots; 1715 1716 System.arraycopy( 1717 children = getChildren(), 1718 0, 1719 roots = new IPackageFragmentRoot[length = children.length], 1720 0, 1721 length); 1722 1723 return roots; 1724 } 1725 1726 1730 public IPackageFragmentRoot[] getPackageFragmentRoots(IClasspathEntry entry) { 1731 return findPackageFragmentRoots(entry); 1732 } 1733 1734 1737 public IPackageFragment[] getPackageFragments() throws JavaModelException { 1738 1739 IPackageFragmentRoot[] roots = getPackageFragmentRoots(); 1740 return getPackageFragmentsInRoots(roots); 1741 } 1742 1743 1749 public IPackageFragment[] getPackageFragmentsInRoots(IPackageFragmentRoot[] roots) { 1750 1751 ArrayList frags = new ArrayList (); 1752 for (int i = 0; i < roots.length; i++) { 1753 IPackageFragmentRoot root = roots[i]; 1754 try { 1755 IJavaElement[] rootFragments = root.getChildren(); 1756 for (int j = 0; j < rootFragments.length; j++) { 1757 frags.add(rootFragments[j]); 1758 } 1759 } catch (JavaModelException e) { 1760 } 1762 } 1763 IPackageFragment[] fragments = new IPackageFragment[frags.size()]; 1764 frags.toArray(fragments); 1765 return fragments; 1766 } 1767 1768 1771 public IPath getPath() { 1772 return this.project.getFullPath(); 1773 } 1774 1775 public JavaModelManager.PerProjectInfo getPerProjectInfo() throws JavaModelException { 1776 return JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(this.project); 1777 } 1778 1779 private IPath getPluginWorkingLocation() { 1780 return this.project.getWorkingLocation(JavaCore.PLUGIN_ID); 1781 } 1782 1783 1793 public Preferences getPreferences(){ 1794 1804 return new Preferences(); 1805 } 1806 1807 1810 public IProject getProject() { 1811 return this.project; 1812 } 1813 1814 public ProjectCache getProjectCache() throws JavaModelException { 1815 return ((JavaProjectElementInfo) getElementInfo()).getProjectCache(this); 1816 } 1817 1818 1821 public IClasspathEntry[] getRawClasspath() throws JavaModelException { 1822 JavaModelManager.PerProjectInfo perProjectInfo = getPerProjectInfo(); 1823 IClasspathEntry[] classpath = perProjectInfo.rawClasspath; 1824 if (classpath != null) return classpath; 1825 1826 classpath = perProjectInfo.readAndCacheClasspath(this); 1827 1828 if (classpath == JavaProject.INVALID_CLASSPATH) 1829 return defaultClasspath(); 1830 1831 return classpath; 1832 } 1833 1834 1837 public String [] getRequiredProjectNames() throws JavaModelException { 1838 1839 return this.projectPrerequisites(getResolvedClasspath()); 1840 } 1841 1842 1845 public IClasspathEntry[] getResolvedClasspath() throws JavaModelException { 1846 PerProjectInfo perProjectInfo = getPerProjectInfo(); 1847 if (perProjectInfo.resolvedClasspath == null) 1848 resolveClasspath(perProjectInfo); 1849 return perProjectInfo.resolvedClasspath; 1850 } 1851 1852 1855 public IClasspathEntry[] getResolvedClasspath(boolean ignoreUnresolvedEntry) throws JavaModelException { 1856 if (JavaModelManager.getJavaModelManager().isClasspathBeingResolved(this)) { 1857 if (JavaModelManager.CP_RESOLVE_VERBOSE_ADVANCED) 1858 verbose_reentering_classpath_resolution(); 1859 return RESOLUTION_IN_PROGRESS; 1860 } 1861 PerProjectInfo perProjectInfo = getPerProjectInfo(); 1862 1863 IClasspathEntry[] resolvedClasspath; 1865 IJavaModelStatus unresolvedEntryStatus; 1866 synchronized (perProjectInfo) { 1867 resolvedClasspath = perProjectInfo.resolvedClasspath; 1868 unresolvedEntryStatus = perProjectInfo.unresolvedEntryStatus; 1869 } 1870 1871 if (resolvedClasspath == null 1872 || (unresolvedEntryStatus != null && !unresolvedEntryStatus.isOK())) { resolveClasspath(perProjectInfo); 1874 synchronized (perProjectInfo) { 1875 resolvedClasspath = perProjectInfo.resolvedClasspath; 1876 unresolvedEntryStatus = perProjectInfo.unresolvedEntryStatus; 1877 } 1878 } 1879 if (!ignoreUnresolvedEntry && unresolvedEntryStatus != null && !unresolvedEntryStatus.isOK()) 1880 throw new JavaModelException(unresolvedEntryStatus); 1881 return resolvedClasspath; 1882 } 1883 1884 private void verbose_reentering_classpath_resolution() { 1885 Util.verbose( 1886 "CPResolution: reentering raw classpath resolution, will use empty classpath instead" + " project: " + getElementName() + '\n' + " invocation stack trace:"); new Exception ("<Fake exception>").printStackTrace(System.out); } 1891 1892 1895 public IResource getResource() { 1896 return this.project; 1897 } 1898 1899 1910 public String getSharedProperty(String key) throws CoreException { 1911 1912 String property = null; 1913 IFile rscFile = this.project.getFile(key); 1914 if (rscFile.exists()) { 1915 byte[] bytes = Util.getResourceContentsAsByteArray(rscFile); 1916 try { 1917 property = new String (bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8); } catch (UnsupportedEncodingException e) { 1919 Util.log(e, "Could not read .classpath with UTF-8 encoding"); property = new String (bytes); 1922 } 1923 } else { 1924 URI location = rscFile.getLocationURI(); 1928 if (location != null) { 1929 File file = Util.toLocalFile(location, null); 1930 if (file != null && file.exists()) { 1931 byte[] bytes; 1932 try { 1933 bytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(file); 1934 } catch (IOException e) { 1935 return null; 1936 } 1937 try { 1938 property = new String (bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8); } catch (UnsupportedEncodingException e) { 1940 Util.log(e, "Could not read .classpath with UTF-8 encoding"); property = new String (bytes); 1943 } 1944 } 1945 } 1946 } 1947 return property; 1948 } 1949 1950 1953 public SourceMapper getSourceMapper() { 1954 1955 return null; 1956 } 1957 1958 1961 public IResource getUnderlyingResource() throws JavaModelException { 1962 if (!exists()) throw newNotPresentException(); 1963 return this.project; 1964 } 1965 1966 1969 public boolean hasBuildState() { 1970 1971 return JavaModelManager.getJavaModelManager().getLastBuiltState(this.project, null) != null; 1972 } 1973 1974 1977 public boolean hasClasspathCycle(IClasspathEntry[] preferredClasspath) { 1978 HashSet cycleParticipants = new HashSet (); 1979 HashMap preferredClasspaths = new HashMap (1); 1980 preferredClasspaths.put(this, preferredClasspath); 1981 updateCycleParticipants(new ArrayList (2), cycleParticipants, ResourcesPlugin.getWorkspace().getRoot(), new HashSet (2), preferredClasspaths); 1982 return !cycleParticipants.isEmpty(); 1983 } 1984 1985 public boolean hasCycleMarker(){ 1986 return this.getCycleMarker() != null; 1987 } 1988 1989 public int hashCode() { 1990 return this.project.hashCode(); 1991 } 1992 1993 1997 public boolean hasSource() { 1998 1999 IClasspathEntry[] entries; 2002 try { 2003 entries = this.getRawClasspath(); 2004 } catch (JavaModelException e) { 2005 return true; } 2007 for (int i = 0, max = entries.length; i < max; i++) { 2008 if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) { 2009 return true; 2010 } 2011 } 2012 return false; 2013 } 2014 2015 2016 2017 2020 public boolean isOnClasspath(IJavaElement element) { 2021 IClasspathEntry[] rawClasspath; 2022 try { 2023 rawClasspath = getRawClasspath(); 2024 } catch(JavaModelException e){ 2025 return false; } 2027 int elementType = element.getElementType(); 2028 boolean isPackageFragmentRoot = false; 2029 boolean isFolderPath = false; 2030 boolean isSource = false; 2031 switch (elementType) { 2032 case IJavaElement.JAVA_MODEL: 2033 return false; 2034 case IJavaElement.JAVA_PROJECT: 2035 break; 2036 case IJavaElement.PACKAGE_FRAGMENT_ROOT: 2037 isPackageFragmentRoot = true; 2038 break; 2039 case IJavaElement.PACKAGE_FRAGMENT: 2040 isFolderPath = !((IPackageFragmentRoot)element.getParent()).isArchive(); 2041 break; 2042 case IJavaElement.COMPILATION_UNIT: 2043 isSource = true; 2044 break; 2045 default: 2046 isSource = element.getAncestor(IJavaElement.COMPILATION_UNIT) != null; 2047 break; 2048 } 2049 IPath elementPath = element.getPath(); 2050 2051 int length = rawClasspath.length; 2053 for (int i = 0; i < length; i++) { 2054 IClasspathEntry entry = rawClasspath[i]; 2055 switch (entry.getEntryKind()) { 2056 case IClasspathEntry.CPE_LIBRARY: 2057 case IClasspathEntry.CPE_PROJECT: 2058 case IClasspathEntry.CPE_SOURCE: 2059 if (isOnClasspathEntry(elementPath, isFolderPath, isPackageFragmentRoot, entry)) 2060 return true; 2061 break; 2062 } 2063 } 2064 2065 if (isSource) 2068 return false; 2069 2070 for (int i = 0; i < length; i++) { 2072 IClasspathEntry rawEntry = rawClasspath[i]; 2073 switch (rawEntry.getEntryKind()) { 2074 case IClasspathEntry.CPE_CONTAINER: 2075 IClasspathContainer container; 2076 try { 2077 container = JavaCore.getClasspathContainer(rawEntry.getPath(), this); 2078 } catch (JavaModelException e) { 2079 break; 2080 } 2081 if (container == null) 2082 break; 2083 IClasspathEntry[] containerEntries = container.getClasspathEntries(); 2084 if (containerEntries == null) 2085 break; 2086 for (int j = 0, containerLength = containerEntries.length; j < containerLength; j++){ 2088 IClasspathEntry resolvedEntry = containerEntries[j]; 2089 if (resolvedEntry == null) { 2090 if (JavaModelManager.CP_RESOLVE_VERBOSE) { 2091 JavaModelManager.getJavaModelManager().verbose_missbehaving_container(this, rawEntry.getPath(), containerEntries); 2092 } 2093 return false; 2094 } 2095 if (isOnClasspathEntry(elementPath, isFolderPath, isPackageFragmentRoot, resolvedEntry)) 2096 return true; 2097 } 2098 break; 2099 case IClasspathEntry.CPE_VARIABLE: 2100 IClasspathEntry resolvedEntry = JavaCore.getResolvedClasspathEntry(rawEntry); 2101 if (resolvedEntry == null) 2102 break; 2103 if (isOnClasspathEntry(elementPath, isFolderPath, isPackageFragmentRoot, resolvedEntry)) 2104 return true; 2105 break; 2106 } 2107 } 2108 2109 return false; 2110 } 2111 2112 2115 public boolean isOnClasspath(IResource resource) { 2116 IPath exactPath = resource.getFullPath(); 2117 IPath path = exactPath; 2118 2119 int resourceType = resource.getType(); 2121 boolean isFolderPath = resourceType == IResource.FOLDER || resourceType == IResource.PROJECT; 2122 2123 IClasspathEntry[] classpath; 2124 try { 2125 classpath = this.getResolvedClasspath(); 2126 } catch(JavaModelException e){ 2127 return false; } 2129 for (int i = 0; i < classpath.length; i++) { 2130 IClasspathEntry entry = classpath[i]; 2131 IPath entryPath = entry.getPath(); 2132 if (entryPath.equals(exactPath)) { return true; 2134 } 2135 if (entryPath.isPrefixOf(path) 2136 && !Util.isExcluded(path, ((ClasspathEntry)entry).fullInclusionPatternChars(), ((ClasspathEntry)entry).fullExclusionPatternChars(), isFolderPath)) { 2137 return true; 2138 } 2139 } 2140 return false; 2141 } 2142 2143 private boolean isOnClasspathEntry(IPath elementPath, boolean isFolderPath, boolean isPackageFragmentRoot, IClasspathEntry entry) { 2144 IPath entryPath = entry.getPath(); 2145 if (isPackageFragmentRoot) { 2146 if (entryPath.equals(elementPath)) 2148 return true; 2149 } else { 2150 if (entryPath.isPrefixOf(elementPath) 2151 && !Util.isExcluded(elementPath, ((ClasspathEntry)entry).fullInclusionPatternChars(), ((ClasspathEntry)entry).fullExclusionPatternChars(), isFolderPath)) 2152 return true; 2153 } 2154 return false; 2155 } 2156 2157 2160 private Preferences loadPreferences() { 2161 2162 Preferences preferences = new Preferences(); 2163 IPath projectMetaLocation = getPluginWorkingLocation(); 2164 if (projectMetaLocation != null) { 2165 File prefFile = projectMetaLocation.append(PREF_FILENAME).toFile(); 2166 if (prefFile.exists()) { InputStream in = null; 2168 try { 2169 in = new BufferedInputStream(new FileInputStream(prefFile)); 2170 preferences.load(in); 2171 } catch (IOException e) { } finally { 2173 if (in != null) { 2174 try { 2175 in.close(); 2176 } catch (IOException e) { } 2178 } 2179 } 2180 prefFile.delete(); 2182 return preferences; 2183 } 2184 } 2185 return null; 2186 } 2187 2188 2191 public IEvaluationContext newEvaluationContext() { 2192 EvaluationContext context = new EvaluationContext(); 2193 context.setLineSeparator(Util.getLineSeparator(null, this)); 2194 return new EvaluationContextWrapper(context, this); 2195 } 2196 2197 2200 public NameLookup newNameLookup(ICompilationUnit[] workingCopies) throws JavaModelException { 2201 return getJavaProjectElementInfo().newNameLookup(this, workingCopies); 2202 } 2203 2204 2207 public NameLookup newNameLookup(WorkingCopyOwner owner) throws JavaModelException { 2208 2209 JavaModelManager manager = JavaModelManager.getJavaModelManager(); 2210 ICompilationUnit[] workingCopies = owner == null ? null : manager.getWorkingCopies(owner, true); 2211 return newNameLookup(workingCopies); 2212 } 2213 2214 2217 public SearchableEnvironment newSearchableNameEnvironment(ICompilationUnit[] workingCopies) throws JavaModelException { 2218 return new SearchableEnvironment(this, workingCopies); 2219 } 2220 2221 2225 public SearchableEnvironment newSearchableNameEnvironment(WorkingCopyOwner owner) throws JavaModelException { 2226 return new SearchableEnvironment(this, owner); 2227 } 2228 2229 2232 public ITypeHierarchy newTypeHierarchy( 2233 IRegion region, 2234 IProgressMonitor monitor) 2235 throws JavaModelException { 2236 2237 return newTypeHierarchy(region, DefaultWorkingCopyOwner.PRIMARY, monitor); 2238 } 2239 2240 2243 public ITypeHierarchy newTypeHierarchy( 2244 IRegion region, 2245 WorkingCopyOwner owner, 2246 IProgressMonitor monitor) 2247 throws JavaModelException { 2248 2249 if (region == null) { 2250 throw new IllegalArgumentException (Messages.hierarchy_nullRegion); 2251 } 2252 ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true); 2253 CreateTypeHierarchyOperation op = 2254 new CreateTypeHierarchyOperation(region, workingCopies, null, true); 2255 op.runOperation(monitor); 2256 return op.getResult(); 2257 } 2258 2259 2262 public ITypeHierarchy newTypeHierarchy( 2263 IType type, 2264 IRegion region, 2265 IProgressMonitor monitor) 2266 throws JavaModelException { 2267 2268 return newTypeHierarchy(type, region, DefaultWorkingCopyOwner.PRIMARY, monitor); 2269 } 2270 2271 2274 public ITypeHierarchy newTypeHierarchy( 2275 IType type, 2276 IRegion region, 2277 WorkingCopyOwner owner, 2278 IProgressMonitor monitor) 2279 throws JavaModelException { 2280 2281 if (type == null) { 2282 throw new IllegalArgumentException (Messages.hierarchy_nullFocusType); 2283 } 2284 if (region == null) { 2285 throw new IllegalArgumentException (Messages.hierarchy_nullRegion); 2286 } 2287 ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true); 2288 CreateTypeHierarchyOperation op = 2289 new CreateTypeHierarchyOperation(region, workingCopies, type, true); 2290 op.runOperation(monitor); 2291 return op.getResult(); 2292 } 2293 public String [] projectPrerequisites(IClasspathEntry[] entries) 2294 throws JavaModelException { 2295 2296 ArrayList prerequisites = new ArrayList (); 2297 entries = resolveClasspath(entries); 2299 for (int i = 0, length = entries.length; i < length; i++) { 2300 IClasspathEntry entry = entries[i]; 2301 if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) { 2302 prerequisites.add(entry.getPath().lastSegment()); 2303 } 2304 } 2305 int size = prerequisites.size(); 2306 if (size == 0) { 2307 return NO_PREREQUISITES; 2308 } else { 2309 String [] result = new String [size]; 2310 prerequisites.toArray(result); 2311 return result; 2312 } 2313 } 2314 2315 2321 public IClasspathEntry[] readFileEntriesWithException(Map unknownElements) throws CoreException, IOException, AssertionFailedException { 2322 String xmlClasspath; 2323 IFile rscFile = this.project.getFile(JavaProject.CLASSPATH_FILENAME); 2324 if (rscFile.exists()) { 2325 byte[] bytes = Util.getResourceContentsAsByteArray(rscFile); 2326 try { 2327 xmlClasspath = new String (bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8); } catch (UnsupportedEncodingException e) { 2329 Util.log(e, "Could not read .classpath with UTF-8 encoding"); xmlClasspath = new String (bytes); 2332 } 2333 } else { 2334 URI location = rscFile.getLocationURI(); 2338 if (location == null) 2339 throw new IOException("Cannot obtain a location URI for " + rscFile); File file = Util.toLocalFile(location, null); 2341 if (file == null) 2342 throw new IOException("Unable to fetch file from " + location); byte[] bytes; 2344 try { 2345 bytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(file); 2346 } catch (IOException e) { 2347 if (!file.exists()) 2348 return defaultClasspath(); 2349 throw e; 2350 } 2351 try { 2352 xmlClasspath = new String (bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8); } catch (UnsupportedEncodingException e) { 2354 Util.log(e, "Could not read .classpath with UTF-8 encoding"); xmlClasspath = new String (bytes); 2357 } 2358 } 2359 return decodeClasspath(xmlClasspath, unknownElements); 2360 } 2361 2362 2367 private IClasspathEntry[] readFileEntries(Map unkwownElements) { 2368 try { 2369 return readFileEntriesWithException(unkwownElements); 2370 } catch (CoreException e) { 2371 Util.log(e, "Exception while reading " + getPath().append(JavaProject.CLASSPATH_FILENAME)); return JavaProject.INVALID_CLASSPATH; 2373 } catch (IOException e) { 2374 Util.log(e, "Exception while reading " + getPath().append(JavaProject.CLASSPATH_FILENAME)); return JavaProject.INVALID_CLASSPATH; 2376 } catch (AssertionFailedException e) { 2377 Util.log(e, "Exception while reading " + getPath().append(JavaProject.CLASSPATH_FILENAME)); return JavaProject.INVALID_CLASSPATH; 2379 } 2380 } 2381 2382 2385 public IPath readOutputLocation() { 2386 IClasspathEntry[] classpath = readFileEntries(null); 2388 if (classpath == JavaProject.INVALID_CLASSPATH) 2389 return defaultOutputLocation(); 2390 2391 IPath outputLocation = null; 2393 if (classpath.length > 0) { 2394 IClasspathEntry entry = classpath[classpath.length - 1]; 2395 if (entry.getContentKind() == ClasspathEntry.K_OUTPUT) { 2396 outputLocation = entry.getPath(); 2397 } 2398 } 2399 return outputLocation; 2400 } 2401 2402 2405 public IClasspathEntry[] readRawClasspath() { 2406 IClasspathEntry[] classpath = readFileEntries(null); 2408 if (classpath == JavaProject.INVALID_CLASSPATH) 2409 return defaultClasspath(); 2410 2411 if (classpath.length > 0) { 2413 IClasspathEntry entry = classpath[classpath.length - 1]; 2414 if (entry.getContentKind() == ClasspathEntry.K_OUTPUT) { 2415 IClasspathEntry[] copy = new IClasspathEntry[classpath.length - 1]; 2416 System.arraycopy(classpath, 0, copy, 0, copy.length); 2417 classpath = copy; 2418 } 2419 } 2420 return classpath; 2421 } 2422 2423 2426 protected void removeFromBuildSpec(String builderID) throws CoreException { 2427 2428 IProjectDescription description = this.project.getDescription(); 2429 ICommand[] commands = description.getBuildSpec(); 2430 for (int i = 0; i < commands.length; ++i) { 2431 if (commands[i].getBuilderName().equals(builderID)) { 2432 ICommand[] newCommands = new ICommand[commands.length - 1]; 2433 System.arraycopy(commands, 0, newCommands, 0, i); 2434 System.arraycopy(commands, i + 1, newCommands, i, commands.length - i - 1); 2435 description.setBuildSpec(newCommands); 2436 this.project.setDescription(description, null); 2437 return; 2438 } 2439 } 2440 } 2441 2442 2445 public void resetCaches() { 2446 JavaProjectElementInfo info = (JavaProjectElementInfo) JavaModelManager.getJavaModelManager().peekAtInfo(this); 2447 if (info != null){ 2448 info.resetCaches(); 2449 } 2450 } 2451 2452 2455 public IClasspathEntry[] resolveClasspath(IClasspathEntry[] rawClasspath) throws JavaModelException { 2456 ArrayList resolvedEntries = new ArrayList (); 2457 for (int i = 0, length = rawClasspath.length; i < length; i++) { 2458 IClasspathEntry rawEntry = rawClasspath[i]; 2459 switch (rawEntry.getEntryKind()){ 2460 case IClasspathEntry.CPE_VARIABLE: 2461 IClasspathEntry resolvedEntry = null; 2462 try { 2463 resolvedEntry = JavaCore.getResolvedClasspathEntry(rawEntry); 2464 } catch (AssertionFailedException e) { 2465 break; 2468 } 2469 if (resolvedEntry != null) 2470 resolvedEntries.add(resolvedEntry); 2471 break; 2472 case IClasspathEntry.CPE_CONTAINER: 2473 IClasspathContainer container = JavaCore.getClasspathContainer(rawEntry.getPath(), this); 2474 if (container == null) 2475 break; 2476 IClasspathEntry[] containerEntries = container.getClasspathEntries(); 2477 if (containerEntries == null) 2478 break; 2479 2480 for (int j = 0, containerLength = containerEntries.length; j < containerLength; j++){ 2482 ClasspathEntry cEntry = (ClasspathEntry) containerEntries[j]; 2483 if (cEntry == null) { 2484 if (JavaModelManager.CP_RESOLVE_VERBOSE) { 2485 JavaModelManager.getJavaModelManager().verbose_missbehaving_container(this, rawEntry.getPath(), containerEntries); 2486 } 2487 break; 2488 } 2489 cEntry = cEntry.combineWith((ClasspathEntry) rawEntry); 2491 resolvedEntries.add(cEntry); 2492 } 2493 break; 2494 default: 2495 resolvedEntries.add(rawEntry); 2496 } 2497 } 2498 IClasspathEntry[] result = new IClasspathEntry[resolvedEntries.size()]; 2499 resolvedEntries.toArray(result); 2500 return result; 2501 } 2502 2503 2506 public void resolveClasspath(PerProjectInfo perProjectInfo) throws JavaModelException { 2507 JavaModelManager manager = JavaModelManager.getJavaModelManager(); 2508 try { 2509 manager.setClasspathBeingResolved(this, true); 2510 2511 IClasspathEntry[] rawClasspath; 2513 IPath outputLocation; 2514 IJavaModelStatus rawClasspathStatus; 2515 synchronized (perProjectInfo) { 2516 rawClasspath= perProjectInfo.rawClasspath; 2517 if (rawClasspath == null) 2518 rawClasspath = perProjectInfo.readAndCacheClasspath(this); 2519 outputLocation = perProjectInfo.outputLocation; 2520 rawClasspathStatus = perProjectInfo.rawClasspathStatus; 2521 } 2522 2523 IJavaModelStatus unresolvedEntryStatus = JavaModelStatus.VERIFIED_OK; 2524 HashMap rawReverseMap = new HashMap (); 2525 Map rootPathToResolvedEntries = new HashMap (); 2526 2527 ArrayList resolvedEntries = new ArrayList (); 2528 int length = rawClasspath.length; 2529 for (int i = 0; i < length; i++) { 2530 2531 IClasspathEntry rawEntry = rawClasspath[i]; 2532 IPath resolvedPath; 2533 2534 switch (rawEntry.getEntryKind()){ 2535 2536 case IClasspathEntry.CPE_VARIABLE : 2537 IClasspathEntry resolvedEntry = null; 2538 try { 2539 resolvedEntry = JavaCore.getResolvedClasspathEntry(rawEntry); 2540 } catch (AssertionFailedException e) { 2541 unresolvedEntryStatus = new JavaModelStatus(IJavaModelStatusConstants.INVALID_PATH, e.getMessage()); 2544 break; 2545 } 2546 if (resolvedEntry == null) { 2547 unresolvedEntryStatus = new JavaModelStatus(IJavaModelStatusConstants.CP_VARIABLE_PATH_UNBOUND, this, rawEntry.getPath()); 2548 } else { 2549 if (rawReverseMap.get(resolvedPath = resolvedEntry.getPath()) == null) { 2550 rawReverseMap.put(resolvedPath , rawEntry); 2551 rootPathToResolvedEntries.put(resolvedPath, resolvedEntry); 2552 } 2553 resolvedEntries.add(resolvedEntry); 2554 } 2555 break; 2556 2557 case IClasspathEntry.CPE_CONTAINER : 2558 IClasspathContainer container = JavaCore.getClasspathContainer(rawEntry.getPath(), this); 2559 if (container == null){ 2560 unresolvedEntryStatus = new JavaModelStatus(IJavaModelStatusConstants.CP_CONTAINER_PATH_UNBOUND, this, rawEntry.getPath()); 2561 break; 2562 } 2563 2564 IClasspathEntry[] containerEntries = container.getClasspathEntries(); 2565 if (containerEntries == null) break; 2566 2567 for (int j = 0, containerLength = containerEntries.length; j < containerLength; j++){ 2569 ClasspathEntry cEntry = (ClasspathEntry) containerEntries[j]; 2570 if (cEntry == null) { 2571 if (JavaModelManager.CP_RESOLVE_VERBOSE) { 2572 JavaModelManager.getJavaModelManager().verbose_missbehaving_container(this, rawEntry.getPath(), containerEntries); 2573 } 2574 break; 2575 } 2576 cEntry = cEntry.combineWith((ClasspathEntry) rawEntry); 2578 if (rawReverseMap.get(resolvedPath = cEntry.getPath()) == null) { 2579 rawReverseMap.put(resolvedPath , rawEntry); 2580 rootPathToResolvedEntries.put(resolvedPath, cEntry); 2581 } 2582 resolvedEntries.add(cEntry); 2583 } 2584 break; 2585 2586 default : 2587 if (rawReverseMap.get(resolvedPath = rawEntry.getPath()) == null) { 2588 rawReverseMap.put(resolvedPath , rawEntry); 2589 rootPathToResolvedEntries.put(resolvedPath, rawEntry); 2590 } 2591 resolvedEntries.add(rawEntry); 2592 2593 } 2594 } 2595 2596 IClasspathEntry[] resolvedClasspath = new IClasspathEntry[resolvedEntries.size()]; 2598 resolvedEntries.toArray(resolvedClasspath); 2599 perProjectInfo.setClasspath(rawClasspath, outputLocation, rawClasspathStatus, resolvedClasspath, rawReverseMap, rootPathToResolvedEntries, unresolvedEntryStatus); 2600 } finally { 2601 manager.setClasspathBeingResolved(this, false); 2602 } 2603 } 2604 2605 2610 public String rootID(){ 2611 return "[PRJ]"+this.project.getFullPath(); } 2613 2614 2623 public boolean saveClasspath(IClasspathEntry[] newClasspath, IPath newOutputLocation) throws JavaModelException { 2624 2625 if (!this.project.isAccessible()) return false; 2626 2627 Map unknownElements = new HashMap (); 2628 IClasspathEntry[] fileEntries = readFileEntries(unknownElements); 2629 if (fileEntries != JavaProject.INVALID_CLASSPATH && areClasspathsEqual(newClasspath, newOutputLocation, fileEntries)) { 2630 return false; 2632 } 2633 2634 try { 2636 setSharedProperty(JavaProject.CLASSPATH_FILENAME, encodeClasspath(newClasspath, newOutputLocation, true, unknownElements)); 2637 return true; 2638 } catch (CoreException e) { 2639 throw new JavaModelException(e); 2640 } 2641 } 2642 2643 2647 private void setJavaCommand( 2648 IProjectDescription description, 2649 ICommand newCommand) 2650 throws CoreException { 2651 2652 ICommand[] oldBuildSpec = description.getBuildSpec(); 2653 int oldJavaCommandIndex = getJavaCommandIndex(oldBuildSpec); 2654 ICommand[] newCommands; 2655 2656 if (oldJavaCommandIndex == -1) { 2657 newCommands = new ICommand[oldBuildSpec.length + 1]; 2659 System.arraycopy(oldBuildSpec, 0, newCommands, 1, oldBuildSpec.length); 2660 newCommands[0] = newCommand; 2661 } else { 2662 oldBuildSpec[oldJavaCommandIndex] = newCommand; 2663 newCommands = oldBuildSpec; 2664 } 2665 2666 description.setBuildSpec(newCommands); 2668 this.project.setDescription(description, null); 2669 } 2670 2671 2674 public void setOption(String optionName, String optionValue) { 2675 if (!JavaModelManager.getJavaModelManager().optionNames.contains(optionName)) return; if (optionValue == null) return; IEclipsePreferences projectPreferences = getEclipsePreferences(); 2678 String defaultValue = JavaCore.getOption(optionName); 2679 if (optionValue.equals(defaultValue)) { 2680 projectPreferences.remove(optionName); 2682 } else { 2683 projectPreferences.put(optionName, optionValue); 2684 } 2685 2686 try { 2688 projectPreferences.flush(); 2689 } catch (BackingStoreException e) { 2690 } 2692 } 2693 2694 2697 public void setOptions(Map newOptions) { 2698 2699 IEclipsePreferences projectPreferences = getEclipsePreferences(); 2700 try { 2701 if (newOptions == null){ 2702 projectPreferences.clear(); 2703 } else { 2704 Iterator entries = newOptions.entrySet().iterator(); 2705 while (entries.hasNext()){ 2706 Map.Entry entry = (Map.Entry ) entries.next(); 2707 String key = (String ) entry.getKey(); 2708 if (!JavaModelManager.getJavaModelManager().optionNames.contains(key)) continue; projectPreferences.put(key, (String ) entry.getValue()); 2711 } 2712 2713 String [] pNames = projectPreferences.keys(); 2717 int ln = pNames.length; 2718 for (int i=0; i<ln; i++) { 2719 String key = pNames[i]; 2720 if (!newOptions.containsKey(key)) { 2721 projectPreferences.remove(key); } 2723 } 2724 } 2725 2726 projectPreferences.flush(); 2728 2729 try { 2731 getPerProjectInfo().options = null; 2732 } catch (JavaModelException e) { 2733 } 2735 } catch (BackingStoreException e) { 2736 } 2738 } 2739 2742 public void setOutputLocation(IPath path, IProgressMonitor monitor) throws JavaModelException { 2743 if (path == null) { 2744 throw new IllegalArgumentException (Messages.path_nullPath); 2745 } 2746 if (path.equals(getOutputLocation())) { 2747 return; 2748 } 2749 setRawClasspath(getRawClasspath(), path, monitor); 2750 } 2751 2752 2759 public void setProject(IProject project) { 2760 2761 this.project = project; 2762 this.parent = JavaModelManager.getJavaModelManager().getJavaModel(); 2763 } 2764 2765 2768 public void setRawClasspath( 2769 IClasspathEntry[] entries, 2770 boolean canModifyResources, 2771 IProgressMonitor monitor) 2772 throws JavaModelException { 2773 2774 setRawClasspath( 2775 entries, 2776 getOutputLocation(), 2777 canModifyResources, 2778 monitor); 2779 } 2780 2781 2784 public void setRawClasspath( 2785 IClasspathEntry[] newRawClasspath, 2786 IPath newOutputLocation, 2787 boolean canModifyResources, 2788 IProgressMonitor monitor) 2789 throws JavaModelException { 2790 2791 try { 2792 if (newRawClasspath == null) newRawClasspath = defaultClasspath(); 2794 2795 SetClasspathOperation op = 2796 new SetClasspathOperation( 2797 this, 2798 newRawClasspath, 2799 newOutputLocation, 2800 canModifyResources); 2801 op.runOperation(monitor); 2802 } catch (JavaModelException e) { 2803 JavaModelManager.getJavaModelManager().getDeltaProcessor().flush(); 2804 throw e; 2805 } 2806 } 2807 2808 2811 public void setRawClasspath( 2812 IClasspathEntry[] entries, 2813 IPath outputLocation, 2814 IProgressMonitor monitor) 2815 throws JavaModelException { 2816 2817 setRawClasspath( 2818 entries, 2819 outputLocation, 2820 true, 2821 monitor); 2822 } 2823 2824 2827 public void setRawClasspath( 2828 IClasspathEntry[] entries, 2829 IProgressMonitor monitor) 2830 throws JavaModelException { 2831 2832 setRawClasspath( 2833 entries, 2834 getOutputLocation(), 2835 true, 2836 monitor); 2837 } 2838 2839 2853 public void setSharedProperty(String key, String value) throws CoreException { 2854 2855 IFile rscFile = this.project.getFile(key); 2856 byte[] bytes = null; 2857 try { 2858 bytes = value.getBytes(org.eclipse.jdt.internal.compiler.util.Util.UTF_8); } catch (UnsupportedEncodingException e) { 2860 Util.log(e, "Could not write .classpath with UTF-8 encoding "); bytes = value.getBytes(); 2863 } 2864 InputStream inputStream = new ByteArrayInputStream(bytes); 2865 if (rscFile.exists()) { 2867 if (rscFile.isReadOnly()) { 2868 ResourcesPlugin.getWorkspace().validateEdit(new IFile[]{rscFile}, null); 2870 } 2871 rscFile.setContents(inputStream, IResource.FORCE, null); 2872 } else { 2873 rscFile.create(inputStream, IResource.FORCE, null); 2874 } 2875 } 2876 2877 2886 public void updateCycleParticipants( 2887 ArrayList prereqChain, 2888 HashSet cycleParticipants, 2889 IWorkspaceRoot workspaceRoot, 2890 HashSet traversed, 2891 Map preferredClasspaths){ 2892 2893 IPath path = this.getPath(); 2894 prereqChain.add(path); 2895 traversed.add(path); 2896 try { 2897 IClasspathEntry[] classpath = null; 2898 if (preferredClasspaths != null) classpath = (IClasspathEntry[])preferredClasspaths.get(this); 2899 if (classpath == null) classpath = getResolvedClasspath(); 2900 for (int i = 0, length = classpath.length; i < length; i++) { 2901 IClasspathEntry entry = classpath[i]; 2902 2903 if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT){ 2904 IPath prereqProjectPath = entry.getPath(); 2905 int index = cycleParticipants.contains(prereqProjectPath) ? 0 : prereqChain.indexOf(prereqProjectPath); 2906 if (index >= 0) { for (int size = prereqChain.size(); index < size; index++) { 2908 cycleParticipants.add(prereqChain.get(index)); 2909 } 2910 } else { 2911 if (!traversed.contains(prereqProjectPath)) { 2912 IResource member = workspaceRoot.findMember(prereqProjectPath); 2913 if (member != null && member.getType() == IResource.PROJECT){ 2914 JavaProject javaProject = (JavaProject)JavaCore.create((IProject)member); 2915 javaProject.updateCycleParticipants(prereqChain, cycleParticipants, workspaceRoot, traversed, preferredClasspaths); 2916 } 2917 } 2918 } 2919 } 2920 } 2921 } catch(JavaModelException e){ 2922 } 2924 prereqChain.remove(path); 2925 } 2926 2927 2930 public void updatePackageFragmentRoots(){ 2931 2932 if (this.isOpen()) { 2933 try { 2934 JavaProjectElementInfo info = getJavaProjectElementInfo(); 2935 computeChildren(info); 2936 info.resetCaches(); } catch(JavaModelException e){ 2938 try { 2939 close(); } catch(JavaModelException ex){ 2941 } 2943 } 2944 } 2945 } 2946 2947 2950 private void updatePreferences(IEclipsePreferences preferences) { 2951 2952 Preferences oldPreferences = loadPreferences(); 2953 if (oldPreferences != null) { 2954 String [] propertyNames = oldPreferences.propertyNames(); 2955 for (int i = 0; i < propertyNames.length; i++){ 2956 String propertyName = propertyNames[i]; 2957 String propertyValue = oldPreferences.getString(propertyName); 2958 if (!"".equals(propertyValue)) { preferences.put(propertyName, propertyValue); 2960 } 2961 } 2962 try { 2963 preferences.flush(); 2965 } catch (BackingStoreException e) { 2966 } 2968 } 2969 } 2970} 2971 | Popular Tags |