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 javax.xml.parsers.DocumentBuilder;
58 import javax.xml.parsers.DocumentBuilderFactory;
59 import javax.xml.parsers.ParserConfigurationException;
60 import org.w3c.dom.Element;
61 import org.w3c.dom.Node;
62 import org.w3c.dom.NodeList;
63 import org.xml.sax.InputSource;
64 import org.xml.sax.SAXException;
66 import org.eclipse.core.resources.ICommand;
67 import org.eclipse.core.resources.IFile;
68 import org.eclipse.core.resources.IFolder;
69 import org.eclipse.core.resources.IMarker;
70 import org.eclipse.core.resources.IProject;
71 import org.eclipse.core.resources.IProjectDescription;
72 import org.eclipse.core.resources.IProjectNature;
73 import org.eclipse.core.resources.IResource;
74 import org.eclipse.core.resources.IWorkspace;
75 import org.eclipse.core.resources.IWorkspaceRoot;
76 import org.eclipse.core.resources.ResourcesPlugin;
77 import org.eclipse.core.runtime.CoreException;
78 import org.eclipse.core.runtime.IPath;
79 import org.eclipse.core.runtime.IProgressMonitor;
80 import org.eclipse.core.runtime.Path;
81 import org.eclipse.core.runtime.Preferences;
82 import org.eclipse.core.runtime.QualifiedName;
83 import net.sourceforge.phpdt.internal.core.ClasspathEntry;
84 import net.sourceforge.phpdt.internal.core.XMLWriter;
85 import org.w3c.dom.Document;
86 import org.w3c.dom.Element;
87 import org.w3c.dom.Node;
88 import org.w3c.dom.NodeList;
89 import org.xml.sax.Attributes;
90 import org.xml.sax.ContentHandler;
91 import org.xml.sax.InputSource;
92 import org.xml.sax.Locator;
93 import org.xml.sax.SAXException;
94 import org.xml.sax.XMLReader;
97 * Handle for a Java Project.
99 * <p>A Java Project internally maintains a devpath that corresponds
100 * to the project's classpath. The classpath may include source folders
101 * from the current project; jars in the current project, other projects,
102 * and the local file system; and binary folders (output location) of other
103 * projects. The Java Model presents source elements corresponding to output
104 * .class files in other projects, and thus uses the devpath rather than
105 * the classpath (which is really a compilation path). The devpath mimics
106 * the classpath, except has source folder entries in place of output
107 * locations in external projects.
109 * <p>Each JavaProject has a NameLookup facility that locates elements
110 * on by name, based on the devpath.
114 public class JavaProject
116 implements IJavaProject , IProjectNature {
119 * Whether the underlying file system is case sensitive.
121 protected static final boolean IS_CASE_SENSITIVE = !new File("Temp").equals(new File("temp")); //$NON-NLS-1$ //$NON-NLS-2$
124 * An empty array of strings indicating that a project doesn't have any prerequesite projects.
126 protected static final String[] NO_PREREQUISITES = new String[0];
129 * The platform project this <code>IJavaProject</code> is based on
131 protected IProject project;
132 protected List fLoadPathEntries;
133 protected boolean fScratched;
136 * Name of file containing project classpath
138 public static final String CLASSPATH_FILENAME = ".classpath"; //$NON-NLS-1$
141 * Name of file containing custom project preferences
143 public static final String PREF_FILENAME = ".jprefs"; //$NON-NLS-1$
146 * Value of the project's raw classpath if the .classpath file contains invalid entries.
148 public static final IClasspathEntry[] INVALID_CLASSPATH = new IClasspathEntry[0];
150 private static final String CUSTOM_DEFAULT_OPTION_VALUE = "#\r\n\r#custom-non-empty-default-value#\r\n\r#"; //$NON-NLS-1$
153 * Returns a canonicalized path from the given external path.
154 * Note that the return path contains the same number of segments
155 * and it contains a device only if the given path contained one.
156 * @see java.io.File for the definition of a canonicalized path
158 public static IPath canonicalizedPath(IPath externalPath) {
160 if (externalPath == null)
163 if (JavaModelManager.VERBOSE) {
164 System.out.println("JAVA MODEL - Canonicalizing " + externalPath.toString()); //$NON-NLS-1$
167 if (IS_CASE_SENSITIVE) {
168 if (JavaModelManager.VERBOSE) {
169 System.out.println("JAVA MODEL - Canonical path is original path (file system is case sensitive)"); //$NON-NLS-1$
174 // if not external path, return original path
175 IWorkspace workspace = ResourcesPlugin.getWorkspace();
176 if (workspace == null) return externalPath; // protection during shutdown (30487)
177 if (workspace.getRoot().findMember(externalPath) != null) {
178 if (JavaModelManager.VERBOSE) {
179 System.out.println("JAVA MODEL - Canonical path is original path (member of workspace)"); //$NON-NLS-1$
184 IPath canonicalPath = null;
187 new Path(new File(externalPath.toOSString()).getCanonicalPath());
188 } catch (IOException e) {
189 // default to original path
190 if (JavaModelManager.VERBOSE) {
191 System.out.println("JAVA MODEL - Canonical path is original path (IOException)"); //$NON-NLS-1$
197 int canonicalLength = canonicalPath.segmentCount();
198 if (canonicalLength == 0) {
199 // the java.io.File canonicalization failed
200 if (JavaModelManager.VERBOSE) {
201 System.out.println("JAVA MODEL - Canonical path is original path (canonical path is empty)"); //$NON-NLS-1$
204 } else if (externalPath.isAbsolute()) {
205 result = canonicalPath;
207 // if path is relative, remove the first segments that were added by the java.io.File canonicalization
208 // e.g. 'lib/classes.zip' was converted to 'd:/myfolder/lib/classes.zip'
209 int externalLength = externalPath.segmentCount();
210 if (canonicalLength >= externalLength) {
211 result = canonicalPath.removeFirstSegments(canonicalLength - externalLength);
213 if (JavaModelManager.VERBOSE) {
214 System.out.println("JAVA MODEL - Canonical path is original path (canonical path is " + canonicalPath.toString() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
220 // keep device only if it was specified (this is because File.getCanonicalPath() converts '/lib/classed.zip' to 'd:/lib/classes/zip')
221 if (externalPath.getDevice() == null) {
222 result = result.setDevice(null);
224 if (JavaModelManager.VERBOSE) {
225 System.out.println("JAVA MODEL - Canonical path is " + result.toString()); //$NON-NLS-1$
231 * Constructor needed for <code>IProject.getNature()</code> and <code>IProject.addNature()</code>.
233 * @see #setProject(IProject)
235 public JavaProject() {
239 public JavaProject(IProject project, JavaElement parent) {
240 super(parent, project.getName());
241 this.project = project;
243 public void addLoadPathEntry(IProject anotherPHPProject) {
246 LoadPathEntry newEntry = new LoadPathEntry(anotherPHPProject);
247 getLoadPathEntries().add(newEntry);
250 public void configure() throws CoreException {
251 // get project description and then the associated build commands
252 IProjectDescription desc = project.getDescription();
253 ICommand[] commands = desc.getBuildSpec();
255 // determine if builder already associated
256 boolean found = false;
257 for (int i = 0; i < commands.length; ++i) {
258 if (commands[i].getBuilderName().equals(PHPeclipsePlugin.BUILDER_PARSER_ID)) {
264 // add builder if not already in project
266 ICommand command = desc.newCommand();
267 command.setBuilderName(PHPeclipsePlugin.BUILDER_PARSER_ID);
268 ICommand[] newCommands = new ICommand[commands.length + 1];
270 // Add it before other builders.
271 System.arraycopy(commands, 0, newCommands, 1, commands.length);
272 newCommands[0] = command;
273 desc.setBuildSpec(newCommands);
274 project.setDescription(desc, null);
278 protected void loadLoadPathEntries() {
279 fLoadPathEntries = new ArrayList();
281 IFile loadPathsFile = getLoadPathEntriesFile();
283 XMLReader reader = null;
285 reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
286 reader.setContentHandler(getLoadPathEntriesContentHandler());
287 reader.parse(new InputSource(loadPathsFile.getContents()));
288 } catch (Exception e) {
289 //the file is nonextant or unreadable
293 public List getLoadPathEntries() {
294 if (fLoadPathEntries == null) {
295 loadLoadPathEntries();
298 return fLoadPathEntries;
301 protected ContentHandler getLoadPathEntriesContentHandler() {
302 return new ContentHandler() {
303 public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
306 public void endDocument() throws SAXException {
309 public void endElement(String arg0, String arg1, String arg2) throws SAXException {
312 public void endPrefixMapping(String arg0) throws SAXException {
315 public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {
318 public void processingInstruction(String arg0, String arg1) throws SAXException {
321 public void setDocumentLocator(Locator arg0) {
324 public void skippedEntity(String arg0) throws SAXException {
327 public void startDocument() throws SAXException {
330 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
331 if ("pathentry".equals(qName))
332 if ("project".equals(atts.getValue("type"))) {
333 IPath referencedProjectPath = new Path(atts.getValue("path"));
334 IProject referencedProject = getProject(referencedProjectPath.lastSegment());
335 fLoadPathEntries.add(new LoadPathEntry(referencedProject));
339 public void startPrefixMapping(String arg0, String arg1) throws SAXException {
344 protected IFile getLoadPathEntriesFile() {
345 return project.getFile(".loadpath");
348 protected String getLoadPathXML() {
349 StringBuffer buffer = new StringBuffer();
350 buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><loadpath>");
352 Iterator pathEntriesIterator = fLoadPathEntries.iterator();
354 while (pathEntriesIterator.hasNext()) {
355 LoadPathEntry entry = (LoadPathEntry) pathEntriesIterator.next();
356 buffer.append(entry.toXML());
359 buffer.append("</loadpath>");
360 return buffer.toString();
363 * Adds a builder to the build spec for the given project.
365 protected void addToBuildSpec(String builderID) throws CoreException {
367 IProjectDescription description = getProject().getDescription();
368 ICommand javaCommand = getJavaCommand(description);
370 if (javaCommand == null) {
372 // Add a Java command to the build spec
373 ICommand command = description.newCommand();
374 command.setBuilderName(builderID);
375 setJavaCommand(description, command);
381 protected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException {
383 // check whether the java project can be opened
384 if (!underlyingResource.isAccessible()) {
385 throw newNotPresentException();
388 IWorkspace workspace = ResourcesPlugin.getWorkspace();
389 IWorkspaceRoot wRoot = workspace.getRoot();
390 // cannot refresh cp markers on opening (emulate cp check on startup) since can create deadlocks (see bug 37274)
391 // IClasspathEntry[] resolvedClasspath = getResolvedClasspath(true/*ignoreUnresolvedEntry*/, false/*don't generateMarkerOnError*/, false/*don't returnResolutionInProgress*/);
393 // // compute the pkg fragment roots
394 // info.setChildren(computePackageFragmentRoots(resolvedClasspath, false));
396 // // remember the timestamps of external libraries the first time they are looked up
397 // for (int i = 0, length = resolvedClasspath.length; i < length; i++) {
398 // IClasspathEntry entry = resolvedClasspath[i];
399 // if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
400 // IPath path = entry.getPath();
401 // Object target = JavaModel.getTarget(wRoot, path, true);
402 // if (target instanceof java.io.File) {
403 // Map externalTimeStamps = JavaModelManager.getJavaModelManager().deltaState.externalTimeStamps;
404 // if (externalTimeStamps.get(path) == null) {
405 // long timestamp = DeltaProcessor.getTimeStamp((java.io.File)target);
406 // externalTimeStamps.put(path, new Long(timestamp));
414 protected void closing(Object info) {
416 // // forget source attachment recommendations
417 // Object[] children = ((JavaElementInfo)info).children;
418 // for (int i = 0, length = children.length; i < length; i++) {
419 // Object child = children[i];
420 // if (child instanceof JarPackageFragmentRoot){
421 // ((JarPackageFragmentRoot)child).setSourceAttachmentProperty(null);
427 // protected void closing(Object info) throws JavaModelException {
429 // // forget source attachment recommendations
430 // IPackageFragmentRoot[] roots = this.getPackageFragmentRoots();
431 //// for (int i = 0; i < roots.length; i++) {
432 //// if (roots[i] instanceof JarPackageFragmentRoot){
433 //// ((JarPackageFragmentRoot) roots[i]).setSourceAttachmentProperty(null);
437 // super.closing(info);
443 * Internal computation of an expanded classpath. It will eliminate duplicates, and produce copies
444 * of exported classpath entries to avoid possible side-effects ever after.
446 private void computeExpandedClasspath(
447 JavaProject initialProject,
448 boolean ignoreUnresolvedVariable,
449 boolean generateMarkerOnError,
450 HashSet visitedProjects,
451 ObjectVector accumulatedEntries) throws JavaModelException {
453 if (visitedProjects.contains(this)){
454 return; // break cycles if any
456 visitedProjects.add(this);
458 if (generateMarkerOnError && !this.equals(initialProject)){
459 generateMarkerOnError = false;
461 IClasspathEntry[] immediateClasspath =
462 getResolvedClasspath(ignoreUnresolvedVariable, generateMarkerOnError);
464 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
465 for (int i = 0, length = immediateClasspath.length; i < length; i++){
466 IClasspathEntry entry = immediateClasspath[i];
468 boolean isInitialProject = this.equals(initialProject);
469 if (isInitialProject || entry.isExported()){
471 accumulatedEntries.add(entry);
473 // recurse in project to get all its indirect exports (only consider exported entries from there on)
474 if (entry.getEntryKind() == ClasspathEntry.CPE_PROJECT) {
475 IResource member = workspaceRoot.findMember(entry.getPath());
476 if (member != null && member.getType() == IResource.PROJECT){ // double check if bound to project (23977)
477 IProject projRsc = (IProject) member;
478 if (JavaProject.hasJavaNature(projRsc)) {
479 JavaProject project = (JavaProject) JavaCore.create(projRsc);
480 project.computeExpandedClasspath(
482 ignoreUnresolvedVariable,
483 generateMarkerOnError,
494 * Returns (local/all) the package fragment roots identified by the given project's classpath.
495 * Note: this follows project classpath references to find required project contributions,
496 * eliminating duplicates silently.
497 * Only works with resolved entries
499 public IPackageFragmentRoot[] computePackageFragmentRoots(IClasspathEntry[] resolvedClasspath, boolean retrieveExportedRoots) throws JavaModelException {
501 ObjectVector accumulatedRoots = new ObjectVector();
502 computePackageFragmentRoots(
505 new HashSet(5), // rootIDs
506 true, // inside original project
507 true, // check existency
508 retrieveExportedRoots);
509 IPackageFragmentRoot[] rootArray = new IPackageFragmentRoot[accumulatedRoots.size()];
510 accumulatedRoots.copyInto(rootArray);
515 * Computes the package fragment roots identified by the given entry.
516 * Only works with resolved entry
518 public IPackageFragmentRoot[] computePackageFragmentRoots(IClasspathEntry resolvedEntry) {
521 computePackageFragmentRoots(
522 new IClasspathEntry[]{ resolvedEntry },
523 false // don't retrieve exported roots
525 } catch (JavaModelException e) {
526 return new IPackageFragmentRoot[] {};
531 * Returns the package fragment roots identified by the given entry. In case it refers to
532 * a project, it will follow its classpath so as to find exported roots as well.
533 * Only works with resolved entry
535 public void computePackageFragmentRoots(
536 IClasspathEntry resolvedEntry,
537 ObjectVector accumulatedRoots,
539 boolean insideOriginalProject,
540 boolean checkExistency,
541 boolean retrieveExportedRoots) throws JavaModelException {
543 String rootID = ((ClasspathEntry)resolvedEntry).rootID();
544 if (rootIDs.contains(rootID)) return;
546 IPath projectPath = getProject().getFullPath();
547 IPath entryPath = resolvedEntry.getPath();
548 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
550 switch(resolvedEntry.getEntryKind()){
553 case IClasspathEntry.CPE_SOURCE :
555 if (projectPath.isPrefixOf(entryPath)){
556 if (checkExistency) {
557 Object target = JavaModel.getTarget(workspaceRoot, entryPath, checkExistency);
558 if (target == null) return;
560 if (target instanceof IFolder || target instanceof IProject){
561 accumulatedRoots.add(
562 getPackageFragmentRoot((IResource)target));
566 IPackageFragmentRoot root = getFolderPackageFragmentRoot(entryPath);
568 accumulatedRoots.add(root);
575 // internal/external JAR or folder
576 case IClasspathEntry.CPE_LIBRARY :
578 if (!insideOriginalProject && !resolvedEntry.isExported()) return;
580 if (checkExistency) {
581 Object target = JavaModel.getTarget(workspaceRoot, entryPath, checkExistency);
582 if (target == null) return;
584 if (target instanceof IResource){
586 IResource resource = (IResource) target;
587 IPackageFragmentRoot root = getPackageFragmentRoot(resource);
589 accumulatedRoots.add(root);
593 // external target - only JARs allowed
594 // if (((java.io.File)target).isFile() && (Util.isArchiveFileName(entryPath.lastSegment()))) {
595 // accumulatedRoots.add(
596 // new JarPackageFragmentRoot(entryPath, this));
597 // rootIDs.add(rootID);
601 IPackageFragmentRoot root = getPackageFragmentRoot(entryPath);
603 accumulatedRoots.add(root);
609 // recurse into required project
610 case IClasspathEntry.CPE_PROJECT :
612 if (!retrieveExportedRoots) return;
613 if (!insideOriginalProject && !resolvedEntry.isExported()) return;
615 IResource member = workspaceRoot.findMember(entryPath);
616 if (member != null && member.getType() == IResource.PROJECT){// double check if bound to project (23977)
617 IProject requiredProjectRsc = (IProject) member;
618 if (JavaProject.hasJavaNature(requiredProjectRsc)){ // special builder binary output
620 JavaProject requiredProject = (JavaProject)JavaCore.create(requiredProjectRsc);
621 requiredProject.computePackageFragmentRoots(
622 requiredProject.getResolvedClasspath(true),
627 retrieveExportedRoots);
635 * Returns (local/all) the package fragment roots identified by the given project's classpath.
636 * Note: this follows project classpath references to find required project contributions,
637 * eliminating duplicates silently.
638 * Only works with resolved entries
640 public void computePackageFragmentRoots(
641 IClasspathEntry[] resolvedClasspath,
642 ObjectVector accumulatedRoots,
644 boolean insideOriginalProject,
645 boolean checkExistency,
646 boolean retrieveExportedRoots) throws JavaModelException {
648 if (insideOriginalProject){
649 rootIDs.add(rootID());
651 for (int i = 0, length = resolvedClasspath.length; i < length; i++){
652 computePackageFragmentRoots(
653 resolvedClasspath[i],
656 insideOriginalProject,
658 retrieveExportedRoots);
663 * Compute the file name to use for a given shared property
665 public String computeSharedPropertyFileName(QualifiedName qName) {
667 return '.' + qName.getLocalName();
671 * Returns whether the given resource is accessible through the children or the non-Java resources of this project.
672 * Returns true if the resource is not in the project.
673 * Assumes that the resource is a folder or a file.
675 public boolean contains(IResource resource) {
677 IClasspathEntry[] classpath;
680 classpath = getResolvedClasspath(true);
681 output = getOutputLocation();
682 } catch (JavaModelException e) {
686 IPath fullPath = resource.getFullPath();
687 IPath innerMostOutput = output.isPrefixOf(fullPath) ? output : null;
688 IClasspathEntry innerMostEntry = null;
689 for (int j = 0, cpLength = classpath.length; j < cpLength; j++) {
690 IClasspathEntry entry = classpath[j];
692 IPath entryPath = entry.getPath();
693 if ((innerMostEntry == null || innerMostEntry.getPath().isPrefixOf(entryPath))
694 && entryPath.isPrefixOf(fullPath)) {
695 innerMostEntry = entry;
697 IPath entryOutput = classpath[j].getOutputLocation();
698 if (entryOutput != null && entryOutput.isPrefixOf(fullPath)) {
699 innerMostOutput = entryOutput;
702 if (innerMostEntry != null) {
703 // special case prj==src and nested output location
704 if (innerMostOutput != null && innerMostOutput.segmentCount() > 1 // output isn't project
705 && innerMostEntry.getPath().segmentCount() == 1) { // 1 segment must be project name
708 if (resource instanceof IFolder) {
709 // folders are always included in src/lib entries
712 switch (innerMostEntry.getEntryKind()) {
713 case IClasspathEntry.CPE_SOURCE:
714 // .class files are not visible in source folders
715 return true; //!net.sourceforge.phpdt.internal.compiler.util.Util.isClassFileName(fullPath.lastSegment());
716 case IClasspathEntry.CPE_LIBRARY:
717 // .java files are not visible in library folders
718 return !net.sourceforge.phpdt.internal.compiler.util.Util.isJavaFileName(fullPath.lastSegment());
721 if (innerMostOutput != null) {
728 * Record a new marker denoting a classpath problem
730 IMarker createClasspathProblemMarker(IJavaModelStatus status) {
732 IMarker marker = null;
734 String[] arguments = new String[0];
735 boolean isCycleProblem = false, isClasspathFileFormatProblem = false;
736 switch (status.getCode()) {
738 case IJavaModelStatusConstants.CLASSPATH_CYCLE :
739 isCycleProblem = true;
740 if (JavaCore.ERROR.equals(getOption(JavaCore.CORE_CIRCULAR_CLASSPATH, true))) {
741 severity = IMarker.SEVERITY_ERROR;
743 severity = IMarker.SEVERITY_WARNING;
747 case IJavaModelStatusConstants.INVALID_CLASSPATH_FILE_FORMAT :
748 isClasspathFileFormatProblem = true;
749 severity = IMarker.SEVERITY_ERROR;
753 IPath path = status.getPath();
754 if (path != null) arguments = new String[] { path.toString() };
755 if (JavaCore.ERROR.equals(getOption(JavaCore.CORE_INCOMPLETE_CLASSPATH, true))) {
756 severity = IMarker.SEVERITY_ERROR;
758 severity = IMarker.SEVERITY_WARNING;
764 marker = getProject().createMarker(IJavaModelMarker.BUILDPATH_PROBLEM_MARKER);
765 marker.setAttributes(
770 IJavaModelMarker.CYCLE_DETECTED,
771 IJavaModelMarker.CLASSPATH_FILE_FORMAT,
773 IJavaModelMarker.ARGUMENTS ,
777 new Integer(severity),
778 Util.bind("classpath.buildPath"),//$NON-NLS-1$
779 isCycleProblem ? "true" : "false",//$NON-NLS-1$ //$NON-NLS-2$
780 isClasspathFileFormatProblem ? "true" : "false",//$NON-NLS-1$ //$NON-NLS-2$
781 new Integer(status.getCode()),
782 Util.getProblemArgumentsForMarker(arguments) ,
785 } catch (CoreException e) {
791 * Returns a new element info for this element.
793 protected Object createElementInfo() {
794 return new JavaProjectElementInfo();
799 * Returns a new search name environment for this project. This name environment first looks in the given working copies.
801 // public ISearchableNameEnvironment newSearchableNameEnvironment(ICompilationUnit[] workingCopies) throws JavaModelException {
802 // return new SearchableEnvironment(this, workingCopies);
806 * Returns a new search name environment for this project. This name environment first looks in the working copies
807 * of the given owner.
809 public ISearchableNameEnvironment newSearchableNameEnvironment(WorkingCopyOwner owner) throws JavaModelException {
810 return new SearchableEnvironment(this, owner);
813 * Reads and decode an XML classpath string
815 protected IClasspathEntry[] decodeClasspath(String xmlClasspath, boolean createMarker, boolean logProblems) {
817 ArrayList paths = new ArrayList();
818 IClasspathEntry defaultOutput = null;
820 if (xmlClasspath == null) return null;
821 StringReader reader = new StringReader(xmlClasspath);
825 DocumentBuilder parser =
826 DocumentBuilderFactory.newInstance().newDocumentBuilder();
827 cpElement = parser.parse(new InputSource(reader)).getDocumentElement();
828 } catch (SAXException e) {
829 throw new IOException(Util.bind("file.badFormat")); //$NON-NLS-1$
830 } catch (ParserConfigurationException e) {
831 throw new IOException(Util.bind("file.badFormat")); //$NON-NLS-1$
836 if (!cpElement.getNodeName().equalsIgnoreCase("classpath")) { //$NON-NLS-1$
837 throw new IOException(Util.bind("file.badFormat")); //$NON-NLS-1$
839 NodeList list = cpElement.getElementsByTagName("classpathentry"); //$NON-NLS-1$
840 int length = list.getLength();
842 for (int i = 0; i < length; ++i) {
843 Node node = list.item(i);
844 if (node.getNodeType() == Node.ELEMENT_NODE) {
845 IClasspathEntry entry = ClasspathEntry.elementDecode((Element)node, this);
847 if (entry.getContentKind() == ClasspathEntry.K_OUTPUT) {
848 defaultOutput = entry; // separate output
855 } catch (IOException e) {
857 if (createMarker && this.getProject().isAccessible()) {
858 this.createClasspathProblemMarker(new JavaModelStatus(
859 IJavaModelStatusConstants.INVALID_CLASSPATH_FILE_FORMAT,
860 Util.bind("classpath.xmlFormatError", this.getElementName(), e.getMessage()))); //$NON-NLS-1$
864 "Exception while retrieving "+ this.getPath() //$NON-NLS-1$
865 +"/.classpath, will mark classpath as invalid"); //$NON-NLS-1$
867 return INVALID_CLASSPATH;
868 } catch (Assert.AssertionFailedException e) {
869 // failed creating CP entries from file
870 if (createMarker && this.getProject().isAccessible()) {
871 this.createClasspathProblemMarker(new JavaModelStatus(
872 IJavaModelStatusConstants.INVALID_CLASSPATH_FILE_FORMAT,
873 Util.bind("classpath.illegalEntryInClasspathFile", this.getElementName(), e.getMessage()))); //$NON-NLS-1$
877 "Exception while retrieving "+ this.getPath() //$NON-NLS-1$
878 +"/.classpath, will mark classpath as invalid"); //$NON-NLS-1$
880 return INVALID_CLASSPATH;
882 int pathSize = paths.size();
883 if (pathSize > 0 || defaultOutput != null) {
884 IClasspathEntry[] entries = new IClasspathEntry[pathSize + (defaultOutput == null ? 0 : 1)];
885 paths.toArray(entries);
886 if (defaultOutput != null) entries[pathSize] = defaultOutput; // ensure output is last item
895 * Removes the Java nature from the project.
897 public void deconfigure() throws CoreException {
899 // deregister Java builder
900 removeFromBuildSpec(PHPeclipsePlugin.BUILDER_PARSER_ID);
904 * Returns a default class path.
905 * This is the root of the project
907 protected IClasspathEntry[] defaultClasspath() throws JavaModelException {
909 return new IClasspathEntry[] {
910 JavaCore.newSourceEntry(getProject().getFullPath())};
914 * Returns a default output location.
915 * This is the project bin folder
917 protected IPath defaultOutputLocation() throws JavaModelException {
918 return getProject().getFullPath().append("bin"); //$NON-NLS-1$
922 * Returns the XML String encoding of the class path.
924 protected String encodeClasspath(IClasspathEntry[] classpath, IPath outputLocation, boolean indent) throws JavaModelException {
926 ByteArrayOutputStream s = new ByteArrayOutputStream();
927 OutputStreamWriter writer = new OutputStreamWriter(s, "UTF8"); //$NON-NLS-1$
928 XMLWriter xmlWriter = new XMLWriter(writer);
930 xmlWriter.startTag("classpath", indent); //$NON-NLS-1$
931 for (int i = 0; i < classpath.length; ++i) {
932 ((ClasspathEntry)classpath[i]).elementEncode(xmlWriter, this.project.getFullPath(), indent, true);
935 if (outputLocation != null) {
936 outputLocation = outputLocation.removeFirstSegments(1);
937 outputLocation = outputLocation.makeRelative();
938 HashMap parameters = new HashMap();
939 parameters.put("kind", ClasspathEntry.kindToString(ClasspathEntry.K_OUTPUT));//$NON-NLS-1$
940 parameters.put("path", String.valueOf(outputLocation));//$NON-NLS-1$
941 xmlWriter.printTag("classpathentry", parameters, indent, true, true);//$NON-NLS-1$
944 xmlWriter.endTag("classpath", indent);//$NON-NLS-1$
947 return s.toString("UTF8");//$NON-NLS-1$
948 } catch (IOException e) {
949 throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
953 * Returns the XML String encoding of the class path.
955 // protected String encodeClasspath(IClasspathEntry[] classpath, IPath outputLocation, boolean useLineSeparator) throws JavaModelException {
957 // Document document = new DocumentImpl();
958 // Element cpElement = document.createElement("classpath"); //$NON-NLS-1$
959 // document.appendChild(cpElement);
961 // for (int i = 0; i < classpath.length; ++i) {
962 // cpElement.appendChild(((ClasspathEntry)classpath[i]).elementEncode(document, getProject().getFullPath()));
965 // if (outputLocation != null) {
966 // outputLocation = outputLocation.removeFirstSegments(1);
967 // outputLocation = outputLocation.makeRelative();
968 // Element oElement = document.createElement("classpathentry"); //$NON-NLS-1$
969 // oElement.setAttribute("kind", ClasspathEntry.kindToString(ClasspathEntry.K_OUTPUT)); //$NON-NLS-1$
970 // oElement.setAttribute("path", outputLocation.toString()); //$NON-NLS-1$
971 // cpElement.appendChild(oElement);
974 // // produce a String output
976 // ByteArrayOutputStream s = new ByteArrayOutputStream();
977 // OutputFormat format = new OutputFormat();
978 // if (useLineSeparator) {
979 // format.setIndenting(true);
980 // format.setLineSeparator(System.getProperty("line.separator")); //$NON-NLS-1$
982 // format.setPreserveSpace(true);
984 // Serializer serializer =
985 // SerializerFactory.getSerializerFactory(Method.XML).makeSerializer(
986 // new OutputStreamWriter(s, "UTF8"), //$NON-NLS-1$
988 // serializer.asDOMSerializer().serialize(document);
989 // return s.toString("UTF8"); //$NON-NLS-1$
990 // } catch (IOException e) {
991 // throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
996 * Returns true if this handle represents the same Java project
997 * as the given handle. Two handles represent the same
998 * project if they are identical or if they represent a project with
999 * the same underlying resource and occurrence counts.
1001 * @see JavaElement#equals
1003 public boolean equals(Object o) {
1008 if (!(o instanceof JavaProject))
1011 JavaProject other = (JavaProject) o;
1012 return getProject().equals(other.getProject())
1013 && occurrenceCount == other.occurrenceCount;
1016 public boolean exists() {
1017 if (!hasJavaNature(project)) return false;
1018 return super.exists();
1024 public IJavaElement findElement(IPath path) throws JavaModelException {
1026 if (path == null || path.isAbsolute()) {
1027 throw new JavaModelException(
1028 new JavaModelStatus(IJavaModelStatusConstants.INVALID_PATH, path));
1032 String extension = path.getFileExtension();
1033 if (extension == null) {
1034 String packageName = path.toString().replace(IPath.SEPARATOR, '.');
1036 // IPackageFragment[] pkgFragments =
1037 // getNameLookup().findPackageFragments(packageName, false);
1038 // if (pkgFragments == null) {
1042 // // try to return one that is a child of this project
1043 // for (int i = 0, length = pkgFragments.length; i < length; i++) {
1045 // IPackageFragment pkgFragment = pkgFragments[i];
1046 // if (this.equals(pkgFragment.getParent().getParent())) {
1047 // return pkgFragment;
1050 // // default to the first one
1051 // return pkgFragments[0];
1054 extension.equalsIgnoreCase("java") //$NON-NLS-1$
1055 || extension.equalsIgnoreCase("class")) { //$NON-NLS-1$
1056 IPath packagePath = path.removeLastSegments(1);
1057 String packageName = packagePath.toString().replace(IPath.SEPARATOR, '.');
1058 String typeName = path.lastSegment();
1059 typeName = typeName.substring(0, typeName.length() - extension.length() - 1);
1060 String qualifiedName = null;
1061 if (packageName.length() > 0) {
1062 qualifiedName = packageName + "." + typeName; //$NON-NLS-1$
1064 qualifiedName = typeName;
1067 // getNameLookup().findType(
1070 // NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES);
1071 // if (type != null) {
1072 // return type.getParent();
1077 // unsupported extension
1080 // } catch (JavaModelException e) {
1081 // if (e.getStatus().getCode()
1082 // == IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST) {
1093 // public IPackageFragment findPackageFragment(IPath path)
1094 // throws JavaModelException {
1096 // return findPackageFragment0(JavaProject.canonicalizedPath(path));
1100 // * non path canonicalizing version
1102 // public IPackageFragment findPackageFragment0(IPath path)
1103 // throws JavaModelException {
1105 // return getNameLookup().findPackageFragment(path);
1111 public IPackageFragmentRoot findPackageFragmentRoot(IPath path)
1112 throws JavaModelException {
1114 return findPackageFragmentRoot0(JavaProject.canonicalizedPath(path));
1118 * no path canonicalization
1120 public IPackageFragmentRoot findPackageFragmentRoot0(IPath path)
1121 throws JavaModelException {
1123 IPackageFragmentRoot[] allRoots = this.getAllPackageFragmentRoots();
1124 if (!path.isAbsolute()) {
1125 throw new IllegalArgumentException(Util.bind("path.mustBeAbsolute")); //$NON-NLS-1$
1127 for (int i= 0; i < allRoots.length; i++) {
1128 IPackageFragmentRoot classpathRoot= allRoots[i];
1129 if (classpathRoot.getPath().equals(path)) {
1130 return classpathRoot;
1138 public IPackageFragmentRoot[] findPackageFragmentRoots(IClasspathEntry entry) {
1140 IClasspathEntry[] classpath = this.getRawClasspath();
1141 for (int i = 0, length = classpath.length; i < length; i++) {
1142 if (classpath[i].equals(entry)) { // entry may need to be resolved
1144 computePackageFragmentRoots(
1145 getResolvedClasspath(new IClasspathEntry[] {entry}, null, true, false, null/*no reverse map*/),
1146 false); // don't retrieve exported roots
1149 } catch (JavaModelException e) {
1151 return new IPackageFragmentRoot[] {};
1155 * @see IJavaProject#findType(String)
1157 // public IType findType(String fullyQualifiedName) throws JavaModelException {
1159 // this.getNameLookup().findType(
1160 // fullyQualifiedName,
1162 // NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES);
1163 // if (type == null) {
1164 // // try to find enclosing type
1165 // int lastDot = fullyQualifiedName.lastIndexOf('.');
1166 // if (lastDot == -1) return null;
1167 // type = this.findType(fullyQualifiedName.substring(0, lastDot));
1168 // if (type != null) {
1169 // type = type.getType(fullyQualifiedName.substring(lastDot+1));
1170 // if (!type.exists()) {
1179 * @see IJavaProject#findType(String, String)
1181 // public IType findType(String packageName, String typeQualifiedName) throws JavaModelException {
1183 // this.getNameLookup().findType(
1184 // typeQualifiedName,
1187 // NameLookup.ACCEPT_CLASSES | NameLookup.ACCEPT_INTERFACES);
1191 * Remove all markers denoting classpath problems
1193 protected void flushClasspathProblemMarkers(boolean flushCycleMarkers, boolean flushClasspathFormatMarkers) {
1195 IProject project = getProject();
1196 if (project.exists()) {
1197 IMarker[] markers = project.findMarkers(IJavaModelMarker.BUILDPATH_PROBLEM_MARKER, false, IResource.DEPTH_ZERO);
1198 for (int i = 0, length = markers.length; i < length; i++) {
1199 IMarker marker = markers[i];
1200 if (flushCycleMarkers && flushClasspathFormatMarkers) {
1203 String cycleAttr = (String)marker.getAttribute(IJavaModelMarker.CYCLE_DETECTED);
1204 String classpathFileFormatAttr = (String)marker.getAttribute(IJavaModelMarker.CLASSPATH_FILE_FORMAT);
1205 if ((flushCycleMarkers == (cycleAttr != null && cycleAttr.equals("true"))) //$NON-NLS-1$
1206 && (flushClasspathFormatMarkers == (classpathFileFormatAttr != null && classpathFileFormatAttr.equals("true")))){ //$NON-NLS-1$
1212 } catch (CoreException e) {
1219 // protected boolean generateInfos(
1220 // OpenableElementInfo info,
1221 // IProgressMonitor pm,
1223 // IResource underlyingResource) throws JavaModelException {
1225 // boolean validInfo = false;
1227 // if (getProject().isOpen()) {
1228 // // put the info now, because computing the roots requires it
1229 // JavaModelManager.getJavaModelManager().putInfo(this, info);
1231 // // compute the pkg fragment roots
1232 // updatePackageFragmentRoots();
1234 // // remember the timestamps of external libraries the first time they are looked up
1235 // IClasspathEntry[] resolvedClasspath = getResolvedClasspath(true/*ignore unresolved variable*/);
1236 // for (int i = 0, length = resolvedClasspath.length; i < length; i++) {
1237 // IClasspathEntry entry = resolvedClasspath[i];
1238 // if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
1239 // IPath path = entry.getPath();
1240 // Object target = JavaModel.getTarget(ResourcesPlugin.getWorkspace().getRoot(), path, true);
1241 // if (target instanceof java.io.File) {
1242 // Map externalTimeStamps = JavaModelManager.getJavaModelManager().deltaProcessor.externalTimeStamps;
1243 // if (externalTimeStamps.get(path) == null) {
1244 // long timestamp = DeltaProcessor.getTimeStamp((java.io.File)target);
1245 // externalTimeStamps.put(path, new Long(timestamp));
1251 // // only valid if reaches here
1252 // validInfo = true;
1256 // JavaModelManager.getJavaModelManager().removeInfo(this);
1258 // return validInfo;
1264 public IPackageFragmentRoot[] getAllPackageFragmentRoots()
1265 throws JavaModelException {
1267 return computePackageFragmentRoots(getResolvedClasspath(true), true);
1271 * Returns the classpath entry that refers to the given path
1272 * or <code>null</code> if there is no reference to the path.
1274 public IClasspathEntry getClasspathEntryFor(IPath path)
1275 throws JavaModelException {
1277 IClasspathEntry[] entries = getExpandedClasspath(true);
1278 for (int i = 0; i < entries.length; i++) {
1279 if (entries[i].getPath().equals(path)) {
1287 * Returns the cycle marker associated with this project or null if none.
1289 public IMarker getCycleMarker(){
1291 IProject project = getProject();
1292 if (project.exists()) {
1293 IMarker[] markers = project.findMarkers(IJavaModelMarker.BUILDPATH_PROBLEM_MARKER, false, IResource.DEPTH_ZERO);
1294 for (int i = 0, length = markers.length; i < length; i++) {
1295 IMarker marker = markers[i];
1296 String cycleAttr = (String)marker.getAttribute(IJavaModelMarker.CYCLE_DETECTED);
1297 if (cycleAttr != null && cycleAttr.equals("true")){ //$NON-NLS-1$
1302 } catch (CoreException e) {
1309 public int getElementType() {
1310 return JAVA_PROJECT;
1313 * This is a helper method returning the expanded classpath for the project, as a list of classpath entries,
1314 * where all classpath variable entries have been resolved and substituted with their final target entries.
1315 * All project exports have been appended to project entries.
1317 public IClasspathEntry[] getExpandedClasspath(boolean ignoreUnresolvedVariable) throws JavaModelException {
1319 return getExpandedClasspath(ignoreUnresolvedVariable, false);
1323 * Internal variant which can create marker on project for invalid entries,
1324 * it will also perform classpath expansion in presence of project prerequisites
1325 * exporting their entries.
1327 public IClasspathEntry[] getExpandedClasspath(
1328 boolean ignoreUnresolvedVariable,
1329 boolean generateMarkerOnError) throws JavaModelException {
1331 ObjectVector accumulatedEntries = new ObjectVector();
1332 computeExpandedClasspath(this, ignoreUnresolvedVariable, generateMarkerOnError, new HashSet(5), accumulatedEntries);
1334 IClasspathEntry[] expandedPath = new IClasspathEntry[accumulatedEntries.size()];
1335 accumulatedEntries.copyInto(expandedPath);
1337 return expandedPath;
1341 * Returns the <code>char</code> that marks the start of this handles
1342 * contribution to a memento.
1344 protected char getHandleMementoDelimiter() {
1346 return JEM_JAVAPROJECT;
1350 * Find the specific Java command amongst the build spec of a given description
1352 private ICommand getJavaCommand(IProjectDescription description)
1353 throws CoreException {
1355 ICommand[] commands = description.getBuildSpec();
1356 for (int i = 0; i < commands.length; ++i) {
1357 if (commands[i].getBuilderName().equals(PHPeclipsePlugin.BUILDER_PARSER_ID)) {
1365 * Convenience method that returns the specific type of info for a Java project.
1367 protected JavaProjectElementInfo getJavaProjectElementInfo()
1368 throws JavaModelException {
1370 return (JavaProjectElementInfo) getElementInfo();
1376 public NameLookup getNameLookup() throws JavaModelException {
1378 JavaProjectElementInfo info = getJavaProjectElementInfo();
1379 // lock on the project info to avoid race condition
1381 NameLookup nameLookup;
1382 if ((nameLookup = info.getNameLookup()) == null){
1383 info.setNameLookup(nameLookup = new NameLookup(this));
1389 * Returns a new name lookup. This name lookup first looks in the given working copies.
1391 public NameLookup newNameLookup(ICompilationUnit[] workingCopies) throws JavaModelException {
1393 JavaProjectElementInfo info = getJavaProjectElementInfo();
1394 // lock on the project info to avoid race condition while computing the pkg fragment roots and package fragment caches
1395 // synchronized(info){
1396 // return new NameLookup(info.getAllPackageFragmentRoots(this), info.getAllPackageFragments(this), workingCopies);
1402 * Returns a new name lookup. This name lookup first looks in the working copies of the given owner.
1404 public NameLookup newNameLookup(WorkingCopyOwner owner) throws JavaModelException {
1406 JavaModelManager manager = JavaModelManager.getJavaModelManager();
1407 ICompilationUnit[] workingCopies = owner == null ? null : manager.getWorkingCopies(owner, true/*add primary WCs*/);
1408 return newNameLookup(workingCopies);
1412 // * Returns an array of non-java resources contained in the receiver.
1414 // public Object[] getNonJavaResources() throws JavaModelException {
1416 // return ((JavaProjectElementInfo) getElementInfo()).getNonJavaResources(this);
1420 * @see org.eclipse.jdt.core.IJavaProject#getOption(String, boolean)
1422 public String getOption(String optionName, boolean inheritJavaCoreOptions) {
1424 if (JavaModelManager.OptionNames.contains(optionName)){
1426 Preferences preferences = getPreferences();
1427 if (preferences == null || preferences.isDefault(optionName)) {
1428 return inheritJavaCoreOptions ? JavaCore.getOption(optionName) : null;
1430 return preferences.getString(optionName).trim();
1436 * @see org.eclipse.jdt.core.IJavaProject#getOptions(boolean)
1438 public Map getOptions(boolean inheritJavaCoreOptions) {
1440 // initialize to the defaults from JavaCore options pool
1441 Map options = inheritJavaCoreOptions ? JavaCore.getOptions() : new Hashtable(5);
1443 Preferences preferences = getPreferences();
1444 if (preferences == null) return options; // cannot do better (non-Java project)
1445 HashSet optionNames = JavaModelManager.OptionNames;
1447 // get preferences set to their default
1448 if (inheritJavaCoreOptions){
1449 String[] defaultPropertyNames = preferences.defaultPropertyNames();
1450 for (int i = 0; i < defaultPropertyNames.length; i++){
1451 String propertyName = defaultPropertyNames[i];
1452 if (optionNames.contains(propertyName)){
1453 options.put(propertyName, preferences.getDefaultString(propertyName).trim());
1457 // get custom preferences not set to their default
1458 String[] propertyNames = preferences.propertyNames();
1459 for (int i = 0; i < propertyNames.length; i++){
1460 String propertyName = propertyNames[i];
1461 if (optionNames.contains(propertyName)){
1462 options.put(propertyName, preferences.getString(propertyName).trim());
1471 public IPath getOutputLocation() throws JavaModelException {
1473 JavaModelManager.PerProjectInfo perProjectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(project);
1474 IPath outputLocation = perProjectInfo.outputLocation;
1475 if (outputLocation != null) return outputLocation;
1477 // force to read classpath - will position output location as well
1478 this.getRawClasspath();
1479 outputLocation = perProjectInfo.outputLocation;
1480 if (outputLocation == null) {
1481 return defaultOutputLocation();
1483 return outputLocation;
1487 * @return A handle to the package fragment root identified by the given path.
1488 * This method is handle-only and the element may or may not exist. Returns
1489 * <code>null</code> if unable to generate a handle from the path (for example,
1490 * an absolute path that has less than 1 segment. The path may be relative or
1493 public IPackageFragmentRoot getPackageFragmentRoot(IPath path) {
1494 if (!path.isAbsolute()) {
1495 path = getPath().append(path);
1497 int segmentCount = path.segmentCount();
1498 switch (segmentCount) {
1503 return getPackageFragmentRoot(getProject());
1505 // a path ending with .jar/.zip is still ambiguous and could still resolve to a source/lib folder
1506 // thus will try to guess based on existing resource
1507 // if (Util.isArchiveFileName(path.lastSegment())) {
1508 // IResource resource = getProject().getWorkspace().getRoot().findMember(path);
1509 // if (resource != null && resource.getType() == IResource.FOLDER){
1510 // return getPackageFragmentRoot(resource);
1512 // return getPackageFragmentRoot0(path);
1514 return getPackageFragmentRoot(getProject().getWorkspace().getRoot().getFolder(path));
1520 * The path is known to match a source/library folder entry.
1522 public IPackageFragmentRoot getFolderPackageFragmentRoot(IPath path) {
1523 if (path.segmentCount() == 1) { // default project root
1524 return getPackageFragmentRoot(getProject());
1526 return getPackageFragmentRoot(getProject().getWorkspace().getRoot().getFolder(path));
1532 public IPackageFragmentRoot getPackageFragmentRoot(IResource resource) {
1534 switch (resource.getType()) {
1535 case IResource.FILE:
1536 // if (Util.isArchiveFileName(resource.getName())) {
1537 // return new JarPackageFragmentRoot(resource, this);
1541 case IResource.FOLDER:
1542 return new PackageFragmentRoot(resource, this, resource.getName());
1543 case IResource.PROJECT:
1544 return new PackageFragmentRoot(resource, this, ""); //$NON-NLS-1$
1553 // public IPackageFragmentRoot getPackageFragmentRoot(String jarPath) {
1555 // return getPackageFragmentRoot0(JavaProject.canonicalizedPath(new Path(jarPath)));
1559 // * no path canonicalization
1561 // public IPackageFragmentRoot getPackageFragmentRoot0(IPath jarPath) {
1563 // return new JarPackageFragmentRoot(jarPath, this);
1569 public IPackageFragmentRoot[] getPackageFragmentRoots()
1570 throws JavaModelException {
1574 IPackageFragmentRoot[] roots;
1577 children = getChildren(),
1579 roots = new IPackageFragmentRoot[length = children.length],
1590 public IPackageFragmentRoot[] getPackageFragmentRoots(IClasspathEntry entry) {
1591 return findPackageFragmentRoots(entry);
1595 * Returns the package fragment root prefixed by the given path, or
1596 * an empty collection if there are no such elements in the model.
1598 protected IPackageFragmentRoot[] getPackageFragmentRoots(IPath path)
1600 throws JavaModelException {
1601 IPackageFragmentRoot[] roots = getAllPackageFragmentRoots();
1602 ArrayList matches = new ArrayList();
1604 for (int i = 0; i < roots.length; ++i) {
1605 if (path.isPrefixOf(roots[i].getPath())) {
1606 matches.add(roots[i]);
1609 IPackageFragmentRoot[] copy = new IPackageFragmentRoot[matches.size()];
1610 matches.toArray(copy);
1617 public IPackageFragment[] getPackageFragments() throws JavaModelException {
1619 IPackageFragmentRoot[] roots = getPackageFragmentRoots();
1620 return getPackageFragmentsInRoots(roots);
1624 * Returns all the package fragments found in the specified
1625 * package fragment roots.
1627 public IPackageFragment[] getPackageFragmentsInRoots(IPackageFragmentRoot[] roots) {
1629 ArrayList frags = new ArrayList();
1630 for (int i = 0; i < roots.length; i++) {
1631 IPackageFragmentRoot root = roots[i];
1633 IJavaElement[] rootFragments = root.getChildren();
1634 for (int j = 0; j < rootFragments.length; j++) {
1635 frags.add(rootFragments[j]);
1637 } catch (JavaModelException e) {
1641 IPackageFragment[] fragments = new IPackageFragment[frags.size()];
1642 frags.toArray(fragments);
1649 public IPath getPath() {
1650 return this.getProject().getFullPath();
1656 public IProject getProject() {
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);
2080 * load preferences from a shareable format (VCM-wise)
2082 public Preferences loadPreferences() {
2084 Preferences preferences = new Preferences();
2086 // File prefFile = getProject().getLocation().append(PREF_FILENAME).toFile();
2087 IPath projectMetaLocation = getProject().getPluginWorkingLocation(JavaCore.getPlugin().getDescriptor());
2088 if (projectMetaLocation != null) {
2089 File prefFile = projectMetaLocation.append(PREF_FILENAME).toFile();
2090 if (prefFile.exists()) { // load preferences from file
2091 InputStream in = null;
2093 in = new BufferedInputStream(new FileInputStream(prefFile));
2094 preferences.load(in);
2096 } catch (IOException e) { // problems loading preference store - quietly ignore
2101 } catch (IOException e) { // ignore problems with close
2111 * @see IJavaProject#newEvaluationContext
2113 // public IEvaluationContext newEvaluationContext() {
2115 // return new EvaluationContextWrapper(new EvaluationContext(), this);
2121 // public ITypeHierarchy newTypeHierarchy(
2123 // IProgressMonitor monitor)
2124 // throws JavaModelException {
2126 // if (region == null) {
2127 // throw new IllegalArgumentException(Util.bind("hierarchy.nullRegion"));//$NON-NLS-1$
2129 // CreateTypeHierarchyOperation op =
2130 // new CreateTypeHierarchyOperation(null, region, this, true);
2131 // runOperation(op, monitor);
2132 // return op.getResult();
2138 // public ITypeHierarchy newTypeHierarchy(
2141 // IProgressMonitor monitor)
2142 // throws JavaModelException {
2144 // if (type == null) {
2145 // throw new IllegalArgumentException(Util.bind("hierarchy.nullFocusType"));//$NON-NLS-1$
2147 // if (region == null) {
2148 // throw new IllegalArgumentException(Util.bind("hierarchy.nullRegion"));//$NON-NLS-1$
2150 // CreateTypeHierarchyOperation op =
2151 // new CreateTypeHierarchyOperation(type, region, this, true);
2152 // runOperation(op, monitor);
2153 // return op.getResult();
2157 // * Open project if resource isn't closed
2159 // protected void openWhenClosed(IProgressMonitor pm) throws JavaModelException {
2161 // if (!this.fProject.isOpen()) {
2162 // throw newNotPresentException();
2164 // super.openWhenClosed(pm);
2168 public String[] projectPrerequisites(IClasspathEntry[] entries)
2169 throws JavaModelException {
2171 ArrayList prerequisites = new ArrayList();
2173 entries = getResolvedClasspath(entries, null, true, false, null/*no reverse map*/);
2174 for (int i = 0, length = entries.length; i < length; i++) {
2175 IClasspathEntry entry = entries[i];
2176 if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
2177 prerequisites.add(entry.getPath().lastSegment());
2180 int size = prerequisites.size();
2182 return NO_PREREQUISITES;
2184 String[] result = new String[size];
2185 prerequisites.toArray(result);
2192 * Reads the .classpath file from disk and returns the list of entries it contains (including output location entry)
2193 * Returns null if .classfile is not present.
2194 * Returns INVALID_CLASSPATH if it has a format problem.
2196 protected IClasspathEntry[] readClasspathFile(boolean createMarker, boolean logProblems) {
2199 String xmlClasspath = getSharedProperty(CLASSPATH_FILENAME);
2200 if (xmlClasspath == null) return null;
2201 return decodeClasspath(xmlClasspath, createMarker, logProblems);
2202 } catch(CoreException e) {
2203 // file does not exist (or not accessible)
2204 if (createMarker && this.getProject().isAccessible()) {
2205 this.createClasspathProblemMarker(new JavaModelStatus(
2206 IJavaModelStatusConstants.INVALID_CLASSPATH_FILE_FORMAT,
2207 Util.bind("classpath.cannotReadClasspathFile", this.getElementName()))); //$NON-NLS-1$
2211 "Exception while retrieving "+ this.getPath() //$NON-NLS-1$
2212 +"/.classpath, will revert to default classpath"); //$NON-NLS-1$
2219 * Removes the given builder from the build spec for the given project.
2221 protected void removeFromBuildSpec(String builderID) throws CoreException {
2223 IProjectDescription description = getProject().getDescription();
2224 ICommand[] commands = description.getBuildSpec();
2225 for (int i = 0; i < commands.length; ++i) {
2226 if (commands[i].getBuilderName().equals(builderID)) {
2227 ICommand[] newCommands = new ICommand[commands.length - 1];
2228 System.arraycopy(commands, 0, newCommands, 0, i);
2229 System.arraycopy(commands, i + 1, newCommands, i, commands.length - i - 1);
2230 description.setBuildSpec(newCommands);
2231 getProject().setDescription(description, null);
2239 * @see JavaElement#rootedAt(IJavaProject)
2241 public IJavaElement rootedAt(IJavaProject project) {
2247 * Answers an ID which is used to distinguish project/entries during package
2248 * fragment root computations
2250 public String rootID(){
2251 return "[PRJ]"+this.getProject().getFullPath(); //$NON-NLS-1$
2255 * Saves the classpath in a shareable format (VCM-wise) only when necessary, that is, if it is semantically different
2256 * from the existing one in file. Will never write an identical one.
2258 * @return Return whether the .classpath file was modified.
2260 public boolean saveClasspath(IClasspathEntry[] newClasspath, IPath newOutputLocation) throws JavaModelException {
2262 if (!getProject().exists()) return false;
2264 IClasspathEntry[] fileEntries = readClasspathFile(false /*don't create markers*/, false/*don't log problems*/);
2265 if (fileEntries != null && isClasspathEqualsTo(newClasspath, newOutputLocation, fileEntries)) {
2266 // no need to save it, it is the same
2270 // actual file saving
2272 setSharedProperty(CLASSPATH_FILENAME, encodeClasspath(newClasspath, newOutputLocation, true));
2274 } catch (CoreException e) {
2275 throw new JavaModelException(e);
2280 * Save project custom preferences to shareable file (.jprefs)
2282 private void savePreferences(Preferences preferences) {
2284 IProject project = getProject();
2285 if (!JavaProject.hasJavaNature(project)) return; // ignore
2287 if (preferences == null || (!preferences.needsSaving() && preferences.propertyNames().length != 0)) {
2292 // preferences need to be saved
2293 // the preferences file is located in the plug-in's state area
2294 // at a well-known name (.jprefs)
2295 // File prefFile = getProject().getLocation().append(PREF_FILENAME).toFile();
2296 File prefFile = project.getPluginWorkingLocation(JavaCore.getPlugin().getDescriptor()).append(PREF_FILENAME).toFile();
2297 if (preferences.propertyNames().length == 0) {
2298 // there are no preference settings
2299 // rather than write an empty file, just delete any existing file
2300 if (prefFile.exists()) {
2301 prefFile.delete(); // don't worry if delete unsuccessful
2306 // write file, overwriting an existing one
2307 OutputStream out = null;
2309 // do it as carefully as we know how so that we don't lose/mangle
2310 // the setting in times of stress
2311 out = new BufferedOutputStream(new FileOutputStream(prefFile));
2312 preferences.store(out, null);
2313 } catch (IOException e) { // problems saving preference store - quietly ignore
2318 } catch (IOException e) { // ignore problems with close
2325 * Update the Java command in the build spec (replace existing one if present,
2326 * add one first if none).
2328 private void setJavaCommand(
2329 IProjectDescription description,
2330 ICommand newCommand)
2331 throws CoreException {
2333 ICommand[] oldCommands = description.getBuildSpec();
2334 ICommand oldJavaCommand = getJavaCommand(description);
2335 ICommand[] newCommands;
2337 if (oldJavaCommand == null) {
2338 // Add a Java build spec before other builders (1FWJK7I)
2339 newCommands = new ICommand[oldCommands.length + 1];
2340 System.arraycopy(oldCommands, 0, newCommands, 1, oldCommands.length);
2341 newCommands[0] = newCommand;
2343 for (int i = 0, max = oldCommands.length; i < max; i++) {
2344 if (oldCommands[i] == oldJavaCommand) {
2345 oldCommands[i] = newCommand;
2349 newCommands = oldCommands;
2352 // Commit the spec change into the project
2353 description.setBuildSpec(newCommands);
2354 getProject().setDescription(description, null);
2358 * @see org.eclipse.jdt.core.IJavaProject#setOptions(Map)
2360 public void setOptions(Map newOptions) {
2362 Preferences preferences;
2363 setPreferences(preferences = new Preferences()); // always reset (26255)
2364 if (newOptions != null){
2365 Iterator keys = newOptions.keySet().iterator();
2366 while (keys.hasNext()){
2367 String key = (String)keys.next();
2368 if (!JavaModelManager.OptionNames.contains(key)) continue; // unrecognized option
2369 // no filtering for encoding (custom encoding for project is allowed)
2370 String value = (String)newOptions.get(key);
2371 preferences.setDefault(key, CUSTOM_DEFAULT_OPTION_VALUE); // empty string isn't the default (26251)
2372 preferences.setValue(key, value);
2377 savePreferences(preferences);
2383 public void setOutputLocation(IPath path, IProgressMonitor monitor)
2384 throws JavaModelException {
2387 throw new IllegalArgumentException(Util.bind("path.nullpath")); //$NON-NLS-1$
2389 if (path.equals(getOutputLocation())) {
2392 this.setRawClasspath(SetClasspathOperation.ReuseClasspath, path, monitor);
2396 * Set cached preferences, no preference file is saved, only info is updated
2398 public void setPreferences(Preferences preferences) {
2399 IProject project = getProject();
2400 if (!JavaProject.hasJavaNature(project)) return; // ignore
2401 JavaModelManager.PerProjectInfo perProjectInfo = JavaModelManager.getJavaModelManager().getPerProjectInfo(project, true);
2402 perProjectInfo.preferences = preferences;
2406 * Sets the underlying kernel project of this Java project,
2407 * and fills in its parent and name.
2408 * Called by IProject.getNature().
2410 * @see IProjectNature#setProject
2412 public void setProject(IProject project) {
2415 parent = JavaModelManager.getJavaModelManager().getJavaModel();
2416 name = project.getName();
2422 public void setRawClasspath(
2423 IClasspathEntry[] entries,
2424 IPath outputLocation,
2425 IProgressMonitor monitor)
2426 throws JavaModelException {
2432 true, // canChangeResource (as per API contract)
2433 getResolvedClasspath(true), // ignoreUnresolvedVariable
2434 true, // needValidation
2435 true); // need to save
2438 public void setRawClasspath(
2439 IClasspathEntry[] newEntries,
2440 IPath newOutputLocation,
2441 IProgressMonitor monitor,
2442 boolean canChangeResource,
2443 IClasspathEntry[] oldResolvedPath,
2444 boolean needValidation,
2446 throws JavaModelException {
2448 JavaModelManager manager =
2449 (JavaModelManager) JavaModelManager.getJavaModelManager();
2451 IClasspathEntry[] newRawPath = newEntries;
2452 if (newRawPath == null) { //are we already with the default classpath
2453 newRawPath = defaultClasspath();
2455 SetClasspathOperation op =
2456 new SetClasspathOperation(
2464 runOperation(op, monitor);
2466 } catch (JavaModelException e) {
2475 public void setRawClasspath(
2476 IClasspathEntry[] entries,
2477 IProgressMonitor monitor)
2478 throws JavaModelException {
2482 SetClasspathOperation.ReuseOutputLocation,
2484 true, // canChangeResource (as per API contract)
2485 getResolvedClasspath(true), // ignoreUnresolvedVariable
2486 true, // needValidation
2487 true); // need to save
2491 * NOTE: <code>null</code> specifies default classpath, and an empty
2492 * array specifies an empty classpath.
2494 * @exception NotPresentException if this project does not exist.
2496 protected void setRawClasspath0(IClasspathEntry[] rawEntries)
2497 throws JavaModelException {
2499 JavaModelManager.PerProjectInfo info = JavaModelManager.getJavaModelManager().getPerProjectInfoCheckExistence(project);
2501 synchronized (info) {
2502 if (rawEntries != null) {
2503 info.classpath = rawEntries;
2506 // clear cache of resolved classpath
2507 info.lastResolvedClasspath = null;
2508 info.resolvedPathToRawEntries = null;
2513 * Record a shared persistent property onto a project.
2514 * Note that it is orthogonal to IResource persistent properties, and client code has to decide
2515 * which form of storage to use appropriately. Shared properties produce real resource files which
2516 * can be shared through a VCM onto a server. Persistent properties are not shareable.
2518 * shared properties end up in resource files, and thus cannot be modified during
2519 * delta notifications (a CoreException would then be thrown).
2521 * @see JavaProject#getSharedProperty(String key)
2523 public void setSharedProperty(String key, String value) throws CoreException {
2525 IFile rscFile = getProject().getFile(key);
2526 InputStream inputStream = new ByteArrayInputStream(value.getBytes());
2527 // update the resource content
2528 if (rscFile.exists()) {
2529 if (rscFile.isReadOnly()) {
2530 // provide opportunity to checkout read-only .classpath file (23984)
2531 ResourcesPlugin.getWorkspace().validateEdit(new IFile[]{rscFile}, null);
2533 rscFile.setContents(inputStream, IResource.FORCE, null);
2535 rscFile.create(inputStream, IResource.FORCE, null);
2540 * Update cycle markers for all java projects
2542 public static void updateAllCycleMarkers() throws JavaModelException {
2544 //long start = System.currentTimeMillis();
2546 JavaModelManager manager = JavaModelManager.getJavaModelManager();
2547 IJavaProject[] projects = manager.getJavaModel().getJavaProjects();
2548 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
2550 HashSet cycleParticipants = new HashSet();
2551 HashSet traversed = new HashSet();
2552 int length = projects.length;
2554 // compute cycle participants
2555 ArrayList prereqChain = new ArrayList();
2556 for (int i = 0; i < length; i++){
2557 JavaProject project = (JavaProject)projects[i];
2558 if (!traversed.contains(project.getPath())){
2559 prereqChain.clear();
2560 project.updateCycleParticipants(null, prereqChain, cycleParticipants, workspaceRoot, traversed);
2563 //System.out.println("updateAllCycleMarkers: " + (System.currentTimeMillis() - start) + " ms");
2565 for (int i = 0; i < length; i++){
2566 JavaProject project = (JavaProject)projects[i];
2568 if (cycleParticipants.contains(project.getPath())){
2569 IMarker cycleMarker = project.getCycleMarker();
2570 String circularCPOption = project.getOption(JavaCore.CORE_CIRCULAR_CLASSPATH, true);
2571 int circularCPSeverity = JavaCore.ERROR.equals(circularCPOption) ? IMarker.SEVERITY_ERROR : IMarker.SEVERITY_WARNING;
2572 if (cycleMarker != null) {
2573 // update existing cycle marker if needed
2575 int existingSeverity = ((Integer)cycleMarker.getAttribute(IMarker.SEVERITY)).intValue();
2576 if (existingSeverity != circularCPSeverity) {
2577 cycleMarker.setAttribute(IMarker.SEVERITY, circularCPSeverity);
2579 } catch (CoreException e) {
2580 throw new JavaModelException(e);
2583 // create new marker
2584 project.createClasspathProblemMarker(
2585 new JavaModelStatus(IJavaModelStatusConstants.CLASSPATH_CYCLE, project));
2588 project.flushClasspathProblemMarkers(true, false);
2594 * If a cycle is detected, then cycleParticipants contains all the paths of projects involved in this cycle (directly and indirectly),
2595 * no cycle if the set is empty (and started empty)
2597 public void updateCycleParticipants(
2598 IClasspathEntry[] preferredClasspath,
2599 ArrayList prereqChain,
2600 HashSet cycleParticipants,
2601 IWorkspaceRoot workspaceRoot,
2604 IPath path = this.getPath();
2605 prereqChain.add(path);
2606 traversed.add(path);
2608 IClasspathEntry[] classpath = preferredClasspath == null ? getResolvedClasspath(true) : preferredClasspath;
2609 for (int i = 0, length = classpath.length; i < length; i++) {
2610 IClasspathEntry entry = classpath[i];
2612 if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT){
2613 IPath prereqProjectPath = entry.getPath();
2614 int index = cycleParticipants.contains(prereqProjectPath) ? 0 : prereqChain.indexOf(prereqProjectPath);
2615 if (index >= 0) { // refer to cycle, or in cycle itself
2616 for (int size = prereqChain.size(); index < size; index++) {
2617 cycleParticipants.add(prereqChain.get(index));
2620 if (!traversed.contains(prereqProjectPath)) {
2621 IResource member = workspaceRoot.findMember(prereqProjectPath);
2622 if (member != null && member.getType() == IResource.PROJECT){
2623 JavaProject project = (JavaProject)JavaCore.create((IProject)member);
2624 project.updateCycleParticipants(null, prereqChain, cycleParticipants, workspaceRoot, traversed);
2630 } catch(JavaModelException e){
2632 prereqChain.remove(path);
2635 * Reset the collection of package fragment roots (local ones) - only if opened.
2636 * Need to check *all* package fragment roots in order to reset NameLookup
2638 public void updatePackageFragmentRoots(){
2640 if (this.isOpen()) {
2642 JavaProjectElementInfo info = getJavaProjectElementInfo();
2644 IClasspathEntry[] classpath = getResolvedClasspath(true);
2645 // NameLookup lookup = info.getNameLookup();
2646 // if (lookup != null){
2647 // IPackageFragmentRoot[] oldRoots = lookup.fPackageFragmentRoots;
2648 // IPackageFragmentRoot[] newRoots = computePackageFragmentRoots(classpath, true);
2649 // checkIdentical: { // compare all pkg fragment root lists
2650 // if (oldRoots.length == newRoots.length){
2651 // for (int i = 0, length = oldRoots.length; i < length; i++){
2652 // if (!oldRoots[i].equals(newRoots[i])){
2653 // break checkIdentical;
2656 // return; // no need to update
2659 // info.setNameLookup(null); // discard name lookup (hold onto roots)
2661 info.setNonJavaResources(null);
2663 computePackageFragmentRoots(classpath, false));
2665 } catch(JavaModelException e){
2667 close(); // could not do better
2668 } catch(JavaModelException ex){
2674 public void removeLoadPathEntry(IProject anotherPHPProject) {
2675 Iterator entries = getLoadPathEntries().iterator();
2676 while (entries.hasNext()) {
2677 LoadPathEntry entry = (LoadPathEntry) entries.next();
2678 if (entry.getType() == LoadPathEntry.TYPE_PROJECT && entry.getProject().getName().equals(anotherPHPProject.getName())) {
2679 getLoadPathEntries().remove(entry);
2686 public void save() throws CoreException {
2688 InputStream xmlPath = new ByteArrayInputStream(getLoadPathXML().getBytes());
2689 IFile loadPathsFile = getLoadPathEntriesFile();
2690 if (!loadPathsFile.exists())
2691 loadPathsFile.create(xmlPath, true, null);
2693 loadPathsFile.setContents(xmlPath, true, false, null);