Refactory: replaced internal copy of Assert class with org.eclipse.core.runtime.Assert
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / JavaElement.java
index afda75c..f5640d2 100644 (file)
@@ -25,9 +25,14 @@ 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.WorkingCopyOwner;
 import net.sourceforge.phpdt.core.jdom.IDOMCompilationUnit;
 import net.sourceforge.phpdt.core.jdom.IDOMNode;
-import net.sourceforge.phpdt.internal.corext.Assert;
+import net.sourceforge.phpdt.internal.core.util.MementoTokenizer;
+import net.sourceforge.phpdt.internal.core.util.Util;
+//incastrix
+//import net.sourceforge.phpdt.internal.corext.Assert;
+import org.eclipse.core.runtime.Assert;
 
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.IResourceStatus;
@@ -39,32 +44,41 @@ import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.Path;
 import org.eclipse.core.runtime.PlatformObject;
 import org.eclipse.core.runtime.jobs.ISchedulingRule;
-import net.sourceforge.phpdt.internal.core.JavaElementInfo;
-
-import net.sourceforge.phpdt.internal.core.JavaModelManager;
-import net.sourceforge.phpdt.internal.core.util.Util;
 
 /**
  * Root of Java element handle hierarchy.
  * 
  * @see IJavaElement
  */
-public abstract class JavaElement extends PlatformObject
-               implements
-                       IJavaElement {
+public abstract class JavaElement extends PlatformObject implements
+               IJavaElement {
+       public static final char JEM_ESCAPE = '\\';
 
        public static final char JEM_JAVAPROJECT = '=';
+
        public static final char JEM_PACKAGEFRAGMENTROOT = Path.SEPARATOR;
+
        public static final char JEM_PACKAGEFRAGMENT = '<';
+
        public static final char JEM_FIELD = '^';
+
        public static final char JEM_METHOD = '~';
+
        public static final char JEM_INITIALIZER = '|';
+
        public static final char JEM_COMPILATIONUNIT = '{';
-       public static final char JEM_CLASSFILE = '(';
+
+       // public static final char JEM_CLASSFILE = '(';
        public static final char JEM_TYPE = '[';
+
        public static final char JEM_PACKAGEDECLARATION = '%';
+
        public static final char JEM_IMPORTDECLARATION = '#';
 
+       public static final char JEM_COUNT = '!';
+
+       public static final char JEM_LOCALVARIABLE = '@';
+
        /**
         * A count to uniquely identify this element in the case that a duplicate
         * named element exists. For example, if there are two fields in a
@@ -78,12 +92,12 @@ public abstract class JavaElement extends PlatformObject
         * This element's type - one of the constants defined in
         * IJavaLanguageElementTypes.
         */
-       //protected int fLEType = 0;
+       // protected int fLEType = 0;
        /**
         * This element's parent, or <code>null</code> if this element does not
         * have a parent.
         */
-       protected IJavaElement parent;
+       protected JavaElement parent;
 
        /**
         * This element's name, or an empty <code>String</code> if this element
@@ -105,27 +119,31 @@ public abstract class JavaElement extends PlatformObject
         * @exception IllegalArgumentException
         *                if the type is not one of the valid Java element type
         *                constants
-        *  
+        * 
         */
        protected JavaElement(JavaElement parent, String name)
                        throws IllegalArgumentException {
                this.parent = parent;
                this.name = name;
        }
+
        /**
         * @see IOpenable
         */
        public void close() throws JavaModelException {
                JavaModelManager.getJavaModelManager().removeInfoAndChildren(this);
        }
+
        /**
         * This element is being closed. Do any necessary cleanup.
         */
        protected abstract void closing(Object info) throws JavaModelException;
+
        /*
         * Returns a new element info for this element.
         */
        protected abstract Object createElementInfo();
+
        /**
         * Returns true if this handle represents the same Java element as the given
         * handle. By default, two handles represent the same element if they are
@@ -155,6 +173,7 @@ public abstract class JavaElement extends PlatformObject
                }
                return false;
        }
+
        /**
         * Returns true if this <code>JavaElement</code> is equivalent to the
         * given <code>IDOMNode</code>.
@@ -162,6 +181,31 @@ public abstract class JavaElement extends PlatformObject
        protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException {
                return false;
        }
+
+       protected void escapeMementoName(StringBuffer buffer, String mementoName) {
+               for (int i = 0, length = mementoName.length(); i < length; i++) {
+                       char character = mementoName.charAt(i);
+                       switch (character) {
+                       case JEM_ESCAPE:
+                       case JEM_COUNT:
+                       case JEM_JAVAPROJECT:
+                       case JEM_PACKAGEFRAGMENTROOT:
+                       case JEM_PACKAGEFRAGMENT:
+                       case JEM_FIELD:
+                       case JEM_METHOD:
+                       case JEM_INITIALIZER:
+                       case JEM_COMPILATIONUNIT:
+                               // case JEM_CLASSFILE:
+                       case JEM_TYPE:
+                       case JEM_PACKAGEDECLARATION:
+                       case JEM_IMPORTDECLARATION:
+                       case JEM_LOCALVARIABLE:
+                               buffer.append(JEM_ESCAPE);
+                       }
+                       buffer.append(character);
+               }
+       }
+
        /**
         * @see IJavaElement
         */
@@ -215,6 +259,7 @@ public abstract class JavaElement extends PlatformObject
                        return null;
                }
        }
+
        /**
         */
        protected IDOMNode followPath(ArrayList path, int position, IDOMNode node) {
@@ -242,6 +287,7 @@ public abstract class JavaElement extends PlatformObject
                }
 
        }
+
        /**
         * @see IJavaElement
         */
@@ -255,6 +301,7 @@ public abstract class JavaElement extends PlatformObject
                }
                return null;
        }
+
        /**
         * Generates the element infos for this element, its ancestors (if they are
         * not opened) and its children (if it is an Openable). Puts the newly
@@ -269,6 +316,7 @@ public abstract class JavaElement extends PlatformObject
        public IJavaElement[] getChildren() throws JavaModelException {
                return ((JavaElementInfo) getElementInfo()).getChildren();
        }
+
        /**
         * Returns a collection of (immediate) children of this node of the
         * specified type.
@@ -288,18 +336,20 @@ public abstract class JavaElement extends PlatformObject
                }
                return list;
        }
+
        /**
         * @see IMember
         */
-       //      public IClassFile getClassFile() {
-       //              return null;
-       //      }
+       // public IClassFile getClassFile() {
+       // return null;
+       // }
        /**
         * @see IMember
         */
        public ICompilationUnit getCompilationUnit() {
                return null;
        }
+
        /**
         * Returns the info for this handle. If this element is not already open, it
         * and all of its parents are opened. Does not return null. NOTE: BinaryType
@@ -311,6 +361,7 @@ public abstract class JavaElement extends PlatformObject
        public Object getElementInfo() throws JavaModelException {
                return getElementInfo(null);
        }
+
        /**
         * Returns the info for this handle. If this element is not already open, it
         * and all of its parents are opened. Does not return null. NOTE: BinaryType
@@ -328,6 +379,7 @@ public abstract class JavaElement extends PlatformObject
                        return info;
                return openWhenClosed(createElementInfo(), monitor);
        }
+
        /**
         * @see IAdaptable
         */
@@ -335,12 +387,47 @@ public abstract class JavaElement extends PlatformObject
                return name;
        }
 
+       /*
+        * Creates a Java element handle from the given memento. The given token is
+        * the current delimiter indicating the type of the next token(s). The given
+        * working copy owner is used only for compilation unit handles.
+        */
+       public abstract IJavaElement getHandleFromMemento(String token,
+                       MementoTokenizer memento, WorkingCopyOwner owner);
+
+       /*
+        * Creates a Java element handle from the given memento. The given working
+        * copy owner is used only for compilation unit handles.
+        */
+       public IJavaElement getHandleFromMemento(MementoTokenizer memento,
+                       WorkingCopyOwner owner) {
+               if (!memento.hasMoreTokens())
+                       return this;
+               String token = memento.nextToken();
+               return getHandleFromMemento(token, memento, owner);
+       }
+
+       /*
+        * Update the occurence count of the receiver and creates a Java element
+        * handle from the given memento. The given working copy owner is used only
+        * for compilation unit handles.
+        */
+       public IJavaElement getHandleUpdatingCountFromMemento(
+                       MementoTokenizer memento, WorkingCopyOwner owner) {
+               this.occurrenceCount = Integer.parseInt(memento.nextToken());
+               if (!memento.hasMoreTokens())
+                       return this;
+               String token = memento.nextToken();
+               return getHandleFromMemento(token, memento, owner);
+       }
+
        /**
         * @see IJavaElement
         */
        public String getHandleIdentifier() {
                return getHandleMemento();
        }
+
        /**
         * @see JavaElement#getHandleMemento()
         */
@@ -348,14 +435,25 @@ public abstract class JavaElement extends PlatformObject
                StringBuffer buff = new StringBuffer(((JavaElement) getParent())
                                .getHandleMemento());
                buff.append(getHandleMementoDelimiter());
-               buff.append(getElementName());
+               escapeMementoName(buff, getElementName());
+               if (this.occurrenceCount > 1) {
+                       buff.append(JEM_COUNT);
+                       buff.append(this.occurrenceCount);
+               }
                return buff.toString();
        }
+
+       /**
+        * Returns the <code>char</code> that marks the start of this handles
+        * contribution to a memento.
+        */
+
        /**
         * Returns the <code>char</code> that marks the start of this handles
         * contribution to a memento.
         */
        protected abstract char getHandleMementoDelimiter();
+
        /**
         * @see IJavaElement
         */
@@ -379,18 +477,21 @@ public abstract class JavaElement extends PlatformObject
                } while ((current = current.getParent()) != null);
                return null;
        }
+
        /**
         * Returns the occurrence count of the handle.
         */
        protected int getOccurrenceCount() {
                return occurrenceCount;
        }
+
        /*
         * @see IJavaElement
         */
        public IOpenable getOpenable() {
                return this.getOpenableParent();
        }
+
        /**
         * Return the first instance of IOpenable in the parent hierarchy of this
         * element.
@@ -402,6 +503,7 @@ public abstract class JavaElement extends PlatformObject
 
                return (IOpenable) parent;
        }
+
        /**
         * @see IJavaElement
         */
@@ -415,6 +517,7 @@ public abstract class JavaElement extends PlatformObject
        public IJavaElement getPrimaryElement() {
                return getPrimaryElement(true);
        }
+
        /*
         * Returns the primary element. If checkOwner, and the cu owner is primary,
         * return this element.
@@ -422,6 +525,7 @@ public abstract class JavaElement extends PlatformObject
        public IJavaElement getPrimaryElement(boolean checkOwner) {
                return this;
        }
+
        /**
         * Returns the element that is located at the given source position in this
         * element. This is a helper method for
@@ -437,9 +541,14 @@ public abstract class JavaElement extends PlatformObject
                        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 (child.name.equals("stopObject")||range==null ||
+                                       // range.getOffset()<=0) {
+                                       // System.out.println(child.name);
+                                       // }
                                        if (position < range.getOffset() + range.getLength()
                                                        && position >= range.getOffset()) {
                                                if (child instanceof IParent) {
@@ -456,26 +565,29 @@ public abstract class JavaElement extends PlatformObject
                }
                return this;
        }
+
        /**
         * Returns the SourceMapper facility for this element, or <code>null</code>
         * if this element does not have a SourceMapper.
         */
-       //      public SourceMapper getSourceMapper() {
-       //              return ((JavaElement)getParent()).getSourceMapper();
-       //      }
+       // public SourceMapper getSourceMapper() {
+       // return ((JavaElement)getParent()).getSourceMapper();
+       // }
        /*
         * (non-Javadoc)
         * 
-        * @see org.eclipse.jdt.core.IJavaElement#getSchedulingRule()
+        * @see net.sourceforge.phpdt.core.IJavaElement#getSchedulingRule()
         */
        public ISchedulingRule getSchedulingRule() {
                IResource resource = getResource();
                if (resource == null) {
                        class NoResourceSchedulingRule implements ISchedulingRule {
                                public IPath path;
+
                                public NoResourceSchedulingRule(IPath path) {
                                        this.path = path;
                                }
+
                                public boolean contains(ISchedulingRule rule) {
                                        if (rule instanceof NoResourceSchedulingRule) {
                                                return this.path
@@ -484,6 +596,7 @@ public abstract class JavaElement extends PlatformObject
                                                return false;
                                        }
                                }
+
                                public boolean isConflicting(ISchedulingRule rule) {
                                        if (rule instanceof NoResourceSchedulingRule) {
                                                IPath otherPath = ((NoResourceSchedulingRule) rule).path;
@@ -499,6 +612,7 @@ public abstract class JavaElement extends PlatformObject
                        return resource;
                }
        }
+
        /**
         * @see IParent
         */
@@ -531,12 +645,12 @@ public abstract class JavaElement extends PlatformObject
         * Returns true if this element is an ancestor of the given element,
         * otherwise false.
         */
-       protected boolean isAncestorOf(IJavaElement e) {
-               IJavaElement parent = e.getParent();
-               while (parent != null && !parent.equals(this)) {
-                       parent = parent.getParent();
+       public boolean isAncestorOf(IJavaElement e) {
+               IJavaElement parentElement = e.getParent();
+               while (parentElement != null && !parentElement.equals(this)) {
+                       parentElement = parentElement.getParent();
                }
-               return parent != null;
+               return parentElement != null;
        }
 
        /**
@@ -545,12 +659,14 @@ public abstract class JavaElement extends PlatformObject
        public boolean isReadOnly() {
                return false;
        }
+
        /**
         * @see IJavaElement
         */
        public boolean isStructureKnown() throws JavaModelException {
                return ((JavaElementInfo) getElementInfo()).isStructureKnown();
        }
+
        /**
         * Creates and returns and not present exception for this element.
         */
@@ -558,34 +674,36 @@ public abstract class JavaElement extends PlatformObject
                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)
+       // 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();
-       //                              }
-       //                      }
-       //              }
-       //      }
+       // if (openableParentInfo == null) {
+       // openableParent.openWhenClosed(null);
+       // } else {
+       // throw newNotPresentException();
+       // }
+       // }
+       // }
+       // }
        /**
         * This element has just been opened. Do any necessary setup.
         */
        protected void opening(Object info) {
        }
+
        /*
         * Opens an <code> Openable </code> that is known to be closed (no check for
         * <code> isOpen() </code> ). Returns the created element info.
@@ -621,32 +739,34 @@ public abstract class JavaElement extends PlatformObject
                }
                return info;
        }
+
        /**
         */
        public String readableName() {
                return this.getElementName();
        }
+
        /**
         * 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.
-       //       */
-       //      public abstract IJavaElement rootedAt(IJavaProject 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
         */
@@ -675,18 +795,21 @@ public abstract class JavaElement extends PlatformObject
                        }
                }
        }
+
        /**
         * Sets the occurrence count of the handle.
         */
        protected void setOccurrenceCount(int count) {
                occurrenceCount = count;
        }
+
        protected String tabString(int tab) {
                StringBuffer buffer = new StringBuffer();
                for (int i = tab; i > 0; i--)
                        buffer.append("  "); //$NON-NLS-1$
                return buffer.toString();
        }
+
        /**
         * Debugging purposes
         */
@@ -695,6 +818,7 @@ public abstract class JavaElement extends PlatformObject
                this.toStringInfo(0, buffer, NO_INFO);
                return buffer.toString();
        }
+
        /**
         * Debugging purposes
         */
@@ -703,8 +827,9 @@ public abstract class JavaElement extends PlatformObject
                toString(0, buffer);
                return buffer.toString();
        }
+
        /**
-        *  Debugging purposes
+        * Debugging purposes
         */
        protected void toStringName(StringBuffer buffer) {
                buffer.append(getElementName());
@@ -713,17 +838,19 @@ public abstract class JavaElement extends PlatformObject
                        buffer.append(this.occurrenceCount);
                }
        }
+
        /**
         * Debugging purposes
         */
        protected void toString(int tab, StringBuffer buffer) {
-               //      Object info = this.toStringInfo(tab, buffer);
+               // Object info = this.toStringInfo(tab, buffer);
                Object info = null;
                if (tab == 0) {
                        this.toStringAncestors(buffer);
                }
                this.toStringChildren(tab, buffer, info);
        }
+
        /**
         * Debugging purposes
         */
@@ -733,6 +860,7 @@ public abstract class JavaElement extends PlatformObject
                this.toStringAncestors(buffer);
                return buffer.toString();
        }
+
        /**
         * Debugging purposes
         */
@@ -745,6 +873,7 @@ public abstract class JavaElement extends PlatformObject
                        buffer.append("]"); //$NON-NLS-1$
                }
        }
+
        /**
         * Debugging purposes
         */
@@ -757,14 +886,15 @@ public abstract class JavaElement extends PlatformObject
                        ((JavaElement) children[i]).toString(tab + 1, buffer);
                }
        }
+
        /**
         * Debugging purposes
         */
-       //      public Object toStringInfo(int tab, StringBuffer buffer) {
-       //              Object info = JavaModelManager.getJavaModelManager().peekAtInfo(this);
-       //              this.toStringInfo(tab, buffer, info);
-       //              return info;
-       //      }
+       // public Object toStringInfo(int tab, StringBuffer buffer) {
+       // Object info = JavaModelManager.getJavaModelManager().peekAtInfo(this);
+       // this.toStringInfo(tab, buffer, info);
+       // return info;
+       // }
        /**
         * Debugging purposes
         */