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