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.jdom.IDOMCompilationUnit;
29 import net.sourceforge.phpdt.core.jdom.IDOMNode;
30 import net.sourceforge.phpdt.internal.core.util.Util;
31 import net.sourceforge.phpdt.internal.corext.Assert;
33 import org.eclipse.core.resources.IResource;
34 import org.eclipse.core.resources.IResourceStatus;
35 import org.eclipse.core.resources.ResourcesPlugin;
36 import org.eclipse.core.runtime.CoreException;
37 import org.eclipse.core.runtime.IAdaptable;
38 import org.eclipse.core.runtime.IPath;
39 import org.eclipse.core.runtime.IProgressMonitor;
40 import org.eclipse.core.runtime.Path;
41 import org.eclipse.core.runtime.PlatformObject;
42 import org.eclipse.core.runtime.jobs.ISchedulingRule;
45 * Root of Java element handle hierarchy.
49 public abstract class JavaElement extends PlatformObject
53 public static final char JEM_JAVAPROJECT = '=';
54 public static final char JEM_PACKAGEFRAGMENTROOT = Path.SEPARATOR;
55 public static final char JEM_PACKAGEFRAGMENT = '<';
56 public static final char JEM_FIELD = '^';
57 public static final char JEM_METHOD = '~';
58 public static final char JEM_INITIALIZER = '|';
59 public static final char JEM_COMPILATIONUNIT = '{';
60 public static final char JEM_CLASSFILE = '(';
61 public static final char JEM_TYPE = '[';
62 public static final char JEM_PACKAGEDECLARATION = '%';
63 public static final char JEM_IMPORTDECLARATION = '#';
66 * A count to uniquely identify this element in the case that a duplicate
67 * named element exists. For example, if there are two fields in a
68 * compilation unit with the same name, the occurrence count is used to
69 * distinguish them. The occurrence count starts at 1 (thus the first
70 * occurrence is occurrence 1, not occurrence 0).
72 protected int occurrenceCount = 1;
75 * This element's type - one of the constants defined in
76 * IJavaLanguageElementTypes.
78 //protected int fLEType = 0;
80 * This element's parent, or <code>null</code> if this element does not
83 protected IJavaElement parent;
86 * This element's name, or an empty <code>String</code> if this element
87 * does not have a name.
89 protected String name;
91 protected static final Object NO_INFO = new Object();
94 * Constructs a handle for a java element with the given parent element and
98 * The parent of java element
100 * The name of java element
102 * @exception IllegalArgumentException
103 * if the type is not one of the valid Java element type
107 protected JavaElement(JavaElement parent, String name)
108 throws IllegalArgumentException {
109 this.parent = parent;
115 public void close() throws JavaModelException {
116 JavaModelManager.getJavaModelManager().removeInfoAndChildren(this);
119 * This element is being closed. Do any necessary cleanup.
121 protected abstract void closing(Object info) throws JavaModelException;
123 * Returns a new element info for this element.
125 protected abstract Object createElementInfo();
127 * Returns true if this handle represents the same Java element as the given
128 * handle. By default, two handles represent the same element if they are
129 * identical or if they represent the same type of element, have equal
130 * names, parents, and occurrence counts.
133 * If a subclass has other requirements for equality, this method must be
138 public boolean equals(Object o) {
143 // Java model parent is null
144 if (this.parent == null)
145 return super.equals(o);
146 if (o instanceof JavaElement) {
147 // assume instanceof check is done in subclass
148 JavaElement other = (JavaElement) o;
149 return this.occurrenceCount == other.occurrenceCount
150 && this.name.equals(other.name)
151 && this.parent.equals(other.parent);
156 * Returns true if this <code>JavaElement</code> is equivalent to the
157 * given <code>IDOMNode</code>.
159 protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException {
165 public boolean exists() {
170 } catch (JavaModelException e) {
176 * Returns the <code>IDOMNode</code> that corresponds to this
177 * <code>JavaElement</code> or <code>null</code> if there is no
178 * corresponding node.
180 public IDOMNode findNode(IDOMCompilationUnit dom) {
181 int type = getElementType();
182 if (type == IJavaElement.COMPILATION_UNIT || type == IJavaElement.FIELD
183 || type == IJavaElement.IMPORT_DECLARATION
184 || type == IJavaElement.INITIALIZER
185 || type == IJavaElement.METHOD
186 || type == IJavaElement.PACKAGE_DECLARATION
187 || type == IJavaElement.TYPE) {
188 ArrayList path = new ArrayList();
189 IJavaElement element = this;
190 while (element != null
191 && element.getElementType() != IJavaElement.COMPILATION_UNIT) {
192 if (element.getElementType() != IJavaElement.IMPORT_CONTAINER) {
193 // the DOM does not have import containers, so skip them
194 path.add(0, element);
196 element = element.getParent();
198 if (path.size() == 0) {
200 if (equalsDOMNode(dom)) {
205 } catch (JavaModelException e) {
209 return ((JavaElement) path.get(0)).followPath(path, 0, dom
217 protected IDOMNode followPath(ArrayList path, int position, IDOMNode node) {
220 if (equalsDOMNode(node)) {
221 if (position == (path.size() - 1)) {
224 if (node.getFirstChild() != null) {
226 return ((JavaElement) path.get(position)).followPath(
227 path, position, node.getFirstChild());
232 } else if (node.getNextNode() != null) {
233 return followPath(path, position, node.getNextNode());
237 } catch (JavaModelException e) {
245 public IJavaElement getAncestor(int ancestorType) {
247 IJavaElement element = this;
248 while (element != null) {
249 if (element.getElementType() == ancestorType)
251 element = element.getParent();
256 * Generates the element infos for this element, its ancestors (if they are
257 * not opened) and its children (if it is an Openable). Puts the newly
258 * created element info in the given map.
260 protected abstract void generateInfos(Object info, HashMap newElements,
261 IProgressMonitor pm) throws JavaModelException;
266 public IJavaElement[] getChildren() throws JavaModelException {
267 return ((JavaElementInfo) getElementInfo()).getChildren();
270 * Returns a collection of (immediate) children of this node of the
274 * one of constants defined by IJavaLanguageElementTypes
276 public ArrayList getChildrenOfType(int type) throws JavaModelException {
277 IJavaElement[] children = getChildren();
278 int size = children.length;
279 ArrayList list = new ArrayList(size);
280 for (int i = 0; i < size; ++i) {
281 JavaElement elt = (JavaElement) children[i];
282 if (elt.getElementType() == type) {
291 // public IClassFile getClassFile() {
297 public ICompilationUnit getCompilationUnit() {
301 * Returns the info for this handle. If this element is not already open, it
302 * and all of its parents are opened. Does not return null. NOTE: BinaryType
303 * infos are NOT rooted under JavaElementInfo.
305 * @exception JavaModelException
306 * if the element is not present or not accessible
308 public Object getElementInfo() throws JavaModelException {
309 return getElementInfo(null);
312 * Returns the info for this handle. If this element is not already open, it
313 * and all of its parents are opened. Does not return null. NOTE: BinaryType
314 * infos are NOT rooted under JavaElementInfo.
316 * @exception JavaModelException
317 * if the element is not present or not accessible
319 public Object getElementInfo(IProgressMonitor monitor)
320 throws JavaModelException {
322 JavaModelManager manager = JavaModelManager.getJavaModelManager();
323 Object info = manager.getInfo(this);
326 return openWhenClosed(createElementInfo(), monitor);
331 public String getElementName() {
338 public String getHandleIdentifier() {
339 return getHandleMemento();
342 * @see JavaElement#getHandleMemento()
344 public String getHandleMemento() {
345 StringBuffer buff = new StringBuffer(((JavaElement) getParent())
346 .getHandleMemento());
347 buff.append(getHandleMementoDelimiter());
348 buff.append(getElementName());
349 return buff.toString();
352 * Returns the <code>char</code> that marks the start of this handles
353 * contribution to a memento.
355 protected abstract char getHandleMementoDelimiter();
359 public IJavaModel getJavaModel() {
360 IJavaElement current = this;
362 if (current instanceof IJavaModel)
363 return (IJavaModel) current;
364 } while ((current = current.getParent()) != null);
371 public IJavaProject getJavaProject() {
372 IJavaElement current = this;
374 if (current instanceof IJavaProject)
375 return (IJavaProject) current;
376 } while ((current = current.getParent()) != null);
380 * Returns the occurrence count of the handle.
382 protected int getOccurrenceCount() {
383 return occurrenceCount;
388 public IOpenable getOpenable() {
389 return this.getOpenableParent();
392 * Return the first instance of IOpenable in the parent hierarchy of this
396 * Subclasses that are not IOpenable's must override this method.
398 public IOpenable getOpenableParent() {
400 return (IOpenable) parent;
405 public IJavaElement getParent() {
410 * @see IJavaElement#getPrimaryElement()
412 public IJavaElement getPrimaryElement() {
413 return getPrimaryElement(true);
416 * Returns the primary element. If checkOwner, and the cu owner is primary,
417 * return this element.
419 public IJavaElement getPrimaryElement(boolean checkOwner) {
423 * Returns the element that is located at the given source position in this
424 * element. This is a helper method for
425 * <code>ICompilationUnit#getElementAt</code>, and only works on
426 * compilation units and types. The position given is known to be within
427 * this element's source range already, and if no finer grained element is
428 * found at the position, this element is returned.
430 protected IJavaElement getSourceElementAt(int position)
431 throws JavaModelException {
432 if (this instanceof ISourceReference) {
433 IJavaElement[] children = getChildren();
435 for (i = 0; i < children.length; i++) {
436 IJavaElement aChild = children[i];
437 if (aChild instanceof SourceRefElement) {
438 SourceRefElement child = (SourceRefElement) children[i];
439 ISourceRange range = child.getSourceRange();
440 if (position < range.getOffset() + range.getLength()
441 && position >= range.getOffset()) {
442 if (child instanceof IParent) {
443 return child.getSourceElementAt(position);
452 Assert.isTrue(false);
457 * Returns the SourceMapper facility for this element, or <code>null</code>
458 * if this element does not have a SourceMapper.
460 // public SourceMapper getSourceMapper() {
461 // return ((JavaElement)getParent()).getSourceMapper();
466 * @see org.eclipse.jdt.core.IJavaElement#getSchedulingRule()
468 public ISchedulingRule getSchedulingRule() {
469 IResource resource = getResource();
470 if (resource == null) {
471 class NoResourceSchedulingRule implements ISchedulingRule {
473 public NoResourceSchedulingRule(IPath path) {
476 public boolean contains(ISchedulingRule rule) {
477 if (rule instanceof NoResourceSchedulingRule) {
479 .isPrefixOf(((NoResourceSchedulingRule) rule).path);
484 public boolean isConflicting(ISchedulingRule rule) {
485 if (rule instanceof NoResourceSchedulingRule) {
486 IPath otherPath = ((NoResourceSchedulingRule) rule).path;
487 return this.path.isPrefixOf(otherPath)
488 || otherPath.isPrefixOf(this.path);
494 return new NoResourceSchedulingRule(getPath());
502 public boolean hasChildren() throws JavaModelException {
503 // if I am not open, return true to avoid opening (case of a Java
504 // project, a compilation unit or a class file).
505 // also see https://bugs.eclipse.org/bugs/show_bug.cgi?id=52474
506 Object elementInfo = JavaModelManager.getJavaModelManager().getInfo(
508 if (elementInfo instanceof JavaElementInfo) {
509 return ((JavaElementInfo) elementInfo).getChildren().length > 0;
516 * Returns the hash code for this Java element. By default, the hash code
517 * for an element is a combination of its name and parent's hash code.
518 * Elements with other requirements must override this method.
520 public int hashCode() {
521 if (this.parent == null)
522 return super.hashCode();
523 return Util.combineHashCodes(this.name.hashCode(), this.parent
528 * Returns true if this element is an ancestor of the given element,
531 protected boolean isAncestorOf(IJavaElement e) {
532 IJavaElement parent = e.getParent();
533 while (parent != null && !parent.equals(this)) {
534 parent = parent.getParent();
536 return parent != null;
542 public boolean isReadOnly() {
548 public boolean isStructureKnown() throws JavaModelException {
549 return ((JavaElementInfo) getElementInfo()).isStructureKnown();
552 * Creates and returns and not present exception for this element.
554 protected JavaModelException newNotPresentException() {
555 return new JavaModelException(new JavaModelStatus(
556 IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, this));
559 * Opens this element and all parents that are not already open.
561 * @exception JavaModelException
562 * this element is not present or accessible
564 // protected void openHierarchy() throws JavaModelException {
565 // if (this instanceof IOpenable) {
566 // ((Openable) this).openWhenClosed(null);
568 // Openable openableParent = (Openable)getOpenableParent();
569 // if (openableParent != null) {
570 // JavaElementInfo openableParentInfo = (JavaElementInfo)
571 // JavaModelManager.getJavaModelManager().getInfo((IJavaElement)
573 // if (openableParentInfo == null) {
574 // openableParent.openWhenClosed(null);
576 // throw newNotPresentException();
582 * This element has just been opened. Do any necessary setup.
584 protected void opening(Object info) {
587 * Opens an <code> Openable </code> that is known to be closed (no check for
588 * <code> isOpen() </code> ). Returns the created element info.
590 protected Object openWhenClosed(Object info, IProgressMonitor monitor)
591 throws JavaModelException {
592 JavaModelManager manager = JavaModelManager.getJavaModelManager();
593 boolean hadTemporaryCache = manager.hasTemporaryCache();
595 HashMap newElements = manager.getTemporaryCache();
596 generateInfos(info, newElements, monitor);
598 info = newElements.get(this);
600 if (info == null) { // a source ref element could not be opened
601 // close any buffer that was opened for the openable parent
602 Iterator iterator = newElements.keySet().iterator();
603 while (iterator.hasNext()) {
604 IJavaElement element = (IJavaElement) iterator.next();
605 if (element instanceof Openable) {
606 ((Openable) element).closeBuffer();
609 throw newNotPresentException();
611 if (!hadTemporaryCache) {
612 manager.putInfos(this, newElements);
615 if (!hadTemporaryCache) {
616 manager.resetTemporaryCache();
623 public String readableName() {
624 return this.getElementName();
627 * Removes all cached info from the Java Model, including all children, but
628 * does not close this element.
630 // protected void removeInfo() {
631 // Object info = JavaModelManager.getJavaModelManager().peekAtInfo(this);
632 // if (info != null) {
633 // if (this instanceof IParent) {
634 // IJavaElement[] children = ((JavaElementInfo)info).getChildren();
635 // for (int i = 0, size = children.length; i < size; ++i) {
636 // JavaElement child = (JavaElement) children[i];
637 // child.removeInfo();
640 // JavaModelManager.getJavaModelManager().removeInfo(this);
644 // * Returns a copy of this element rooted at the given project.
646 // public abstract IJavaElement rootedAt(IJavaProject project);
648 * Runs a Java Model Operation
650 public static void runOperation(JavaModelOperation operation,
651 IProgressMonitor monitor) throws JavaModelException {
653 if (operation.isReadOnly()
654 || ResourcesPlugin.getWorkspace().isTreeLocked()) {
655 operation.run(monitor);
657 // use IWorkspace.run(...) to ensure that a build will be done
659 ResourcesPlugin.getWorkspace().run(operation, monitor);
661 } catch (CoreException ce) {
662 if (ce instanceof JavaModelException) {
663 throw (JavaModelException) ce;
665 if (ce.getStatus().getCode() == IResourceStatus.OPERATION_FAILED) {
666 Throwable e = ce.getStatus().getException();
667 if (e instanceof JavaModelException) {
668 throw (JavaModelException) e;
671 throw new JavaModelException(ce);
676 * Sets the occurrence count of the handle.
678 protected void setOccurrenceCount(int count) {
679 occurrenceCount = count;
681 protected String tabString(int tab) {
682 StringBuffer buffer = new StringBuffer();
683 for (int i = tab; i > 0; i--)
684 buffer.append(" "); //$NON-NLS-1$
685 return buffer.toString();
690 public String toDebugString() {
691 StringBuffer buffer = new StringBuffer();
692 this.toStringInfo(0, buffer, NO_INFO);
693 return buffer.toString();
698 public String toString() {
699 StringBuffer buffer = new StringBuffer();
701 return buffer.toString();
706 protected void toStringName(StringBuffer buffer) {
707 buffer.append(getElementName());
708 if (this.occurrenceCount > 1) {
709 buffer.append("#"); //$NON-NLS-1$
710 buffer.append(this.occurrenceCount);
716 protected void toString(int tab, StringBuffer buffer) {
717 // Object info = this.toStringInfo(tab, buffer);
720 this.toStringAncestors(buffer);
722 this.toStringChildren(tab, buffer, info);
727 public String toStringWithAncestors() {
728 StringBuffer buffer = new StringBuffer();
729 this.toStringInfo(0, buffer, NO_INFO);
730 this.toStringAncestors(buffer);
731 return buffer.toString();
736 protected void toStringAncestors(StringBuffer buffer) {
737 JavaElement parent = (JavaElement) this.getParent();
738 if (parent != null && parent.getParent() != null) {
739 buffer.append(" [in "); //$NON-NLS-1$
740 parent.toStringInfo(0, buffer, NO_INFO);
741 parent.toStringAncestors(buffer);
742 buffer.append("]"); //$NON-NLS-1$
748 protected void toStringChildren(int tab, StringBuffer buffer, Object info) {
749 if (info == null || !(info instanceof JavaElementInfo))
751 IJavaElement[] children = ((JavaElementInfo) info).getChildren();
752 for (int i = 0; i < children.length; i++) {
753 buffer.append("\n"); //$NON-NLS-1$
754 ((JavaElement) children[i]).toString(tab + 1, buffer);
760 // public Object toStringInfo(int tab, StringBuffer buffer) {
761 // Object info = JavaModelManager.getJavaModelManager().peekAtInfo(this);
762 // this.toStringInfo(tab, buffer, info);
768 protected void toStringInfo(int tab, StringBuffer buffer, Object info) {
769 buffer.append(this.tabString(tab));
770 buffer.append(getElementName());
772 buffer.append(" (not open)"); //$NON-NLS-1$