X-Git-Url: http://git.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/core/CompilationUnit.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/core/CompilationUnit.java index 5a56373..3c07bac 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/core/CompilationUnit.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/core/CompilationUnit.java @@ -10,25 +10,46 @@ *******************************************************************************/ package net.sourceforge.phpdt.internal.core; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + import net.sourceforge.phpdt.core.IBuffer; +import net.sourceforge.phpdt.core.IBufferFactory; import net.sourceforge.phpdt.core.ICompilationUnit; import net.sourceforge.phpdt.core.IJavaElement; +import net.sourceforge.phpdt.core.IJavaModelStatusConstants; import net.sourceforge.phpdt.core.IJavaProject; +import net.sourceforge.phpdt.core.IMethod; +import net.sourceforge.phpdt.core.IPackageDeclaration; import net.sourceforge.phpdt.core.IPackageFragment; +import net.sourceforge.phpdt.core.IPackageFragmentRoot; +import net.sourceforge.phpdt.core.IProblemRequestor; +import net.sourceforge.phpdt.core.ISourceRange; +import net.sourceforge.phpdt.core.IType; import net.sourceforge.phpdt.core.JavaModelException; +import net.sourceforge.phpdt.core.Signature; import net.sourceforge.phpdt.core.compiler.CharOperation; +import net.sourceforge.phpdt.core.jdom.IDOMNode; +import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor; +import net.sourceforge.phpdt.internal.compiler.IProblemFactory; +import net.sourceforge.phpdt.internal.compiler.SourceElementParser; +import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory; import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.Path; -/** +/** * @see ICompilationUnit */ -public class CompilationUnit extends Openable implements ICompilationUnit { +public class CompilationUnit extends Openable implements ICompilationUnit, net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit { public static boolean SHARED_WC_VERBOSE = false; @@ -44,9 +65,9 @@ public class CompilationUnit extends Openable implements ICompilationUnit { */ protected CompilationUnit(IPackageFragment parent, String name) { super(COMPILATION_UNIT, parent, name); -// if (!Util.isJavaFileName(name)) { -// throw new IllegalArgumentException(org.eclipse.jdt.internal.core.Util.bind("convention.unit.notJavaName")); //$NON-NLS-1$ -// } + if (!Util.isJavaFileName(name)) { + throw new IllegalArgumentException(net.sourceforge.phpdt.internal.core.Util.bind("convention.unit.notJavaName")); //$NON-NLS-1$ + } } /** * Accepts the given visitor onto the parsed tree of this compilation unit, after @@ -65,67 +86,67 @@ protected CompilationUnit(IPackageFragment parent, String name) { //public void accept(IAbstractSyntaxTreeVisitor visitor) throws JavaModelException { // CompilationUnitVisitor.visit(this, visitor); //} -// -//protected void buildStructure(OpenableElementInfo info, IProgressMonitor monitor) throws JavaModelException { -// -// if (monitor != null && monitor.isCanceled()) return; -// -// // remove existing (old) infos -// removeInfo(); -// -// HashMap newElements = new HashMap(11); -// info.setIsStructureKnown(generateInfos(info, monitor, newElements, getResource())); -// JavaModelManager.getJavaModelManager().getElementsOutOfSynchWithBuffers().remove(this); -// for (Iterator iter = newElements.keySet().iterator(); iter.hasNext();) { -// IJavaElement key = (IJavaElement) iter.next(); -// Object value = newElements.get(key); -// JavaModelManager.getJavaModelManager().putInfo(key, value); -// } -// // add the info for this at the end, to ensure that a getInfo cannot reply null in case the LRU cache needs -// // to be flushed. Might lead to performance issues. -// // see PR 1G2K5S7: ITPJCORE:ALL - NPE when accessing source for a binary type -// JavaModelManager.getJavaModelManager().putInfo(this, info); -//} -// + +protected void buildStructure(OpenableElementInfo info, IProgressMonitor monitor) throws JavaModelException { + + if (monitor != null && monitor.isCanceled()) return; + + // remove existing (old) infos + removeInfo(); + + HashMap newElements = new HashMap(11); + info.setIsStructureKnown(generateInfos(info, monitor, newElements, getResource())); + JavaModelManager.getJavaModelManager().getElementsOutOfSynchWithBuffers().remove(this); + for (Iterator iter = newElements.keySet().iterator(); iter.hasNext();) { + IJavaElement key = (IJavaElement) iter.next(); + Object value = newElements.get(key); + JavaModelManager.getJavaModelManager().putInfo(key, value); + } + // add the info for this at the end, to ensure that a getInfo cannot reply null in case the LRU cache needs + // to be flushed. Might lead to performance issues. + // see PR 1G2K5S7: ITPJCORE:ALL - NPE when accessing source for a binary type + JavaModelManager.getJavaModelManager().putInfo(this, info); +} + ///** // * @see ICodeAssist#codeComplete(int, ICompletionRequestor) // */ //public void codeComplete(int offset, ICompletionRequestor requestor) throws JavaModelException { // codeComplete(this, isWorkingCopy() ? (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) getOriginalElement() : this, offset, requestor); //} -///** -// * @see ICodeAssist#codeSelect(int, int) -// */ +/** + * @see ICodeAssist#codeSelect(int, int) + */ //public IJavaElement[] codeSelect(int offset, int length) throws JavaModelException { // return super.codeSelect(this, offset, length); //} -///** -// * @see IWorkingCopy#commit(boolean, IProgressMonitor) -// */ -//public void commit(boolean force, IProgressMonitor monitor) throws JavaModelException { -// throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, this)); -//} +/** + * @see IWorkingCopy#commit(boolean, IProgressMonitor) + */ +public void commit(boolean force, IProgressMonitor monitor) throws JavaModelException { + throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, this)); +} /** * @see ISourceManipulation#copy(IJavaElement, IJavaElement, String, boolean, IProgressMonitor) */ -//public void copy(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException { -// if (container == null) { -// throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$ -// } -// IJavaElement[] elements = new IJavaElement[] {this}; -// IJavaElement[] containers = new IJavaElement[] {container}; -// String[] renamings = null; -// if (rename != null) { -// renamings = new String[] {rename}; -// } -// getJavaModel().copy(elements, containers, null, renamings, force, monitor); -//} +public void copy(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException { + if (container == null) { + throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$ + } + IJavaElement[] elements = new IJavaElement[] {this}; + IJavaElement[] containers = new IJavaElement[] {container}; + String[] renamings = null; + if (rename != null) { + renamings = new String[] {rename}; + } + getJavaModel().copy(elements, containers, null, renamings, force, monitor); +} /** * Returns a new element info for this element. */ -//protected OpenableElementInfo createElementInfo() { -// return new CompilationUnitElementInfo(); -//} +protected OpenableElementInfo createElementInfo() { + return new CompilationUnitElementInfo(); +} ///** // * @see ICompilationUnit#createImport(String, IJavaElement, IProgressMonitor) // */ @@ -140,12 +161,12 @@ protected CompilationUnit(IPackageFragment parent, String name) { /** * @see ICompilationUnit#createPackageDeclaration(String, IProgressMonitor) */ -//public IPackageDeclaration createPackageDeclaration(String name, IProgressMonitor monitor) throws JavaModelException { -// -// CreatePackageDeclarationOperation op= new CreatePackageDeclarationOperation(name, this); -// runOperation(op, monitor); -// return getPackageDeclaration(name); -//} +public IPackageDeclaration createPackageDeclaration(String name, IProgressMonitor monitor) throws JavaModelException { + + CreatePackageDeclarationOperation op= new CreatePackageDeclarationOperation(name, this); + runOperation(op, monitor); + return getPackageDeclaration(name); +} ///** // * @see ICompilationUnit#createType(String, IJavaElement, boolean, IProgressMonitor) // */ @@ -171,10 +192,10 @@ protected CompilationUnit(IPackageFragment parent, String name) { /** * @see ISourceManipulation#delete(boolean, IProgressMonitor) */ -//public void delete(boolean force, IProgressMonitor monitor) throws JavaModelException { -// IJavaElement[] elements= new IJavaElement[] {this}; -// getJavaModel().delete(elements, force, monitor); -//} +public void delete(boolean force, IProgressMonitor monitor) throws JavaModelException { + IJavaElement[] elements= new IJavaElement[] {this}; + getJavaModel().delete(elements, force, monitor); +} /** * This is not a working copy, do nothing. * @@ -192,46 +213,46 @@ public void destroy() { * * @see Object#equals(java.lang.Object) */ -//public boolean equals(Object o) { -// return super.equals(o) && !((ICompilationUnit)o).isWorkingCopy(); -//} -///** -// * @see JavaElement#equalsDOMNode(IDOMNode) -// */ -//protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException { -// String name = getElementName(); -// if (node.getNodeType() == IDOMNode.COMPILATION_UNIT && name != null ) { -// String nodeName = node.getName(); -// if (nodeName == null) return false; -// if (name.equals(nodeName)) { -// return true; -// } else { -// // iterate through all the types inside the receiver and see if one of them can fit -// IType[] types = getTypes(); -// String typeNodeName = nodeName.substring(0, nodeName.indexOf(".java")); //$NON-NLS-1$ -// for (int i = 0, max = types.length; i < max; i++) { -// if (types[i].getElementName().equals(typeNodeName)) { -// return true; -// } -// } -// } -// } -// return false; -//} +public boolean equals(Object o) { + return super.equals(o) && !((ICompilationUnit)o).isWorkingCopy(); +} +/** + * @see JavaElement#equalsDOMNode(IDOMNode) + */ +protected boolean equalsDOMNode(IDOMNode node) throws JavaModelException { + String name = getElementName(); + if (node.getNodeType() == IDOMNode.COMPILATION_UNIT && name != null ) { + String nodeName = node.getName(); + if (nodeName == null) return false; + if (name.equals(nodeName)) { + return true; + } else { + // iterate through all the types inside the receiver and see if one of them can fit + IType[] types = getTypes(); + String typeNodeName = nodeName.substring(0, nodeName.indexOf(".java")); //$NON-NLS-1$ + for (int i = 0, max = types.length; i < max; i++) { + if (types[i].getElementName().equals(typeNodeName)) { + return true; + } + } + } + } + return false; +} /** * @see IWorkingCopy#findElements(IJavaElement) */ -//public IJavaElement[] findElements(IJavaElement element) { -// ArrayList children = new ArrayList(); -// while (element != null && element.getElementType() != IJavaElement.COMPILATION_UNIT) { -// children.add(element); -// element = element.getParent(); -// } -// if (element == null) return null; -// IJavaElement currentElement = this; -// for (int i = children.size()-1; i >= 0; i--) { -// IJavaElement child = (IJavaElement)children.get(i); -// switch (child.getElementType()) { +public IJavaElement[] findElements(IJavaElement element) { + ArrayList children = new ArrayList(); + while (element != null && element.getElementType() != IJavaElement.COMPILATION_UNIT) { + children.add(element); + element = element.getParent(); + } + if (element == null) return null; + IJavaElement currentElement = this; + for (int i = children.size()-1; i >= 0; i--) { + IJavaElement child = (IJavaElement)children.get(i); + switch (child.getElementType()) { // case IJavaElement.PACKAGE_DECLARATION: // currentElement = ((ICompilationUnit)currentElement).getPackageDeclaration(child.getElementName()); // break; @@ -241,108 +262,109 @@ public void destroy() { // case IJavaElement.IMPORT_DECLARATION: // currentElement = ((IImportContainer)currentElement).getImport(child.getElementName()); // break; -// case IJavaElement.TYPE: -// if (currentElement.getElementType() == IJavaElement.COMPILATION_UNIT) { -// currentElement = ((ICompilationUnit)currentElement).getType(child.getElementName()); -// } else { -// currentElement = ((IType)currentElement).getType(child.getElementName()); -// } -// break; + case IJavaElement.TYPE: + if (currentElement.getElementType() == IJavaElement.COMPILATION_UNIT) { + currentElement = ((ICompilationUnit)currentElement).getType(child.getElementName()); + } else { + currentElement = ((IType)currentElement).getType(child.getElementName()); + } + break; // case IJavaElement.INITIALIZER: // currentElement = ((IType)currentElement).getInitializer(((JavaElement)child).getOccurrenceCount()); // break; -// case IJavaElement.FIELD: -// currentElement = ((IType)currentElement).getField(child.getElementName()); -// break; -// case IJavaElement.METHOD: -// return ((IType)currentElement).findMethods((IMethod)child); -// } -// -// } -// if (currentElement != null && currentElement.exists()) { -// return new IJavaElement[] {currentElement}; -// } else { -// return null; -// } -//} + case IJavaElement.FIELD: + currentElement = ((IType)currentElement).getField(child.getElementName()); + break; + case IJavaElement.METHOD: + return ((IType)currentElement).findMethods((IMethod)child); + } + + } + if (currentElement != null && currentElement.exists()) { + return new IJavaElement[] {currentElement}; + } else { + return null; + } +} /** * @see IWorkingCopy#findPrimaryType() */ -//public IType findPrimaryType() { -// String typeName = Signature.getQualifier(this.getElementName()); -// IType primaryType= this.getType(typeName); -// if (primaryType.exists()) { -// return primaryType; -// } -// return null; -//} +public IType findPrimaryType() { + String typeName = Signature.getQualifier(this.getElementName()); + IType primaryType= this.getType(typeName); + if (primaryType.exists()) { + return primaryType; + } + return null; +} /** * @see IWorkingCopy#findSharedWorkingCopy(IBufferFactory) */ -//public IJavaElement findSharedWorkingCopy(IBufferFactory factory) { -// -// // if factory is null, default factory must be used -// if (factory == null) factory = this.getBufferManager().getDefaultBufferFactory(); -// -// // In order to be shared, working copies have to denote the same compilation unit -// // AND use the same buffer factory. -// // Assuming there is a little set of buffer factories, then use a 2 level Map cache. -// Map sharedWorkingCopies = JavaModelManager.getJavaModelManager().sharedWorkingCopies; -// -// Map perFactoryWorkingCopies = (Map) sharedWorkingCopies.get(factory); -// if (perFactoryWorkingCopies == null) return null; -// return (WorkingCopy)perFactoryWorkingCopies.get(this); -//} +public IJavaElement findSharedWorkingCopy(IBufferFactory factory) { + + // if factory is null, default factory must be used + if (factory == null) factory = this.getBufferManager().getDefaultBufferFactory(); + + // In order to be shared, working copies have to denote the same compilation unit + // AND use the same buffer factory. + // Assuming there is a little set of buffer factories, then use a 2 level Map cache. + Map sharedWorkingCopies = JavaModelManager.getJavaModelManager().sharedWorkingCopies; + + Map perFactoryWorkingCopies = (Map) sharedWorkingCopies.get(factory); + if (perFactoryWorkingCopies == null) return null; + return (WorkingCopy)perFactoryWorkingCopies.get(this); +} + +protected boolean generateInfos(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException { -//protected boolean generateInfos(OpenableElementInfo info, IProgressMonitor pm, Map newElements, IResource underlyingResource) throws JavaModelException { -// // if (getParent() instanceof JarPackageFragment) { // // ignore .java files in jar // throw newNotPresentException(); // } else { -// // put the info now, because getting the contents requires it -// JavaModelManager.getJavaModelManager().putInfo(this, info); -// CompilationUnitElementInfo unitInfo = (CompilationUnitElementInfo) info; -// -// // generate structure -// CompilationUnitStructureRequestor requestor = new CompilationUnitStructureRequestor(this, unitInfo, newElements); -// IProblemFactory factory = new DefaultProblemFactory(); + // put the info now, because getting the contents requires it + JavaModelManager.getJavaModelManager().putInfo(this, info); + CompilationUnitElementInfo unitInfo = (CompilationUnitElementInfo) info; + + // generate structure + CompilationUnitStructureRequestor requestor = new CompilationUnitStructureRequestor(this, unitInfo, newElements); + IProblemFactory factory = new DefaultProblemFactory(); // SourceElementParser parser = new SourceElementParser(requestor, factory, new CompilerOptions(getJavaProject().getOptions(true))); -// requestor.parser = parser; -// parser.parseCompilationUnit(this, false); -// if (isWorkingCopy()) { -// CompilationUnit original = (CompilationUnit) getOriginalElement(); -// // might be IResource.NULL_STAMP if original does not exist -// unitInfo.fTimestamp = ((IFile) original.getResource()).getModificationStamp(); -// } -// return unitInfo.isStructureKnown(); + SourceElementParser parser = new SourceElementParser(requestor, factory); + requestor.parser = parser; + parser.parseCompilationUnit(this, false); + if (isWorkingCopy()) { + CompilationUnit original = (CompilationUnit) getOriginalElement(); + // might be IResource.NULL_STAMP if original does not exist + unitInfo.fTimestamp = ((IFile) original.getResource()).getModificationStamp(); + } + return unitInfo.isStructureKnown(); // } -//} +} /** * @see ICompilationUnit#getAllTypes() */ -//public IType[] getAllTypes() throws JavaModelException { -// IJavaElement[] types = getTypes(); -// int i; -// ArrayList allTypes = new ArrayList(types.length); -// ArrayList typesToTraverse = new ArrayList(types.length); -// for (i = 0; i < types.length; i++) { -// typesToTraverse.add(types[i]); -// } -// while (!typesToTraverse.isEmpty()) { -// IType type = (IType) typesToTraverse.get(0); -// typesToTraverse.remove(type); -// allTypes.add(type); -// types = type.getTypes(); -// for (i = 0; i < types.length; i++) { -// typesToTraverse.add(types[i]); -// } -// } -// IType[] arrayOfAllTypes = new IType[allTypes.size()]; -// allTypes.toArray(arrayOfAllTypes); -// return arrayOfAllTypes; -//} +public IType[] getAllTypes() throws JavaModelException { + IJavaElement[] types = getTypes(); + int i; + ArrayList allTypes = new ArrayList(types.length); + ArrayList typesToTraverse = new ArrayList(types.length); + for (i = 0; i < types.length; i++) { + typesToTraverse.add(types[i]); + } + while (!typesToTraverse.isEmpty()) { + IType type = (IType) typesToTraverse.get(0); + typesToTraverse.remove(type); + allTypes.add(type); + types = type.getTypes(); + for (i = 0; i < types.length; i++) { + typesToTraverse.add(types[i]); + } + } + IType[] arrayOfAllTypes = new IType[allTypes.size()]; + allTypes.toArray(arrayOfAllTypes); + return arrayOfAllTypes; +} /** * @see IMember#getCompilationUnit() */ @@ -366,26 +388,26 @@ public char[] getContents() { * * @see IJavaElement#getCorrespondingResource() */ -//public IResource getCorrespondingResource() throws JavaModelException { -// IPackageFragmentRoot root= (IPackageFragmentRoot)getParent().getParent(); -// if (root.isArchive()) { -// return null; -// } else { -// return getUnderlyingResource(); -// } -//} -///** -// * @see ICompilationUnit#getElementAt(int) -// */ -//public IJavaElement getElementAt(int position) throws JavaModelException { -// -// IJavaElement e= getSourceElementAt(position); -// if (e == this) { -// return null; -// } else { -// return e; -// } -//} +public IResource getCorrespondingResource() throws JavaModelException { + IPackageFragmentRoot root= (IPackageFragmentRoot)getParent().getParent(); + if (root.isArchive()) { + return null; + } else { + return getUnderlyingResource(); + } +} +/** + * @see ICompilationUnit#getElementAt(int) + */ +public IJavaElement getElementAt(int position) throws JavaModelException { + + IJavaElement e= getSourceElementAt(position); + if (e == this) { + return null; + } else { + return e; + } +} public char[] getFileName(){ return getElementName().toCharArray(); } @@ -454,18 +476,18 @@ public IJavaElement getOriginalElement() { /** * @see ICompilationUnit#getPackageDeclaration(String) */ -//public IPackageDeclaration getPackageDeclaration(String name) { -// return new PackageDeclaration(this, name); -//} -///** -// * @see ICompilationUnit#getPackageDeclarations() -// */ -//public IPackageDeclaration[] getPackageDeclarations() throws JavaModelException { -// ArrayList list = getChildrenOfType(PACKAGE_DECLARATION); -// IPackageDeclaration[] array= new IPackageDeclaration[list.size()]; -// list.toArray(array); -// return array; -//} +public IPackageDeclaration getPackageDeclaration(String name) { + return new PackageDeclaration(this, name); +} +/** + * @see ICompilationUnit#getPackageDeclarations() + */ +public IPackageDeclaration[] getPackageDeclarations() throws JavaModelException { + ArrayList list = getChildrenOfType(PACKAGE_DECLARATION); + IPackageDeclaration[] array= new IPackageDeclaration[list.size()]; + list.toArray(array); + return array; +} /** * @see org.eclipse.jdt.internal.compiler.env.ICompilationUnit#getPackageName() */ @@ -475,14 +497,14 @@ public char[][] getPackageName() { /** * @see IJavaElement#getPath() */ -//public IPath getPath() { -// PackageFragmentRoot root = this.getPackageFragmentRoot(); -// if (root.isArchive()) { -// return root.getPath(); -// } else { -// return this.getParent().getPath().append(this.getElementName()); -// } -//} +public IPath getPath() { + PackageFragmentRoot root = this.getPackageFragmentRoot(); + if (root.isArchive()) { + return root.getPath(); + } else { + return this.getParent().getPath().append(this.getElementName()); + } +} /** * @see IJavaElement#getResource() */ @@ -506,81 +528,81 @@ public String getSource() throws JavaModelException { /** * @see ISourceReference#getSourceRange() */ -//public ISourceRange getSourceRange() throws JavaModelException { -// return ((CompilationUnitElementInfo) getElementInfo()).getSourceRange(); -//} -///** -// * @see ICompilationUnit#getType(String) -// */ -//public IType getType(String name) { -// return new SourceType(this, name); -//} -///** -// * @see ICompilationUnit#getTypes() -// */ -//public IType[] getTypes() throws JavaModelException { -// ArrayList list = getChildrenOfType(TYPE); -// IType[] array= new IType[list.size()]; -// list.toArray(array); -// return array; -//} -//public IResource getUnderlyingResource() throws JavaModelException { -// if (FIX_BUG25184) { -// return super.getUnderlyingResource(); -// } else { -// return getResource(); -// } -//} -///** -// * @see IWorkingCopy#getSharedWorkingCopy(IProgressMonitor, IBufferFactory, IProblemRequestor) -// */ -//public IJavaElement getSharedWorkingCopy(IProgressMonitor pm, IBufferFactory factory, IProblemRequestor problemRequestor) throws JavaModelException { -// -// // if factory is null, default factory must be used -// if (factory == null) factory = this.getBufferManager().getDefaultBufferFactory(); -// -// JavaModelManager manager = JavaModelManager.getJavaModelManager(); -// -// // In order to be shared, working copies have to denote the same compilation unit -// // AND use the same buffer factory. -// // Assuming there is a little set of buffer factories, then use a 2 level Map cache. -// Map sharedWorkingCopies = manager.sharedWorkingCopies; -// -// Map perFactoryWorkingCopies = (Map) sharedWorkingCopies.get(factory); -// if (perFactoryWorkingCopies == null){ -// perFactoryWorkingCopies = new HashMap(); -// sharedWorkingCopies.put(factory, perFactoryWorkingCopies); -// } -// WorkingCopy workingCopy = (WorkingCopy)perFactoryWorkingCopies.get(this); -// if (workingCopy != null) { -// workingCopy.useCount++; -// -// if (SHARED_WC_VERBOSE) { -// System.out.println("Incrementing use count of shared working copy " + workingCopy.toStringWithAncestors()); //$NON-NLS-1$ -// } -// -// return workingCopy; -// } else { -// CreateWorkingCopyOperation op = new CreateWorkingCopyOperation(this, perFactoryWorkingCopies, factory, problemRequestor); -// runOperation(op, pm); -// return op.getResultElements()[0]; -// } -//} +public ISourceRange getSourceRange() throws JavaModelException { + return ((CompilationUnitElementInfo) getElementInfo()).getSourceRange(); +} +/** + * @see ICompilationUnit#getType(String) + */ +public IType getType(String name) { + return new SourceType(this, name); +} +/** + * @see ICompilationUnit#getTypes() + */ +public IType[] getTypes() throws JavaModelException { + ArrayList list = getChildrenOfType(TYPE); + IType[] array= new IType[list.size()]; + list.toArray(array); + return array; +} +public IResource getUnderlyingResource() throws JavaModelException { + if (FIX_BUG25184) { + return super.getUnderlyingResource(); + } else { + return getResource(); + } +} +/** + * @see IWorkingCopy#getSharedWorkingCopy(IProgressMonitor, IBufferFactory, IProblemRequestor) + */ +public IJavaElement getSharedWorkingCopy(IProgressMonitor pm, IBufferFactory factory, IProblemRequestor problemRequestor) throws JavaModelException { + + // if factory is null, default factory must be used + if (factory == null) factory = this.getBufferManager().getDefaultBufferFactory(); + + JavaModelManager manager = JavaModelManager.getJavaModelManager(); + + // In order to be shared, working copies have to denote the same compilation unit + // AND use the same buffer factory. + // Assuming there is a little set of buffer factories, then use a 2 level Map cache. + Map sharedWorkingCopies = manager.sharedWorkingCopies; + + Map perFactoryWorkingCopies = (Map) sharedWorkingCopies.get(factory); + if (perFactoryWorkingCopies == null){ + perFactoryWorkingCopies = new HashMap(); + sharedWorkingCopies.put(factory, perFactoryWorkingCopies); + } + WorkingCopy workingCopy = (WorkingCopy)perFactoryWorkingCopies.get(this); + if (workingCopy != null) { + workingCopy.useCount++; + + if (SHARED_WC_VERBOSE) { + System.out.println("Incrementing use count of shared working copy " + workingCopy.toStringWithAncestors()); //$NON-NLS-1$ + } + + return workingCopy; + } else { + CreateWorkingCopyOperation op = new CreateWorkingCopyOperation(this, perFactoryWorkingCopies, factory, problemRequestor); + runOperation(op, pm); + return op.getResultElements()[0]; + } +} /** * @see IWorkingCopy#getWorkingCopy() */ -//public IJavaElement getWorkingCopy() throws JavaModelException { -// return this.getWorkingCopy(null, null, null); -//} +public IJavaElement getWorkingCopy() throws JavaModelException { + return this.getWorkingCopy(null, null, null); +} /** * @see IWorkingCopy#getWorkingCopy(IProgressMonitor, IBufferFactory, IProblemRequestor) */ -//public IJavaElement getWorkingCopy(IProgressMonitor pm, IBufferFactory factory, IProblemRequestor problemRequestor) throws JavaModelException { -// CreateWorkingCopyOperation op = new CreateWorkingCopyOperation(this, null, factory, problemRequestor); -// runOperation(op, pm); -// return op.getResultElements()[0]; -//} +public IJavaElement getWorkingCopy(IProgressMonitor pm, IBufferFactory factory, IProblemRequestor problemRequestor) throws JavaModelException { + CreateWorkingCopyOperation op = new CreateWorkingCopyOperation(this, null, factory, problemRequestor); + runOperation(op, pm); + return op.getResultElements()[0]; +} /** * @see Openable#hasBuffer() @@ -612,9 +634,9 @@ public boolean isBasedOn(IResource resource) { /** * @see IOpenable#isConsistent() */ -//public boolean isConsistent() throws JavaModelException { -// return JavaModelManager.getJavaModelManager().getElementsOutOfSynchWithBuffers().get(this) == null; -//} +public boolean isConsistent() throws JavaModelException { + return JavaModelManager.getJavaModelManager().getElementsOutOfSynchWithBuffers().get(this) == null; +} /** * @see Openable#isSourceElement() */ @@ -630,77 +652,77 @@ public boolean isWorkingCopy() { /** * @see IOpenable#makeConsistent(IProgressMonitor) */ -//public void makeConsistent(IProgressMonitor monitor) throws JavaModelException { -// if (!isConsistent()) { // TODO: this code isn't synchronized with regular opening of a working copy -// // create a new info and make it the current info -// OpenableElementInfo info = createElementInfo(); -// buildStructure(info, monitor); -// } -//} +public void makeConsistent(IProgressMonitor monitor) throws JavaModelException { + if (!isConsistent()) { // TODO: this code isn't synchronized with regular opening of a working copy + // create a new info and make it the current info + OpenableElementInfo info = createElementInfo(); + buildStructure(info, monitor); + } +} /** * @see ISourceManipulation#move(IJavaElement, IJavaElement, String, boolean, IProgressMonitor) */ -//public void move(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException { -// if (container == null) { -// throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$ -// } -// IJavaElement[] elements= new IJavaElement[] {this}; -// IJavaElement[] containers= new IJavaElement[] {container}; -// -// String[] renamings= null; -// if (rename != null) { -// renamings= new String[] {rename}; -// } -// getJavaModel().move(elements, containers, null, renamings, force, monitor); -//} +public void move(IJavaElement container, IJavaElement sibling, String rename, boolean force, IProgressMonitor monitor) throws JavaModelException { + if (container == null) { + throw new IllegalArgumentException(Util.bind("operation.nullContainer")); //$NON-NLS-1$ + } + IJavaElement[] elements= new IJavaElement[] {this}; + IJavaElement[] containers= new IJavaElement[] {container}; + + String[] renamings= null; + if (rename != null) { + renamings= new String[] {rename}; + } + getJavaModel().move(elements, containers, null, renamings, force, monitor); +} /** * @see Openable#openBuffer(IProgressMonitor) */ -//protected IBuffer openBuffer(IProgressMonitor pm) throws JavaModelException { -// -// // create buffer - compilation units only use default buffer factory -// BufferManager bufManager = getBufferManager(); -// IBuffer buffer = getBufferFactory().createBuffer(this); -// if (buffer == null) return null; -// -// // set the buffer source -// if (buffer.getCharacters() == null){ -// IFile file = (IFile)this.getResource(); -// if (file == null || !file.exists()) throw newNotPresentException(); -// buffer.setContents(Util.getResourceContentsAsCharArray(file)); -// } -// -// // add buffer to buffer cache -// bufManager.addBuffer(buffer); -// -// // listen to buffer changes -// buffer.addBufferChangedListener(this); -// -// return buffer; -//} -//protected void openParent(IProgressMonitor pm) throws JavaModelException { -// if (FIX_BUG25184) { -// super.openParent(pm); -// } else { -// try { -// super.openParent(pm); -// } catch(JavaModelException e){ -// // allow parent to not exist for compilation units defined outside classpath -// if (!e.isDoesNotExist()){ -// throw e; -// } -// } -// } -//} -//protected boolean parentExists() { -// if (FIX_BUG25184) { -// return super.parentExists(); -// } else { -// return true; // tolerate units outside classpath -// } -//} +protected IBuffer openBuffer(IProgressMonitor pm) throws JavaModelException { + + // create buffer - compilation units only use default buffer factory + BufferManager bufManager = getBufferManager(); + IBuffer buffer = getBufferFactory().createBuffer(this); + if (buffer == null) return null; + + // set the buffer source + if (buffer.getCharacters() == null){ + IFile file = (IFile)this.getResource(); + if (file == null || !file.exists()) throw newNotPresentException(); + buffer.setContents(Util.getResourceContentsAsCharArray(file)); + } + + // add buffer to buffer cache + bufManager.addBuffer(buffer); + + // listen to buffer changes + buffer.addBufferChangedListener(this); + + return buffer; +} +protected void openParent(IProgressMonitor pm) throws JavaModelException { + if (FIX_BUG25184) { + super.openParent(pm); + } else { + try { + super.openParent(pm); + } catch(JavaModelException e){ + // allow parent to not exist for compilation units defined outside classpath + if (!e.isDoesNotExist()){ + throw e; + } + } + } +} +protected boolean parentExists() { + if (FIX_BUG25184) { + return super.parentExists(); + } else { + return true; // tolerate units outside classpath + } +} /** * @see IWorkingCopy#reconcile() @@ -723,15 +745,15 @@ public void reconcile( /** * @see ISourceManipulation#rename(String, boolean, IProgressMonitor) */ -//public void rename(String name, boolean force, IProgressMonitor monitor) throws JavaModelException { -// if (name == null) { -// throw new IllegalArgumentException(Util.bind("operation.nullName")); //$NON-NLS-1$ -// } -// IJavaElement[] elements= new IJavaElement[] {this}; -// IJavaElement[] dests= new IJavaElement[] {this.getParent()}; -// String[] renamings= new String[] {name}; -// getJavaModel().rename(elements, dests, renamings, force, monitor); -//} +public void rename(String name, boolean force, IProgressMonitor monitor) throws JavaModelException { + if (name == null) { + throw new IllegalArgumentException(Util.bind("operation.nullName")); //$NON-NLS-1$ + } + IJavaElement[] elements= new IJavaElement[] {this}; + IJavaElement[] dests= new IJavaElement[] {this.getParent()}; + String[] renamings= new String[] {name}; + getJavaModel().rename(elements, dests, renamings, force, monitor); +} /** * Does nothing - this is not a working copy. *