X-Git-Url: http://secure.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/core/JavaElement.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/core/JavaElement.java index c45befb..a55c97a 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/core/JavaElement.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/core/JavaElement.java @@ -18,8 +18,18 @@ import net.sourceforge.phpdt.core.IJavaModel; import net.sourceforge.phpdt.core.IJavaModelStatusConstants; import net.sourceforge.phpdt.core.IJavaProject; import net.sourceforge.phpdt.core.IOpenable; +import net.sourceforge.phpdt.core.IParent; +import net.sourceforge.phpdt.core.ISourceRange; +import net.sourceforge.phpdt.core.ISourceReference; import net.sourceforge.phpdt.core.JavaModelException; +import net.sourceforge.phpdt.core.jdom.IDOMCompilationUnit; +import net.sourceforge.phpdt.core.jdom.IDOMNode; +import net.sourceforge.phpdt.internal.corext.Assert; +import org.eclipse.core.resources.IResourceStatus; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.PlatformObject; @@ -94,34 +104,34 @@ public abstract class JavaElement extends PlatformObject implements IJavaElement /** * @see IOpenable */ -// public void close() throws JavaModelException { -// Object info = JavaModelManager.getJavaModelManager().peekAtInfo(this); -// if (info != null) { -// boolean wasVerbose = false; -// try { -// if (JavaModelManager.VERBOSE) { -// System.out.println("CLOSING Element ("+ Thread.currentThread()+"): " + this.toStringWithAncestors()); //$NON-NLS-1$//$NON-NLS-2$ -// wasVerbose = true; -// JavaModelManager.VERBOSE = false; -// } -// if (this instanceof IParent) { -// IJavaElement[] children = ((JavaElementInfo) info).getChildren(); -// for (int i = 0, size = children.length; i < size; ++i) { -// JavaElement child = (JavaElement) children[i]; -// child.close(); -// } -// } -// closing(info); -// JavaModelManager.getJavaModelManager().removeInfo(this); -// if (wasVerbose) { -// System.out.println("-> Package cache size = " + JavaModelManager.getJavaModelManager().cache.pkgSize()); //$NON-NLS-1$ -// System.out.println("-> Openable cache filling ratio = " + JavaModelManager.getJavaModelManager().cache.openableFillingRatio() + "%"); //$NON-NLS-1$//$NON-NLS-2$ -// } -// } finally { -// JavaModelManager.VERBOSE = wasVerbose; -// } -// } -// } + public void close() throws JavaModelException { + Object info = JavaModelManager.getJavaModelManager().peekAtInfo(this); + if (info != null) { + boolean wasVerbose = false; + try { + if (JavaModelManager.VERBOSE) { + System.out.println("CLOSING Element ("+ Thread.currentThread()+"): " + this.toStringWithAncestors()); //$NON-NLS-1$//$NON-NLS-2$ + wasVerbose = true; + JavaModelManager.VERBOSE = false; + } + if (this instanceof IParent) { + IJavaElement[] children = ((JavaElementInfo) info).getChildren(); + for (int i = 0, size = children.length; i < size; ++i) { + JavaElement child = (JavaElement) children[i]; + child.close(); + } + } + closing(info); + JavaModelManager.getJavaModelManager().removeInfo(this); + if (wasVerbose) { + System.out.println("-> Package cache size = " + JavaModelManager.getJavaModelManager().cache.pkgSize()); //$NON-NLS-1$ + System.out.println("-> Openable cache filling ratio = " + JavaModelManager.getJavaModelManager().cache.openableFillingRatio() + "%"); //$NON-NLS-1$//$NON-NLS-2$ + } + } finally { + JavaModelManager.VERBOSE = wasVerbose; + } + } + } /** * This element is being closed. Do any necessary cleanup. */ @@ -159,9 +169,9 @@ public abstract class JavaElement extends PlatformObject implements IJavaElement * Returns true if this JavaElement is equivalent to the given * IDOMNode. */ -// protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException { -// return false; -// } + protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException { + return false; + } /** * @see IJavaElement */ @@ -179,66 +189,66 @@ public abstract class JavaElement extends PlatformObject implements IJavaElement * Returns the IDOMNode that corresponds to this JavaElement * or null if there is no corresponding node. */ -// public IDOMNode findNode(IDOMCompilationUnit dom) { -// int type = getElementType(); -// if (type == IJavaElement.COMPILATION_UNIT || -// type == IJavaElement.FIELD || -// type == IJavaElement.IMPORT_DECLARATION || -// type == IJavaElement.INITIALIZER || -// type == IJavaElement.METHOD || -// type == IJavaElement.PACKAGE_DECLARATION || -// type == IJavaElement.TYPE) { -// ArrayList path = new ArrayList(); -// IJavaElement element = this; -// while (element != null && element.getElementType() != IJavaElement.COMPILATION_UNIT) { -// if (element.getElementType() != IJavaElement.IMPORT_CONTAINER) { -// // the DOM does not have import containers, so skip them -// path.add(0, element); -// } -// element = element.getParent(); -// } -// if (path.size() == 0) { -// try { -// if (equalsDOMNode(dom)) { -// return dom; -// } else { -// return null; -// } -// } catch(JavaModelException e) { -// return null; -// } -// } -// return ((JavaElement) path.get(0)).followPath(path, 0, dom.getFirstChild()); -// } else { -// return null; -// } -// } -// /** -// */ -// protected IDOMNode followPath(ArrayList path, int position, IDOMNode node) { -// -// try { -// if (equalsDOMNode(node)) { -// if (position == (path.size() - 1)) { -// return node; -// } else { -// if (node.getFirstChild() != null) { -// position++; -// return ((JavaElement)path.get(position)).followPath(path, position, node.getFirstChild()); -// } else { -// return null; -// } -// } -// } else if (node.getNextNode() != null) { -// return followPath(path, position, node.getNextNode()); -// } else { -// return null; -// } -// } catch (JavaModelException e) { -// return null; -// } -// -// } + public IDOMNode findNode(IDOMCompilationUnit dom) { + int type = getElementType(); + if (type == IJavaElement.COMPILATION_UNIT || + type == IJavaElement.FIELD || + type == IJavaElement.IMPORT_DECLARATION || + type == IJavaElement.INITIALIZER || + type == IJavaElement.METHOD || + type == IJavaElement.PACKAGE_DECLARATION || + type == IJavaElement.TYPE) { + ArrayList path = new ArrayList(); + IJavaElement element = this; + while (element != null && element.getElementType() != IJavaElement.COMPILATION_UNIT) { + if (element.getElementType() != IJavaElement.IMPORT_CONTAINER) { + // the DOM does not have import containers, so skip them + path.add(0, element); + } + element = element.getParent(); + } + if (path.size() == 0) { + try { + if (equalsDOMNode(dom)) { + return dom; + } else { + return null; + } + } catch(JavaModelException e) { + return null; + } + } + return ((JavaElement) path.get(0)).followPath(path, 0, dom.getFirstChild()); + } else { + return null; + } + } + /** + */ + protected IDOMNode followPath(ArrayList path, int position, IDOMNode node) { + + try { + if (equalsDOMNode(node)) { + if (position == (path.size() - 1)) { + return node; + } else { + if (node.getFirstChild() != null) { + position++; + return ((JavaElement)path.get(position)).followPath(path, position, node.getFirstChild()); + } else { + return null; + } + } + } else if (node.getNextNode() != null) { + return followPath(path, position, node.getNextNode()); + } else { + return null; + } + } catch (JavaModelException e) { + return null; + } + + } /** * @see IJavaElement */ @@ -295,32 +305,31 @@ public abstract class JavaElement extends PlatformObject implements IJavaElement * @exception JavaModelException if the element is not present or not accessible */ public Object getElementInfo() throws JavaModelException { -return null; // workaround to ensure parent project resolved classpath is available to avoid triggering initializers // while the JavaModelManager lock is acquired (can cause deadlocks in clients) -// IJavaProject project = getJavaProject(); -// if (project != null && !project.isOpen()) { -// // TODO: need to revisit, since deadlock could still occur if perProjectInfo is removed concurrent before entering the lock -// try { -// project.getResolvedClasspath(true); // trigger all possible container/variable initialization outside the model lock -// } catch (JavaModelException e) { -// // project is not accessible or is not a java project -// } -// } -// -// // element info creation is done inside a lock on the JavaModelManager -// JavaModelManager manager; -// synchronized(manager = JavaModelManager.getJavaModelManager()){ -// Object info = manager.getInfo(this); -// if (info == null) { -// openHierarchy(); -// info= manager.getInfo(this); -// if (info == null) { -// throw newNotPresentException(); -// } -// } -// return info; -// } + IJavaProject project = getJavaProject(); + if (project != null && !project.isOpen()) { + // TODO: need to revisit, since deadlock could still occur if perProjectInfo is removed concurrent before entering the lock + try { + project.getResolvedClasspath(true); // trigger all possible container/variable initialization outside the model lock + } catch (JavaModelException e) { + // project is not accessible or is not a java project + } + } + + // element info creation is done inside a lock on the JavaModelManager + JavaModelManager manager; + synchronized(manager = JavaModelManager.getJavaModelManager()){ + Object info = manager.getInfo(this); + if (info == null) { + openHierarchy(); + info= manager.getInfo(this); + if (info == null) { + throw newNotPresentException(); + } + } + return info; + } } /** * @see IAdaptable @@ -411,30 +420,30 @@ return null; * known to be within this element's source range already, and if no finer * grained element is found at the position, this element is returned. */ -// protected IJavaElement getSourceElementAt(int position) throws JavaModelException { -// if (this instanceof ISourceReference) { -// IJavaElement[] children = getChildren(); -// int i; -// for (i = 0; i < children.length; i++) { -// IJavaElement aChild = children[i]; -// if (aChild instanceof SourceRefElement) { -// SourceRefElement child = (SourceRefElement) children[i]; -// ISourceRange range = child.getSourceRange(); -// if (position < range.getOffset() + range.getLength() && position >= range.getOffset()) { -// if (child instanceof IParent) { -// return child.getSourceElementAt(position); -// } else { -// return child; -// } -// } -// } -// } -// } else { -// // should not happen -// Assert.isTrue(false); -// } -// return this; -// } + protected IJavaElement getSourceElementAt(int position) throws JavaModelException { + if (this instanceof ISourceReference) { + IJavaElement[] children = getChildren(); + int i; + for (i = 0; i < children.length; i++) { + IJavaElement aChild = children[i]; + if (aChild instanceof SourceRefElement) { + SourceRefElement child = (SourceRefElement) children[i]; + ISourceRange range = child.getSourceRange(); + if (position < range.getOffset() + range.getLength() && position >= range.getOffset()) { + if (child instanceof IParent) { + return child.getSourceElementAt(position); + } else { + return child; + } + } + } + } + } else { + // should not happen + Assert.isTrue(false); + } + return this; + } /** * Returns the SourceMapper facility for this element, or * null if this element does not have a @@ -475,35 +484,35 @@ return null; /** * @see IJavaElement */ -// public boolean isStructureKnown() throws JavaModelException { -// return ((JavaElementInfo)getElementInfo()).isStructureKnown(); -// } + public boolean isStructureKnown() throws JavaModelException { + return ((JavaElementInfo)getElementInfo()).isStructureKnown(); + } /** * Creates and returns and not present exception for this element. */ protected JavaModelException newNotPresentException() { return new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, this)); } -// /** -// * Opens this element and all parents that are not already open. -// * -// * @exception JavaModelException this element is not present or accessible -// */ -// protected void openHierarchy() throws JavaModelException { -// if (this instanceof IOpenable) { -// ((Openable) this).openWhenClosed(null); -// } else { -// Openable openableParent = (Openable)getOpenableParent(); -// if (openableParent != null) { -// JavaElementInfo openableParentInfo = (JavaElementInfo) JavaModelManager.getJavaModelManager().getInfo((IJavaElement) openableParent); -// if (openableParentInfo == null) { -// openableParent.openWhenClosed(null); -// } else { -// throw newNotPresentException(); -// } -// } -// } -// } + /** + * Opens this element and all parents that are not already open. + * + * @exception JavaModelException this element is not present or accessible + */ + protected void openHierarchy() throws JavaModelException { + if (this instanceof IOpenable) { + ((Openable) this).openWhenClosed(null); + } else { + Openable openableParent = (Openable)getOpenableParent(); + if (openableParent != null) { + JavaElementInfo openableParentInfo = (JavaElementInfo) JavaModelManager.getJavaModelManager().getInfo((IJavaElement) openableParent); + if (openableParentInfo == null) { + openableParent.openWhenClosed(null); + } else { + throw newNotPresentException(); + } + } + } + } /** * This element has just been opened. Do any necessary setup. */ @@ -518,48 +527,48 @@ return null; * Removes all cached info from the Java Model, including all children, * but does not close this element. */ -// protected void removeInfo() { -// Object info = JavaModelManager.getJavaModelManager().peekAtInfo(this); -// if (info != null) { -// if (this instanceof IParent) { -// IJavaElement[] children = ((JavaElementInfo)info).getChildren(); -// for (int i = 0, size = children.length; i < size; ++i) { -// JavaElement child = (JavaElement) children[i]; -// child.removeInfo(); -// } -// } -// JavaModelManager.getJavaModelManager().removeInfo(this); -// } -// } -// /** -// * Returns a copy of this element rooted at the given project. -// */ + protected void removeInfo() { + Object info = JavaModelManager.getJavaModelManager().peekAtInfo(this); + if (info != null) { + if (this instanceof IParent) { + IJavaElement[] children = ((JavaElementInfo)info).getChildren(); + for (int i = 0, size = children.length; i < size; ++i) { + JavaElement child = (JavaElement) children[i]; + child.removeInfo(); + } + } + JavaModelManager.getJavaModelManager().removeInfo(this); + } + } + /** + * Returns a copy of this element rooted at the given project. + */ public abstract IJavaElement rootedAt(IJavaProject project); -// /** -// * Runs a Java Model Operation -// */ -// public static void runOperation(JavaModelOperation operation, IProgressMonitor monitor) throws JavaModelException { -// try { -// if (operation.isReadOnly() || ResourcesPlugin.getWorkspace().isTreeLocked()) { -// operation.run(monitor); -// } else { -// // use IWorkspace.run(...) to ensure that a build will be done in autobuild mode -// ResourcesPlugin.getWorkspace().run(operation, monitor); -// } -// } catch (CoreException ce) { -// if (ce instanceof JavaModelException) { -// throw (JavaModelException)ce; -// } else { -// if (ce.getStatus().getCode() == IResourceStatus.OPERATION_FAILED) { -// Throwable e= ce.getStatus().getException(); -// if (e instanceof JavaModelException) { -// throw (JavaModelException) e; -// } -// } -// throw new JavaModelException(ce); -// } -// } -// } + /** + * Runs a Java Model Operation + */ + public static void runOperation(JavaModelOperation operation, IProgressMonitor monitor) throws JavaModelException { + try { + if (operation.isReadOnly() || ResourcesPlugin.getWorkspace().isTreeLocked()) { + operation.run(monitor); + } else { + // use IWorkspace.run(...) to ensure that a build will be done in autobuild mode + ResourcesPlugin.getWorkspace().run(operation, monitor); + } + } catch (CoreException ce) { + if (ce instanceof JavaModelException) { + throw (JavaModelException)ce; + } else { + if (ce.getStatus().getCode() == IResourceStatus.OPERATION_FAILED) { + Throwable e= ce.getStatus().getException(); + if (e instanceof JavaModelException) { + throw (JavaModelException) e; + } + } + throw new JavaModelException(ce); + } + } + } /** * Sets the occurrence count of the handle. */