fixed wrong deprecation warning
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / CompilationUnit.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import net.sourceforge.phpdt.core.IBuffer;
18 import net.sourceforge.phpdt.core.IBufferFactory;
19 import net.sourceforge.phpdt.core.ICodeAssist;
20 import net.sourceforge.phpdt.core.ICompilationUnit;
21 import net.sourceforge.phpdt.core.IImportContainer;
22 import net.sourceforge.phpdt.core.IImportDeclaration;
23 import net.sourceforge.phpdt.core.IJavaElement;
24 import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
25 import net.sourceforge.phpdt.core.IJavaProject;
26 import net.sourceforge.phpdt.core.IMember;
27 import net.sourceforge.phpdt.core.IMethod;
28 import net.sourceforge.phpdt.core.IOpenable;
29 import net.sourceforge.phpdt.core.IPackageDeclaration;
30 import net.sourceforge.phpdt.core.IPackageFragmentRoot;
31 import net.sourceforge.phpdt.core.IParent;
32 import net.sourceforge.phpdt.core.IProblemRequestor;
33 import net.sourceforge.phpdt.core.ISourceManipulation;
34 import net.sourceforge.phpdt.core.ISourceRange;
35 import net.sourceforge.phpdt.core.ISourceReference;
36 import net.sourceforge.phpdt.core.IType;
37 import net.sourceforge.phpdt.core.IWorkingCopy;
38 import net.sourceforge.phpdt.core.JavaModelException;
39 import net.sourceforge.phpdt.core.Signature;
40 import net.sourceforge.phpdt.core.WorkingCopyOwner;
41 import net.sourceforge.phpdt.core.compiler.CharOperation;
42 import net.sourceforge.phpdt.core.jdom.IDOMNode;
43 import net.sourceforge.phpdt.internal.compiler.IProblemFactory;
44 import net.sourceforge.phpdt.internal.compiler.SourceElementParser;
45 import net.sourceforge.phpdt.internal.compiler.ast.CompilationUnitDeclaration;
46 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
47 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
48 import net.sourceforge.phpdt.internal.core.util.MementoTokenizer;
49 import net.sourceforge.phpdt.internal.core.util.Util;
50
51 import org.eclipse.core.resources.IContainer;
52 import org.eclipse.core.resources.IFile;
53 import org.eclipse.core.resources.IMarker;
54 import org.eclipse.core.resources.IResource;
55 import org.eclipse.core.runtime.IPath;
56 import org.eclipse.core.runtime.IProgressMonitor;
57 import org.eclipse.core.runtime.Path;
58
59 /**
60  * @see ICompilationUnit
61  */
62
63 public class CompilationUnit extends Openable implements ICompilationUnit,
64         net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit {
65   public WorkingCopyOwner owner;
66   /**
67    * Constructs a handle to a compilation unit with the given name in the specified package for the specified owner
68    *
69    * @exception IllegalArgumentException
70    *              if the name of the compilation unit does not end with ".java"
71    */
72   protected CompilationUnit(PackageFragment parent, String name, WorkingCopyOwner owner) {
73         super(parent, name);
74         this.owner = owner;
75   }
76
77   /**
78    * Accepts the given visitor onto the parsed tree of this compilation unit, after having runned the name resolution. The visitor's
79    * corresponding <code>visit</code> method is called with the corresponding parse tree. If the visitor returns <code>true</code>,
80    * this method visits this parse node's members.
81    *
82    * @param visitor
83    *          the visitor
84    * @exception JavaModelException
85    *              if this method fails. Reasons include:
86    *              <ul>
87    *              <li>This element does not exist.</li>
88    *              <li>The visitor failed with this exception.</li>
89    *              </ul>
90    */
91   //public void accept(IAbstractSyntaxTreeVisitor visitor) throws JavaModelException {
92   //    CompilationUnitVisitor.visit(this, visitor);
93   //}
94   /*
95    * @see ICompilationUnit#becomeWorkingCopy(IProblemRequestor, IProgressMonitor)
96    */
97   public void becomeWorkingCopy(IProblemRequestor problemRequestor, IProgressMonitor monitor) throws JavaModelException {
98         JavaModelManager manager = JavaModelManager.getJavaModelManager();
99         JavaModelManager.PerWorkingCopyInfo perWorkingCopyInfo = manager.getPerWorkingCopyInfo(this, false/* don't create */,
100                 true /* record usage */, null/* no problem requestor needed */);
101         if (perWorkingCopyInfo == null) {
102           // close cu and its children
103           close();
104
105           BecomeWorkingCopyOperation operation = new BecomeWorkingCopyOperation(this, problemRequestor);
106           operation.runOperation(monitor);
107         }
108   }
109
110   //protected void buildStructure(OpenableElementInfo info, IProgressMonitor monitor) throws JavaModelException {
111   //
112   //    if (monitor != null && monitor.isCanceled()) return;
113   //
114   //    // remove existing (old) infos
115   //    removeInfo();
116   //
117   //    HashMap newElements = new HashMap(11);
118   //    info.setIsStructureKnown(generateInfos(info, monitor, newElements, getResource()));
119   //    JavaModelManager.getJavaModelManager().getElementsOutOfSynchWithBuffers().remove(this);
120   //    for (Iterator iter = newElements.keySet().iterator(); iter.hasNext();) {
121   //            IJavaElement key = (IJavaElement) iter.next();
122   //            Object value = newElements.get(key);
123   //            JavaModelManager.getJavaModelManager().putInfo(key, value);
124   //    }
125   //    // add the info for this at the end, to ensure that a getInfo cannot reply null in case the LRU cache needs
126   //    // to be flushed. Might lead to performance issues.
127   //    // see PR 1G2K5S7: ITPJCORE:ALL - NPE when accessing source for a binary type
128   //    JavaModelManager.getJavaModelManager().putInfo(this, info);
129   //}
130   protected boolean buildStructure(OpenableElementInfo info, final IProgressMonitor pm, Map newElements,
131           IResource underlyingResource) throws JavaModelException {
132
133         // check if this compilation unit can be opened
134         if (!isWorkingCopy()) { // no check is done on root kind or exclusion pattern for working copies
135           if ( // ((IPackageFragment)getParent()).getKind() == IPackageFragmentRoot.K_BINARY||
136           !isValidCompilationUnit() || !underlyingResource.isAccessible()) {
137                 throw newNotPresentException();
138           }
139         }
140
141         // prevents reopening of non-primary working copies (they are closed when they are discarded and should not be reopened)
142         if (!isPrimary() && getPerWorkingCopyInfo() == null) {
143           throw newNotPresentException();
144         }
145
146         CompilationUnitElementInfo unitInfo = (CompilationUnitElementInfo) info;
147
148         // get buffer contents
149         IBuffer buffer = getBufferManager().getBuffer(CompilationUnit.this);
150         if (buffer == null) {
151           buffer = openBuffer(pm, unitInfo); // open buffer independently from the info, since we are building the info
152         }
153         final char[] contents = buffer == null ? null : buffer.getCharacters();
154
155         // generate structure and compute syntax problems if needed
156         CompilationUnitStructureRequestor requestor = new CompilationUnitStructureRequestor(this, unitInfo, newElements);
157         JavaModelManager.PerWorkingCopyInfo perWorkingCopyInfo = getPerWorkingCopyInfo();
158         IJavaProject project = getJavaProject();
159         boolean computeProblems = JavaProject.hasJavaNature(project.getProject()) && perWorkingCopyInfo != null
160                 && perWorkingCopyInfo.isActive();
161         IProblemFactory problemFactory = new DefaultProblemFactory();
162         Map options = project.getOptions(true);
163
164         if (underlyingResource == null) {
165           underlyingResource = getResource();
166         }
167
168         SourceElementParser parser = new SourceElementParser(requestor, problemFactory,
169                 new CompilerOptions(options));
170         //, true/*report local declarations*/);
171         requestor.parser = parser;
172         CompilationUnitDeclaration unit = parser.parseCompilationUnit(
173                 new net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit() {
174                   public char[] getContents() {
175                         return contents;
176                   }
177
178                   public char[] getMainTypeName() {
179                         return CompilationUnit.this.getMainTypeName();
180                   }
181
182                   public char[][] getPackageName() {
183                         return CompilationUnit.this.getPackageName();
184                   }
185
186                   public char[] getFileName() {
187                         return CompilationUnit.this.getFileName();
188                   }
189
190                   public IResource getResource() {
191                         return CompilationUnit.this.getResource();
192                   }
193                 }, true /* full parse to find local elements */);
194
195         // update timestamp (might be IResource.NULL_STAMP if original does not exist)
196
197         unitInfo.timestamp = ((IFile) underlyingResource).getModificationStamp();
198         // compute other problems if needed
199         CompilationUnitDeclaration compilationUnitDeclaration = null;
200         try {
201           if (computeProblems) {
202                 perWorkingCopyInfo.beginReporting();
203                 compilationUnitDeclaration = CompilationUnitProblemFinder.process(unit, this, contents, parser, this.owner,
204                         perWorkingCopyInfo, problemFactory, false/* don't cleanup cu */, pm);
205                 perWorkingCopyInfo.endReporting();
206           }
207
208           //            if (info instanceof ASTHolderCUInfo) {
209           //                    int astLevel = ((ASTHolderCUInfo) info).astLevel;
210           //                    net.sourceforge.phpdt.core.dom.CompilationUnit cu = AST.convertCompilationUnit(astLevel, unit, contents, options, pm);
211           //                    ((ASTHolderCUInfo) info).ast = cu;
212           //            }
213         } finally {
214           if (compilationUnitDeclaration != null) {
215                 compilationUnitDeclaration.cleanUp();
216           }
217         }
218
219         return unitInfo.isStructureKnown();
220   }
221
222   ///**
223   // * @see ICodeAssist#codeComplete(int, ICompletionRequestor)
224   // */
225   //public void codeComplete(int offset, ICompletionRequestor requestor) throws JavaModelException {
226   //    codeComplete(this, isWorkingCopy() ? (net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit) getOriginalElement() : this,
227   // offset, requestor);
228   //}
229   /**
230    * @see ICodeAssist#codeSelect(int, int)
231    */
232   //public IJavaElement[] codeSelect(int offset, int length) throws JavaModelException {
233   //    return super.codeSelect(this, offset, length);
234   //}
235   /**
236    * @see IWorkingCopy#commit(boolean, IProgressMonitor)
237    */
238   public void commit(boolean force, IProgressMonitor monitor) throws JavaModelException {
239         commitWorkingCopy(force, monitor);
240         //      throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, this));
241   }
242
243   /**
244    * @see ICompilationUnit#commitWorkingCopy(boolean, IProgressMonitor)
245    */
246   public void commitWorkingCopy(boolean force, IProgressMonitor monitor) throws JavaModelException {
247         CommitWorkingCopyOperation op = new CommitWorkingCopyOperation(this, force);
248         op.runOperation(monitor);
249   }
250
251   /**
252    * @see ISourceManipulation#copy(IJavaElement, IJavaElement, String, boolean, IProgressMonitor)
253    */
254   public void copy(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor)
255           throws JavaModelException {
256         if (container == null) {
257           throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$
258         }
259         IJavaElement[] elements = new IJavaElement[] { this };
260         IJavaElement[] containers = new IJavaElement[] { container };
261         String[] renamings = null;
262         if (rename != null) {
263           renamings = new String[] { rename };
264         }
265         getJavaModel().copy(elements, containers, null, renamings, force, monitor);
266   }
267
268   /**
269    * Returns a new element info for this element.
270    */
271   protected Object createElementInfo() {
272         return new CompilationUnitElementInfo();
273   }
274
275   ///**
276   // * @see ICompilationUnit#createImport(String, IJavaElement, IProgressMonitor)
277   // */
278   //public IImportDeclaration createImport(String name, IJavaElement sibling, IProgressMonitor monitor) throws JavaModelException {
279   //    CreateImportOperation op = new CreateImportOperation(name, this);
280   //    if (sibling != null) {
281   //            op.createBefore(sibling);
282   //    }
283   //    runOperation(op, monitor);
284   //    return getImport(name);
285   //}
286   /**
287    * @see ICompilationUnit#createPackageDeclaration(String, IProgressMonitor)
288    */
289   public IPackageDeclaration createPackageDeclaration(String name, IProgressMonitor monitor) throws JavaModelException {
290
291         CreatePackageDeclarationOperation op = new CreatePackageDeclarationOperation(name, this);
292         runOperation(op, monitor);
293         return getPackageDeclaration(name);
294   }
295
296   ///**
297   // * @see ICompilationUnit#createType(String, IJavaElement, boolean, IProgressMonitor)
298   // */
299   //public IType createType(String content, IJavaElement sibling, boolean force, IProgressMonitor monitor) throws
300   // JavaModelException {
301   //    if (!exists()) {
302   //            //autogenerate this compilation unit
303   //            IPackageFragment pkg = (IPackageFragment) getParent();
304   //            String source = ""; //$NON-NLS-1$
305   //            if (pkg.getElementName().length() > 0) {
306   //                    //not the default package...add the package declaration
307   //                    source = "package " + pkg.getElementName() + ";" + net.sourceforge.phpdt.internal.compiler.util.ProjectPrefUtil.LINE_SEPARATOR +
308   // net.sourceforge.phpdt.internal.compiler.util.ProjectPrefUtil.LINE_SEPARATOR; //$NON-NLS-1$ //$NON-NLS-2$
309   //            }
310   //            CreateCompilationUnitOperation op = new CreateCompilationUnitOperation(pkg, fName, source, force);
311   //            runOperation(op, monitor);
312   //    }
313   //    CreateTypeOperation op = new CreateTypeOperation(this, content, force);
314   //    if (sibling != null) {
315   //            op.createBefore(sibling);
316   //    }
317   //    runOperation(op, monitor);
318   //    return (IType) op.getResultElements()[0];
319   //}
320   /**
321    * @see ISourceManipulation#delete(boolean, IProgressMonitor)
322    */
323   public void delete(boolean force, IProgressMonitor monitor) throws JavaModelException {
324         IJavaElement[] elements = new IJavaElement[] { this };
325         getJavaModel().delete(elements, force, monitor);
326   }
327
328   /**
329    * @see IWorkingCopy#destroy()
330    * @deprecated
331    */
332   public void destroy() {
333         try {
334           discardWorkingCopy();
335         } catch (JavaModelException e) {
336           e.printStackTrace();
337         }
338   }
339
340   /*
341    * @see ICompilationUnit#discardWorkingCopy
342    */
343   public void discardWorkingCopy() throws JavaModelException {
344         // discard working copy and its children
345         DiscardWorkingCopyOperation op = new DiscardWorkingCopyOperation(this);
346         op.runOperation(null);
347   }
348
349   /**
350    * Returns true if this handle represents the same Java element as the given handle.
351    *
352    * @see Object#equals(java.lang.Object)
353    */
354   public boolean equals(Object obj) {
355         if (!(obj instanceof CompilationUnit))
356           return false;
357         CompilationUnit other = (CompilationUnit) obj;
358         return this.owner.equals(other.owner) && super.equals(obj);
359   }
360
361   /**
362    * @see JavaElement#equalsDOMNode(IDOMNode)
363    */
364   protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException {
365         String name = getElementName();
366         if (node.getNodeType() == IDOMNode.COMPILATION_UNIT && name != null) {
367           String nodeName = node.getName();
368           if (nodeName == null)
369                 return false;
370           if (name.equals(nodeName)) {
371                 return true;
372           } else {
373                 // iterate through all the types inside the receiver and see if one of them can fit
374                 IType[] types = getTypes();
375                 String typeNodeName = nodeName.substring(0, nodeName.indexOf(".java")); //$NON-NLS-1$
376                 for (int i = 0, max = types.length; i < max; i++) {
377                   if (types[i].getElementName().equals(typeNodeName)) {
378                         return true;
379                   }
380                 }
381           }
382         }
383         return false;
384   }
385
386   /**
387    * @see IWorkingCopy#findElements(IJavaElement)
388    */
389   public IJavaElement[] findElements(IJavaElement element) {
390         ArrayList children = new ArrayList();
391         while (element != null && element.getElementType() != IJavaElement.COMPILATION_UNIT) {
392           children.add(element);
393           element = element.getParent();
394         }
395         if (element == null)
396           return null;
397         IJavaElement currentElement = this;
398         for (int i = children.size() - 1; i >= 0; i--) {
399           IJavaElement child = (IJavaElement) children.get(i);
400           switch (child.getElementType()) {
401           //                    case IJavaElement.PACKAGE_DECLARATION:
402           //                            currentElement = ((ICompilationUnit)currentElement).getPackageDeclaration(child.getElementName());
403           //                            break;
404           //                    case IJavaElement.IMPORT_CONTAINER:
405           //                            currentElement = ((ICompilationUnit)currentElement).getImportContainer();
406           //                            break;
407           //                    case IJavaElement.IMPORT_DECLARATION:
408           //                            currentElement = ((IImportContainer)currentElement).getImport(child.getElementName());
409           //                            break;
410           case IJavaElement.TYPE:
411                 if (currentElement.getElementType() == IJavaElement.COMPILATION_UNIT) {
412                   currentElement = ((ICompilationUnit) currentElement).getType(child.getElementName());
413                 } else {
414                   currentElement = ((IType) currentElement).getType(child.getElementName());
415                 }
416                 break;
417           //                    case IJavaElement.INITIALIZER:
418           //                            currentElement = ((IType)currentElement).getInitializer(((JavaElement)child).getOccurrenceCount());
419           //                            break;
420           case IJavaElement.FIELD:
421                 currentElement = ((IType) currentElement).getField(child.getElementName());
422                 break;
423           case IJavaElement.METHOD:
424                 return ((IType) currentElement).findMethods((IMethod) child);
425           }
426
427         }
428         if (currentElement != null && currentElement.exists()) {
429           return new IJavaElement[] { currentElement };
430         } else {
431           return null;
432         }
433   }
434
435   /**
436    * @see IWorkingCopy#findPrimaryType()
437    */
438   public IType findPrimaryType() {
439         String typeName = Signature.getQualifier(this.getElementName());
440         IType primaryType = this.getType(typeName);
441         if (primaryType.exists()) {
442           return primaryType;
443         }
444         return null;
445   }
446
447   /**
448    * @see IWorkingCopy#findSharedWorkingCopy(IBufferFactory)
449    * @deprecated
450    */
451   public IJavaElement findSharedWorkingCopy(IBufferFactory factory) {
452
453         // if factory is null, default factory must be used
454         if (factory == null)
455           factory = this.getBufferManager().getDefaultBufferFactory();
456
457         return findWorkingCopy(BufferFactoryWrapper.create(factory));
458   }
459
460   /**
461    * @see ICompilationUnit#findWorkingCopy(WorkingCopyOwner)
462    */
463   public ICompilationUnit findWorkingCopy(WorkingCopyOwner workingCopyOwner) {
464         CompilationUnit cu = new CompilationUnit((PackageFragment) this.parent, getElementName(), workingCopyOwner);
465         if (workingCopyOwner == DefaultWorkingCopyOwner.PRIMARY) {
466           return cu;
467         } else {
468           // must be a working copy
469           JavaModelManager.PerWorkingCopyInfo perWorkingCopyInfo = cu.getPerWorkingCopyInfo();
470           if (perWorkingCopyInfo != null) {
471                 return perWorkingCopyInfo.getWorkingCopy();
472           } else {
473                 return null;
474           }
475         }
476   }
477
478   //protected boolean generateInfos(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource)
479   // throws JavaModelException {
480   //
481   //// if (getParent() instanceof JarPackageFragment) {
482   //// // ignore .java files in jar
483   //// throw newNotPresentException();
484   //// } else {
485   //            // put the info now, because getting the contents requires it
486   //            JavaModelManager.getJavaModelManager().putInfo(this, info);
487   //            CompilationUnitElementInfo unitInfo = (CompilationUnitElementInfo) info;
488   //
489   //            // generate structure
490   //            CompilationUnitStructureRequestor requestor = new CompilationUnitStructureRequestor(this, unitInfo, newElements);
491   //            IProblemFactory factory = new DefaultProblemFactory();
492   //            SourceElementParser parser = new SourceElementParser(requestor, factory, new
493   // CompilerOptions(getJavaProject().getOptions(true)));
494   //// SourceElementParser parser = new SourceElementParser(requestor, factory);
495   //            requestor.parser = parser;
496   //            parser.parseCompilationUnit(this, false);
497   //            if (isWorkingCopy()) {
498   //                    CompilationUnit original = (CompilationUnit) getOriginalElement();
499   //                    // might be IResource.NULL_STAMP if original does not exist
500   //                    unitInfo.timestamp = ((IFile) original.getResource()).getModificationStamp();
501   //            }
502   //            return unitInfo.isStructureKnown();
503   //// }
504   //}
505   /**
506    * @see ICompilationUnit#getAllTypes()
507    */
508   public IType[] getAllTypes() throws JavaModelException {
509         IJavaElement[] types = getTypes();
510         int i;
511         ArrayList allTypes = new ArrayList(types.length);
512         ArrayList typesToTraverse = new ArrayList(types.length);
513         for (i = 0; i < types.length; i++) {
514           typesToTraverse.add(types[i]);
515         }
516         while (!typesToTraverse.isEmpty()) {
517           IType type = (IType) typesToTraverse.get(0);
518           typesToTraverse.remove(type);
519           allTypes.add(type);
520           types = type.getTypes();
521           for (i = 0; i < types.length; i++) {
522                 typesToTraverse.add(types[i]);
523           }
524         }
525         IType[] arrayOfAllTypes = new IType[allTypes.size()];
526         allTypes.toArray(arrayOfAllTypes);
527         return arrayOfAllTypes;
528   }
529
530   /**
531    * @see IMember#getCompilationUnit()
532    */
533   public ICompilationUnit getCompilationUnit() {
534         return this;
535   }
536
537   /**
538    * @see net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit#getContents()
539    */
540   public char[] getContents() {
541         try {
542           IBuffer buffer = this.getBuffer();
543           return buffer == null ? null : buffer.getCharacters();
544         } catch (JavaModelException e) {
545           return CharOperation.NO_CHAR;
546         }
547   }
548
549   /**
550    * A compilation unit has a corresponding resource unless it is contained in a jar.
551    *
552    * @see IJavaElement#getCorrespondingResource()
553    */
554   public IResource getCorrespondingResource() throws JavaModelException {
555         IPackageFragmentRoot root = (IPackageFragmentRoot) getParent().getParent();
556         if (root.isArchive()) {
557           return null;
558         } else {
559           return getUnderlyingResource();
560         }
561   }
562
563   /**
564    * @see ICompilationUnit#getElementAt(int)
565    */
566   public IJavaElement getElementAt(int position) throws JavaModelException {
567
568         IJavaElement e = getSourceElementAt(position);
569         if (e == this) {
570           return null;
571         } else {
572           return e;
573         }
574   }
575
576   /**
577    * @see IJavaElement
578    */
579   public int getElementType() {
580         return COMPILATION_UNIT;
581   }
582
583   public char[] getFileName() {
584         return getElementName().toCharArray();
585   }
586   /*
587    * @see JavaElement
588    */
589   public IJavaElement getHandleFromMemento(String token, MementoTokenizer memento, WorkingCopyOwner workingCopyOwner) {
590         switch (token.charAt(0)) {
591                 case JEM_COUNT:
592                         return getHandleUpdatingCountFromMemento(memento, workingCopyOwner);
593                 case JEM_IMPORTDECLARATION:
594                         JavaElement container = (JavaElement)getImportContainer();
595                         return container.getHandleFromMemento(token, memento, workingCopyOwner);
596                 case JEM_PACKAGEDECLARATION:
597                         String pkgName = memento.nextToken();
598                         JavaElement pkgDecl = (JavaElement)getPackageDeclaration(pkgName);
599                         return pkgDecl.getHandleFromMemento(memento, workingCopyOwner);
600                 case JEM_TYPE:
601                         String typeName = memento.nextToken();
602                         JavaElement type = (JavaElement)getType(typeName);
603                         return type.getHandleFromMemento(memento, workingCopyOwner);
604         }
605         return null;
606   }
607
608   /**
609    * @see JavaElement#getHandleMementoDelimiter()
610    */
611   protected char getHandleMementoDelimiter() {
612         return JavaElement.JEM_COMPILATIONUNIT;
613   }
614   /**
615    * @see ICompilationUnit#getImport(String)
616    */
617   public IImportDeclaration getImport(String importName) {
618         return new ImportDeclaration((ImportContainer)getImportContainer(), importName);
619   }
620   /**
621    * @see ICompilationUnit#getImportContainer()
622    */
623   public IImportContainer getImportContainer() {
624         return new ImportContainer(this);
625   }
626
627
628   /**
629    * @see ICompilationUnit#getImports()
630    */
631   public IImportDeclaration[] getImports() throws JavaModelException {
632         IImportContainer container= getImportContainer();
633         if (container.exists()) {
634                 IJavaElement[] elements= container.getChildren();
635                 IImportDeclaration[] imprts= new IImportDeclaration[elements.length];
636                 System.arraycopy(elements, 0, imprts, 0, elements.length);
637                 return imprts;
638         } else if (!exists()) {
639                         throw newNotPresentException();
640         } else {
641                 return new IImportDeclaration[0];
642         }
643
644   }
645
646   /**
647    * @see net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit#getMainTypeName()
648    */
649   public char[] getMainTypeName() {
650         String name = getElementName();
651         //remove the .java
652         name = name.substring(0, name.length() - 5);
653         return name.toCharArray();
654   }
655
656   /**
657    * @see IWorkingCopy#getOriginal(IJavaElement)
658    * @deprecated
659    */
660   public IJavaElement getOriginal(IJavaElement workingCopyElement) {
661         // backward compatibility
662         if (!isWorkingCopy())
663           return null;
664         CompilationUnit cu = (CompilationUnit) workingCopyElement.getAncestor(COMPILATION_UNIT);
665         if (cu == null || !this.owner.equals(cu.owner)) {
666           return null;
667         }
668
669         return workingCopyElement.getPrimaryElement();
670   }
671
672   /**
673    * @see IWorkingCopy#getOriginalElement()
674    * @deprecated
675    */
676   public IJavaElement getOriginalElement() {
677         // backward compatibility
678         if (!isWorkingCopy())
679           return null;
680
681         return getPrimaryElement();
682   }
683
684   /*
685    * @see ICompilationUnit#getOwner()
686    */
687   public WorkingCopyOwner getOwner() {
688         return isPrimary() || !isWorkingCopy() ? null : this.owner;
689   }
690
691   /**
692    * @see ICompilationUnit#getPackageDeclaration(String)
693    */
694   public IPackageDeclaration getPackageDeclaration(String name) {
695         return new PackageDeclaration(this, name);
696   }
697
698   /**
699    * @see ICompilationUnit#getPackageDeclarations()
700    */
701   public IPackageDeclaration[] getPackageDeclarations() throws JavaModelException {
702         ArrayList list = getChildrenOfType(PACKAGE_DECLARATION);
703         IPackageDeclaration[] array = new IPackageDeclaration[list.size()];
704         list.toArray(array);
705         return array;
706   }
707
708   /**
709    * @see net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit#getPackageName()
710    */
711   public char[][] getPackageName() {
712         return null;
713   }
714
715   /**
716    * @see IJavaElement#getPath()
717    */
718   public IPath getPath() {
719         PackageFragmentRoot root = this.getPackageFragmentRoot();
720         if (root.isArchive()) {
721           return root.getPath();
722         } else {
723           return this.getParent().getPath().append(this.getElementName());
724         }
725   }
726
727   /*
728    * Returns the per working copy info for the receiver, or null if none exist. Note: the use count of the per working copy info is
729    * NOT incremented.
730    */
731   public JavaModelManager.PerWorkingCopyInfo getPerWorkingCopyInfo() {
732         return JavaModelManager.getJavaModelManager().getPerWorkingCopyInfo(this, false/* don't create */, false/* don't record usage */,
733                 null/* no problem requestor needed */);
734   }
735
736   /*
737    * @see ICompilationUnit#getPrimary()
738    */
739   public ICompilationUnit getPrimary() {
740         return (ICompilationUnit) getPrimaryElement(true);
741   }
742
743   /*
744    * @see JavaElement#getPrimaryElement(boolean)
745    */
746   public IJavaElement getPrimaryElement(boolean checkOwner) {
747         if (checkOwner && isPrimary())
748           return this;
749         return new CompilationUnit((PackageFragment) getParent(), getElementName(), DefaultWorkingCopyOwner.PRIMARY);
750   }
751
752   /**
753    * @see IJavaElement#getResource()
754    */
755   public IResource getResource() {
756         PackageFragmentRoot root = this.getPackageFragmentRoot();
757         if (root.isArchive()) {
758           return root.getResource();
759         } else {
760           return ((IContainer) this.getParent().getResource()).getFile(new Path(this.getElementName()));
761         }
762   }
763
764   /**
765    * @see ISourceReference#getSource()
766    */
767   public String getSource() throws JavaModelException {
768         IBuffer buffer = getBuffer();
769         if (buffer == null)
770           return ""; //$NON-NLS-1$
771         return buffer.getContents();
772   }
773
774   /**
775    * @see ISourceReference#getSourceRange()
776    */
777   public ISourceRange getSourceRange() throws JavaModelException {
778         return ((CompilationUnitElementInfo) getElementInfo()).getSourceRange();
779   }
780
781   /**
782    * @see ICompilationUnit#getType(String)
783    */
784   public IType getType(String name) {
785         return new SourceType(this, name);
786   }
787
788   /**
789    * @see ICompilationUnit#getTypes()
790    */
791   public IType[] getTypes() throws JavaModelException {
792         ArrayList list = getChildrenOfType(TYPE);
793         IType[] array = new IType[list.size()];
794         list.toArray(array);
795         return array;
796   }
797
798   /**
799    * @see IJavaElement
800    */
801   public IResource getUnderlyingResource() throws JavaModelException {
802         if (isWorkingCopy() && !isPrimary())
803           return null;
804         return super.getUnderlyingResource();
805   }
806
807   ///**
808   // * @see IWorkingCopy#getSharedWorkingCopy(IProgressMonitor, IBufferFactory, IProblemRequestor)
809   // */
810   //public IJavaElement getSharedWorkingCopy(IProgressMonitor pm, IBufferFactory factory, IProblemRequestor problemRequestor)
811   // throws JavaModelException {
812   //
813   //    // if factory is null, default factory must be used
814   //    if (factory == null) factory = this.getBufferManager().getDefaultBufferFactory();
815   //
816   //    JavaModelManager manager = JavaModelManager.getJavaModelManager();
817   //
818   //    // In order to be shared, working copies have to denote the same compilation unit
819   //    // AND use the same buffer factory.
820   //    // Assuming there is a little set of buffer factories, then use a 2 level Map cache.
821   //    Map sharedWorkingCopies = manager.sharedWorkingCopies;
822   //
823   //    Map perFactoryWorkingCopies = (Map) sharedWorkingCopies.get(factory);
824   //    if (perFactoryWorkingCopies == null){
825   //            perFactoryWorkingCopies = new HashMap();
826   //            sharedWorkingCopies.put(factory, perFactoryWorkingCopies);
827   //    }
828   //    WorkingCopy workingCopy = (WorkingCopy)perFactoryWorkingCopies.get(this);
829   //    if (workingCopy != null) {
830   //            workingCopy.useCount++;
831   //
832   //            if (SHARED_WC_VERBOSE) {
833   //                    System.out.println("Incrementing use count of shared working copy " + workingCopy.toStringWithAncestors()); //$NON-NLS-1$
834   //            }
835   //
836   //            return workingCopy;
837   //    } else {
838   //            CreateWorkingCopyOperation op = new CreateWorkingCopyOperation(this, perFactoryWorkingCopies, factory, problemRequestor);
839   //            runOperation(op, pm);
840   //            return op.getResultElements()[0];
841   //    }
842   //}
843   ///**
844   // * @see IWorkingCopy#getWorkingCopy()
845   // */
846   //public IJavaElement getWorkingCopy() throws JavaModelException {
847   //    return this.getWorkingCopy(null, null, null);
848   //}
849   //
850   ///**
851   // * @see IWorkingCopy#getWorkingCopy(IProgressMonitor, IBufferFactory, IProblemRequestor)
852   // */
853   //public IJavaElement getWorkingCopy(IProgressMonitor pm, IBufferFactory factory, IProblemRequestor problemRequestor) throws
854   // JavaModelException {
855   //    CreateWorkingCopyOperation op = new CreateWorkingCopyOperation(this, null, factory, problemRequestor);
856   //    runOperation(op, pm);
857   //    return op.getResultElements()[0];
858   //}
859   /**
860    * @see IWorkingCopy#getSharedWorkingCopy(IProgressMonitor, IBufferFactory, IProblemRequestor)
861    * @deprecated
862    */
863   public IJavaElement getSharedWorkingCopy(IProgressMonitor pm, IBufferFactory factory, IProblemRequestor problemRequestor)
864           throws JavaModelException {
865
866         // if factory is null, default factory must be used
867         if (factory == null)
868           factory = this.getBufferManager().getDefaultBufferFactory();
869
870         return getWorkingCopy(BufferFactoryWrapper.create(factory), problemRequestor, pm);
871   }
872
873   /**
874    * @see IWorkingCopy#getWorkingCopy()
875    * @deprecated
876    */
877   public IJavaElement getWorkingCopy() throws JavaModelException {
878         return getWorkingCopy(null);
879   }
880
881   /**
882    * @see ICompilationUnit#getWorkingCopy(IProgressMonitor)
883    */
884   public ICompilationUnit getWorkingCopy(IProgressMonitor monitor) throws JavaModelException {
885         return getWorkingCopy(new WorkingCopyOwner() {/* non shared working copy */
886         }, null/* no problem requestor */, monitor);
887   }
888
889   /**
890    * @see IWorkingCopy#getWorkingCopy(IProgressMonitor, IBufferFactory, IProblemRequestor)
891    * @deprecated
892    */
893   public IJavaElement getWorkingCopy(IProgressMonitor monitor, IBufferFactory factory, IProblemRequestor problemRequestor)
894           throws JavaModelException {
895         return getWorkingCopy(BufferFactoryWrapper.create(factory), problemRequestor, monitor);
896   }
897
898   /**
899    * @see ICompilationUnit#getWorkingCopy(WorkingCopyOwner, IProblemRequestor, IProgressMonitor)
900    */
901   public ICompilationUnit getWorkingCopy(WorkingCopyOwner workingCopyOwner, IProblemRequestor problemRequestor,
902           IProgressMonitor monitor) throws JavaModelException {
903         if (!isPrimary())
904           return this;
905
906         JavaModelManager manager = JavaModelManager.getJavaModelManager();
907
908         CompilationUnit workingCopy = new CompilationUnit((PackageFragment) getParent(), getElementName(), workingCopyOwner);
909         JavaModelManager.PerWorkingCopyInfo perWorkingCopyInfo = manager.getPerWorkingCopyInfo(workingCopy, false/* don't create */,
910                 true/* record usage */, null/* not used since don't create */);
911         if (perWorkingCopyInfo != null) {
912           return perWorkingCopyInfo.getWorkingCopy(); // return existing handle instead of the one created above
913         }
914         BecomeWorkingCopyOperation op = new BecomeWorkingCopyOperation(workingCopy, problemRequestor);
915         op.runOperation(monitor);
916         return workingCopy;
917   }
918
919   /**
920    * If I am not open, return true to avoid parsing.
921    *
922    * @see IParent#hasChildren()
923    */
924   public boolean hasChildren() throws JavaModelException {
925         //      if (isOpen()) {
926         //              return getChildren().length > 0;
927         //      } else {
928         //              return true;
929         //      }
930         return false;
931   }
932
933   /**
934    * @see Openable#hasBuffer()
935    */
936   protected boolean hasBuffer() {
937         return true;
938   }
939
940   /*
941    * @see ICompilationUnit#hasResourceChanged()
942    */
943   public boolean hasResourceChanged() {
944         if (!isWorkingCopy())
945           return false;
946
947         // if resource got deleted, then #getModificationStamp() will answer IResource.NULL_STAMP, which is always different from the
948         // cached
949         // timestamp
950         Object info = JavaModelManager.getJavaModelManager().getInfo(this);
951         if (info == null)
952           return false;
953         return ((CompilationUnitElementInfo) info).timestamp != getResource().getModificationStamp();
954   }
955
956   /**
957    * @see IWorkingCopy#isBasedOn(IResource)
958    * @deprecated
959    */
960   public boolean isBasedOn(IResource resource) {
961         if (!isWorkingCopy())
962           return false;
963         if (!getResource().equals(resource))
964           return false;
965         return !hasResourceChanged();
966   }
967
968   /**
969    * @see IOpenable#isConsistent()
970    */
971   public boolean isConsistent() {
972         return JavaModelManager.getJavaModelManager().getElementsOutOfSynchWithBuffers().get(this) == null;
973   }
974
975   /**
976    *
977    * @see IOpenable
978    */
979   public boolean isOpen() {
980         Object info = JavaModelManager.getJavaModelManager().getInfo(this);
981         return info != null && ((CompilationUnitElementInfo) info).isOpen();
982   }
983
984   public boolean isPrimary() {
985         return this.owner == DefaultWorkingCopyOwner.PRIMARY;
986   }
987
988   /**
989    * @see Openable#isSourceElement()
990    */
991   protected boolean isSourceElement() {
992         return true;
993   }
994
995   protected boolean isValidCompilationUnit() {
996         IPackageFragmentRoot root = getPackageFragmentRoot();
997         try {
998           if (root.getKind() != IPackageFragmentRoot.K_SOURCE)
999                 return false;
1000         } catch (JavaModelException e) {
1001           return false;
1002         }
1003         //      IResource resource = getResource();
1004         //      if (resource != null) {
1005         //              char[][] inclusionPatterns = ((PackageFragmentRoot)root).fullInclusionPatternChars();
1006         //              char[][] exclusionPatterns = ((PackageFragmentRoot)root).fullExclusionPatternChars();
1007         //              if (ProjectPrefUtil.isExcluded(resource, inclusionPatterns, exclusionPatterns)) return false;
1008         //      }
1009         if (!Util.isValidCompilationUnitName(getElementName()))
1010           return false;
1011         return true;
1012   }
1013
1014   /*
1015    * @see ICompilationUnit#isWorkingCopy()
1016    */
1017   public boolean isWorkingCopy() {
1018         // For backward compatibility, non primary working copies are always returning true; in removal
1019         // delta, clients can still check that element was a working copy before being discarded.
1020         return !isPrimary() || getPerWorkingCopyInfo() != null;
1021   }
1022
1023   /**
1024    * @see IOpenable#makeConsistent(IProgressMonitor)
1025    */
1026   public void makeConsistent(IProgressMonitor monitor) throws JavaModelException {
1027         makeConsistent(false/* don't create AST */, 0, monitor);
1028   }
1029
1030   public Object makeConsistent(boolean createAST, int astLevel, IProgressMonitor monitor) throws JavaModelException {
1031         if (isConsistent())
1032           return null;
1033
1034         // create a new info and make it the current info
1035         // (this will remove the info and its children just before storing the new infos)
1036         //      if (createAST) {
1037         //              ASTHolderCUInfo info = new ASTHolderCUInfo();
1038         //              info.astLevel = astLevel;
1039         //              openWhenClosed(info, monitor);
1040         //              net.sourceforge.phpdt.core.dom.CompilationUnit result = info.ast;
1041         //              info.ast = null;
1042         //              return result;
1043         //      } else {
1044         openWhenClosed(createElementInfo(), monitor);
1045         return null;
1046         //      }
1047   }
1048
1049   //public net.sourceforge.phpdt.core.dom.CompilationUnit makeConsistent(boolean createAST, int astLevel, IProgressMonitor monitor)
1050   // throws JavaModelException {
1051   //    if (isConsistent()) return null;
1052   //
1053   //    // create a new info and make it the current info
1054   //    // (this will remove the info and its children just before storing the new infos)
1055   //    if (createAST) {
1056   //            ASTHolderCUInfo info = new ASTHolderCUInfo();
1057   //            info.astLevel = astLevel;
1058   //            openWhenClosed(info, monitor);
1059   //            net.sourceforge.phpdt.core.dom.CompilationUnit result = info.ast;
1060   //            info.ast = null;
1061   //            return result;
1062   //    } else {
1063   //            openWhenClosed(createElementInfo(), monitor);
1064   //            return null;
1065   //    }
1066   //}
1067
1068   /**
1069    * @see ISourceManipulation#move(IJavaElement, IJavaElement, String, boolean, IProgressMonitor)
1070    */
1071   public void move(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor)
1072           throws JavaModelException {
1073         if (container == null) {
1074           throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$
1075         }
1076         IJavaElement[] elements = new IJavaElement[] { this };
1077         IJavaElement[] containers = new IJavaElement[] { container };
1078
1079         String[] renamings = null;
1080         if (rename != null) {
1081           renamings = new String[] { rename };
1082         }
1083         getJavaModel().move(elements, containers, null, renamings, force, monitor);
1084   }
1085
1086   ///**
1087   // * @see Openable#openBuffer(IProgressMonitor)
1088   // */
1089   //protected IBuffer openBuffer(IProgressMonitor pm) throws JavaModelException {
1090   //
1091   //    // create buffer - compilation units only use default buffer factory
1092   //    BufferManager bufManager = getBufferManager();
1093   //    IBuffer buffer = getBufferFactory().createBuffer(this);
1094   //    if (buffer == null) return null;
1095   //
1096   //    // set the buffer source
1097   //    if (buffer.getCharacters() == null){
1098   //            IFile file = (IFile)this.getResource();
1099   //            if (file == null || !file.exists()) throw newNotPresentException();
1100   //            buffer.setContents(ProjectPrefUtil.getResourceContentsAsCharArray(file));
1101   //    }
1102   //
1103   //    // add buffer to buffer cache
1104   //    bufManager.addBuffer(buffer);
1105   //
1106   //    // listen to buffer changes
1107   //    buffer.addBufferChangedListener(this);
1108   //
1109   //    return buffer;
1110   //}
1111   /**
1112    * @see Openable#openBuffer(IProgressMonitor, Object)
1113    */
1114   protected IBuffer openBuffer(IProgressMonitor pm, Object info) throws JavaModelException {
1115
1116         // create buffer
1117         boolean isWorkingCopy = isWorkingCopy();
1118         IBuffer buffer = isWorkingCopy ? this.owner.createBuffer(this) : BufferManager.getDefaultBufferManager().createBuffer(this);
1119         if (buffer == null)
1120           return null;
1121
1122         // set the buffer source
1123         if (buffer.getCharacters() == null) {
1124           if (isWorkingCopy) {
1125                 ICompilationUnit original;
1126                 if (!isPrimary()
1127                         && (original = new CompilationUnit((PackageFragment) getParent(), getElementName(), DefaultWorkingCopyOwner.PRIMARY))
1128                                 .isOpen()) {
1129                   buffer.setContents(original.getSource());
1130                 } else {
1131                   IFile file = (IFile) getResource();
1132                   if (file == null || !file.exists()) {
1133                         // initialize buffer with empty contents
1134                         buffer.setContents(CharOperation.NO_CHAR);
1135                   } else {
1136                         buffer.setContents(Util.getResourceContentsAsCharArray(file));
1137                   }
1138                 }
1139           } else {
1140                 IFile file = (IFile) this.getResource();
1141                 if (file == null || !file.exists())
1142                   throw newNotPresentException();
1143                 buffer.setContents(Util.getResourceContentsAsCharArray(file));
1144           }
1145         }
1146
1147         // add buffer to buffer cache
1148         BufferManager bufManager = getBufferManager();
1149         bufManager.addBuffer(buffer);
1150
1151         // listen to buffer changes
1152         buffer.addBufferChangedListener(this);
1153
1154         return buffer;
1155   }
1156
1157   /*
1158    * @see Openable#openParent
1159    */
1160   protected void openParent(Object childInfo, HashMap newElements, IProgressMonitor pm) throws JavaModelException {
1161         try {
1162           super.openParent(childInfo, newElements, pm);
1163         } catch (JavaModelException e) {
1164           // allow parent to not exist for working copies defined outside classpath
1165           if (!isWorkingCopy() && !e.isDoesNotExist()) {
1166                 throw e;
1167           }
1168         }
1169   }
1170
1171   /**
1172    * @see ICompilationUnit#reconcile()
1173    * @deprecated
1174    */
1175   public IMarker[] reconcile() throws JavaModelException {
1176         reconcile(NO_AST, false/* don't force problem detection */, null/* use primary owner */, null/* no progress monitor */);
1177         return null;
1178   }
1179
1180   /**
1181    * @see ICompilationUnit#reconcile(int, boolean, WorkingCopyOwner, IProgressMonitor)
1182    */
1183   public void reconcile(boolean forceProblemDetection, IProgressMonitor monitor) throws JavaModelException {
1184         reconcile(NO_AST, forceProblemDetection, null/* use primary owner */, monitor);
1185   }
1186
1187   /**
1188    * @see ICompilationUnit#reconcile(int, boolean, WorkingCopyOwner, IProgressMonitor)
1189    * @since 3.0
1190    */
1191   //public net.sourceforge.phpdt.core.dom.CompilationUnit reconcile(
1192   public Object reconcile(int astLevel, boolean forceProblemDetection, WorkingCopyOwner workingCopyOwner, IProgressMonitor monitor)
1193           throws JavaModelException {
1194
1195         if (!isWorkingCopy())
1196           return null; // Reconciling is not supported on non working copies
1197         if (workingCopyOwner == null)
1198           workingCopyOwner = DefaultWorkingCopyOwner.PRIMARY;
1199
1200         boolean createAST = false;
1201         //      if (astLevel == AST.JLS2) {
1202         //              // client asking for level 2 AST; these are supported
1203         //              createAST = true;
1204         //      } else if (astLevel == AST.JLS3) {
1205         //              // client asking for level 3 ASTs; these are not supported
1206         //              // TODO (jerome) - these should also be supported in 1.5 stream
1207         //              createAST = false;
1208         //      } else {
1209         //              // client asking for no AST (0) or unknown ast level
1210         //              // either way, request denied
1211         //              createAST = false;
1212         //      }
1213         ReconcileWorkingCopyOperation op = new ReconcileWorkingCopyOperation(this, createAST, astLevel, forceProblemDetection,
1214                 workingCopyOwner);
1215         op.runOperation(monitor);
1216         //      return op.ast;
1217         return null;
1218   }
1219
1220   /**
1221    * @see ISourceManipulation#rename(String, boolean, IProgressMonitor)
1222    */
1223   public void rename(String name, boolean force, IProgressMonitor monitor) throws JavaModelException {
1224         if (name == null) {
1225           throw new IllegalArgumentException(Util.bind("operation.nullName")); //$NON-NLS-1$
1226         }
1227         IJavaElement[] elements = new IJavaElement[] { this };
1228         IJavaElement[] dests = new IJavaElement[] { this.getParent() };
1229         String[] renamings = new String[] { name };
1230         getJavaModel().rename(elements, dests, renamings, force, monitor);
1231   }
1232
1233   /*
1234    * @see ICompilationUnit
1235    */
1236   public void restore() throws JavaModelException {
1237
1238         if (!isWorkingCopy())
1239           return;
1240
1241         CompilationUnit original = (CompilationUnit) getOriginalElement();
1242         IBuffer buffer = this.getBuffer();
1243         if (buffer == null)
1244           return;
1245         buffer.setContents(original.getContents());
1246         updateTimeStamp(original);
1247         makeConsistent(null);
1248   }
1249
1250 //  /**
1251 //   * @see ICodeAssist#codeComplete(int, ICodeCompletionRequestor)
1252 //   * @deprecated - use codeComplete(int, ICompletionRequestor)
1253 //   */
1254   //public void codeComplete(int offset, final ICodeCompletionRequestor requestor) throws JavaModelException {
1255   //
1256   //    if (requestor == null){
1257   //            codeComplete(offset, (ICompletionRequestor)null);
1258   //            return;
1259   //    }
1260   //    codeComplete(
1261   //            offset,
1262   //            new ICompletionRequestor(){
1263   //                    public void acceptAnonymousType(char[] superTypePackageName,char[] superTypeName,char[][] parameterPackageNames,char[][]
1264   // parameterTypeNames,char[][] parameterNames,char[] completionName,int modifiers,int completionStart,int completionEnd, int
1265   // relevance){
1266   //                    }
1267   //                    public void acceptClass(char[] packageName, char[] className, char[] completionName, int modifiers, int completionStart, int
1268   // completionEnd, int relevance) {
1269   //                            requestor.acceptClass(packageName, className, completionName, modifiers, completionStart, completionEnd);
1270   //                    }
1271   //                    public void acceptError(IProblem error) {
1272   //                            if (true) return; // was disabled in 1.0
1273   //
1274   //                            try {
1275   //                                    IMarker marker = ResourcesPlugin.getWorkspace().getRoot().createMarker(IJavaModelMarker.TRANSIENT_PROBLEM);
1276   //                                    marker.setAttribute(IJavaModelMarker.ID, error.getID());
1277   //                                    marker.setAttribute(IMarker.CHAR_START, error.getSourceStart());
1278   //                                    marker.setAttribute(IMarker.CHAR_END, error.getSourceEnd() + 1);
1279   //                                    marker.setAttribute(IMarker.LINE_NUMBER, error.getSourceLineNumber());
1280   //                                    marker.setAttribute(IMarker.MESSAGE, error.getMessage());
1281   //                                    marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
1282   //                                    requestor.acceptError(marker);
1283   //                            } catch(CoreException e){
1284   //                            }
1285   //                    }
1286   //                    public void acceptField(char[] declaringTypePackageName, char[] declaringTypeName, char[] name, char[] typePackageName, char[]
1287   // typeName, char[] completionName, int modifiers, int completionStart, int completionEnd, int relevance) {
1288   //                            requestor.acceptField(declaringTypePackageName, declaringTypeName, name, typePackageName, typeName, completionName, modifiers,
1289   // completionStart, completionEnd);
1290   //                    }
1291   //                    public void acceptInterface(char[] packageName,char[] interfaceName,char[] completionName,int modifiers,int completionStart,int
1292   // completionEnd, int relevance) {
1293   //                            requestor.acceptInterface(packageName, interfaceName, completionName, modifiers, completionStart, completionEnd);
1294   //                    }
1295   //                    public void acceptKeyword(char[] keywordName,int completionStart,int completionEnd, int relevance){
1296   //                            requestor.acceptKeyword(keywordName, completionStart, completionEnd);
1297   //                    }
1298   //                    public void acceptLabel(char[] labelName,int completionStart,int completionEnd, int relevance){
1299   //                            requestor.acceptLabel(labelName, completionStart, completionEnd);
1300   //                    }
1301   //                    public void acceptLocalVariable(char[] name,char[] typePackageName,char[] typeName,int modifiers,int completionStart,int
1302   // completionEnd, int relevance){
1303   //                            // ignore
1304   //                    }
1305   //                    public void acceptMethod(char[] declaringTypePackageName,char[] declaringTypeName,char[] selector,char[][]
1306   // parameterPackageNames,char[][] parameterTypeNames,char[][] parameterNames,char[] returnTypePackageName,char[]
1307   // returnTypeName,char[] completionName,int modifiers,int completionStart,int completionEnd, int relevance){
1308   //                            // skip parameter names
1309   //                            requestor.acceptMethod(declaringTypePackageName, declaringTypeName, selector, parameterPackageNames, parameterTypeNames,
1310   // returnTypePackageName, returnTypeName, completionName, modifiers, completionStart, completionEnd);
1311   //                    }
1312   //                    public void acceptMethodDeclaration(char[] declaringTypePackageName,char[] declaringTypeName,char[] selector,char[][]
1313   // parameterPackageNames,char[][] parameterTypeNames,char[][] parameterNames,char[] returnTypePackageName,char[]
1314   // returnTypeName,char[] completionName,int modifiers,int completionStart,int completionEnd, int relevance){
1315   //                            // ignore
1316   //                    }
1317   //                    public void acceptModifier(char[] modifierName,int completionStart,int completionEnd, int relevance){
1318   //                            requestor.acceptModifier(modifierName, completionStart, completionEnd);
1319   //                    }
1320   //                    public void acceptPackage(char[] packageName,char[] completionName,int completionStart,int completionEnd, int relevance){
1321   //                            requestor.acceptPackage(packageName, completionName, completionStart, completionEnd);
1322   //                    }
1323   //                    public void acceptType(char[] packageName,char[] typeName,char[] completionName,int completionStart,int completionEnd, int
1324   // relevance){
1325   //                            requestor.acceptType(packageName, typeName, completionName, completionStart, completionEnd);
1326   //                    }
1327   //                    public void acceptVariableName(char[] typePackageName,char[] typeName,char[] name,char[] completionName,int completionStart,int
1328   // completionEnd, int relevance){
1329   //                            // ignore
1330   //                    }
1331   //            });
1332   //}
1333   ///**
1334   // * @see JavaElement#rootedAt(IJavaProject)
1335   // */
1336   //public IJavaElement rootedAt(IJavaProject project) {
1337   //    return
1338   //            new CompilationUnit(
1339   //                    (IPackageFragment)((JavaElement)parent).rootedAt(project),
1340   //                    name);
1341   //}
1342
1343   /*
1344    * Assume that this is a working copy
1345    */
1346   protected void updateTimeStamp(CompilationUnit original) throws JavaModelException {
1347         long timeStamp = ((IFile) original.getResource()).getModificationStamp();
1348         if (timeStamp == IResource.NULL_STAMP) {
1349           throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_RESOURCE));
1350         }
1351         ((CompilationUnitElementInfo) getElementInfo()).timestamp = timeStamp;
1352   }
1353 }