### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: model/org/eclipse/jdt/internal/core/SourceRefElement.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/SourceRefElement.java,v retrieving revision 1.46 diff -u -r1.46 SourceRefElement.java --- model/org/eclipse/jdt/internal/core/SourceRefElement.java 24 Nov 2005 12:14:48 -0000 1.46 +++ model/org/eclipse/jdt/internal/core/SourceRefElement.java 17 Jul 2006 19:23:48 -0000 @@ -151,6 +151,12 @@ String token = memento.nextToken(); return getHandleFromMemento(token, memento, owner); } +/** + * @see IMember + */ +public IJavaFile getJavaFile() { + return ((JavaElement)getParent()).getJavaFile(); +} /* * @see IMember#getOccurrenceCount() */ Index: model/org/eclipse/jdt/internal/core/CompilationUnit.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/CompilationUnit.java,v retrieving revision 1.233 diff -u -r1.233 CompilationUnit.java --- model/org/eclipse/jdt/internal/core/CompilationUnit.java 17 Jul 2006 11:40:27 -0000 1.233 +++ model/org/eclipse/jdt/internal/core/CompilationUnit.java 17 Jul 2006 19:23:48 -0000 @@ -586,6 +586,12 @@ return this; } /** + * @see IMember#getJavaFile() + */ +public IJavaFile getJavaFile() { + return this; +} +/** * @see org.eclipse.jdt.internal.compiler.env.ICompilationUnit#getContents() */ public char[] getContents() { Index: model/org/eclipse/jdt/internal/core/ClassFile.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClassFile.java,v retrieving revision 1.127 diff -u -r1.127 ClassFile.java --- model/org/eclipse/jdt/internal/core/ClassFile.java 23 Jun 2006 11:16:24 -0000 1.127 +++ model/org/eclipse/jdt/internal/core/ClassFile.java 17 Jul 2006 19:23:46 -0000 @@ -55,6 +55,13 @@ * @see IClassFile#becomeWorkingCopy(IProblemRequestor, WorkingCopyOwner, IProgressMonitor) */ public ICompilationUnit becomeWorkingCopy(IProblemRequestor problemRequestor, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException { + return getWorkingCopy(owner, problemRequestor, monitor); +} + +/* + * @see IClassFile#getWorkingCopy(WorkingCopyOwner, IProblemRequestor, IProgressMonitor) + */ +public ICompilationUnit getWorkingCopy(WorkingCopyOwner owner, IProblemRequestor problemRequestor, IProgressMonitor monitor) throws JavaModelException { JavaModelManager manager = JavaModelManager.getJavaModelManager(); CompilationUnit workingCopy = new ClassFileWorkingCopy(this, owner == null ? DefaultWorkingCopyOwner.PRIMARY : owner); JavaModelManager.PerWorkingCopyInfo perWorkingCopyInfo = manager.getPerWorkingCopyInfo(workingCopy, false/*don't create*/, true /*record usage*/, null/*no problem requestor needed*/); @@ -239,6 +246,9 @@ } return elt; } +public IType findPrimaryType() { + return getType(); +} public String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException { return this.getType().getAttachedJavadoc(monitor); } @@ -327,6 +337,12 @@ return this; } /** + * @see IMember + */ +public IJavaFile getJavaFile() { + return this; +} +/** * A class file has a corresponding resource unless it is contained * in a jar. * Index: model/org/eclipse/jdt/internal/core/JavaElement.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/JavaElement.java,v retrieving revision 1.122 diff -u -r1.122 JavaElement.java --- model/org/eclipse/jdt/internal/core/JavaElement.java 10 May 2006 17:05:15 -0000 1.122 +++ model/org/eclipse/jdt/internal/core/JavaElement.java 17 Jul 2006 19:23:48 -0000 @@ -294,6 +294,12 @@ */ protected abstract char getHandleMementoDelimiter(); /** + * @see IMember + */ + public IJavaFile getJavaFile() { + return null; + } + /** * @see IJavaElement */ public IJavaModel getJavaModel() { Index: dom/org/eclipse/jdt/core/dom/ASTParser.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java,v retrieving revision 1.65 diff -u -r1.65 ASTParser.java --- dom/org/eclipse/jdt/core/dom/ASTParser.java 23 Jun 2006 13:44:54 -0000 1.65 +++ dom/org/eclipse/jdt/core/dom/ASTParser.java 17 Jul 2006 19:23:45 -0000 @@ -17,6 +17,7 @@ import org.eclipse.jdt.core.IClassFile; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IJavaFile; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; @@ -154,14 +155,9 @@ private char[] rawSource = null; /** - * Java mode compilation unit supplying the source. + * Java model class file or compilation unit supplying the source. */ - private ICompilationUnit compilationUnitSource = null; - - /** - * Java model class file supplying the source. - */ - private IClassFile classFileSource = null; + private IJavaFile javaFileSource = null; /** * Character-based offset into the source string where parsing is to @@ -218,8 +214,7 @@ private void initializeDefaults() { this.astKind = K_COMPILATION_UNIT; this.rawSource = null; - this.classFileSource = null; - this.compilationUnitSource = null; + this.javaFileSource = null; this.resolveBindings = false; this.sourceLength = -1; this.sourceOffset = 0; @@ -452,9 +447,8 @@ */ public void setSource(char[] source) { this.rawSource = source; - // clear the others - this.compilationUnitSource = null; - this.classFileSource = null; + // clear the other + this.javaFileSource = null; } /** @@ -467,16 +461,7 @@ * is to be parsed, or null if none */ public void setSource(ICompilationUnit source) { - this.compilationUnitSource = source; - // clear the others - this.rawSource = null; - this.classFileSource = null; - if (source != null) { - this.project = source.getJavaProject(); - Map options = this.project.getOptions(true); - options.remove(JavaCore.COMPILER_TASK_TAGS); // no need to parse task tags - this.compilerOptions = options; - } + setSource((IJavaFile) source); } /** @@ -491,10 +476,25 @@ * is to be parsed, or null if none */ public void setSource(IClassFile source) { - this.classFileSource = source; - // clear the others + setSource((IJavaFile) source); + } + + /** + * Sets the source code to be parsed. + *

This method automatically sets the project (and compiler + * options) based on the given compilation unit of class file, in a manner + * equivalent to setProject(source.getJavaProject()).

+ *

If source is a class file has without source attachment, the creation of the + * ast will fail with an IllegalStateException.

+ * + * @param source the Java model compilation unit or class file whose corresponding source code + * is to be parsed, or null if none + * @since 3.3 + */ + public void setSource(IJavaFile source) { + this.javaFileSource = source; + // clear the other this.rawSource = null; - this.compilationUnitSource = null; if (source != null) { this.project = source.getJavaProject(); Map options = this.project.getOptions(true); @@ -623,8 +623,7 @@ if (monitor != null) monitor.beginTask("", 1); //$NON-NLS-1$ try { if ((this.rawSource == null) - && (this.compilationUnitSource == null) - && (this.classFileSource == null)) { + && (this.javaFileSource == null)) { throw new IllegalStateException("source not specified"); //$NON-NLS-1$ } result = internalCreateAST(monitor); @@ -782,22 +781,22 @@ try { NodeSearcher searcher = null; org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = null; - IJavaElement element = null; - if (this.compilationUnitSource != null) { - sourceUnit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) this.compilationUnitSource; - // use a BasicCompilation that caches the source instead of using the compilationUnitSource directly + IJavaFile javaFile = null; + if (this.javaFileSource instanceof ICompilationUnit) { + sourceUnit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) this.javaFileSource; + // use a BasicCompilation that caches the source instead of using the javaFileSource directly // (if it is a working copy, the source can change between the parse and the AST convertion) // (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=75632) sourceUnit = new BasicCompilationUnit(sourceUnit.getContents(), sourceUnit.getPackageName(), new String(sourceUnit.getFileName()), this.project); - element = this.compilationUnitSource; - } else if (this.classFileSource != null) { + javaFile = this.javaFileSource; + } else if (this.javaFileSource instanceof IClassFile) { try { - String sourceString = this.classFileSource.getSource(); + String sourceString = this.javaFileSource.getSource(); if (sourceString == null) { throw new IllegalStateException(); } - PackageFragment packageFragment = (PackageFragment) this.classFileSource.getParent(); - BinaryType type = (BinaryType) this.classFileSource.getType(); + PackageFragment packageFragment = (PackageFragment) this.javaFileSource.getParent(); + BinaryType type = (BinaryType) this.javaFileSource.findPrimaryType(); IBinaryType binaryType = (IBinaryType) type.getElementInfo(); // file name is used to recreate the Java element, so it has to be the toplevel .class file name char[] fileName = binaryType.getFileName(); @@ -811,7 +810,7 @@ fileName = newFileName; } sourceUnit = new BasicCompilationUnit(sourceString.toCharArray(), Util.toCharArrays(packageFragment.names), new String(fileName), this.project); - element = this.classFileSource; + javaFile = this.javaFileSource; } catch(JavaModelException e) { // an error occured accessing the java element throw new IllegalStateException(); @@ -859,10 +858,10 @@ this.apiLevel, this.compilerOptions, needToResolveBindings, - this.compilationUnitSource == null ? this.workingCopyOwner : this.compilationUnitSource.getOwner(), + ! (this.javaFileSource instanceof ICompilationUnit) ? this.workingCopyOwner : ((ICompilationUnit) this.javaFileSource).getOwner(), needToResolveBindings ? new DefaultBindingResolver.BindingTables() : null, monitor); - result.setJavaElement(element); + result.setJavaElement(javaFile); return result; } finally { if (compilationUnitDeclaration != null && this.resolveBindings) { Index: dom/org/eclipse/jdt/core/dom/CompilationUnit.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/CompilationUnit.java,v retrieving revision 1.83 diff -u -r1.83 CompilationUnit.java --- dom/org/eclipse/jdt/core/dom/CompilationUnit.java 23 Jun 2006 13:44:54 -0000 1.83 +++ dom/org/eclipse/jdt/core/dom/CompilationUnit.java 17 Jul 2006 19:23:45 -0000 @@ -18,6 +18,7 @@ import java.util.Map; import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IJavaFile; import org.eclipse.jdt.core.compiler.IProblem; import org.eclipse.jdt.internal.compiler.parser.Scanner; import org.eclipse.jface.text.IDocument; @@ -123,11 +124,11 @@ private DefaultCommentMapper commentMapper = null; /** - * The Java element (an org.eclipse.jdt.core.ICompilationUnit or an org.eclipse.jdt.core.IClassFile) - * this compilation unit was created from, or null if it was not created from a Java element. + * The Java file (an org.eclipse.jdt.core.ICompilationUnit or an org.eclipse.jdt.core.IClassFile) + * this compilation unit was created from, or null if it was not created from a Java file. * @since 3.1 */ - private IJavaElement element = null; + private IJavaFile javaFile = null; /** * The list of import declarations in textual order order; @@ -492,7 +493,18 @@ * @since 3.1 */ public IJavaElement getJavaElement() { - return this.element; + return this.javaFile; + } + + /** + * The Java file (an org.eclipse.jdt.core.ICompilationUnit or an org.eclipse.jdt.core.IClassFile) + * this compilation unit was created from, or null if it was not created from a Java file. + * + * @return the Java file this compilation unit was created from, or null if none + * @since 3.3 + */ + public IJavaFile getJavaFile() { + return this.javaFile; } /** @@ -936,11 +948,12 @@ * Sets the Java element (an org.eclipse.jdt.core.ICompilationUnit or an org.eclipse.jdt.core.IClassFile) * this compilation unit was created from, or null if it was not created from a Java element. * - * @param element the Java element this compilation unit was created from + * @param javaFile the Java file this compilation unit was created from * @since 3.1 */ - void setJavaElement(IJavaElement element) { - this.element = element; + //TODO: rename to setJavaFile(..) + void setJavaElement(IJavaFile javaFile) { + this.javaFile = javaFile; } /** Index: model/org/eclipse/jdt/core/IMember.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/core/IMember.java,v retrieving revision 1.23 diff -u -r1.23 IMember.java --- model/org/eclipse/jdt/core/IMember.java 10 May 2006 18:03:42 -0000 1.23 +++ model/org/eclipse/jdt/core/IMember.java 17 Jul 2006 19:23:45 -0000 @@ -49,6 +49,14 @@ */ ICompilationUnit getCompilationUnit(); /** + * Returns the Java file in which this member is declared. + * This is a handle-only method. + * + * @return the Java file in which this member is declared + * @since 3.3 + */ +IJavaFile getJavaFile(); +/** * Returns the type in which this member is declared, or null * if this member is not declared in a type (for example, a top-level type). * This is a handle-only method. Index: model/org/eclipse/jdt/core/ICompilationUnit.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/core/ICompilationUnit.java,v retrieving revision 1.57 diff -u -r1.57 ICompilationUnit.java --- model/org/eclipse/jdt/core/ICompilationUnit.java 29 Mar 2006 03:08:46 -0000 1.57 +++ model/org/eclipse/jdt/core/ICompilationUnit.java 17 Jul 2006 19:23:45 -0000 @@ -30,7 +30,7 @@ * This interface is not intended to be implemented by clients. *

*/ -public interface ICompilationUnit extends IJavaElement, ISourceReference, IParent, IOpenable, IWorkingCopy, ISourceManipulation, ICodeAssist { +public interface ICompilationUnit extends IJavaFile, IWorkingCopy, ISourceManipulation { /** * Constant indicating that a reconcile operation should not return an AST. * @since 3.0 @@ -286,20 +286,6 @@ */ IType[] getAllTypes() throws JavaModelException; /** - * Returns the smallest element within this compilation unit that - * includes the given source position (that is, a method, field, etc.), or - * null if there is no element other than the compilation - * unit itself at the given position, or if the given position is not - * within the source range of this compilation unit. - * - * @param position a source position inside the compilation unit - * @return the innermost Java element enclosing a given source position or null - * if none (excluding the compilation unit). - * @throws JavaModelException if the compilation unit does not exist or if an - * exception occurs while accessing its corresponding resource - */ -IJavaElement getElementAt(int position) throws JavaModelException; -/** * Returns the first import declaration in this compilation unit with the given name. * This is a handle-only method. The import declaration may or may not exist. This * is a convenience method - imports can also be accessed from a compilation unit's Index: model/org/eclipse/jdt/core/IClassFile.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/core/IClassFile.java,v retrieving revision 1.20 diff -u -r1.20 IClassFile.java --- model/org/eclipse/jdt/core/IClassFile.java 23 Jun 2006 13:44:54 -0000 1.20 +++ model/org/eclipse/jdt/core/IClassFile.java 17 Jul 2006 19:23:45 -0000 @@ -32,9 +32,15 @@ * @see IPackageFragmentRoot#attachSource(org.eclipse.core.runtime.IPath, org.eclipse.core.runtime.IPath, IProgressMonitor) */ -public interface IClassFile extends IJavaElement, IParent, IOpenable, ISourceReference, ICodeAssist { +public interface IClassFile extends IJavaFile { /** + * @since 3.2 + * @deprecated use {@link #getWorkingCopy(WorkingCopyOwner, IProblemRequestor, IProgressMonitor)} + */ +ICompilationUnit becomeWorkingCopy(IProblemRequestor problemRequestor, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException; + +/** * Changes this class file handle into a working copy. A new {@link IBuffer} is * created using the given owner. Uses the primary owner if null is * specified. @@ -56,7 +62,7 @@ * If this class file was already in working copy mode, an internal counter is incremented and no * other action is taken on this working copy. To bring this working copy back into the original mode * (where it reflects the underlying resource), {@link ICompilationUnit#discardWorkingCopy} must be call as many - * times as {@link #becomeWorkingCopy(IProblemRequestor, WorkingCopyOwner, IProgressMonitor)}. + * times as {@link #getWorkingCopy(WorkingCopyOwner, IProblemRequestor, IProgressMonitor)}. *

*

* The primary compilation unit of a class file's working copy does not exist if the class file is not @@ -66,34 +72,18 @@ * The resource of a class file's working copy is null if the class file is in an external jar file. *

* + * @param owner the given {@link WorkingCopyOwner}, or null for the primary owner * @param problemRequestor a requestor which will get notified of problems detected during * reconciling as they are discovered. The requestor can be set to null indicating * that the client is not interested in problems. - * @param owner the given {@link WorkingCopyOwner}, or null for the primary owner * @param monitor a progress monitor used to report progress while opening this compilation unit * or null if no progress should be reported * @return a working copy for this class file * @throws JavaModelException if this compilation unit could not become a working copy. * @see ICompilationUnit#discardWorkingCopy() - * @since 3.2 - */ -ICompilationUnit becomeWorkingCopy(IProblemRequestor problemRequestor, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException; - -/** - * Returns the smallest element within this class file that - * includes the given source position (a method, field, etc.), or - * null if there is no element other than the class file - * itself at the given position, or if the given position is not - * within the source range of this class file. - * - * @param position a source position inside the class file - * @return the innermost Java element enclosing a given source position or null - * if none (excluding the class file). - * - * @exception JavaModelException if this element does not exist or if an - * exception occurs while accessing its corresponding resource + * @since 3.3 */ -IJavaElement getElementAt(int position) throws JavaModelException; +ICompilationUnit getWorkingCopy(WorkingCopyOwner owner, IProblemRequestor problemRequestor, IProgressMonitor monitor) throws JavaModelException; /** * Returns the type contained in this class file. * Index: model/org/eclipse/jdt/core/IJavaFile.java =================================================================== RCS file: model/org/eclipse/jdt/core/IJavaFile.java diff -N model/org/eclipse/jdt/core/IJavaFile.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ model/org/eclipse/jdt/core/IJavaFile.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,58 @@ +/******************************************************************************* + * Copyright (c) 2006 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.jdt.core; + +import org.eclipse.core.runtime.IProgressMonitor; + + +/** + * Represents an entire Java type container (either an ICompilationUnit + * or an IClassFile). + * + *

+ * This interface is not intended to be implemented by clients. + *

+ * + * @see ICompilationUnit + * @see IClassFile + * @since 3.3 + */ +public interface IJavaFile extends IJavaElement, IParent, IOpenable, ISourceReference, ICodeAssist { + +/* + * TODO: summarize essence of javadoc from subinterfaces + */ +ICompilationUnit getWorkingCopy(WorkingCopyOwner owner, IProblemRequestor problemRequestor, IProgressMonitor monitor) throws JavaModelException; + +/** + * Returns the smallest element within this java file that + * includes the given source position (that is, a method, field, etc.), or + * null if there is no element other than the java file + * itself at the given position, or if the given position is not + * within the source range of this compilation unit. + * + * @param position a source position inside the java file + * @return the innermost Java element enclosing a given source position or null + * if none (excluding the java file). + * @throws JavaModelException if the java file does not exist or if an + * exception occurs while accessing its corresponding resource + */ +IJavaElement getElementAt(int position) throws JavaModelException; + +/** + * Finds the primary type of this java file (that is, the type with the same name as the + * compilation unit, or the type of a class file), or null if no such a type exists. + * + * @return the found primary type of this java file, or null if no such a type exists + */ +IType findPrimaryType(); + +}