improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / SourceType.java
index 9af3643..4088fad 100644 (file)
  *******************************************************************************/
 package net.sourceforge.phpdt.internal.core;
 
+import java.io.InputStream;
 import java.util.ArrayList;
 
+import net.sourceforge.phpdt.core.ICompilationUnit;
 import net.sourceforge.phpdt.core.IField;
 import net.sourceforge.phpdt.core.IJavaElement;
+import net.sourceforge.phpdt.core.IJavaProject;
 import net.sourceforge.phpdt.core.IMember;
 import net.sourceforge.phpdt.core.IMethod;
 import net.sourceforge.phpdt.core.IPackageFragment;
 import net.sourceforge.phpdt.core.IParent;
 import net.sourceforge.phpdt.core.IType;
+import net.sourceforge.phpdt.core.ITypeHierarchy;
+import net.sourceforge.phpdt.core.IWorkingCopy;
 import net.sourceforge.phpdt.core.JavaModelException;
+import net.sourceforge.phpdt.core.WorkingCopyOwner;
 import net.sourceforge.phpdt.core.jdom.IDOMNode;
 import net.sourceforge.phpdt.internal.core.util.Util;
 import net.sourceforge.phpdt.internal.corext.Assert;
 
+import org.eclipse.core.runtime.IProgressMonitor;
+
 /**
  * Handle for a source type. Info object is a SourceTypeElementInfo.
  * 
@@ -37,7 +45,6 @@ public class SourceType extends Member implements IType {
    * An empty list of Strings
    */
   protected static final String[] fgEmptyList = new String[] {};
-
   protected SourceType(JavaElement parent, String name) {
     super(parent, name);
     Assert.isTrue(name.indexOf('.') == -1, Util.bind("sourcetype.invalidName", name)); //$NON-NLS-1$
@@ -365,66 +372,186 @@ public class SourceType extends Member implements IType {
   /**
    * @see IType
    */
-  //public ITypeHierarchy loadTypeHierachy(InputStream input, IProgressMonitor monitor) throws JavaModelException {
-  //   return TypeHierarchy.load(this, input);
-  //}
+//  public ITypeHierarchy loadTypeHierachy(InputStream input, IProgressMonitor monitor) throws JavaModelException {
+//     return loadTypeHierachy(input, DefaultWorkingCopyOwner.PRIMARY, monitor);
+//  }
+  /**
+   * NOTE: This method is not part of the API has it is not clear clients would easily use it: they would need to
+   * first make sure all working copies for the given owner exist before calling it. This is especially har at startup 
+   * time.
+   * In case clients want this API, here is how it should be specified:
+   * <p>
+   * Loads a previously saved ITypeHierarchy from an input stream. A type hierarchy can
+   * be stored using ITypeHierachy#store(OutputStream). A compilation unit of a
+   * loaded type has the given owner if such a working copy exists, otherwise the type's 
+   * compilation unit is a primary compilation unit.
+   * 
+   * Only hierarchies originally created by the following methods can be loaded:
+   * <ul>
+   * <li>IType#newSupertypeHierarchy(IProgressMonitor)</li>
+   * <li>IType#newSupertypeHierarchy(WorkingCopyOwner, IProgressMonitor)</li>
+   * <li>IType#newTypeHierarchy(IJavaProject, IProgressMonitor)</li>
+   * <li>IType#newTypeHierarchy(IJavaProject, WorkingCopyOwner, IProgressMonitor)</li>
+   * <li>IType#newTypeHierarchy(IProgressMonitor)</li>
+   * <li>IType#newTypeHierarchy(WorkingCopyOwner, IProgressMonitor)</li>
+   * </u>
+   * 
+   * @param input stream where hierarchy will be read
+   * @param monitor the given progress monitor
+   * @return the stored hierarchy
+   * @exception JavaModelException if the hierarchy could not be restored, reasons include:
+   *      - type is not the focus of the hierarchy or 
+   *           - unable to read the input stream (wrong format, IOException during reading, ...)
+   * @see ITypeHierarchy#store(java.io.OutputStream, IProgressMonitor)
+   * @since 3.0
+   */
+//  public ITypeHierarchy loadTypeHierachy(InputStream input, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException {
+//     // TODO monitor should be passed to TypeHierarchy.load(...)
+//     return TypeHierarchy.load(this, input, owner);
+//  }
   /**
    * @see IType
    */
-  //public ITypeHierarchy newSupertypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
-  //   return this.newSupertypeHierarchy(null, monitor);
-  //}
-  /**
+//  public ITypeHierarchy newSupertypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
+//     return this.newSupertypeHierarchy(DefaultWorkingCopyOwner.PRIMARY, monitor);
+//  }
+  /*
+   * @see IType#newSupertypeHierarchy(ICompilationUnit[], IProgressMonitor)
+   */
+//  public ITypeHierarchy newSupertypeHierarchy(
+//     ICompilationUnit[] workingCopies,
+//     IProgressMonitor monitor)
+//     throws JavaModelException {
+//
+//     CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), false);
+//     op.runOperation(monitor);
+//     return op.getResult();
+//  }
+  /**
+   * @param workingCopies the working copies that take precedence over their original compilation units
+   * @param monitor the given progress monitor
+   * @return a type hierarchy for this type containing this type and all of its supertypes
+   * @exception JavaModelException if this element does not exist or if an
+   *           exception occurs while accessing its corresponding resource.
+   *
    * @see IType#newSupertypeHierarchy(IWorkingCopy[], IProgressMonitor)
+   * @deprecated
    */
-  //public ITypeHierarchy newSupertypeHierarchy(
-  //   IWorkingCopy[] workingCopies,
-  //   IProgressMonitor monitor)
-  //   throws JavaModelException {
-  //
-  //   CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(),
-  // false);
-  //   runOperation(op, monitor);
-  //   return op.getResult();
-  //}
+  public ITypeHierarchy newSupertypeHierarchy(
+       IWorkingCopy[] workingCopies,
+       IProgressMonitor monitor)
+       throws JavaModelException {
+
+       ICompilationUnit[] copies;
+       if (workingCopies == null) {
+               copies = null;
+       } else {
+               int length = workingCopies.length;
+               System.arraycopy(workingCopies, 0, copies = new ICompilationUnit[length], 0, length);
+       }
+       return newSupertypeHierarchy(copies, monitor);
+  }
   /**
-   * @see IType
+   * @see IType#newSupertypeHierarchy(WorkingCopyOwner, IProgressMonitor)
    */
-  //public ITypeHierarchy newTypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
-  //   return this.newTypeHierarchy((IWorkingCopy[])null, monitor);
-  //}
+//  public ITypeHierarchy newSupertypeHierarchy(
+//     WorkingCopyOwner owner,
+//     IProgressMonitor monitor)
+//     throws JavaModelException {
+//
+//     ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true/*add primary working copies*/);
+//     CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), false);
+//     op.runOperation(monitor);
+//     return op.getResult();
+//  }
   /**
-   * @see IType#newTypeHierarchy(IWorkingCopy[], IProgressMonitor)
+   * @see IType
    */
-  //public ITypeHierarchy newTypeHierarchy(
-  //   IWorkingCopy[] workingCopies,
-  //   IProgressMonitor monitor)
-  //   throws JavaModelException {
-  //           
-  //   CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(),
-  // true);
-  //   runOperation(op, monitor);
-  //   return op.getResult();
-  //}
+//  public ITypeHierarchy newTypeHierarchy(IJavaProject project, IProgressMonitor monitor) throws JavaModelException {
+//     return newTypeHierarchy(project, DefaultWorkingCopyOwner.PRIMARY, monitor);
+//  }
+  /**
+   * @see IType#newTypeHierarchy(IJavaProject, WorkingCopyOwner, IProgressMonitor)
+   */
+//  public ITypeHierarchy newTypeHierarchy(IJavaProject project, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException {
+//     if (project == null) {
+//             throw new IllegalArgumentException(Util.bind("hierarchy.nullProject")); //$NON-NLS-1$
+//     }
+//     ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true/*add primary working copies*/);
+//     ICompilationUnit[] projectWCs = null;
+//     if (workingCopies != null) {
+//             int length = workingCopies.length;
+//             projectWCs = new ICompilationUnit[length];
+//             int index = 0;
+//             for (int i = 0; i < length; i++) {
+//                     ICompilationUnit wc = workingCopies[i];
+//                     if (project.equals(wc.getJavaProject())) {
+//                             projectWCs[index++] = wc;
+//                     }
+//             }
+//             if (index != length) {
+//                     System.arraycopy(projectWCs, 0, projectWCs = new ICompilationUnit[index], 0, index);
+//             }
+//     }
+//     CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(
+//             this, 
+//             projectWCs,
+//             project, 
+//             true);
+//     op.runOperation(monitor);
+//     return op.getResult();
+//  }
   /**
    * @see IType
    */
-  //public ITypeHierarchy newTypeHierarchy(IJavaProject project, IProgressMonitor monitor) throws JavaModelException {
-  //   if (project == null) {
-  //           throw new IllegalArgumentException(ProjectPrefUtil.bind("hierarchy.nullProject")); //$NON-NLS-1$
-  //   }
-  //   
-  //   CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(
-  //           this,
-  //           (IWorkingCopy[])null, // no working copies
-  //           project,
-  //           true);
-  //   runOperation(op, monitor);
-  //   return op.getResult();
-  //}
+//  public ITypeHierarchy newTypeHierarchy(IProgressMonitor monitor) throws JavaModelException {
+//     CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, null, SearchEngine.createWorkspaceScope(), true);
+//     op.runOperation(monitor);
+//     return op.getResult();
+//  }
+  /*
+   * @see IType#newTypeHierarchy(ICompilationUnit[], IProgressMonitor)
+   */
+//  public ITypeHierarchy newTypeHierarchy(
+//     ICompilationUnit[] workingCopies,
+//     IProgressMonitor monitor)
+//     throws JavaModelException {
+//             
+//     CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), true);
+//     op.runOperation(monitor);
+//     return op.getResult();
+//  }
   /**
-   * See ISourceType.resolveType(...)
-   */
+   * @see IType#newTypeHierarchy(IWorkingCopy[], IProgressMonitor)
+   * @deprecated
+   */
+  public ITypeHierarchy newTypeHierarchy(
+       IWorkingCopy[] workingCopies,
+       IProgressMonitor monitor)
+       throws JavaModelException {
+               
+       ICompilationUnit[] copies;
+       if (workingCopies == null) {
+               copies = null;
+       } else {
+               int length = workingCopies.length;
+               System.arraycopy(workingCopies, 0, copies = new ICompilationUnit[length], 0, length);
+       }
+       return newTypeHierarchy(copies, monitor);
+  }
+  /**
+   * @see IType#newTypeHierarchy(WorkingCopyOwner, IProgressMonitor)
+   */
+//  public ITypeHierarchy newTypeHierarchy(
+//     WorkingCopyOwner owner,
+//     IProgressMonitor monitor)
+//     throws JavaModelException {
+//             
+//     ICompilationUnit[] workingCopies = JavaModelManager.getJavaModelManager().getWorkingCopies(owner, true/*add primary working copies*/);
+//     CreateTypeHierarchyOperation op= new CreateTypeHierarchyOperation(this, workingCopies, SearchEngine.createWorkspaceScope(), true);
+//     op.runOperation(monitor);
+//     return op.getResult();  
+//  }
 
   // public String[][] resolveType(String typeName) throws JavaModelException {
   //   ISourceType info = (ISourceType) this.getElementInfo();