1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import java.io.BufferedInputStream;
14 import java.io.BufferedOutputStream;
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
18 import java.io.FileInputStream;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.io.OutputStreamWriter;
24 import java.io.StringReader;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.Hashtable;
29 import java.util.Iterator;
30 import java.util.List;
33 import javax.xml.parsers.DocumentBuilder;
34 import javax.xml.parsers.DocumentBuilderFactory;
35 import javax.xml.parsers.ParserConfigurationException;
36 import javax.xml.parsers.SAXParserFactory;
38 import net.sourceforge.phpdt.core.IClasspathEntry;
39 import net.sourceforge.phpdt.core.ICompilationUnit;
40 import net.sourceforge.phpdt.core.IJavaElement;
41 import net.sourceforge.phpdt.core.IJavaModelMarker;
42 import net.sourceforge.phpdt.core.IJavaModelStatus;
43 import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
44 import net.sourceforge.phpdt.core.IJavaProject;
45 import net.sourceforge.phpdt.core.IPackageFragment;
46 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
47 import net.sourceforge.phpdt.core.JavaCore;
48 import net.sourceforge.phpdt.core.JavaModelException;
49 import net.sourceforge.phpdt.core.WorkingCopyOwner;
50 import net.sourceforge.phpdt.internal.codeassist.ISearchableNameEnvironment;
51 import net.sourceforge.phpdt.internal.compiler.util.ObjectVector;
52 import net.sourceforge.phpdt.internal.core.util.Util;
53 import net.sourceforge.phpdt.internal.corext.Assert;
54 import net.sourceforge.phpeclipse.LoadPathEntry;
55 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
57 import org.eclipse.core.resources.ICommand;
58 import org.eclipse.core.resources.IFile;
59 import org.eclipse.core.resources.IFolder;
60 import org.eclipse.core.resources.IMarker;
61 import org.eclipse.core.resources.IProject;
62 import org.eclipse.core.resources.IProjectDescription;
63 import org.eclipse.core.resources.IProjectNature;
64 import org.eclipse.core.resources.IResource;
65 import org.eclipse.core.resources.IWorkspace;
66 import org.eclipse.core.resources.IWorkspaceRoot;
67 import org.eclipse.core.resources.ResourcesPlugin;
68 import org.eclipse.core.runtime.CoreException;
69 import org.eclipse.core.runtime.IPath;
70 import org.eclipse.core.runtime.IProgressMonitor;
71 import org.eclipse.core.runtime.Path;
72 import org.eclipse.core.runtime.Preferences;
73 import org.eclipse.core.runtime.QualifiedName;
74 import org.w3c.dom.Element;
75 import org.w3c.dom.Node;
76 import org.w3c.dom.NodeList;
77 import org.xml.sax.Attributes;
78 import org.xml.sax.ContentHandler;
79 import org.xml.sax.InputSource;
80 import org.xml.sax.Locator;
81 import org.xml.sax.SAXException;
82 import org.xml.sax.XMLReader;
85 * Handle for a Java Project.
87 * <p>A Java Project internally maintains a devpath that corresponds
88 * to the project's classpath. The classpath may include source folders
89 * from the current project; jars in the current project, other projects,
90 * and the local file system; and binary folders (output location) of other
91 * projects. The Java Model presents source elements corresponding to output
92 * .class files in other projects, and thus uses the devpath rather than
93 * the classpath (which is really a compilation path). The devpath mimics
94 * the classpath, except has source folder entries in place of output
95 * locations in external projects.
97 * <p>Each JavaProject has a NameLookup facility that locates elements
98 * on by name, based on the devpath.
102 public class JavaProject
104 implements IJavaProject , IProjectNature {
107 * Whether the underlying file system is case sensitive.
109 protected static final boolean IS_CASE_SENSITIVE = !new File("Temp").equals(new File("temp")); //$NON-NLS-1$ //$NON-NLS-2$
112 * An empty array of strings indicating that a project doesn't have any prerequesite projects.
114 protected static final String[] NO_PREREQUISITES = new String[0];
117 * The platform project this <code>IJavaProject</code> is based on
119 protected IProject project;
120 protected List fLoadPathEntries;
121 protected boolean fScratched;
124 * Name of file containing project classpath
126 public static final String CLASSPATH_FILENAME = ".classpath"; //$NON-NLS-1$
129 * Name of file containing custom project preferences
131 public static final String PREF_FILENAME = ".jprefs"; //$NON-NLS-1$
134 * Value of the project's raw classpath if the .classpath file contains invalid entries.
136 public static final IClasspathEntry[] INVALID_CLASSPATH = new IClasspathEntry[0];
138 private static final String CUSTOM_DEFAULT_OPTION_VALUE = "#\r\n\r#custom-non-empty-default-value#\r\n\r#"; //$NON-NLS-1$
141 * Returns a canonicalized path from the given external path.
142 * Note that the return path contains the same number of segments
143 * and it contains a device only if the given path contained one.
144 * @see java.io.File for the definition of a canonicalized path
146 public static IPath canonicalizedPath(IPath externalPath) {
148 if (externalPath == null)
151 if (JavaModelManager.VERBOSE) {
152 System.out.println("JAVA MODEL - Canonicalizing " + externalPath.toString()); //$NON-NLS-1$
155 if (IS_CASE_SENSITIVE) {
156 if (JavaModelManager.VERBOSE) {
157 System.out.println("JAVA MODEL - Canonical path is original path (file system is case sensitive)"); //$NON-NLS-1$
162 // if not external path, return original path
163 IWorkspace workspace = ResourcesPlugin.getWorkspace();
164 if (workspace == null) return externalPath; // protection during shutdown (30487)
165 if (workspace.getRoot().findMember(externalPath) != null) {
166 if (JavaModelManager.VERBOSE) {
167 System.out.println("JAVA MODEL - Canonical path is original path (member of workspace)"); //$NON-NLS-1$
172 IPath canonicalPath = null;
175 new Path(new File(externalPath.toOSString()).getCanonicalPath());
176 } catch (IOException e) {
177 // default to original path
178 if (JavaModelManager.VERBOSE) {
179 System.out.println("JAVA MODEL - Canonical path is original path (IOException)"); //$NON-NLS-1$
185 int canonicalLength = canonicalPath.segmentCount();
186 if (canonicalLength == 0) {
187 // the java.io.File canonicalization failed
188 if (JavaModelManager.VERBOSE) {
189 System.out.println("JAVA MODEL - Canonical path is original path (canonical path is empty)"); //$NON-NLS-1$
192 } else if (externalPath.isAbsolute()) {
193 result = canonicalPath;
195 // if path is relative, remove the first segments that were added by the java.io.File canonicalization
196 // e.g. 'lib/classes.zip' was converted to 'd:/myfolder/lib/classes.zip'
197 int externalLength = externalPath.segmentCount();
198 if (canonicalLength >= externalLength) {
199 result = canonicalPath.removeFirstSegments(canonicalLength - externalLength);
201 if (JavaModelManager.VERBOSE) {
202 System.out.println("JAVA MODEL - Canonical path is original path (canonical path is " + canonicalPath.toString() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
208 // keep device only if it was specified (this is because File.getCanonicalPath() converts '/lib/classed.zip' to 'd:/lib/classes/zip')
209 if (externalPath.getDevice() == null) {
210 result = result.setDevice(null);
212 if (JavaModelManager.VERBOSE) {
213 System.out.println("JAVA MODEL - Canonical path is " + result.toString()); //$NON-NLS-1$
219 * Constructor needed for <code>IProject.getNature()</code> and <code>IProject.addNature()</code>.
221 * @see #setProject(IProject)
223 public JavaProject() {
227 public JavaProject(IProject project, JavaElement parent) {
228 super(parent, project.getName());
229 this.project = project;
231 public void addLoadPathEntry(IProject anotherPHPProject) {
234 LoadPathEntry newEntry = new LoadPathEntry(anotherPHPProject);
235 getLoadPathEntries().add(newEntry);
238 public void configure() throws CoreException {
239 // get project description and then the associated build commands
240 IProjectDescription desc = project.getDescription();
241 ICommand[] commands = desc.getBuildSpec();
243 // determine if builder already associated
244 boolean found = false;
245 for (int i = 0; i < commands.length; ++i) {
246 if (commands[i].getBuilderName().equals(PHPeclipsePlugin.BUILDER_PARSER_ID)) {
252 // add builder if not already in project
254 ICommand command = desc.newCommand();
255 command.setBuilderName(PHPeclipsePlugin.BUILDER_PARSER_ID);
256 ICommand[] newCommands = new ICommand[commands.length + 1];
258 // Add it before other builders.
259 System.arraycopy(commands, 0, newCommands, 1, commands.length);
260 newCommands[0] = command;
261 desc.setBuildSpec(newCommands);
262 project.setDescription(desc, null);
266 protected void loadLoadPathEntries() {
267 fLoadPathEntries = new ArrayList();
269 IFile loadPathsFile = getLoadPathEntriesFile();
271 XMLReader reader = null;
273 reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
274 reader.setContentHandler(getLoadPathEntriesContentHandler());
275 reader.parse(new InputSource(loadPathsFile.getContents()));
276 } catch (Exception e) {
277 //the file is nonextant or unreadable
281 public List getLoadPathEntries() {
282 if (fLoadPathEntries == null) {
283 loadLoadPathEntries();
286 return fLoadPathEntries;
289 protected ContentHandler getLoadPathEntriesContentHandler() {
290 return new ContentHandler() {
291 public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
294 public void endDocument() throws SAXException {
297 public void endElement(String arg0, String arg1, String arg2) throws SAXException {
300 public void endPrefixMapping(String arg0) throws SAXException {
303 public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {
306 public void processingInstruction(String arg0, String arg1) throws SAXException {
309 public void setDocumentLocator(Locator arg0) {
312 public void skippedEntity(String arg0) throws SAXException {
315 public void startDocument() throws SAXException {
318 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
319 if ("pathentry".equals(qName))
320 if ("project".equals(atts.getValue("type"))) {
321 IPath referencedProjectPath = new Path(atts.getValue("path"));
322 IProject referencedProject = getProject(referencedProjectPath.lastSegment());
323 fLoadPathEntries.add(new LoadPathEntry(referencedProject));
327 public void startPrefixMapping(String arg0, String arg1) throws SAXException {
332 protected IFile getLoadPathEntriesFile() {
333 return project.getFile(".loadpath");
336 protected String getLoadPathXML() {
337 StringBuffer buffer = new StringBuffer();
338 buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><loadpath>");
340 Iterator pathEntriesIterator = fLoadPathEntries.iterator();
342 while (pathEntriesIterator.hasNext()) {
343 LoadPathEntry entry = (LoadPathEntry) pathEntriesIterator.next();
344 buffer.append(entry.toXML());
347 buffer.append("</loadpath>");
348 return buffer.toString();
351 * Adds a builder to the build spec for the given project.
353 protected void addToBuildSpec(String builderID) throws CoreException {
355 IProjectDescription description = getProject().getDescription();
356 ICommand javaCommand = getJavaCommand(description);
358 if (javaCommand == null) {
360 // Add a Java command to the build spec
361 ICommand command = description.newCommand();
362 command.setBuilderName(builderID);
363 setJavaCommand(description, command);
369 protected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException {
371 // check whether the java project can be opened
372 if (!underlyingResource.isAccessible()) {
373 throw newNotPresentException();
376 IWorkspace workspace = ResourcesPlugin.getWorkspace();
377 IWorkspaceRoot wRoot = workspace.getRoot();
378 // cannot refresh cp markers on opening (emulate cp check on startup) since can create deadlocks (see bug 37274)
379 // IClasspathEntry[] resolvedClasspath = getResolvedClasspath(true/*ignoreUnresolvedEntry*/, false/*don't generateMarkerOnError*/, false/*don't returnResolutionInProgress*/);
381 // // compute the pkg fragment roots
382 // info.setChildren(computePackageFragmentRoots(resolvedClasspath, false));
384 // // remember the timestamps of external libraries the first time they are looked up
385 // for (int i = 0, length = resolvedClasspath.length; i < length; i++) {
386 // IClasspathEntry entry = resolvedClasspath[i];
387 // if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
388 // IPath path = entry.getPath();
389 // Object target = JavaModel.getTarget(wRoot, path, true);
390 // if (target instanceof java.io.File) {
391 // Map externalTimeStamps = JavaModelManager.getJavaModelManager().deltaState.externalTimeStamps;
392 // if (externalTimeStamps.get(path) == null) {
393 // long timestamp = DeltaProcessor.getTimeStamp((java.io.File)target);
394 // externalTimeStamps.put(path, new Long(timestamp));
402 protected void closing(Object info) {
404 // // forget source attachment recommendations
405 // Object[] children = ((JavaElementInfo)info).children;
406 // for (int i = 0, length = children.length; i < length; i++) {
407 // Object child = children[i];
408 // if (child instanceof JarPackageFragmentRoot){
409 // ((JarPackageFragmentRoot)child).setSourceAttachmentProperty(null);
415 // protected void closing(Object info) throws JavaModelException {
417 // // forget source attachment recommendations
418 // IPackageFragmentRoot[] roots = this.getPackageFragmentRoots();
419 //// for (int i = 0; i < roots.length; i++) {
420 //// if (roots[i] instanceof JarPackageFragmentRoot){
421 //// ((JarPackageFragmentRoot) roots[i]).setSourceAttachmentProperty(null);
425 // super.closing(info);
431 * Internal computation of an expanded classpath. It will eliminate duplicates, and produce copies
432 * of exported classpath entries to avoid possible side-effects ever after.
434 private void computeExpandedClasspath(
435 JavaProject initialProject,
436 boolean ignoreUnresolvedVariable,
437 boolean generateMarkerOnError,
438 HashSet visitedProjects,
439 ObjectVector accumulatedEntries) throws JavaModelException {
441 if (visitedProjects.contains(this)){
442 return; // break cycles if any
444 visitedProjects.add(this);
446 if (generateMarkerOnError && !this.equals(initialProject)){
447 generateMarkerOnError = false;
449 IClasspathEntry[] immediateClasspath =
450 getResolvedClasspath(ignoreUnresolvedVariable, generateMarkerOnError);
452 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
453 for (int i = 0, length = immediateClasspath.length; i < length; i++){
454 IClasspathEntry entry = immediateClasspath[i];
456 boolean isInitialProject = this.equals(initialProject);
457 if (isInitialProject || entry.isExported()){
459 accumulatedEntries.add(entry);
461 // recurse in project to get all its indirect exports (only consider exported entries from there on)
462 if (entry.getEntryKind() == ClasspathEntry.CPE_PROJECT) {
463 IResource member = workspaceRoot.findMember(entry.getPath());
464 if (member != null && member.getType() == IResource.PROJECT){ // double check if bound to project (23977)
465 IProject projRsc = (IProject) member;
466 if (JavaProject.hasJavaNature(projRsc)) {
467 JavaProject project = (JavaProject) JavaCore.create(projRsc);
468 project.computeExpandedClasspath(
470 ignoreUnresolvedVariable,
471 generateMarkerOnError,
482 * Returns (local/all) the package fragment roots identified by the given project's classpath.
483 * Note: this follows project classpath references to find required project contributions,
484 * eliminating duplicates silently.
485 * Only works with resolved entries
487 public IPackageFragmentRoot[] computePackageFragmentRoots(IClasspathEntry[] resolvedClasspath, boolean retrieveExportedRoots) throws JavaModelException {
489 ObjectVector accumulatedRoots = new ObjectVector();
490 computePackageFragmentRoots(
493 new HashSet(5), // rootIDs
494 true, // inside original project
495 true, // check existency
496 retrieveExportedRoots);
497 IPackageFragmentRoot[] rootArray = new IPackageFragmentRoot[accumulatedRoots.size()];
498 accumulatedRoots.copyInto(rootArray);
503 * Computes the package fragment roots identified by the given entry.
504 * Only works with resolved entry
506 public IPackageFragmentRoot[] computePackageFragmentRoots(IClasspathEntry resolvedEntry) {
509 computePackageFragmentRoots(
510 new IClasspathEntry[]{ resolvedEntry },
511 false // don't retrieve exported roots
513 } catch (JavaModelException e) {
514 return new IPackageFragmentRoot[] {};
519 * Returns the package fragment roots identified by the given entry. In case it refers to
520 * a project, it will follow its classpath so as to find exported roots as well.
521 * Only works with resolved entry
523 public void computePackageFragmentRoots(
524 IClasspathEntry resolvedEntry,
525 ObjectVector accumulatedRoots,
527 boolean insideOriginalProject,
528 boolean checkExistency,
529 boolean retrieveExportedRoots) throws JavaModelException {
531 String rootID = ((ClasspathEntry)resolvedEntry).rootID();
532 if (rootIDs.contains(rootID)) return;
534 IPath projectPath = getProject().getFullPath();
535 IPath entryPath = resolvedEntry.getPath();
536 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
538 switch(resolvedEntry.getEntryKind()){
541 case IClasspathEntry.CPE_SOURCE :
543 if (projectPath.isPrefixOf(entryPath)){
544 if (checkExistency) {
545 Object target = JavaModel.getTarget(workspaceRoot, entryPath, checkExistency);
546 if (target == null) return;
548 if (target instanceof IFolder || target instanceof IProject){
549 accumulatedRoots.add(
550 getPackageFragmentRoot((IResource)target));
554 IPackageFragmentRoot root = getFolderPackageFragmentRoot(entryPath);
556 accumulatedRoots.add(root);
563 // internal/external JAR or folder
564 case IClasspathEntry.CPE_LIBRARY :
566 if (!insideOriginalProject && !resolvedEntry.isExported()) return;
568 if (checkExistency) {
569 Object target = JavaModel.getTarget(workspaceRoot, entryPath, checkExistency);
570 if (target == null) return;
572 if (target instanceof IResource){
574 IResource resource = (IResource) target;
575 IPackageFragmentRoot root = getPackageFragmentRoot(resource);
577 accumulatedRoots.add(root);
581 // external target - only JARs allowed
582 // if (((java.io.File)target).isFile() && (ProjectPrefUtil.isArchiveFileName(entryPath.lastSegment()))) {
583 // accumulatedRoots.add(
584 // new JarPackageFragmentRoot(entryPath, this));
585 // rootIDs.add(rootID);
589 IPackageFragmentRoot root = getPackageFragmentRoot(entryPath);
591 accumulatedRoots.add(root);
597 // recurse into required project
598 case IClasspathEntry.CPE_PROJECT :
600 if (!retrieveExportedRoots) return;
601 if (!insideOriginalProject && !resolvedEntry.isExported()) return;
603 IResource member = workspaceRoot.findMember(entryPath);
604 if (member != null && member.getType() == IResource.PROJECT){// double check if bound to project (23977)
605 IProject requiredProjectRsc = (IProject) member;
606 if (JavaProject.hasJavaNature(requiredProjectRsc)){ // special builder binary output
608 JavaProject requiredProject = (JavaProject)JavaCore.create(requiredProjectRsc);
609 requiredProject.computePackageFragmentRoots(
610 requiredProject.getResolvedClasspath(true),
615 retrieveExportedRoots);
623 * Returns (local/all) the package fragment roots identified by the given project's classpath.
624 * Note: this follows project classpath references to find required project contributions,
625 * eliminating duplicates silently.
626 * Only works with resolved entries
628 public void computePackageFragmentRoots(
629 IClasspathEntry[] resolvedClasspath,
630 ObjectVector accumulatedRoots,
632 boolean insideOriginalProject,
633 boolean checkExistency,
634 boolean retrieveExportedRoots) throws JavaModelException {
636 if (insideOriginalProject){
637 rootIDs.add(rootID());
639 for (int i = 0, length = resolvedClasspath.length; i < length; i++){
640 computePackageFragmentRoots(
641 resolvedClasspath[i],
644 insideOriginalProject,
646 retrieveExportedRoots);
651 * Compute the file name to use for a given shared property
653 public String computeSharedPropertyFileName(QualifiedName qName) {
655 return '.' + qName.getLocalName();
659 * Returns whether the given resource is accessible through the children or the non-Java resources of this project.
660 * Returns true if the resource is not in the project.
661 * Assumes that the resource is a folder or a file.
663 public boolean contains(IResource resource) {
665 IClasspathEntry[] classpath;
668 classpath = getResolvedClasspath(true);
669 output = getOutputLocation();
670 } catch (JavaModelException e) {
674 IPath fullPath = resource.getFullPath();
675 IPath innerMostOutput = output.isPrefixOf(fullPath) ? output : null;
676 IClasspathEntry innerMostEntry = null;
677 for (int j = 0, cpLength = classpath.length; j < cpLength; j++) {
678 IClasspathEntry entry = classpath[j];
680 IPath entryPath = entry.getPath();
681 if ((innerMostEntry == null || innerMostEntry.getPath().isPrefixOf(entryPath))
682 && entryPath.isPrefixOf(fullPath)) {
683 innerMostEntry = entry;
685 IPath entryOutput = classpath[j].getOutputLocation();
686 if (entryOutput != null && entryOutput.isPrefixOf(fullPath)) {
687 innerMostOutput = entryOutput;
690 if (innerMostEntry != null) {
691 // special case prj==src and nested output location
692 if (innerMostOutput != null && innerMostOutput.segmentCount() > 1 // output isn't project
693 && innerMostEntry.getPath().segmentCount() == 1) { // 1 segment must be project name
696 if (resource instanceof IFolder) {
697 // folders are always included in src/lib entries
700 switch (innerMostEntry.getEntryKind()) {
701 case IClasspathEntry.CPE_SOURCE:
702 // .class files are not visible in source folders
703 return true; //!net.sourceforge.phpdt.internal.compiler.util.ProjectPrefUtil.isClassFileName(fullPath.lastSegment());
704 case IClasspathEntry.CPE_LIBRARY:
705 // .java files are not visible in library folders
706 return !net.sourceforge.phpdt.internal.compiler.util.Util.isJavaFileName(fullPath.lastSegment());
709 if (innerMostOutput != null) {
716 * Record a new marker denoting a classpath problem
718 IMarker createClasspathProblemMarker(IJavaModelStatus status) {
720 IMarker marker = null;
722 String[] arguments = new String[0];
723 boolean isCycleProblem = false, isClasspathFileFormatProblem = false;
724 switch (status.getCode()) {
726 case IJavaModelStatusConstants.CLASSPATH_CYCLE :
727 isCycleProblem = true;
728 if (JavaCore.ERROR.equals(getOption(JavaCore.CORE_CIRCULAR_CLASSPATH, true))) {
729 severity = IMarker.SEVERITY_ERROR;
731 severity = IMarker.SEVERITY_WARNING;
735 case IJavaModelStatusConstants.INVALID_CLASSPATH_FILE_FORMAT :
736 isClasspathFileFormatProblem = true;
737 severity = IMarker.SEVERITY_ERROR;
741 IPath path = status.getPath();
742 if (path != null) arguments = new String[] { path.toString() };
743 if (JavaCore.ERROR.equals(getOption(JavaCore.CORE_INCOMPLETE_CLASSPATH, true))) {
744 severity = IMarker.SEVERITY_ERROR;
746 severity = IMarker.SEVERITY_WARNING;
752 marker = getProject().createMarker(IJavaModelMarker.BUILDPATH_PROBLEM_MARKER);
753 marker.setAttributes(
758 IJavaModelMarker.CYCLE_DETECTED,
759 IJavaModelMarker.CLASSPATH_FILE_FORMAT,
761 IJavaModelMarker.ARGUMENTS ,
765 new Integer(severity),
766 Util.bind("classpath.buildPath"),//$NON-NLS-1$
767 isCycleProblem ? "true" : "false",//$NON-NLS-1$ //$NON-NLS-2$
768 isClasspathFileFormatProblem ? "true" : "false",//$NON-NLS-1$ //$NON-NLS-2$
769 new Integer(status.getCode()),
770 Util.getProblemArgumentsForMarker(arguments) ,
773 } catch (CoreException e) {
779 * Returns a new element info for this element.
781 protected Object createElementInfo() {
782 return new JavaProjectElementInfo();
787 * Returns a new search name environment for this project. This name environment first looks in the given working copies.
789 // public ISearchableNameEnvironment newSearchableNameEnvironment(ICompilationUnit[] workingCopies) throws JavaModelException {
790 // return new SearchableEnvironment(this, workingCopies);
794 * Returns a new search name environment for this project. This name environment first looks in the working copies
795 * of the given owner.
797 public ISearchableNameEnvironment newSearchableNameEnvironment(WorkingCopyOwner owner) throws JavaModelException {
798 return new SearchableEnvironment(this, owner);
801 * Reads and decode an XML classpath string
803 protected IClasspathEntry[] decodeClasspath(String xmlClasspath, boolean createMarker, boolean logProblems) {
805 ArrayList paths = new ArrayList();
806 IClasspathEntry defaultOutput = null;
808 if (xmlClasspath == null) return null;
809 StringReader reader = new StringReader(xmlClasspath);
813 DocumentBuilder parser =
814 DocumentBuilderFactory.newInstance().newDocumentBuilder();
815 cpElement = parser.parse(new InputSource(reader)).getDocumentElement();
816 } catch (SAXException e) {
817 throw new IOException(Util.bind("file.badFormat")); //$NON-NLS-1$
818 } catch (ParserConfigurationException e) {
819 throw new IOException(Util.bind("file.badFormat")); //$NON-NLS-1$
824 if (!cpElement.getNodeName().equalsIgnoreCase("classpath")) { //$NON-NLS-1$
825 throw new IOException(Util.bind("file.badFormat")); //$NON-NLS-1$
827 NodeList list = cpElement.getElementsByTagName("classpathentry"); //$NON-NLS-1$
828 int length = list.getLength();
830 for (int i = 0; i < length; ++i) {
831 Node node = list.item(i);
832 if (node.getNodeType() == Node.ELEMENT_NODE) {
833 IClasspathEntry entry = ClasspathEntry.elementDecode((Element)node, this);
835 if (entry.getContentKind() == ClasspathEntry.K_OUTPUT) {
836 defaultOutput = entry; // separate output
843 } catch (IOException e) {
845 if (createMarker && this.getProject().isAccessible()) {
846 this.createClasspathProblemMarker(new JavaModelStatus(
847 IJavaModelStatusConstants.INVALID_CLASSPATH_FILE_FORMAT,
848 Util.bind("classpath.xmlFormatError", this.getElementName(), e.getMessage()))); //$NON-NLS-1$
852 "Exception while retrieving "+ this.getPath() //$NON-NLS-1$
853 +"/.classpath, will mark classpath as invalid"); //$NON-NLS-1$
855 return INVALID_CLASSPATH;
856 } catch (Assert.AssertionFailedException e) {
857 // failed creating CP entries from file
858 if (createMarker && this.getProject().isAccessible()) {
859 this.createClasspathProblemMarker(new JavaModelStatus(
860 IJavaModelStatusConstants.INVALID_CLASSPATH_FILE_FORMAT,
861 Util.bind("classpath.illegalEntryInClasspathFile", this.getElementName(), e.getMessage()))); //$NON-NLS-1$
865 "Exception while retrieving "+ this.getPath() //$NON-NLS-1$
866 +"/.classpath, will mark classpath as invalid"); //$NON-NLS-1$
868 return INVALID_CLASSPATH;
870 int pathSize = paths.size();
871 if (pathSize > 0 || defaultOutput != null) {
872 IClasspathEntry[] entries = new IClasspathEntry[pathSize + (defaultOutput == null ? 0 : 1)];
873 paths.toArray(entries);
874 if (defaultOutput != null) entries[pathSize] = defaultOutput; // ensure output is last item
883 * Removes the Java nature from the project.
885 public void deconfigure() throws CoreException {
887 // deregister Java builder
888 removeFromBuildSpec(PHPeclipsePlugin.BUILDER_PARSER_ID);
892 * Returns a default class path.
893 * This is the root of the project
895 protected IClasspathEntry[] defaultClasspath() throws JavaModelException {
897 return new IClasspathEntry[] {
898 JavaCore.newSourceEntry(getProject().getFullPath())};
902 * Returns a default output location.
903 * This is the project bin folder
905 protected IPath defaultOutputLocation() throws JavaModelException {
906 return null; //getProject().getFullPath().append("bin"); //$NON-NLS-1$
910 * Returns the XML String encoding of the class path.
912 protected String encodeClasspath(IClasspathEntry[] classpath, IPath outputLocation, boolean indent) throws JavaModelException {
914 ByteArrayOutputStream s = new ByteArrayOutputStream();
915 OutputStreamWriter writer = new OutputStreamWriter(s, "UTF8"); //$NON-NLS-1$
916 XMLWriter xmlWriter = new XMLWriter(writer);
918 xmlWriter.startTag("classpath", indent); //$NON-NLS-1$
919 for (int i = 0; i < classpath.length; ++i) {
920 ((ClasspathEntry)classpath[i]).elementEncode(xmlWriter, this.project.getFullPath(), indent, true);
923 if (outputLocation != null) {
924 outputLocation = outputLocation.removeFirstSegments(1);
925 outputLocation = outputLocation.makeRelative();
926 HashMap parameters = new HashMap();
927 parameters.put("kind", ClasspathEntry.kindToString(ClasspathEntry.K_OUTPUT));//$NON-NLS-1$
928 parameters.put("path", String.valueOf(outputLocation));//$NON-NLS-1$
929 xmlWriter.printTag("classpathentry", parameters, indent, true, true);//$NON-NLS-1$
932 xmlWriter.endTag("classpath", indent);//$NON-NLS-1$
935 return s.toString("UTF8");//$NON-NLS-1$
936 } catch (IOException e) {
937 throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
941 * Returns the XML String encoding of the class path.
943 // protected String encodeClasspath(IClasspathEntry[] classpath, IPath outputLocation, boolean useLineSeparator) throws JavaModelException {
945 // Document document = new DocumentImpl();
946 // Element cpElement = document.createElement("classpath"); //$NON-NLS-1$
947 // document.appendChild(cpElement);
949 // for (int i = 0; i < classpath.length; ++i) {
950 // cpElement.appendChild(((ClasspathEntry)classpath[i]).elementEncode(document, getProject().getFullPath()));
953 // if (outputLocation != null) {
954 // outputLocation = outputLocation.removeFirstSegments(1);
955 // outputLocation = outputLocation.makeRelative();
956 // Element oElement = document.createElement("classpathentry"); //$NON-NLS-1$
957 // oElement.setAttribute("kind", ClasspathEntry.kindToString(ClasspathEntry.K_OUTPUT)); //$NON-NLS-1$
958 // oElement.setAttribute("path", outputLocation.toString()); //$NON-NLS-1$
959 // cpElement.appendChild(oElement);
962 // // produce a String output
964 // ByteArrayOutputStream s = new ByteArrayOutputStream();
965 // OutputFormat format = new OutputFormat();
966 // if (useLineSeparator) {
967 // format.setIndenting(true);
968 // format.setLineSeparator(System.getProperty("line.separator")); //$NON-NLS-1$
970 // format.setPreserveSpace(true);
972 // Serializer serializer =
973 // SerializerFactory.getSerializerFactory(Method.XML).makeSerializer(
974 // new OutputStreamWriter(s, "UTF8"), //$NON-NLS-1$
976 // serializer.asDOMSerializer().serialize(document);
977 // return s.toString("UTF8"); //$NON-NLS-1$
978 // } catch (IOException e) {
979 // throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
984 * Returns true if this handle represents the same Java project
985 * as the given handle. Two handles represent the same
986 * project if they are identical or if they represent a project with
987 * the same underlying resource and occurrence counts.
989 * @see JavaElement#equals
991 public boolean equals(Object o) {
996 if (!(o instanceof JavaProject))
999 JavaProject other = (JavaProject) o;
1000 return getProject().equals(other.getProject())
1001 && occurrenceCount == other.occurrenceCount;
1004 public boolean exists() {
1005 if (!hasJavaNature(project)) return false;
1006 return super.exists();
1012 public IJavaElement findElement(IPath path) throws JavaModelException {
1014 if (path == null || path.isAbsolute()) {
1015 throw new JavaModelException(
1016 new JavaModelStatus(IJavaModelStatusConstants.INVALID_PATH, path));
1020 String extension = path.getFileExtension();
1021 if (extension == null) {
1022 String packageName = path.toString().replace(IPath.SEPARATOR, '.');
1024 // IPackageFragment[] pkgFragments =
1025 // getNameLookup().findPackageFragments(packageName, false);
1026 // if (pkgFragments == null) {
1030 // // try to return one that is a child of this project
1031 // for (int i = 0, length = pkgFragments.length; i < length; i++) {
1033 // IPackageFragment pkgFragment = pkgFragments[i];
1034 // if (this.equals(pkgFragment.getParent().getParent())) {
1035 // return pkgFragment;
1038 // // default to the first one
1039 // return pkgFragments[0];
1042 extension.equalsIgnoreCase("java") //$NON-NLS-1$
1043 || extension.equalsIgnoreCase("class")) { //$NON-NLS-1$
1044 IPath packagePath = path.removeLastSegments(1);
1045 String packageName = packagePath.toString().replace(IPath.SEPARATOR, '.');
1046 String typeName = path.lastSegment();
1047 typeName = typeName.substring(0, typeName.length() - extension.length() - 1);
1048 String qualifiedName = null;
1049 if (packageName.length() > 0) {
1050 qualifiedName = packageName + "." + typeName; //$NON-NLS-1$
1052 qualifiedName = typeName;
1055 // getNameLookup().findType(
1058 // NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES);
1059 // if (type != null) {
1060 // return type.getParent();
1065 // unsupported extension
1068 // } catch (JavaModelException e) {
1069 // if (e.getStatus().getCode()
1070 // == IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST) {
1081 // public IPackageFragment findPackageFragment(IPath path)
1082 // throws JavaModelException {
1084 // return findPackageFragment0(JavaProject.canonicalizedPath(path));
1088 // * non path canonicalizing version
1090 // public IPackageFragment findPackageFragment0(IPath path)
1091 // throws JavaModelException {
1093 // return getNameLookup().findPackageFragment(path);
1099 public IPackageFragmentRoot findPackageFragmentRoot(IPath path)
1100 throws JavaModelException {
1102 return findPackageFragmentRoot0(JavaProject.canonicalizedPath(path));
1106 * no path canonicalization
1108 public IPackageFragmentRoot findPackageFragmentRoot0(IPath path)
1109 throws JavaModelException {
1111 IPackageFragmentRoot[] allRoots = this.getAllPackageFragmentRoots();
1112 if (!path.isAbsolute()) {
1113 throw new IllegalArgumentException(Util.bind("path.mustBeAbsolute")); //$NON-NLS-1$
1115 for (int i= 0; i < allRoots.length; i++) {
1116 IPackageFragmentRoot classpathRoot= allRoots[i];
1117 if (classpathRoot.getPath().equals(path)) {
1118 return classpathRoot;
1126 public IPackageFragmentRoot[] findPackageFragmentRoots(IClasspathEntry entry) {
1128 IClasspathEntry[] classpath = this.getRawClasspath();
1129 for (int i = 0, length = classpath.length; i < length; i++) {
1130 if (classpath[i].equals(entry)) { // entry may need to be resolved
1132 computePackageFragmentRoots(
1133 getResolvedClasspath(new IClasspathEntry[] {entry}, null, true, false, null/*no reverse map*/),
1134 false); // don't retrieve exported roots
1137 } catch (JavaModelException e) {
1139 return new IPackageFragmentRoot[] {};
1143 * @see IJavaProject#findType(String)
1145 // public IType findType(String fullyQualifiedName) throws JavaModelException {
1147 // this.getNameLookup().findType(
1148 // fullyQualifiedName,
1150 // NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES);
1151 // if (type == null) {
1152 // // try to find enclosing type
1153 // int lastDot = fullyQualifiedName.lastIndexOf('.');
1154 // if (lastDot == -1) return null;
1155 // type = this.findType(fullyQualifiedName.substring(0, lastDot));
1156 // if (type != null) {
1157 // type = type.getType(fullyQualifiedName.substring(lastDot+1));
1158 // if (!type.exists()) {
1167 * @see IJavaProject#findType(String, String)
1169 // public IType findType(String packageName, String typeQualifiedName) throws JavaModelException {
1171 // this.getNameLookup().findType(
1172 // typeQualifiedName,
1175 // NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES);
1179 * Remove all markers denoting classpath problems
1181 protected void flushClasspathProblemMarkers(boolean flushCycleMarkers, boolean flushClasspathFormatMarkers) {
1183 IProject project = getProject();
1184 if (project.exists()) {
1185 IMarker[] markers = project.findMarkers(IJavaModelMarker.BUILDPATH_PROBLEM_MARKER, false, IResource.DEPTH_ZERO);
1186 for (int i = 0, length = markers.length; i < length; i++) {
1187 IMarker marker = markers[i];
1188 if (flushCycleMarkers && flushClasspathFormatMarkers) {
1191 String cycleAttr = (String)marker.getAttribute(IJavaModelMarker.CYCLE_DETECTED);
1192 String classpathFileFormatAttr = (String)marker.getAttribute(IJavaModelMarker.CLASSPATH_FILE_FORMAT);
1193 if ((flushCycleMarkers == (cycleAttr != null && cycleAttr.equals("true"))) //$NON-NLS-1$
1194 && (flushClasspathFormatMarkers == (classpathFileFormatAttr != null && classpathFileFormatAttr.equals("true")))){ //$NON-NLS-1$
1200 } catch (CoreException e) {
1207 // protected boolean generateInfos(
1208 // OpenableElementInfo info,
1209 // IProgressMonitor pm,
1211 // IResource underlyingResource) throws JavaModelException {
1213 // boolean validInfo = false;
1215 // if (getProject().isOpen()) {
1216 // // put the info now, because computing the roots requires it
1217 // JavaModelManager.getJavaModelManager().putInfo(this, info);
1219 // // compute the pkg fragment roots
1220 // updatePackageFragmentRoots();
1222 // // remember the timestamps of external libraries the first time they are looked up
1223 // IClasspathEntry[] resolvedClasspath = getResolvedClasspath(true/*ignore unresolved variable*/);
1224 // for (int i = 0, length = resolvedClasspath.length; i < length; i++) {
1225 // IClasspathEntry entry = resolvedClasspath[i];
1226 // if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
1227 // IPath path = entry.getPath();
1228 // Object target = JavaModel.getTarget(ResourcesPlugin.getWorkspace().getRoot(), path, true);
1229 // if (target instanceof java.io.File) {
1230 // Map externalTimeStamps = JavaModelManager.getJavaModelManager().deltaProcessor.externalTimeStamps;
1231 // if (externalTimeStamps.get(path) == null) {
1232 // long timestamp = DeltaProcessor.getTimeStamp((java.io.File)target);
1233 // externalTimeStamps.put(path, new Long(timestamp));
1239 // // only valid if reaches here
1240 // validInfo = true;
1244 // JavaModelManager.getJavaModelManager().removeInfo(this);
1246 // return validInfo;
1252 public IPackageFragmentRoot[] getAllPackageFragmentRoots()
1253 throws JavaModelException {
1255 return computePackageFragmentRoots(getResolvedClasspath(true), true);
1259 * Returns the classpath entry that refers to the given path
1260 * or <code>null</code> if there is no reference to the path.
1262 public IClasspathEntry getClasspathEntryFor(IPath path)
1263 throws JavaModelException {
1265 IClasspathEntry[] entries = getExpandedClasspath(true);
1266 for (int i = 0; i < entries.length; i++) {
1267 if (entries[i].getPath().equals(path)) {
1275 * Returns the cycle marker associated with this project or null if none.
1277 public IMarker getCycleMarker(){
1279 IProject project = getProject();
1280 if (project.exists()) {
1281 IMarker[] markers = 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 String cycleAttr = (String)marker.getAttribute(IJavaModelMarker.CYCLE_DETECTED);
1285 if (cycleAttr != null && cycleAttr.equals("true")){ //$NON-NLS-1$
1290 } catch (CoreException e) {
1297 public int getElementType() {
1298 return JAVA_PROJECT;
1301 * This is a helper method returning the expanded classpath for the project, as a list of classpath entries,
1302 * where all classpath variable entries have been resolved and substituted with their final target entries.
1303 * All project exports have been appended to project entries.
1305 public IClasspathEntry[] getExpandedClasspath(boolean ignoreUnresolvedVariable) throws JavaModelException {
1307 return getExpandedClasspath(ignoreUnresolvedVariable, false);
1311 * Internal variant which can create marker on project for invalid entries,
1312 * it will also perform classpath expansion in presence of project prerequisites
1313 * exporting their entries.
1315 public IClasspathEntry[] getExpandedClasspath(
1316 boolean ignoreUnresolvedVariable,
1317 boolean generateMarkerOnError) throws JavaModelException {
1319 ObjectVector accumulatedEntries = new ObjectVector();
1320 computeExpandedClasspath(this, ignoreUnresolvedVariable, generateMarkerOnError, new HashSet(5), accumulatedEntries);
1322 IClasspathEntry[] expandedPath = new IClasspathEntry[accumulatedEntries.size()];
1323 accumulatedEntries.copyInto(expandedPath);
1325 return expandedPath;
1329 * Returns the <code>char</code> that marks the start of this handles
1330 * contribution to a memento.
1332 protected char getHandleMementoDelimiter() {
1334 return JEM_JAVAPROJECT;
1338 * Find the specific Java command amongst the build spec of a given description
1340 private ICommand getJavaCommand(IProjectDescription description)
1341 throws CoreException {
1343 ICommand[] commands = description.getBuildSpec();
1344 for (int i = 0; i < commands.length; ++i) {
1345 if (commands[i].getBuilderName().equals(PHPeclipsePlugin.BUILDER_PARSER_ID)) {
1353 * Convenience method that returns the specific type of info for a Java project.
1355 protected JavaProjectElementInfo getJavaProjectElementInfo()
1356 throws JavaModelException {
1358 return (JavaProjectElementInfo) getElementInfo();
1364 public NameLookup getNameLookup() throws JavaModelException {
1366 JavaProjectElementInfo info = getJavaProjectElementInfo();
1367 // lock on the project info to avoid race condition
1369 NameLookup nameLookup;
1370 if ((nameLookup = info.getNameLookup()) == null){
1371 info.setNameLookup(nameLookup = new NameLookup(this));
1377 * Returns a new name lookup. This name lookup first looks in the given working copies.
1379 public NameLookup newNameLookup(ICompilationUnit[] workingCopies) throws JavaModelException {
1381 JavaProjectElementInfo info = getJavaProjectElementInfo();
1382 // lock on the project info to avoid race condition while computing the pkg fragment roots and package fragment caches
1383 // synchronized(info){
1384 // return new NameLookup(info.getAllPackageFragmentRoots(this), info.getAllPackageFragments(this), workingCopies);
1390 * Returns a new name lookup. This name lookup first looks in the working copies of the given owner.
1392 public NameLookup newNameLookup(WorkingCopyOwner owner) throws JavaModelException {
1394 JavaModelManager manager = JavaModelManager.getJavaModelManager();
1395 ICompilationUnit[] workingCopies = owner == null ? null : manager.getWorkingCopies(owner, true/*add primary WCs*/);
1396 return newNameLookup(workingCopies);
1400 // * Returns an array of non-java resources contained in the receiver.
1402 // public Object[] getNonJavaResources() throws JavaModelException {
1404 // return ((JavaProjectElementInfo) getElementInfo()).getNonJavaResources(this);
1408 * @see org.eclipse.jdt.core.IJavaProject#getOption(String, boolean)
1410 public String getOption(String optionName, boolean inheritJavaCoreOptions) {
1412 if (JavaModelManager.OptionNames.contains(optionName)){
1414 Preferences preferences = getPreferences();
1415 if (preferences == null || preferences.isDefault(optionName)) {
1416 return inheritJavaCoreOptions ? JavaCore.getOption(optionName) : null;
1418 return preferences.getString(optionName).trim();
1424 * @see org.eclipse.jdt.core.IJavaProject#getOptions(boolean)
1426 public Map getOptions(boolean inheritJavaCoreOptions) {
1428 // initialize to the defaults from JavaCore options pool
1429 Map options = inheritJavaCoreOptions ? JavaCore.getOptions() : new Hashtable(5);
1431 Preferences preferences = getPreferences();
1432 if (preferences == null) return options; // cannot do better (non-Java project)
1433 HashSet optionNames = JavaModelManager.OptionNames;
1435 // get preferences set to their default
1436 if (inheritJavaCoreOptions){
1437 String[] defaultPropertyNames = preferences.defaultPropertyNames();
1438 for (int i = 0; i < defaultPropertyNames.length; i++){
1439 String propertyName = defaultPropertyNames[i];
1440 if (optionNames.contains(propertyName)){
1441 options.put(propertyName, preferences.getDefaultString(propertyName).trim());
1445 // get custom preferences not set to their default
1446 String[] propertyNames = preferences.propertyNames();
1447 for (int i = 0; i < propertyNames.length; i++){
1448 String propertyName = propertyNames[i];
1449 if (optionNames.contains(propertyName)){
1450 options.put(propertyName, preferences.getString(propertyName).trim());
1459 public IPath getOutputLocation() throws JavaModelException {
1461 JavaModelManager.PerProjectInfo perProjectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(project);
1462 IPath outputLocation = perProjectInfo.outputLocation;
1463 if (outputLocation != null) return outputLocation;
1465 // force to read classpath - will position output location as well
1466 this.getRawClasspath();
1467 outputLocation = perProjectInfo.outputLocation;
1468 if (outputLocation == null) {
1469 return defaultOutputLocation();
1471 return outputLocation;
1475 * @return A handle to the package fragment root identified by the given path.
1476 * This method is handle-only and the element may or may not exist. Returns
1477 * <code>null</code> if unable to generate a handle from the path (for example,
1478 * an absolute path that has less than 1 segment. The path may be relative or
1481 public IPackageFragmentRoot getPackageFragmentRoot(IPath path) {
1482 if (!path.isAbsolute()) {
1483 path = getPath().append(path);
1485 int segmentCount = path.segmentCount();
1486 switch (segmentCount) {
1491 return getPackageFragmentRoot(getProject());
1493 // a path ending with .jar/.zip is still ambiguous and could still resolve to a source/lib folder
1494 // thus will try to guess based on existing resource
1495 // if (ProjectPrefUtil.isArchiveFileName(path.lastSegment())) {
1496 // IResource resource = getProject().getWorkspace().getRoot().findMember(path);
1497 // if (resource != null && resource.getType() == IResource.FOLDER){
1498 // return getPackageFragmentRoot(resource);
1500 // return getPackageFragmentRoot0(path);
1502 return getPackageFragmentRoot(getProject().getWorkspace().getRoot().getFolder(path));
1508 * The path is known to match a source/library folder entry.
1510 public IPackageFragmentRoot getFolderPackageFragmentRoot(IPath path) {
1511 if (path.segmentCount() == 1) { // default project root
1512 return getPackageFragmentRoot(getProject());
1514 return getPackageFragmentRoot(getProject().getWorkspace().getRoot().getFolder(path));
1520 public IPackageFragmentRoot getPackageFragmentRoot(IResource resource) {
1522 switch (resource.getType()) {
1523 case IResource.FILE:
1524 // if (ProjectPrefUtil.isArchiveFileName(resource.getName())) {
1525 // return new JarPackageFragmentRoot(resource, this);
1529 case IResource.FOLDER:
1530 return new PackageFragmentRoot(resource, this, resource.getName());
1531 case IResource.PROJECT:
1532 return new PackageFragmentRoot(resource, this, ""); //$NON-NLS-1$
1541 // public IPackageFragmentRoot getPackageFragmentRoot(String jarPath) {
1543 // return getPackageFragmentRoot0(JavaProject.canonicalizedPath(new Path(jarPath)));
1547 // * no path canonicalization
1549 // public IPackageFragmentRoot getPackageFragmentRoot0(IPath jarPath) {
1551 // return new JarPackageFragmentRoot(jarPath, this);
1557 public IPackageFragmentRoot[] getPackageFragmentRoots()
1558 throws JavaModelException {
1562 IPackageFragmentRoot[] roots;
1565 children = getChildren(),
1567 roots = new IPackageFragmentRoot[length = children.length],
1578 public IPackageFragmentRoot[] getPackageFragmentRoots(IClasspathEntry entry) {
1579 return findPackageFragmentRoots(entry);
1583 * Returns the package fragment root prefixed by the given path, or
1584 * an empty collection if there are no such elements in the model.
1586 protected IPackageFragmentRoot[] getPackageFragmentRoots(IPath path)
1588 throws JavaModelException {
1589 IPackageFragmentRoot[] roots = getAllPackageFragmentRoots();
1590 ArrayList matches = new ArrayList();
1592 for (int i = 0; i < roots.length; ++i) {
1593 if (path.isPrefixOf(roots[i].getPath())) {
1594 matches.add(roots[i]);
1597 IPackageFragmentRoot[] copy = new IPackageFragmentRoot[matches.size()];
1598 matches.toArray(copy);
1605 public IPackageFragment[] getPackageFragments() throws JavaModelException {
1607 IPackageFragmentRoot[] roots = getPackageFragmentRoots();
1608 return getPackageFragmentsInRoots(roots);
1612 * Returns all the package fragments found in the specified
1613 * package fragment roots.
1615 public IPackageFragment[] getPackageFragmentsInRoots(IPackageFragmentRoot[] roots) {
1617 ArrayList frags = new ArrayList();
1618 for (int i = 0; i < roots.length; i++) {
1619 IPackageFragmentRoot root = roots[i];
1621 IJavaElement[] rootFragments = root.getChildren();
1622 for (int j = 0; j < rootFragments.length; j++) {
1623 frags.add(rootFragments[j]);
1625 } catch (JavaModelException e) {
1629 IPackageFragment[] fragments = new IPackageFragment[frags.size()];
1630 frags.toArray(fragments);
1637 public IPath getPath() {
1638 return this.getProject().getFullPath();
1644 public IProject getProject() {
1649 * Sets the underlying kernel project of this Java project,
1650 * and fills in its parent and name.
1651 * Called by IProject.getNature().
1653 * @see IProjectNature#setProject(IProject)
1655 public void setProject(IProject project) {
1657 this.project = project;
1658 this.parent = JavaModelManager.getJavaModelManager().getJavaModel();
1659 this.name = project.getName();
1661 protected IProject getProject(String name) {
1662 return PHPeclipsePlugin.getWorkspace().getRoot().getProject(name);
1665 public List getReferencedProjects() {
1666 List referencedProjects = new ArrayList();
1668 Iterator iterator = getLoadPathEntries().iterator();
1669 while (iterator.hasNext()) {
1670 LoadPathEntry pathEntry = (LoadPathEntry) iterator.next();
1671 if (pathEntry.getType() == LoadPathEntry.TYPE_PROJECT)
1672 referencedProjects.add(pathEntry.getProject());
1675 return referencedProjects;
1679 * Returns the project custom preference pool.
1680 * Project preferences may include custom encoding.
1682 public Preferences getPreferences(){
1683 IProject project = getProject();
1684 if (!JavaProject.hasJavaNature(project)) return null;
1685 JavaModelManager.PerProjectInfo perProjectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfo(project, true);
1686 Preferences preferences = perProjectInfo.preferences;
1687 if (preferences != null) return preferences;
1688 preferences = loadPreferences();
1689 if (preferences == null) preferences = new Preferences();
1690 perProjectInfo.preferences = preferences;
1697 public IClasspathEntry[] getRawClasspath() throws JavaModelException {
1699 JavaModelManager.PerProjectInfo perProjectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(project);
1700 IClasspathEntry[] classpath = perProjectInfo.classpath;
1701 if (classpath != null) return classpath;
1702 classpath = this.readClasspathFile(false/*don't create markers*/, true/*log problems*/);
1704 // extract out the output location
1705 IPath outputLocation = null;
1706 if (classpath != null && classpath.length > 0) {
1707 IClasspathEntry entry = classpath[classpath.length - 1];
1708 if (entry.getContentKind() == ClasspathEntry.K_OUTPUT) {
1709 outputLocation = entry.getPath();
1710 IClasspathEntry[] copy = new IClasspathEntry[classpath.length - 1];
1711 System.arraycopy(classpath, 0, copy, 0, copy.length);
1715 if (classpath == null) {
1716 return defaultClasspath();
1718 /* Disable validate: classpath can contain CP variables and container that need to be resolved
1719 if (classpath != INVALID_CLASSPATH
1720 && !JavaConventions.validateClasspath(this, classpath, outputLocation).isOK()) {
1721 classpath = INVALID_CLASSPATH;
1724 perProjectInfo.classpath = classpath;
1725 perProjectInfo.outputLocation = outputLocation;
1730 * @see IJavaProject#getRequiredProjectNames
1732 public String[] getRequiredProjectNames() throws JavaModelException {
1734 return this.projectPrerequisites(getResolvedClasspath(true));
1740 public IClasspathEntry[] getResolvedClasspath(boolean ignoreUnresolvedEntry)
1741 throws JavaModelException {
1744 this.getResolvedClasspath(
1745 ignoreUnresolvedEntry,
1746 false); // generateMarkerOnError
1750 * Internal variant which can create marker on project for invalid entries
1751 * and caches the resolved classpath on perProjectInfo
1753 public IClasspathEntry[] getResolvedClasspath(
1754 boolean ignoreUnresolvedEntry,
1755 boolean generateMarkerOnError)
1756 throws JavaModelException {
1758 JavaModelManager manager = JavaModelManager.getJavaModelManager();
1759 JavaModelManager.PerProjectInfo perProjectInfo = manager.getPerProjectInfoCheckExistence(project);
1761 // reuse cache if not needing to refresh markers or checking bound variables
1762 if (ignoreUnresolvedEntry && !generateMarkerOnError && perProjectInfo != null){
1763 // resolved path is cached on its info
1764 IClasspathEntry[] infoPath = perProjectInfo.lastResolvedClasspath;
1765 if (infoPath != null) return infoPath;
1767 Map reverseMap = perProjectInfo == null ? null : new HashMap(5);
1768 IClasspathEntry[] resolvedPath = getResolvedClasspath(
1770 generateMarkerOnError ? getOutputLocation() : null,
1771 ignoreUnresolvedEntry,
1772 generateMarkerOnError,
1775 if (perProjectInfo != null){
1776 if (perProjectInfo.classpath == null // .classpath file could not be read
1777 && generateMarkerOnError
1778 && JavaProject.hasJavaNature(project)) {
1779 this.createClasspathProblemMarker(new JavaModelStatus(
1780 IJavaModelStatusConstants.INVALID_CLASSPATH_FILE_FORMAT,
1781 Util.bind("classpath.cannotReadClasspathFile", this.getElementName()))); //$NON-NLS-1$
1784 perProjectInfo.lastResolvedClasspath = resolvedPath;
1785 perProjectInfo.resolvedPathToRawEntries = reverseMap;
1787 return resolvedPath;
1791 * Internal variant which can process any arbitrary classpath
1793 public IClasspathEntry[] getResolvedClasspath(
1794 IClasspathEntry[] classpathEntries,
1795 IPath projectOutputLocation, // only set if needing full classpath validation (and markers)
1796 boolean ignoreUnresolvedEntry, // if unresolved entries are met, should it trigger initializations
1797 boolean generateMarkerOnError,
1798 Map reverseMap) // can be null if not interested in reverse mapping
1799 throws JavaModelException {
1801 IJavaModelStatus status;
1802 if (generateMarkerOnError){
1803 flushClasspathProblemMarkers(false, false);
1806 int length = classpathEntries.length;
1807 ArrayList resolvedEntries = new ArrayList();
1809 for (int i = 0; i < length; i++) {
1811 IClasspathEntry rawEntry = classpathEntries[i];
1815 /* validation if needed */
1816 // if (generateMarkerOnError || !ignoreUnresolvedEntry) {
1817 // status = JavaConventions.validateClasspathEntry(this, rawEntry, false);
1818 // if (generateMarkerOnError && !status.isOK()) createClasspathProblemMarker(status);
1821 switch (rawEntry.getEntryKind()){
1823 case IClasspathEntry.CPE_VARIABLE :
1825 IClasspathEntry resolvedEntry = JavaCore.getResolvedClasspathEntry(rawEntry);
1826 if (resolvedEntry == null) {
1827 if (!ignoreUnresolvedEntry) throw new JavaModelException(status);
1829 if (reverseMap != null && reverseMap.get(resolvedPath = resolvedEntry.getPath()) == null) reverseMap.put(resolvedPath , rawEntry);
1830 resolvedEntries.add(resolvedEntry);
1834 // case IClasspathEntry.CPE_CONTAINER :
1836 // IClasspathContainer container = PHPCore.getClasspathContainer(rawEntry.getPath(), this);
1837 // if (container == null){
1838 // if (!ignoreUnresolvedEntry) throw new JavaModelException(status);
1842 // IClasspathEntry[] containerEntries = container.getClasspathEntries();
1843 // if (containerEntries == null) break;
1845 // // container was bound
1846 // for (int j = 0, containerLength = containerEntries.length; j < containerLength; j++){
1847 // IClasspathEntry cEntry = containerEntries[j];
1849 // if (generateMarkerOnError) {
1850 // IJavaModelStatus containerStatus = JavaConventions.validateClasspathEntry(this, cEntry, false);
1851 // if (!containerStatus.isOK()) createClasspathProblemMarker(containerStatus);
1853 // // if container is exported, then its nested entries must in turn be exported (21749)
1854 // if (rawEntry.isExported()){
1855 // cEntry = new ClasspathEntry(cEntry.getContentKind(),
1856 // cEntry.getEntryKind(), cEntry.getPath(),
1857 // cEntry.getExclusionPatterns(), cEntry.getSourceAttachmentPath(),
1858 // cEntry.getSourceAttachmentRootPath(), cEntry.getOutputLocation(),
1859 // true); // duplicate container entry for tagging it as exported
1861 // if (reverseMap != null && reverseMap.get(resolvedPath = cEntry.getPath()) == null) reverseMap.put(resolvedPath, rawEntry);
1862 // resolvedEntries.add(cEntry);
1868 if (reverseMap != null && reverseMap.get(resolvedPath = rawEntry.getPath()) == null) reverseMap.put(resolvedPath, rawEntry);
1869 resolvedEntries.add(rawEntry);
1874 IClasspathEntry[] resolvedPath = new IClasspathEntry[resolvedEntries.size()];
1875 resolvedEntries.toArray(resolvedPath);
1877 // if (generateMarkerOnError && projectOutputLocation != null) {
1878 // status = JavaConventions.validateClasspath(this, resolvedPath, projectOutputLocation);
1879 // if (!status.isOK()) createClasspathProblemMarker(status);
1881 return resolvedPath;
1887 public IResource getResource() {
1888 return this.getProject();
1894 public ISearchableNameEnvironment getSearchableNameEnvironment()
1895 throws JavaModelException {
1897 JavaProjectElementInfo info = getJavaProjectElementInfo();
1898 if (info.getSearchableEnvironment() == null) {
1899 info.setSearchableEnvironment(new SearchableEnvironment(this));
1901 return info.getSearchableEnvironment();
1905 * Retrieve a shared property on a project. If the property is not defined, answers null.
1906 * Note that it is orthogonal to IResource persistent properties, and client code has to decide
1907 * which form of storage to use appropriately. Shared properties produce real resource files which
1908 * can be shared through a VCM onto a server. Persistent properties are not shareable.
1910 * @see JavaProject#setSharedProperty(String, String)
1912 public String getSharedProperty(String key) throws CoreException {
1914 String property = null;
1915 IFile rscFile = getProject().getFile(key);
1916 if (rscFile.exists()) {
1917 property = new String(Util.getResourceContentsAsByteArray(rscFile));
1925 // public SourceMapper getSourceMapper() {
1933 public IResource getUnderlyingResource() throws JavaModelException {
1934 if (!exists()) throw newNotPresentException();
1935 return getProject();
1941 public boolean hasBuildState() {
1943 return JavaModelManager.getJavaModelManager().getLastBuiltState(this.getProject(), null) != null;
1949 public boolean hasClasspathCycle(IClasspathEntry[] preferredClasspath) {
1950 HashSet cycleParticipants = new HashSet();
1951 updateCycleParticipants(preferredClasspath, new ArrayList(2), cycleParticipants, ResourcesPlugin.getWorkspace().getRoot(), new HashSet(2));
1952 return !cycleParticipants.isEmpty();
1955 public boolean hasCycleMarker(){
1956 return this.getCycleMarker() != null;
1959 public int hashCode() {
1960 return project.hashCode();
1964 * Returns true if the given project is accessible and it has
1965 * a java nature, otherwise false.
1967 public static boolean hasJavaNature(IProject project) {
1969 return project.hasNature(PHPeclipsePlugin.PHP_NATURE_ID);
1970 } catch (CoreException e) {
1971 // project does not exist or is not open
1977 * Answers true if the project potentially contains any source. A project which has no source is immutable.
1979 public boolean hasSource() {
1981 // look if any source folder on the classpath
1982 // no need for resolved path given source folder cannot be abstracted
1983 IClasspathEntry[] entries;
1985 entries = this.getRawClasspath();
1986 } catch (JavaModelException e) {
1987 return true; // unsure
1989 for (int i = 0, max = entries.length; i < max; i++) {
1990 if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
1998 * Compare current classpath with given one to see if any different.
1999 * Note that the argument classpath contains its binary output.
2001 public boolean isClasspathEqualsTo(IClasspathEntry[] newClasspath, IPath newOutputLocation, IClasspathEntry[] otherClasspathWithOutput)
2002 throws JavaModelException {
2004 if (otherClasspathWithOutput != null && otherClasspathWithOutput.length > 0) {
2006 int length = otherClasspathWithOutput.length;
2007 if (length == newClasspath.length + 1) {
2008 // output is amongst file entries (last one)
2010 // compare classpath entries
2011 for (int i = 0; i < length - 1; i++) {
2012 if (!otherClasspathWithOutput[i].equals(newClasspath[i]))
2015 // compare binary outputs
2016 IClasspathEntry output = otherClasspathWithOutput[length - 1];
2017 if (output.getContentKind() == ClasspathEntry.K_OUTPUT
2018 && output.getPath().equals(newOutputLocation))
2030 public boolean isOnClasspath(IJavaElement element) {
2031 IPath path = element.getPath();
2032 switch (element.getElementType()) {
2033 case IJavaElement.PACKAGE_FRAGMENT_ROOT:
2034 if (!((IPackageFragmentRoot)element).isArchive()) {
2035 // ensure that folders are only excluded if all of their children are excluded
2036 path = path.append("*"); //$NON-NLS-1$
2039 case IJavaElement.PACKAGE_FRAGMENT:
2040 if (!((IPackageFragmentRoot)element.getParent()).isArchive()) {
2041 // ensure that folders are only excluded if all of their children are excluded
2042 path = path.append("*"); //$NON-NLS-1$
2046 return this.isOnClasspath(path);
2048 private boolean isOnClasspath(IPath path) {
2049 IClasspathEntry[] classpath;
2051 classpath = this.getResolvedClasspath(true/*ignore unresolved variable*/);
2052 } catch(JavaModelException e){
2053 return false; // not a Java project
2055 for (int i = 0; i < classpath.length; i++) {
2056 IClasspathEntry entry = classpath[i];
2057 if (entry.getPath().isPrefixOf(path)
2058 && !Util.isExcluded(path, ((ClasspathEntry)entry).fullExclusionPatternChars())) {
2067 public boolean isOnClasspath(IResource resource) {
2068 IPath path = resource.getFullPath();
2070 // ensure that folders are only excluded if all of their children are excluded
2071 if (resource.getType() == IResource.FOLDER) {
2072 path = path.append("*"); //$NON-NLS-1$
2075 return this.isOnClasspath(path);
2078 private IPath getPluginWorkingLocation() {
2079 return this.project.getWorkingLocation(JavaCore.PLUGIN_ID);
2082 * load preferences from a shareable format (VCM-wise)
2084 public Preferences loadPreferences() {
2086 Preferences preferences = new Preferences();
2088 // File prefFile = getProject().getLocation().append(PREF_FILENAME).toFile();
2089 IPath projectMetaLocation = getPluginWorkingLocation();
2090 if (projectMetaLocation != null) {
2091 File prefFile = projectMetaLocation.append(PREF_FILENAME).toFile();
2092 if (prefFile.exists()) { // load preferences from file
2093 InputStream in = null;
2095 in = new BufferedInputStream(new FileInputStream(prefFile));
2096 preferences.load(in);
2098 } catch (IOException e) { // problems loading preference store - quietly ignore
2103 } catch (IOException e) { // ignore problems with close
2113 * @see IJavaProject#newEvaluationContext
2115 // public IEvaluationContext newEvaluationContext() {
2117 // return new EvaluationContextWrapper(new EvaluationContext(), this);
2123 // public ITypeHierarchy newTypeHierarchy(
2125 // IProgressMonitor monitor)
2126 // throws JavaModelException {
2128 // if (region == null) {
2129 // throw new IllegalArgumentException(ProjectPrefUtil.bind("hierarchy.nullRegion"));//$NON-NLS-1$
2131 // CreateTypeHierarchyOperation op =
2132 // new CreateTypeHierarchyOperation(null, region, this, true);
2133 // runOperation(op, monitor);
2134 // return op.getResult();
2140 // public ITypeHierarchy newTypeHierarchy(
2143 // IProgressMonitor monitor)
2144 // throws JavaModelException {
2146 // if (type == null) {
2147 // throw new IllegalArgumentException(ProjectPrefUtil.bind("hierarchy.nullFocusType"));//$NON-NLS-1$
2149 // if (region == null) {
2150 // throw new IllegalArgumentException(ProjectPrefUtil.bind("hierarchy.nullRegion"));//$NON-NLS-1$
2152 // CreateTypeHierarchyOperation op =
2153 // new CreateTypeHierarchyOperation(type, region, this, true);
2154 // runOperation(op, monitor);
2155 // return op.getResult();
2159 // * Open project if resource isn't closed
2161 // protected void openWhenClosed(IProgressMonitor pm) throws JavaModelException {
2163 // if (!this.fProject.isOpen()) {
2164 // throw newNotPresentException();
2166 // super.openWhenClosed(pm);
2170 public String[] projectPrerequisites(IClasspathEntry[] entries)
2171 throws JavaModelException {
2173 ArrayList prerequisites = new ArrayList();
2175 entries = getResolvedClasspath(entries, null, true, false, null/*no reverse map*/);
2176 for (int i = 0, length = entries.length; i < length; i++) {
2177 IClasspathEntry entry = entries[i];
2178 if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
2179 prerequisites.add(entry.getPath().lastSegment());
2182 int size = prerequisites.size();
2184 return NO_PREREQUISITES;
2186 String[] result = new String[size];
2187 prerequisites.toArray(result);
2194 * Reads the .classpath file from disk and returns the list of entries it contains (including output location entry)
2195 * Returns null if .classfile is not present.
2196 * Returns INVALID_CLASSPATH if it has a format problem.
2198 protected IClasspathEntry[] readClasspathFile(boolean createMarker, boolean logProblems) {
2201 String xmlClasspath = getSharedProperty(CLASSPATH_FILENAME);
2202 if (xmlClasspath == null) return null;
2203 return decodeClasspath(xmlClasspath, createMarker, logProblems);
2204 } catch(CoreException e) {
2205 // file does not exist (or not accessible)
2206 if (createMarker && this.getProject().isAccessible()) {
2207 this.createClasspathProblemMarker(new JavaModelStatus(
2208 IJavaModelStatusConstants.INVALID_CLASSPATH_FILE_FORMAT,
2209 Util.bind("classpath.cannotReadClasspathFile", this.getElementName()))); //$NON-NLS-1$
2213 "Exception while retrieving "+ this.getPath() //$NON-NLS-1$
2214 +"/.classpath, will revert to default classpath"); //$NON-NLS-1$
2221 * Removes the given builder from the build spec for the given project.
2223 protected void removeFromBuildSpec(String builderID) throws CoreException {
2225 IProjectDescription description = getProject().getDescription();
2226 ICommand[] commands = description.getBuildSpec();
2227 for (int i = 0; i < commands.length; ++i) {
2228 if (commands[i].getBuilderName().equals(builderID)) {
2229 ICommand[] newCommands = new ICommand[commands.length - 1];
2230 System.arraycopy(commands, 0, newCommands, 0, i);
2231 System.arraycopy(commands, i + 1, newCommands, i, commands.length - i - 1);
2232 description.setBuildSpec(newCommands);
2233 getProject().setDescription(description, null);
2241 * @see JavaElement#rootedAt(IJavaProject)
2243 public IJavaElement rootedAt(IJavaProject project) {
2249 * Answers an ID which is used to distinguish project/entries during package
2250 * fragment root computations
2252 public String rootID(){
2253 return "[PRJ]"+this.getProject().getFullPath(); //$NON-NLS-1$
2257 * Saves the classpath in a shareable format (VCM-wise) only when necessary, that is, if it is semantically different
2258 * from the existing one in file. Will never write an identical one.
2260 * @return Return whether the .classpath file was modified.
2262 public boolean saveClasspath(IClasspathEntry[] newClasspath, IPath newOutputLocation) throws JavaModelException {
2264 if (!getProject().exists()) return false;
2266 IClasspathEntry[] fileEntries = readClasspathFile(false /*don't create markers*/, false/*don't log problems*/);
2267 if (fileEntries != null && isClasspathEqualsTo(newClasspath, newOutputLocation, fileEntries)) {
2268 // no need to save it, it is the same
2272 // actual file saving
2274 setSharedProperty(CLASSPATH_FILENAME, encodeClasspath(newClasspath, newOutputLocation, true));
2276 } catch (CoreException e) {
2277 throw new JavaModelException(e);
2283 * Save project custom preferences to shareable file (.jprefs)
2285 private void savePreferences(Preferences preferences) {
2287 if (!JavaProject.hasJavaNature(this.project)) return; // ignore
2289 if (preferences == null || (!preferences.needsSaving() && preferences.propertyNames().length != 0)) {
2294 // preferences need to be saved
2295 // the preferences file is located in the plug-in's state area
2296 // at a well-known name (.jprefs)
2297 // File prefFile = this.project.getLocation().append(PREF_FILENAME).toFile();
2298 File prefFile = getPluginWorkingLocation().append(PREF_FILENAME).toFile();
2299 if (preferences.propertyNames().length == 0) {
2300 // there are no preference settings
2301 // rather than write an empty file, just delete any existing file
2302 if (prefFile.exists()) {
2303 prefFile.delete(); // don't worry if delete unsuccessful
2308 // write file, overwriting an existing one
2309 OutputStream out = null;
2311 // do it as carefully as we know how so that we don't lose/mangle
2312 // the setting in times of stress
2313 out = new BufferedOutputStream(new FileOutputStream(prefFile));
2314 preferences.store(out, null);
2315 } catch (IOException e) { // problems saving preference store - quietly ignore
2320 } catch (IOException e) { // ignore problems with close
2326 * Update the Java command in the build spec (replace existing one if present,
2327 * add one first if none).
2329 private void setJavaCommand(
2330 IProjectDescription description,
2331 ICommand newCommand)
2332 throws CoreException {
2334 ICommand[] oldCommands = description.getBuildSpec();
2335 ICommand oldJavaCommand = getJavaCommand(description);
2336 ICommand[] newCommands;
2338 if (oldJavaCommand == null) {
2339 // Add a Java build spec before other builders (1FWJK7I)
2340 newCommands = new ICommand[oldCommands.length + 1];
2341 System.arraycopy(oldCommands, 0, newCommands, 1, oldCommands.length);
2342 newCommands[0] = newCommand;
2344 for (int i = 0, max = oldCommands.length; i < max; i++) {
2345 if (oldCommands[i] == oldJavaCommand) {
2346 oldCommands[i] = newCommand;
2350 newCommands = oldCommands;
2353 // Commit the spec change into the project
2354 description.setBuildSpec(newCommands);
2355 getProject().setDescription(description, null);
2359 * @see org.eclipse.jdt.core.IJavaProject#setOptions(Map)
2361 public void setOptions(Map newOptions) {
2363 Preferences preferences;
2364 setPreferences(preferences = new Preferences()); // always reset (26255)
2365 if (newOptions != null){
2366 Iterator keys = newOptions.keySet().iterator();
2367 while (keys.hasNext()){
2368 String key = (String)keys.next();
2369 if (!JavaModelManager.OptionNames.contains(key)) continue; // unrecognized option
2370 // no filtering for encoding (custom encoding for project is allowed)
2371 String value = (String)newOptions.get(key);
2372 preferences.setDefault(key, CUSTOM_DEFAULT_OPTION_VALUE); // empty string isn't the default (26251)
2373 preferences.setValue(key, value);
2378 savePreferences(preferences);
2384 public void setOutputLocation(IPath path, IProgressMonitor monitor)
2385 throws JavaModelException {
2388 throw new IllegalArgumentException(Util.bind("path.nullpath")); //$NON-NLS-1$
2390 if (path.equals(getOutputLocation())) {
2393 this.setRawClasspath(SetClasspathOperation.ReuseClasspath, path, monitor);
2397 * Set cached preferences, no preference file is saved, only info is updated
2399 public void setPreferences(Preferences preferences) {
2400 IProject project = getProject();
2401 if (!JavaProject.hasJavaNature(project)) return; // ignore
2402 JavaModelManager.PerProjectInfo perProjectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfo(project, true);
2403 perProjectInfo.preferences = preferences;
2409 public void setRawClasspath(
2410 IClasspathEntry[] entries,
2411 IPath outputLocation,
2412 IProgressMonitor monitor)
2413 throws JavaModelException {
2419 true, // canChangeResource (as per API contract)
2420 getResolvedClasspath(true), // ignoreUnresolvedVariable
2421 true, // needValidation
2422 true); // need to save
2425 public void setRawClasspath(
2426 IClasspathEntry[] newEntries,
2427 IPath newOutputLocation,
2428 IProgressMonitor monitor,
2429 boolean canChangeResource,
2430 IClasspathEntry[] oldResolvedPath,
2431 boolean needValidation,
2433 throws JavaModelException {
2435 JavaModelManager manager =
2436 (JavaModelManager) JavaModelManager.getJavaModelManager();
2438 IClasspathEntry[] newRawPath = newEntries;
2439 if (newRawPath == null) { //are we already with the default classpath
2440 newRawPath = defaultClasspath();
2442 SetClasspathOperation op =
2443 new SetClasspathOperation(
2451 runOperation(op, monitor);
2453 } catch (JavaModelException e) {
2462 public void setRawClasspath(
2463 IClasspathEntry[] entries,
2464 IProgressMonitor monitor)
2465 throws JavaModelException {
2469 SetClasspathOperation.ReuseOutputLocation,
2471 true, // canChangeResource (as per API contract)
2472 getResolvedClasspath(true), // ignoreUnresolvedVariable
2473 true, // needValidation
2474 true); // need to save
2478 * NOTE: <code>null</code> specifies default classpath, and an empty
2479 * array specifies an empty classpath.
2481 * @exception NotPresentException if this project does not exist.
2483 protected void setRawClasspath0(IClasspathEntry[] rawEntries)
2484 throws JavaModelException {
2486 JavaModelManager.PerProjectInfo info = JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(project);
2488 synchronized (info) {
2489 if (rawEntries != null) {
2490 info.classpath = rawEntries;
2493 // clear cache of resolved classpath
2494 info.lastResolvedClasspath = null;
2495 info.resolvedPathToRawEntries = null;
2500 * Record a shared persistent property onto a project.
2501 * Note that it is orthogonal to IResource persistent properties, and client code has to decide
2502 * which form of storage to use appropriately. Shared properties produce real resource files which
2503 * can be shared through a VCM onto a server. Persistent properties are not shareable.
2505 * shared properties end up in resource files, and thus cannot be modified during
2506 * delta notifications (a CoreException would then be thrown).
2508 * @see JavaProject#getSharedProperty(String key)
2510 public void setSharedProperty(String key, String value) throws CoreException {
2512 IFile rscFile = getProject().getFile(key);
2513 InputStream inputStream = new ByteArrayInputStream(value.getBytes());
2514 // update the resource content
2515 if (rscFile.exists()) {
2516 if (rscFile.isReadOnly()) {
2517 // provide opportunity to checkout read-only .classpath file (23984)
2518 ResourcesPlugin.getWorkspace().validateEdit(new IFile[]{rscFile}, null);
2520 rscFile.setContents(inputStream, IResource.FORCE, null);
2522 rscFile.create(inputStream, IResource.FORCE, null);
2527 * Update cycle markers for all java projects
2529 public static void updateAllCycleMarkers() throws JavaModelException {
2531 //long start = System.currentTimeMillis();
2533 JavaModelManager manager = JavaModelManager.getJavaModelManager();
2534 IJavaProject[] projects = manager.getJavaModel().getJavaProjects();
2535 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
2537 HashSet cycleParticipants = new HashSet();
2538 HashSet traversed = new HashSet();
2539 int length = projects.length;
2541 // compute cycle participants
2542 ArrayList prereqChain = new ArrayList();
2543 for (int i = 0; i < length; i++){
2544 JavaProject project = (JavaProject)projects[i];
2545 if (!traversed.contains(project.getPath())){
2546 prereqChain.clear();
2547 project.updateCycleParticipants(null, prereqChain, cycleParticipants, workspaceRoot, traversed);
2550 //System.out.println("updateAllCycleMarkers: " + (System.currentTimeMillis() - start) + " ms");
2552 for (int i = 0; i < length; i++){
2553 JavaProject project = (JavaProject)projects[i];
2555 if (cycleParticipants.contains(project.getPath())){
2556 IMarker cycleMarker = project.getCycleMarker();
2557 String circularCPOption = project.getOption(JavaCore.CORE_CIRCULAR_CLASSPATH, true);
2558 int circularCPSeverity = JavaCore.ERROR.equals(circularCPOption) ? IMarker.SEVERITY_ERROR : IMarker.SEVERITY_WARNING;
2559 if (cycleMarker != null) {
2560 // update existing cycle marker if needed
2562 int existingSeverity = ((Integer)cycleMarker.getAttribute(IMarker.SEVERITY)).intValue();
2563 if (existingSeverity != circularCPSeverity) {
2564 cycleMarker.setAttribute(IMarker.SEVERITY, circularCPSeverity);
2566 } catch (CoreException e) {
2567 throw new JavaModelException(e);
2570 // create new marker
2571 project.createClasspathProblemMarker(
2572 new JavaModelStatus(IJavaModelStatusConstants.CLASSPATH_CYCLE, project));
2575 project.flushClasspathProblemMarkers(true, false);
2581 * If a cycle is detected, then cycleParticipants contains all the paths of projects involved in this cycle (directly and indirectly),
2582 * no cycle if the set is empty (and started empty)
2584 public void updateCycleParticipants(
2585 IClasspathEntry[] preferredClasspath,
2586 ArrayList prereqChain,
2587 HashSet cycleParticipants,
2588 IWorkspaceRoot workspaceRoot,
2591 IPath path = this.getPath();
2592 prereqChain.add(path);
2593 traversed.add(path);
2595 IClasspathEntry[] classpath = preferredClasspath == null ? getResolvedClasspath(true) : preferredClasspath;
2596 for (int i = 0, length = classpath.length; i < length; i++) {
2597 IClasspathEntry entry = classpath[i];
2599 if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT){
2600 IPath prereqProjectPath = entry.getPath();
2601 int index = cycleParticipants.contains(prereqProjectPath) ? 0 : prereqChain.indexOf(prereqProjectPath);
2602 if (index >= 0) { // refer to cycle, or in cycle itself
2603 for (int size = prereqChain.size(); index < size; index++) {
2604 cycleParticipants.add(prereqChain.get(index));
2607 if (!traversed.contains(prereqProjectPath)) {
2608 IResource member = workspaceRoot.findMember(prereqProjectPath);
2609 if (member != null && member.getType() == IResource.PROJECT){
2610 JavaProject project = (JavaProject)JavaCore.create((IProject)member);
2611 project.updateCycleParticipants(null, prereqChain, cycleParticipants, workspaceRoot, traversed);
2617 } catch(JavaModelException e){
2619 prereqChain.remove(path);
2622 * Reset the collection of package fragment roots (local ones) - only if opened.
2623 * Need to check *all* package fragment roots in order to reset NameLookup
2625 public void updatePackageFragmentRoots(){
2627 if (this.isOpen()) {
2629 JavaProjectElementInfo info = getJavaProjectElementInfo();
2631 IClasspathEntry[] classpath = getResolvedClasspath(true);
2632 // NameLookup lookup = info.getNameLookup();
2633 // if (lookup != null){
2634 // IPackageFragmentRoot[] oldRoots = lookup.fPackageFragmentRoots;
2635 // IPackageFragmentRoot[] newRoots = computePackageFragmentRoots(classpath, true);
2636 // checkIdentical: { // compare all pkg fragment root lists
2637 // if (oldRoots.length == newRoots.length){
2638 // for (int i = 0, length = oldRoots.length; i < length; i++){
2639 // if (!oldRoots[i].equals(newRoots[i])){
2640 // break checkIdentical;
2643 // return; // no need to update
2646 // info.setNameLookup(null); // discard name lookup (hold onto roots)
2648 info.setNonJavaResources(null);
2650 computePackageFragmentRoots(classpath, false));
2652 } catch(JavaModelException e){
2654 close(); // could not do better
2655 } catch(JavaModelException ex){
2661 public void removeLoadPathEntry(IProject anotherPHPProject) {
2662 Iterator entries = getLoadPathEntries().iterator();
2663 while (entries.hasNext()) {
2664 LoadPathEntry entry = (LoadPathEntry) entries.next();
2665 if (entry.getType() == LoadPathEntry.TYPE_PROJECT && entry.getProject().getName().equals(anotherPHPProject.getName())) {
2666 getLoadPathEntries().remove(entry);
2673 public void save() throws CoreException {
2675 InputStream xmlPath = new ByteArrayInputStream(getLoadPathXML().getBytes());
2676 IFile loadPathsFile = getLoadPathEntriesFile();
2677 if (!loadPathsFile.exists())
2678 loadPathsFile.create(xmlPath, true, null);
2680 loadPathsFile.setContents(xmlPath, true, false, null);