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.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Iterator;
17 import net.sourceforge.phpdt.core.ICompilationUnit;
18 import net.sourceforge.phpdt.core.IJavaElement;
19 import net.sourceforge.phpdt.core.IJavaModel;
20 import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
21 import net.sourceforge.phpdt.core.IJavaProject;
22 import net.sourceforge.phpdt.core.IMember;
23 import net.sourceforge.phpdt.core.IOpenable;
24 import net.sourceforge.phpdt.core.IParent;
25 import net.sourceforge.phpdt.core.ISourceRange;
26 import net.sourceforge.phpdt.core.ISourceReference;
27 import net.sourceforge.phpdt.core.JavaModelException;
28 import net.sourceforge.phpdt.core.WorkingCopyOwner;
29 import net.sourceforge.phpdt.core.jdom.IDOMCompilationUnit;
30 import net.sourceforge.phpdt.core.jdom.IDOMNode;
31 import net.sourceforge.phpdt.internal.core.util.MementoTokenizer;
32 import net.sourceforge.phpdt.internal.core.util.Util;
33 import net.sourceforge.phpdt.internal.corext.Assert;
35 import org.eclipse.core.resources.IResource;
36 import org.eclipse.core.resources.IResourceStatus;
37 import org.eclipse.core.resources.ResourcesPlugin;
38 import org.eclipse.core.runtime.CoreException;
39 import org.eclipse.core.runtime.IAdaptable;
40 import org.eclipse.core.runtime.IPath;
41 import org.eclipse.core.runtime.IProgressMonitor;
42 import org.eclipse.core.runtime.Path;
43 import org.eclipse.core.runtime.PlatformObject;
44 import org.eclipse.core.runtime.jobs.ISchedulingRule;
47 * Root of Java element handle hierarchy.
51 public abstract class JavaElement extends PlatformObject implements
53 public static final char JEM_ESCAPE = '\\';
55 public static final char JEM_JAVAPROJECT = '=';
57 public static final char JEM_PACKAGEFRAGMENTROOT = Path.SEPARATOR;
59 public static final char JEM_PACKAGEFRAGMENT = '<';
61 public static final char JEM_FIELD = '^';
63 public static final char JEM_METHOD = '~';
65 public static final char JEM_INITIALIZER = '|';
67 public static final char JEM_COMPILATIONUNIT = '{';
69 // public static final char JEM_CLASSFILE = '(';
70 public static final char JEM_TYPE = '[';
72 public static final char JEM_PACKAGEDECLARATION = '%';
74 public static final char JEM_IMPORTDECLARATION = '#';
76 public static final char JEM_COUNT = '!';
78 public static final char JEM_LOCALVARIABLE = '@';
81 * A count to uniquely identify this element in the case that a duplicate
82 * named element exists. For example, if there are two fields in a
83 * compilation unit with the same name, the occurrence count is used to
84 * distinguish them. The occurrence count starts at 1 (thus the first
85 * occurrence is occurrence 1, not occurrence 0).
87 protected int occurrenceCount = 1;
90 * This element's type - one of the constants defined in
91 * IJavaLanguageElementTypes.
93 // protected int fLEType = 0;
95 * This element's parent, or <code>null</code> if this element does not
98 protected JavaElement parent;
101 * This element's name, or an empty <code>String</code> if this element
102 * does not have a name.
104 protected String name;
106 protected static final Object NO_INFO = new Object();
109 * Constructs a handle for a java element with the given parent element and
113 * The parent of java element
115 * The name of java element
117 * @exception IllegalArgumentException
118 * if the type is not one of the valid Java element type
122 protected JavaElement(JavaElement parent, String name)
123 throws IllegalArgumentException {
124 this.parent = parent;
131 public void close() throws JavaModelException {
132 JavaModelManager.getJavaModelManager().removeInfoAndChildren(this);
136 * This element is being closed. Do any necessary cleanup.
138 protected abstract void closing(Object info) throws JavaModelException;
141 * Returns a new element info for this element.
143 protected abstract Object createElementInfo();
146 * Returns true if this handle represents the same Java element as the given
147 * handle. By default, two handles represent the same element if they are
148 * identical or if they represent the same type of element, have equal
149 * names, parents, and occurrence counts.
152 * If a subclass has other requirements for equality, this method must be
157 public boolean equals(Object o) {
162 // Java model parent is null
163 if (this.parent == null)
164 return super.equals(o);
165 if (o instanceof JavaElement) {
166 // assume instanceof check is done in subclass
167 JavaElement other = (JavaElement) o;
168 return this.occurrenceCount == other.occurrenceCount
169 && this.name.equals(other.name)
170 && this.parent.equals(other.parent);
176 * Returns true if this <code>JavaElement</code> is equivalent to the
177 * given <code>IDOMNode</code>.
179 protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException {
183 protected void escapeMementoName(StringBuffer buffer, String mementoName) {
184 for (int i = 0, length = mementoName.length(); i < length; i++) {
185 char character = mementoName.charAt(i);
189 case JEM_JAVAPROJECT:
190 case JEM_PACKAGEFRAGMENTROOT:
191 case JEM_PACKAGEFRAGMENT:
194 case JEM_INITIALIZER:
195 case JEM_COMPILATIONUNIT:
196 // case JEM_CLASSFILE:
198 case JEM_PACKAGEDECLARATION:
199 case JEM_IMPORTDECLARATION:
200 case JEM_LOCALVARIABLE:
201 buffer.append(JEM_ESCAPE);
203 buffer.append(character);
210 public boolean exists() {
215 } catch (JavaModelException e) {
221 * Returns the <code>IDOMNode</code> that corresponds to this
222 * <code>JavaElement</code> or <code>null</code> if there is no
223 * corresponding node.
225 public IDOMNode findNode(IDOMCompilationUnit dom) {
226 int type = getElementType();
227 if (type == IJavaElement.COMPILATION_UNIT || type == IJavaElement.FIELD
228 || type == IJavaElement.IMPORT_DECLARATION
229 || type == IJavaElement.INITIALIZER
230 || type == IJavaElement.METHOD
231 || type == IJavaElement.PACKAGE_DECLARATION
232 || type == IJavaElement.TYPE) {
233 ArrayList path = new ArrayList();
234 IJavaElement element = this;
235 while (element != null
236 && element.getElementType() != IJavaElement.COMPILATION_UNIT) {
237 if (element.getElementType() != IJavaElement.IMPORT_CONTAINER) {
238 // the DOM does not have import containers, so skip them
239 path.add(0, element);
241 element = element.getParent();
243 if (path.size() == 0) {
245 if (equalsDOMNode(dom)) {
250 } catch (JavaModelException e) {
254 return ((JavaElement) path.get(0)).followPath(path, 0, dom
263 protected IDOMNode followPath(ArrayList path, int position, IDOMNode node) {
266 if (equalsDOMNode(node)) {
267 if (position == (path.size() - 1)) {
270 if (node.getFirstChild() != null) {
272 return ((JavaElement) path.get(position)).followPath(
273 path, position, node.getFirstChild());
278 } else if (node.getNextNode() != null) {
279 return followPath(path, position, node.getNextNode());
283 } catch (JavaModelException e) {
292 public IJavaElement getAncestor(int ancestorType) {
294 IJavaElement element = this;
295 while (element != null) {
296 if (element.getElementType() == ancestorType)
298 element = element.getParent();
304 * Generates the element infos for this element, its ancestors (if they are
305 * not opened) and its children (if it is an Openable). Puts the newly
306 * created element info in the given map.
308 protected abstract void generateInfos(Object info, HashMap newElements,
309 IProgressMonitor pm) throws JavaModelException;
314 public IJavaElement[] getChildren() throws JavaModelException {
315 return ((JavaElementInfo) getElementInfo()).getChildren();
319 * Returns a collection of (immediate) children of this node of the
323 * one of constants defined by IJavaLanguageElementTypes
325 public ArrayList getChildrenOfType(int type) throws JavaModelException {
326 IJavaElement[] children = getChildren();
327 int size = children.length;
328 ArrayList list = new ArrayList(size);
329 for (int i = 0; i < size; ++i) {
330 JavaElement elt = (JavaElement) children[i];
331 if (elt.getElementType() == type) {
341 // public IClassFile getClassFile() {
347 public ICompilationUnit getCompilationUnit() {
352 * Returns the info for this handle. If this element is not already open, it
353 * and all of its parents are opened. Does not return null. NOTE: BinaryType
354 * infos are NOT rooted under JavaElementInfo.
356 * @exception JavaModelException
357 * if the element is not present or not accessible
359 public Object getElementInfo() throws JavaModelException {
360 return getElementInfo(null);
364 * Returns the info for this handle. If this element is not already open, it
365 * and all of its parents are opened. Does not return null. NOTE: BinaryType
366 * infos are NOT rooted under JavaElementInfo.
368 * @exception JavaModelException
369 * if the element is not present or not accessible
371 public Object getElementInfo(IProgressMonitor monitor)
372 throws JavaModelException {
374 JavaModelManager manager = JavaModelManager.getJavaModelManager();
375 Object info = manager.getInfo(this);
378 return openWhenClosed(createElementInfo(), monitor);
384 public String getElementName() {
389 * Creates a Java element handle from the given memento. The given token is
390 * the current delimiter indicating the type of the next token(s). The given
391 * working copy owner is used only for compilation unit handles.
393 public abstract IJavaElement getHandleFromMemento(String token,
394 MementoTokenizer memento, WorkingCopyOwner owner);
397 * Creates a Java element handle from the given memento. The given working
398 * copy owner is used only for compilation unit handles.
400 public IJavaElement getHandleFromMemento(MementoTokenizer memento,
401 WorkingCopyOwner owner) {
402 if (!memento.hasMoreTokens())
404 String token = memento.nextToken();
405 return getHandleFromMemento(token, memento, owner);
409 * Update the occurence count of the receiver and creates a Java element
410 * handle from the given memento. The given working copy owner is used only
411 * for compilation unit handles.
413 public IJavaElement getHandleUpdatingCountFromMemento(
414 MementoTokenizer memento, WorkingCopyOwner owner) {
415 this.occurrenceCount = Integer.parseInt(memento.nextToken());
416 if (!memento.hasMoreTokens())
418 String token = memento.nextToken();
419 return getHandleFromMemento(token, memento, owner);
425 public String getHandleIdentifier() {
426 return getHandleMemento();
430 * @see JavaElement#getHandleMemento()
432 public String getHandleMemento() {
433 StringBuffer buff = new StringBuffer(((JavaElement) getParent())
434 .getHandleMemento());
435 buff.append(getHandleMementoDelimiter());
436 escapeMementoName(buff, getElementName());
437 if (this.occurrenceCount > 1) {
438 buff.append(JEM_COUNT);
439 buff.append(this.occurrenceCount);
441 return buff.toString();
445 * Returns the <code>char</code> that marks the start of this handles
446 * contribution to a memento.
450 * Returns the <code>char</code> that marks the start of this handles
451 * contribution to a memento.
453 protected abstract char getHandleMementoDelimiter();
458 public IJavaModel getJavaModel() {
459 IJavaElement current = this;
461 if (current instanceof IJavaModel)
462 return (IJavaModel) current;
463 } while ((current = current.getParent()) != null);
470 public IJavaProject getJavaProject() {
471 IJavaElement current = this;
473 if (current instanceof IJavaProject)
474 return (IJavaProject) current;
475 } while ((current = current.getParent()) != null);
480 * Returns the occurrence count of the handle.
482 protected int getOccurrenceCount() {
483 return occurrenceCount;
489 public IOpenable getOpenable() {
490 return this.getOpenableParent();
494 * Return the first instance of IOpenable in the parent hierarchy of this
498 * Subclasses that are not IOpenable's must override this method.
500 public IOpenable getOpenableParent() {
502 return (IOpenable) parent;
508 public IJavaElement getParent() {
513 * @see IJavaElement#getPrimaryElement()
515 public IJavaElement getPrimaryElement() {
516 return getPrimaryElement(true);
520 * Returns the primary element. If checkOwner, and the cu owner is primary,
521 * return this element.
523 public IJavaElement getPrimaryElement(boolean checkOwner) {
528 * Returns the element that is located at the given source position in this
529 * element. This is a helper method for
530 * <code>ICompilationUnit#getElementAt</code>, and only works on
531 * compilation units and types. The position given is known to be within
532 * this element's source range already, and if no finer grained element is
533 * found at the position, this element is returned.
535 protected IJavaElement getSourceElementAt(int position)
536 throws JavaModelException {
537 if (this instanceof ISourceReference) {
538 IJavaElement[] children = getChildren();
540 for (i = 0; i < children.length; i++) {
541 IJavaElement aChild = children[i];
543 if (aChild instanceof SourceRefElement) {
544 SourceRefElement child = (SourceRefElement) children[i];
545 ISourceRange range = child.getSourceRange();
546 // if (child.name.equals("stopObject")||range==null ||
547 // range.getOffset()<=0) {
548 // System.out.println(child.name);
550 if (position < range.getOffset() + range.getLength()
551 && position >= range.getOffset()) {
552 if (child instanceof IParent) {
553 return child.getSourceElementAt(position);
562 Assert.isTrue(false);
568 * Returns the SourceMapper facility for this element, or <code>null</code>
569 * if this element does not have a SourceMapper.
571 // public SourceMapper getSourceMapper() {
572 // return ((JavaElement)getParent()).getSourceMapper();
577 * @see net.sourceforge.phpdt.core.IJavaElement#getSchedulingRule()
579 public ISchedulingRule getSchedulingRule() {
580 IResource resource = getResource();
581 if (resource == null) {
582 class NoResourceSchedulingRule implements ISchedulingRule {
585 public NoResourceSchedulingRule(IPath path) {
589 public boolean contains(ISchedulingRule rule) {
590 if (rule instanceof NoResourceSchedulingRule) {
592 .isPrefixOf(((NoResourceSchedulingRule) rule).path);
598 public boolean isConflicting(ISchedulingRule rule) {
599 if (rule instanceof NoResourceSchedulingRule) {
600 IPath otherPath = ((NoResourceSchedulingRule) rule).path;
601 return this.path.isPrefixOf(otherPath)
602 || otherPath.isPrefixOf(this.path);
608 return new NoResourceSchedulingRule(getPath());
617 public boolean hasChildren() throws JavaModelException {
618 // if I am not open, return true to avoid opening (case of a Java
619 // project, a compilation unit or a class file).
620 // also see https://bugs.eclipse.org/bugs/show_bug.cgi?id=52474
621 Object elementInfo = JavaModelManager.getJavaModelManager().getInfo(
623 if (elementInfo instanceof JavaElementInfo) {
624 return ((JavaElementInfo) elementInfo).getChildren().length > 0;
631 * Returns the hash code for this Java element. By default, the hash code
632 * for an element is a combination of its name and parent's hash code.
633 * Elements with other requirements must override this method.
635 public int hashCode() {
636 if (this.parent == null)
637 return super.hashCode();
638 return Util.combineHashCodes(this.name.hashCode(), this.parent
643 * Returns true if this element is an ancestor of the given element,
646 public boolean isAncestorOf(IJavaElement e) {
647 IJavaElement parentElement = e.getParent();
648 while (parentElement != null && !parentElement.equals(this)) {
649 parentElement = parentElement.getParent();
651 return parentElement != null;
657 public boolean isReadOnly() {
664 public boolean isStructureKnown() throws JavaModelException {
665 return ((JavaElementInfo) getElementInfo()).isStructureKnown();
669 * Creates and returns and not present exception for this element.
671 protected JavaModelException newNotPresentException() {
672 return new JavaModelException(new JavaModelStatus(
673 IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, this));
677 * Opens this element and all parents that are not already open.
679 * @exception JavaModelException
680 * this element is not present or accessible
682 // protected void openHierarchy() throws JavaModelException {
683 // if (this instanceof IOpenable) {
684 // ((Openable) this).openWhenClosed(null);
686 // Openable openableParent = (Openable)getOpenableParent();
687 // if (openableParent != null) {
688 // JavaElementInfo openableParentInfo = (JavaElementInfo)
689 // JavaModelManager.getJavaModelManager().getInfo((IJavaElement)
691 // if (openableParentInfo == null) {
692 // openableParent.openWhenClosed(null);
694 // throw newNotPresentException();
700 * This element has just been opened. Do any necessary setup.
702 protected void opening(Object info) {
706 * Opens an <code> Openable </code> that is known to be closed (no check for
707 * <code> isOpen() </code> ). Returns the created element info.
709 protected Object openWhenClosed(Object info, IProgressMonitor monitor)
710 throws JavaModelException {
711 JavaModelManager manager = JavaModelManager.getJavaModelManager();
712 boolean hadTemporaryCache = manager.hasTemporaryCache();
714 HashMap newElements = manager.getTemporaryCache();
715 generateInfos(info, newElements, monitor);
717 info = newElements.get(this);
719 if (info == null) { // a source ref element could not be opened
720 // close any buffer that was opened for the openable parent
721 Iterator iterator = newElements.keySet().iterator();
722 while (iterator.hasNext()) {
723 IJavaElement element = (IJavaElement) iterator.next();
724 if (element instanceof Openable) {
725 ((Openable) element).closeBuffer();
728 throw newNotPresentException();
730 if (!hadTemporaryCache) {
731 manager.putInfos(this, newElements);
734 if (!hadTemporaryCache) {
735 manager.resetTemporaryCache();
743 public String readableName() {
744 return this.getElementName();
748 * Removes all cached info from the Java Model, including all children, but
749 * does not close this element.
751 // protected void removeInfo() {
752 // Object info = JavaModelManager.getJavaModelManager().peekAtInfo(this);
753 // if (info != null) {
754 // if (this instanceof IParent) {
755 // IJavaElement[] children = ((JavaElementInfo)info).getChildren();
756 // for (int i = 0, size = children.length; i < size; ++i) {
757 // JavaElement child = (JavaElement) children[i];
758 // child.removeInfo();
761 // JavaModelManager.getJavaModelManager().removeInfo(this);
765 // * Returns a copy of this element rooted at the given project.
767 // public abstract IJavaElement rootedAt(IJavaProject project);
769 * Runs a Java Model Operation
771 public static void runOperation(JavaModelOperation operation,
772 IProgressMonitor monitor) throws JavaModelException {
774 if (operation.isReadOnly()
775 || ResourcesPlugin.getWorkspace().isTreeLocked()) {
776 operation.run(monitor);
778 // use IWorkspace.run(...) to ensure that a build will be done
780 ResourcesPlugin.getWorkspace().run(operation, monitor);
782 } catch (CoreException ce) {
783 if (ce instanceof JavaModelException) {
784 throw (JavaModelException) ce;
786 if (ce.getStatus().getCode() == IResourceStatus.OPERATION_FAILED) {
787 Throwable e = ce.getStatus().getException();
788 if (e instanceof JavaModelException) {
789 throw (JavaModelException) e;
792 throw new JavaModelException(ce);
798 * Sets the occurrence count of the handle.
800 protected void setOccurrenceCount(int count) {
801 occurrenceCount = count;
804 protected String tabString(int tab) {
805 StringBuffer buffer = new StringBuffer();
806 for (int i = tab; i > 0; i--)
807 buffer.append(" "); //$NON-NLS-1$
808 return buffer.toString();
814 public String toDebugString() {
815 StringBuffer buffer = new StringBuffer();
816 this.toStringInfo(0, buffer, NO_INFO);
817 return buffer.toString();
823 public String toString() {
824 StringBuffer buffer = new StringBuffer();
826 return buffer.toString();
832 protected void toStringName(StringBuffer buffer) {
833 buffer.append(getElementName());
834 if (this.occurrenceCount > 1) {
835 buffer.append("#"); //$NON-NLS-1$
836 buffer.append(this.occurrenceCount);
843 protected void toString(int tab, StringBuffer buffer) {
844 // Object info = this.toStringInfo(tab, buffer);
847 this.toStringAncestors(buffer);
849 this.toStringChildren(tab, buffer, info);
855 public String toStringWithAncestors() {
856 StringBuffer buffer = new StringBuffer();
857 this.toStringInfo(0, buffer, NO_INFO);
858 this.toStringAncestors(buffer);
859 return buffer.toString();
865 protected void toStringAncestors(StringBuffer buffer) {
866 JavaElement parent = (JavaElement) this.getParent();
867 if (parent != null && parent.getParent() != null) {
868 buffer.append(" [in "); //$NON-NLS-1$
869 parent.toStringInfo(0, buffer, NO_INFO);
870 parent.toStringAncestors(buffer);
871 buffer.append("]"); //$NON-NLS-1$
878 protected void toStringChildren(int tab, StringBuffer buffer, Object info) {
879 if (info == null || !(info instanceof JavaElementInfo))
881 IJavaElement[] children = ((JavaElementInfo) info).getChildren();
882 for (int i = 0; i < children.length; i++) {
883 buffer.append("\n"); //$NON-NLS-1$
884 ((JavaElement) children[i]).toString(tab + 1, buffer);
891 // public Object toStringInfo(int tab, StringBuffer buffer) {
892 // Object info = JavaModelManager.getJavaModelManager().peekAtInfo(this);
893 // this.toStringInfo(tab, buffer, info);
899 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
900 buffer.append(this.tabString(tab));
901 buffer.append(getElementName());
903 buffer.append(" (not open)"); //$NON-NLS-1$