View | Details | Raw Unified | Return to bug 125504 | Differences between
and this patch

Collapse All | Expand All

(-)model/org/eclipse/jdt/internal/core/SourceRefElement.java (+6 lines)
Lines 151-156 Link Here
151
	String token = memento.nextToken();
151
	String token = memento.nextToken();
152
	return getHandleFromMemento(token, memento, owner);
152
	return getHandleFromMemento(token, memento, owner);
153
}
153
}
154
/**
155
 * @see IMember
156
 */
157
public ISourceUnit getSourceUnit() {
158
	return ((JavaElement)getParent()).getSourceUnit();
159
}
154
/*
160
/*
155
 * @see IMember#getOccurrenceCount()
161
 * @see IMember#getOccurrenceCount()
156
 */
162
 */
(-)model/org/eclipse/jdt/internal/core/ClassFile.java (+9 lines)
Lines 239-244 Link Here
239
	}
239
	}
240
	return elt;
240
	return elt;
241
}
241
}
242
public IType findPrimaryType() {
243
	return getType();
244
}
242
public String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException {
245
public String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException {
243
	return this.getType().getAttachedJavadoc(monitor);
246
	return this.getType().getAttachedJavadoc(monitor);
244
}
247
}
Lines 327-332 Link Here
327
	return this;
330
	return this;
328
}
331
}
329
/**
332
/**
333
 * @see IMember
334
 */
335
public ISourceUnit getSourceUnit() {
336
	return this;
337
}
338
/**
330
 * A class file has a corresponding resource unless it is contained
339
 * A class file has a corresponding resource unless it is contained
331
 * in a jar.
340
 * in a jar.
332
 *
341
 *
(-)model/org/eclipse/jdt/internal/core/JavaElement.java (+6 lines)
Lines 294-299 Link Here
294
	 */
294
	 */
295
	protected abstract char getHandleMementoDelimiter();
295
	protected abstract char getHandleMementoDelimiter();
296
	/**
296
	/**
297
	 * @see IMember
298
	 */
299
	public ISourceUnit getSourceUnit() {
300
		return null;
301
	}
302
	/**
297
	 * @see IJavaElement
303
	 * @see IJavaElement
298
	 */
304
	 */
299
	public IJavaModel getJavaModel() {
305
	public IJavaModel getJavaModel() {
(-)model/org/eclipse/jdt/internal/core/CompilationUnit.java (+12 lines)
Lines 715-720 Link Here
715
	System.arraycopy(elements, 0, imports, 0, length);
715
	System.arraycopy(elements, 0, imports, 0, length);
716
	return imports;
716
	return imports;
717
}
717
}
718
/*
719
 * @see IMember
720
 */
721
public ISourceUnit getSourceUnit() {
722
	return this;
723
}
718
/**
724
/**
719
 * @see org.eclipse.jdt.internal.compiler.env.ICompilationUnit#getMainTypeName()
725
 * @see org.eclipse.jdt.internal.compiler.env.ICompilationUnit#getMainTypeName()
720
 */
726
 */
Lines 880-885 Link Here
880
	return getWorkingCopy(new WorkingCopyOwner() {/*non shared working copy*/}, null/*no problem requestor*/, monitor);
886
	return getWorkingCopy(new WorkingCopyOwner() {/*non shared working copy*/}, null/*no problem requestor*/, monitor);
881
}
887
}
882
/**
888
/**
889
 * @see ISourceUnit#getWorkingCopy(WorkingCopyOwner, IProgressMonitor)
890
 */
891
public ICompilationUnit getWorkingCopy(WorkingCopyOwner workingCopyOwner, IProgressMonitor monitor) throws JavaModelException {
892
	return getWorkingCopy(workingCopyOwner, null, monitor);
893
}
894
/**
883
 * @see IWorkingCopy#getWorkingCopy(IProgressMonitor, IBufferFactory, IProblemRequestor)
895
 * @see IWorkingCopy#getWorkingCopy(IProgressMonitor, IBufferFactory, IProblemRequestor)
884
 * @deprecated
896
 * @deprecated
885
 */
897
 */
(-)dom/org/eclipse/jdt/core/dom/ASTParser.java (-56 / +44 lines)
Lines 17-22 Link Here
17
import org.eclipse.jdt.core.IClassFile;
17
import org.eclipse.jdt.core.IClassFile;
18
import org.eclipse.jdt.core.ICompilationUnit;
18
import org.eclipse.jdt.core.ICompilationUnit;
19
import org.eclipse.jdt.core.IJavaElement;
19
import org.eclipse.jdt.core.IJavaElement;
20
import org.eclipse.jdt.core.ISourceUnit;
20
import org.eclipse.jdt.core.IJavaProject;
21
import org.eclipse.jdt.core.IJavaProject;
21
import org.eclipse.jdt.core.JavaCore;
22
import org.eclipse.jdt.core.JavaCore;
22
import org.eclipse.jdt.core.JavaModelException;
23
import org.eclipse.jdt.core.JavaModelException;
Lines 154-167 Link Here
154
    private char[] rawSource = null;
155
    private char[] rawSource = null;
155
    
156
    
156
    /**
157
    /**
157
     * Java mode compilation unit supplying the source.
158
     * Java model class file or compilation unit supplying the source.
158
     */
159
     */
159
    private ICompilationUnit compilationUnitSource = null;
160
    private ISourceUnit sourceUnit = null;
160
    
161
    /**
162
     * Java model class file supplying the source.
163
     */
164
    private IClassFile classFileSource = null;
165
    
161
    
166
    /**
162
    /**
167
     * Character-based offset into the source string where parsing is to
163
     * Character-based offset into the source string where parsing is to
Lines 218-225 Link Here
218
	private void initializeDefaults() {
214
	private void initializeDefaults() {
219
		this.astKind = K_COMPILATION_UNIT;
215
		this.astKind = K_COMPILATION_UNIT;
220
		this.rawSource = null;
216
		this.rawSource = null;
221
		this.classFileSource = null;
217
		this.sourceUnit = null;
222
		this.compilationUnitSource = null;
223
		this.resolveBindings = false;
218
		this.resolveBindings = false;
224
		this.sourceLength = -1;
219
		this.sourceLength = -1;
225
		this.sourceOffset = 0;
220
		this.sourceOffset = 0;
Lines 452-460 Link Here
452
     */
447
     */
453
	public void setSource(char[] source) {
448
	public void setSource(char[] source) {
454
		this.rawSource = source;
449
		this.rawSource = source;
455
		// clear the others
450
		// clear the other
456
		this.compilationUnitSource = null;
451
		this.sourceUnit = null;
457
		this.classFileSource = null;
458
	}
452
	}
459
453
460
	/**
454
	/**
Lines 467-482 Link Here
467
     * is to be parsed, or <code>null</code> if none
461
     * is to be parsed, or <code>null</code> if none
468
      */
462
      */
469
	public void setSource(ICompilationUnit source) {
463
	public void setSource(ICompilationUnit source) {
470
		this.compilationUnitSource = source;
464
		setSourceUnit(source);
471
		// clear the others
472
		this.rawSource = null;
473
		this.classFileSource = null;
474
		if (source != null) {
475
			this.project = source.getJavaProject();
476
			Map options = this.project.getOptions(true);
477
			options.remove(JavaCore.COMPILER_TASK_TAGS); // no need to parse task tags
478
			this.compilerOptions = options;
479
		}
480
	}
465
	}
481
	
466
	
482
	/**
467
	/**
Lines 491-500 Link Here
491
     * is to be parsed, or <code>null</code> if none
476
     * is to be parsed, or <code>null</code> if none
492
     */
477
     */
493
	public void setSource(IClassFile source) {
478
	public void setSource(IClassFile source) {
494
		this.classFileSource = source;
479
		setSourceUnit(source);
495
		// clear the others
480
	}
481
482
	/* (non-Javadoc)
483
	 * Sets the source code to be parsed.
484
	 */
485
	void setSourceUnit(ISourceUnit source) {
486
		this.sourceUnit = source;
487
		// clear the other
496
		this.rawSource = null;
488
		this.rawSource = null;
497
		this.compilationUnitSource = null;
498
		if (source != null) {
489
		if (source != null) {
499
			this.project = source.getJavaProject();
490
			this.project = source.getJavaProject();
500
			Map options = this.project.getOptions(true);
491
			Map options = this.project.getOptions(true);
Lines 622-630 Link Here
622
	   ASTNode result = null;
613
	   ASTNode result = null;
623
	   if (monitor != null) monitor.beginTask("", 1); //$NON-NLS-1$
614
	   if (monitor != null) monitor.beginTask("", 1); //$NON-NLS-1$
624
		try {
615
		try {
625
			if ((this.rawSource == null)
616
			if (this.rawSource == null && this.sourceUnit == null) {
626
		   	  && (this.compilationUnitSource == null)
627
		   	  && (this.classFileSource == null)) {
628
		   	  throw new IllegalStateException("source not specified"); //$NON-NLS-1$
617
		   	  throw new IllegalStateException("source not specified"); //$NON-NLS-1$
629
		   }
618
		   }
630
	   		result = internalCreateAST(monitor);
619
	   		result = internalCreateAST(monitor);
Lines 781-809 Link Here
781
				CompilationUnitDeclaration compilationUnitDeclaration = null;
770
				CompilationUnitDeclaration compilationUnitDeclaration = null;
782
				try {
771
				try {
783
					NodeSearcher searcher = null;
772
					NodeSearcher searcher = null;
784
					org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = null;
773
					org.eclipse.jdt.internal.compiler.env.ICompilationUnit basicCompilUnit = null;
785
					IJavaElement element = null;
774
					WorkingCopyOwner wcOwner = this.workingCopyOwner;
786
					if (this.compilationUnitSource != null) {
775
					if (this.sourceUnit instanceof ICompilationUnit) {
787
						/*
776
							/*
788
						 * this.compilationUnitSource is an instance of org.eclipse.jdt.internal.core.CompilationUnit that implements
777
							 * this.compilationUnitSource is an instance of org.eclipse.jdt.internal.core.CompilationUnit that implements
789
						 * both org.eclipse.jdt.core.ICompilationUnit and org.eclipse.jdt.internal.compiler.env.ICompilationUnit
778
							 * both org.eclipse.jdt.core.ICompilationUnit and org.eclipse.jdt.internal.compiler.env.ICompilationUnit
790
						 */ 
779
							 */ 
791
						sourceUnit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) this.compilationUnitSource;
780
							basicCompilUnit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) this.sourceUnit;
792
						/*
781
							/*
793
						 * use a BasicCompilation that caches the source instead of using the compilationUnitSource directly
782
							 * use a BasicCompilation that caches the source instead of using the compilationUnitSource directly
794
						 * (if it is a working copy, the source can change between the parse and the AST convertion)
783
							 * (if it is a working copy, the source can change between the parse and the AST convertion)
795
						 * (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=75632)
784
							 * (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=75632)
796
						 */
785
							 */
797
						sourceUnit = new BasicCompilationUnit(sourceUnit.getContents(), sourceUnit.getPackageName(), new String(sourceUnit.getFileName()), this.project);
786
							basicCompilUnit = new BasicCompilationUnit(basicCompilUnit.getContents(), basicCompilUnit.getPackageName(), new String(basicCompilUnit.getFileName()), this.project);
798
						element = this.compilationUnitSource;
787
							wcOwner = ((ICompilationUnit) this.sourceUnit).getOwner();
799
					} else if (this.classFileSource != null) {
788
					} else if (this.sourceUnit instanceof IClassFile) {
800
						try {
789
						try {
801
							String sourceString = this.classFileSource.getSource();
790
							String sourceString = this.sourceUnit.getSource();
802
							if (sourceString == null) {
791
							if (sourceString == null) {
803
								throw new IllegalStateException();
792
								throw new IllegalStateException();
804
							}
793
							}
805
							PackageFragment packageFragment = (PackageFragment) this.classFileSource.getParent();
794
							PackageFragment packageFragment = (PackageFragment) this.sourceUnit.getParent();
806
							BinaryType type = (BinaryType) this.classFileSource.getType();
795
							BinaryType type = (BinaryType) this.sourceUnit.findPrimaryType();
807
							IBinaryType binaryType = (IBinaryType) type.getElementInfo();
796
							IBinaryType binaryType = (IBinaryType) type.getElementInfo();
808
							// file name is used to recreate the Java element, so it has to be the toplevel .class file name
797
							// file name is used to recreate the Java element, so it has to be the toplevel .class file name
809
							char[] fileName = binaryType.getFileName();
798
							char[] fileName = binaryType.getFileName();
Lines 816-830 Link Here
816
								System.arraycopy(suffix, 0, newFileName, firstDollar, suffixLength);
805
								System.arraycopy(suffix, 0, newFileName, firstDollar, suffixLength);
817
								fileName = newFileName;
806
								fileName = newFileName;
818
							}
807
							}
819
							sourceUnit = new BasicCompilationUnit(sourceString.toCharArray(), Util.toCharArrays(packageFragment.names), new String(fileName), this.project);
808
							basicCompilUnit = new BasicCompilationUnit(sourceString.toCharArray(), Util.toCharArrays(packageFragment.names), new String(fileName), this.project);
820
							element = this.classFileSource;
821
						} catch(JavaModelException e) {
809
						} catch(JavaModelException e) {
822
							// an error occured accessing the java element
810
							// an error occured accessing the java element
823
							throw new IllegalStateException();
811
							throw new IllegalStateException();
824
						}
812
						}
825
					} else if (this.rawSource != null) {
813
					} else if (this.rawSource != null) {
826
						needToResolveBindings = this.resolveBindings && this.unitName != null && this.project != null && this.compilerOptions != null;
814
						needToResolveBindings = this.resolveBindings && this.unitName != null && this.project != null && this.compilerOptions != null;
827
						sourceUnit = new BasicCompilationUnit(this.rawSource, null, this.unitName == null ? "" : this.unitName, this.project); //$NON-NLS-1$
815
						basicCompilUnit = new BasicCompilationUnit(this.rawSource, null, this.unitName == null ? "" : this.unitName, this.project); //$NON-NLS-1$
828
					} else {
816
					} else {
829
						throw new IllegalStateException();
817
						throw new IllegalStateException();
830
					}
818
					}
Lines 836-842 Link Here
836
							// parse and resolve
824
							// parse and resolve
837
							compilationUnitDeclaration = 
825
							compilationUnitDeclaration = 
838
								CompilationUnitResolver.resolve(
826
								CompilationUnitResolver.resolve(
839
									sourceUnit,
827
									basicCompilUnit,
840
									this.project,
828
									this.project,
841
									searcher,
829
									searcher,
842
									this.compilerOptions,
830
									this.compilerOptions,
Lines 845-851 Link Here
845
									monitor);
833
									monitor);
846
						} catch (JavaModelException e) {
834
						} catch (JavaModelException e) {
847
							compilationUnitDeclaration = CompilationUnitResolver.parse(
835
							compilationUnitDeclaration = CompilationUnitResolver.parse(
848
									sourceUnit,
836
									basicCompilUnit,
849
									searcher,
837
									searcher,
850
									this.compilerOptions,
838
									this.compilerOptions,
851
									this.statementsRecovery);
839
									this.statementsRecovery);
Lines 853-859 Link Here
853
						}
841
						}
854
					} else {
842
					} else {
855
						compilationUnitDeclaration = CompilationUnitResolver.parse(
843
						compilationUnitDeclaration = CompilationUnitResolver.parse(
856
								sourceUnit,
844
								basicCompilUnit,
857
								searcher,
845
								searcher,
858
								this.compilerOptions,
846
								this.compilerOptions,
859
								this.statementsRecovery);
847
								this.statementsRecovery);
Lines 861-874 Link Here
861
					}
849
					}
862
					CompilationUnit result = CompilationUnitResolver.convert(
850
					CompilationUnit result = CompilationUnitResolver.convert(
863
						compilationUnitDeclaration, 
851
						compilationUnitDeclaration, 
864
						sourceUnit.getContents(),
852
						basicCompilUnit.getContents(),
865
						this.apiLevel, 
853
						this.apiLevel, 
866
						this.compilerOptions,
854
						this.compilerOptions,
867
						needToResolveBindings,
855
						needToResolveBindings,
868
						this.compilationUnitSource == null ? this.workingCopyOwner : this.compilationUnitSource.getOwner(),
856
						wcOwner,
869
						needToResolveBindings ? new DefaultBindingResolver.BindingTables() : null, 
857
						needToResolveBindings ? new DefaultBindingResolver.BindingTables() : null, 
870
						monitor);
858
						monitor);
871
					result.setJavaElement(element);
859
					result.setSourceUnit(this.sourceUnit);
872
					return result;
860
					return result;
873
				} finally {
861
				} finally {
874
					if (compilationUnitDeclaration != null && this.resolveBindings) {
862
					if (compilationUnitDeclaration != null && this.resolveBindings) {
(-)dom/org/eclipse/jdt/core/dom/CompilationUnitResolver.java (-2 / +2 lines)
Lines 362-368 Link Here
362
				
362
				
363
				// convert AST
363
				// convert AST
364
				CompilationUnit node = convert(compilationUnitDeclaration, parser.scanner.getSource(), apiLevel, options, false/*don't resolve binding*/, null/*no owner needed*/, null/*no binding table needed*/, monitor);
364
				CompilationUnit node = convert(compilationUnitDeclaration, parser.scanner.getSource(), apiLevel, options, false/*don't resolve binding*/, null/*no owner needed*/, null/*no binding table needed*/, monitor);
365
				node.setJavaElement(compilationUnits[i]);
365
				node.setSourceUnit(compilationUnits[i]);
366
				
366
				
367
				// accept AST
367
				// accept AST
368
				astRequestor.acceptAST(compilationUnits[i], node);
368
				astRequestor.acceptAST(compilationUnits[i], node);
Lines 703-709 Link Here
703
						ast.setBindingResolver(resolver);
703
						ast.setBindingResolver(resolver);
704
						converter.setAST(ast);
704
						converter.setAST(ast);
705
						CompilationUnit compilationUnit = converter.convert(unit, contents);
705
						CompilationUnit compilationUnit = converter.convert(unit, contents);
706
						compilationUnit.setJavaElement(source);
706
						compilationUnit.setSourceUnit(source);
707
						compilationUnit.setLineEndTable(compilationResult.getLineSeparatorPositions());
707
						compilationUnit.setLineEndTable(compilationResult.getLineSeparatorPositions());
708
						ast.setDefaultNodeFlag(0);
708
						ast.setDefaultNodeFlag(0);
709
						ast.setOriginalModificationCount(ast.modificationCount());
709
						ast.setOriginalModificationCount(ast.modificationCount());
(-)dom/org/eclipse/jdt/core/dom/AST.java (-1 / +1 lines)
Lines 269-275 Link Here
269
	
269
	
270
		CompilationUnit unit = converter.convert(compilationUnitDeclaration, source);
270
		CompilationUnit unit = converter.convert(compilationUnitDeclaration, source);
271
		unit.setLineEndTable(compilationUnitDeclaration.compilationResult.getLineSeparatorPositions());
271
		unit.setLineEndTable(compilationUnitDeclaration.compilationResult.getLineSeparatorPositions());
272
		unit.setJavaElement(workingCopy);
272
		unit.setSourceUnit(workingCopy);
273
		ast.setDefaultNodeFlag(savedDefaultNodeFlag);
273
		ast.setDefaultNodeFlag(savedDefaultNodeFlag);
274
		return unit;
274
		return unit;
275
	}
275
	}
(-)dom/org/eclipse/jdt/core/dom/CompilationUnit.java (-12 / +22 lines)
Lines 18-23 Link Here
18
import java.util.Map;
18
import java.util.Map;
19
19
20
import org.eclipse.jdt.core.IJavaElement;
20
import org.eclipse.jdt.core.IJavaElement;
21
import org.eclipse.jdt.core.ISourceUnit;
21
import org.eclipse.jdt.core.compiler.IProblem;
22
import org.eclipse.jdt.core.compiler.IProblem;
22
import org.eclipse.jdt.internal.compiler.parser.Scanner;
23
import org.eclipse.jdt.internal.compiler.parser.Scanner;
23
import org.eclipse.jface.text.IDocument;
24
import org.eclipse.jface.text.IDocument;
Lines 123-133 Link Here
123
	private DefaultCommentMapper commentMapper = null;
124
	private DefaultCommentMapper commentMapper = null;
124
	
125
	
125
	/**
126
	/**
126
	 * The Java element (an <code>org.eclipse.jdt.core.ICompilationUnit</code> or an <code>org.eclipse.jdt.core.IClassFile</code>) 
127
	 * The Java source unit (an <code>org.eclipse.jdt.core.ICompilationUnit</code> or an <code>org.eclipse.jdt.core.IClassFile</code>) 
127
	 * this compilation unit was created from, or <code>null</code> if it was not created from a Java element.
128
	 * this compilation unit was created from, or <code>null</code> if it was not created from a Java source unit.
128
	 * @since 3.1
129
	 */
129
	 */
130
	private IJavaElement element = null;
130
	private ISourceUnit sourceUnit = null;
131
	
131
	
132
	/**
132
	/**
133
	 * The list of import declarations in textual order order; 
133
	 * The list of import declarations in textual order order; 
Lines 492-498 Link Here
492
	 * @since 3.1
492
	 * @since 3.1
493
	 */
493
	 */
494
	public IJavaElement getJavaElement() {
494
	public IJavaElement getJavaElement() {
495
		return this.element;
495
		return this.sourceUnit;
496
	}
496
	}
497
	
497
	
498
	/**
498
	/**
Lines 610-615 Link Here
610
	public IProblem[] getProblems() {
610
	public IProblem[] getProblems() {
611
		return this.problems;
611
		return this.problems;
612
	}
612
	}
613
	
614
	/**
615
	 * The Java source unit (a {@link org.eclipse.jdt.core.ICompilationUnit compilation unit} or a {@link org.eclipse.jdt.core.IClassFile class file}) 
616
	 * this compilation unit was created from, or <code>null</code> if it was not created from a Java source unit.
617
	 * 
618
	 * @return the Java source unit this compilation unit was created from, or <code>null</code> if none
619
	 * @since 3.3
620
	 */
621
	public ISourceUnit getSourceUnit() {
622
		return this.sourceUnit;
623
	}
613
624
614
	/**
625
	/**
615
	 * Returns the live list of nodes for the import declarations of this 
626
	 * Returns the live list of nodes for the import declarations of this 
Lines 931-946 Link Here
931
			this.optionalCommentList = Collections.unmodifiableList(commentList);
942
			this.optionalCommentList = Collections.unmodifiableList(commentList);
932
		}
943
		}
933
	}
944
	}
934
		
945
935
	/**
946
	/**
936
	 * Sets the Java element (an <code>org.eclipse.jdt.core.ICompilationUnit</code> or an <code>org.eclipse.jdt.core.IClassFile</code>) 
947
	 * Sets the Java source unit (a {@link org.eclipse.jdt.core.ICompilationUnit compilation unit} or a {@link org.eclipse.jdt.core.IClassFile class file})
937
	 * this compilation unit was created from, or <code>null</code> if it was not created from a Java element.
948
	 * this compilation unit was created from, or <code>null</code> if it was not created from a Java source unit.
938
	 * 
949
	 * 
939
	 * @param element the Java element this compilation unit was created from
950
	 * @param sourceUnit the Java source unit this compilation unit was created from
940
	 * @since 3.1
941
	 */
951
	 */
942
	void setJavaElement(IJavaElement element) {
952
	void setSourceUnit(ISourceUnit sourceUnit) {
943
		this.element = element;
953
		this.sourceUnit = sourceUnit;
944
	}
954
	}
945
	
955
	
946
	/**
956
	/**
(-)dom/org/eclipse/jdt/core/dom/rewrite/ImportRewrite.java (-2 / +5 lines)
Lines 21-26 Link Here
21
import org.eclipse.jdt.core.Flags;
21
import org.eclipse.jdt.core.Flags;
22
import org.eclipse.jdt.core.ICompilationUnit;
22
import org.eclipse.jdt.core.ICompilationUnit;
23
import org.eclipse.jdt.core.IImportDeclaration;
23
import org.eclipse.jdt.core.IImportDeclaration;
24
import org.eclipse.jdt.core.IJavaElement;
25
import org.eclipse.jdt.core.ISourceUnit;
24
import org.eclipse.jdt.core.JavaModelException;
26
import org.eclipse.jdt.core.JavaModelException;
25
import org.eclipse.jdt.core.Signature;
27
import org.eclipse.jdt.core.Signature;
26
import org.eclipse.jdt.core.dom.*;
28
import org.eclipse.jdt.core.dom.*;
Lines 182-188 Link Here
182
		if (astRoot == null) {
184
		if (astRoot == null) {
183
			throw new IllegalArgumentException("AST must not be null"); //$NON-NLS-1$
185
			throw new IllegalArgumentException("AST must not be null"); //$NON-NLS-1$
184
		}
186
		}
185
		if (!(astRoot.getJavaElement() instanceof ICompilationUnit)) {
187
		ISourceUnit sourceUnit = astRoot.getSourceUnit();
188
		if (!(sourceUnit instanceof ICompilationUnit)) {
186
			throw new IllegalArgumentException("AST must have been constructed from a Java element"); //$NON-NLS-1$
189
			throw new IllegalArgumentException("AST must have been constructed from a Java element"); //$NON-NLS-1$
187
		}
190
		}
188
		List existingImport= null;
191
		List existingImport= null;
Lines 201-207 Link Here
201
				existingImport.add(buf.toString());
204
				existingImport.add(buf.toString());
202
			}
205
			}
203
		}
206
		}
204
		return new ImportRewrite((ICompilationUnit) astRoot.getJavaElement(), astRoot, existingImport);
207
		return new ImportRewrite((ICompilationUnit) sourceUnit, astRoot, existingImport);
205
	}
208
	}
206
		
209
		
207
	private ImportRewrite(ICompilationUnit cu, CompilationUnit astRoot, List existingImports) {
210
	private ImportRewrite(ICompilationUnit cu, CompilationUnit astRoot, List existingImports) {
(-)dom/org/eclipse/jdt/core/dom/rewrite/ASTRewrite.java (-2 / +5 lines)
Lines 15-20 Link Here
15
import java.util.Map;
15
import java.util.Map;
16
16
17
import org.eclipse.jdt.core.ICompilationUnit;
17
import org.eclipse.jdt.core.ICompilationUnit;
18
import org.eclipse.jdt.core.IJavaElement;
19
import org.eclipse.jdt.core.ISourceUnit;
18
import org.eclipse.jdt.core.JavaCore;
20
import org.eclipse.jdt.core.JavaCore;
19
import org.eclipse.jdt.core.JavaModelException;
21
import org.eclipse.jdt.core.JavaModelException;
20
import org.eclipse.jdt.core.dom.AST;
22
import org.eclipse.jdt.core.dom.AST;
Lines 231-240 Link Here
231
			throw new IllegalArgumentException("This API can only be used if the AST is created from a compilation unit"); //$NON-NLS-1$
233
			throw new IllegalArgumentException("This API can only be used if the AST is created from a compilation unit"); //$NON-NLS-1$
232
		}
234
		}
233
		CompilationUnit astRoot= (CompilationUnit) root;
235
		CompilationUnit astRoot= (CompilationUnit) root;
234
		if (!(astRoot.getJavaElement() instanceof ICompilationUnit)) {
236
		ISourceUnit sourceUnit = astRoot.getSourceUnit();
237
		if (!(sourceUnit instanceof ICompilationUnit)) {
235
			throw new IllegalArgumentException("This API can only be used if the AST is created from a compilation unit"); //$NON-NLS-1$
238
			throw new IllegalArgumentException("This API can only be used if the AST is created from a compilation unit"); //$NON-NLS-1$
236
		}
239
		}
237
		ICompilationUnit cu= (ICompilationUnit) astRoot.getJavaElement();
240
		ICompilationUnit cu= (ICompilationUnit) sourceUnit;
238
		
241
		
239
		char[] content= cu.getBuffer().getCharacters();
242
		char[] content= cu.getBuffer().getCharacters();
240
		LineInformation lineInfo= LineInformation.create(astRoot);
243
		LineInformation lineInfo= LineInformation.create(astRoot);
(-)model/org/eclipse/jdt/core/IMember.java (+8 lines)
Lines 124-129 Link Here
124
 */
124
 */
125
int getOccurrenceCount();
125
int getOccurrenceCount();
126
/**
126
/**
127
 * Returns the Java source unit in which this member is declared.
128
 * This is a handle-only method.
129
 * 
130
 * @return the Java source unit in which this member is declared.
131
 * @since 3.3
132
 */
133
ISourceUnit getSourceUnit();
134
/**
127
 * Returns the local or anonymous type declared in this source member with the given simple name and/or
135
 * Returns the local or anonymous type declared in this source member with the given simple name and/or
128
 * with the specified position relative to the order they are defined in the source.
136
 * with the specified position relative to the order they are defined in the source.
129
 * The name is empty if it is an anonymous type.
137
 * The name is empty if it is an anonymous type.
(-)model/org/eclipse/jdt/core/ICompilationUnit.java (-23 / +1 lines)
Lines 30-36 Link Here
30
 * This interface is not intended to be implemented by clients.
30
 * This interface is not intended to be implemented by clients.
31
 * </p>
31
 * </p>
32
 */
32
 */
33
public interface ICompilationUnit extends IJavaElement, ISourceReference, IParent, IOpenable, IWorkingCopy, ISourceManipulation, ICodeAssist {
33
public interface ICompilationUnit extends ISourceUnit, IWorkingCopy, ISourceManipulation {
34
/**
34
/**
35
 * Constant indicating that a reconcile operation should not return an AST.
35
 * Constant indicating that a reconcile operation should not return an AST.
36
 * @since 3.0
36
 * @since 3.0
Lines 254-267 Link Here
254
 */
254
 */
255
IJavaElement[] findElements(IJavaElement element);
255
IJavaElement[] findElements(IJavaElement element);
256
/**
256
/**
257
 * Finds the primary type of this compilation unit (that is, the type with the same name as the
258
 * compilation unit), or <code>null</code> if no such a type exists.
259
 * 
260
 * @return the found primary type of this compilation unit, or <code>null</code> if no such a type exists
261
 * @since 3.0
262
 */
263
IType findPrimaryType();
264
/**
265
 * Finds the working copy for this compilation unit, given a {@link WorkingCopyOwner}. 
257
 * Finds the working copy for this compilation unit, given a {@link WorkingCopyOwner}. 
266
 * If no working copy has been created for this compilation unit associated with this
258
 * If no working copy has been created for this compilation unit associated with this
267
 * working copy owner, returns <code>null</code>.
259
 * working copy owner, returns <code>null</code>.
Lines 286-305 Link Here
286
 */
278
 */
287
IType[] getAllTypes() throws JavaModelException;
279
IType[] getAllTypes() throws JavaModelException;
288
/**
280
/**
289
 * Returns the smallest element within this compilation unit that 
290
 * includes the given source position (that is, a method, field, etc.), or
291
 * <code>null</code> if there is no element other than the compilation
292
 * unit itself at the given position, or if the given position is not
293
 * within the source range of this compilation unit.
294
 *
295
 * @param position a source position inside the compilation unit
296
 * @return the innermost Java element enclosing a given source position or <code>null</code>
297
 *	if none (excluding the compilation unit).
298
 * @throws JavaModelException if the compilation unit does not exist or if an
299
 *		exception occurs while accessing its corresponding resource
300
 */
301
IJavaElement getElementAt(int position) throws JavaModelException;
302
/**
303
 * Returns the first import declaration in this compilation unit with the given name.
281
 * Returns the first import declaration in this compilation unit with the given name.
304
 * This is a handle-only method. The import declaration may or may not exist. This
282
 * This is a handle-only method. The import declaration may or may not exist. This
305
 * is a convenience method - imports can also be accessed from a compilation unit's
283
 * is a convenience method - imports can also be accessed from a compilation unit's
(-)model/org/eclipse/jdt/core/IClassFile.java (-40 / +2 lines)
Lines 32-38 Link Here
32
 * @see IPackageFragmentRoot#attachSource(org.eclipse.core.runtime.IPath, org.eclipse.core.runtime.IPath, IProgressMonitor)
32
 * @see IPackageFragmentRoot#attachSource(org.eclipse.core.runtime.IPath, org.eclipse.core.runtime.IPath, IProgressMonitor)
33
 */
33
 */
34
 
34
 
35
public interface IClassFile extends IJavaElement, IParent, IOpenable, ISourceReference, ICodeAssist {
35
public interface IClassFile extends ISourceUnit {
36
	
36
	
37
/**
37
/**
38
 * Changes this class file handle into a working copy. A new {@link IBuffer} is
38
 * Changes this class file handle into a working copy. A new {@link IBuffer} is
Lines 80-100 Link Here
80
ICompilationUnit becomeWorkingCopy(IProblemRequestor problemRequestor, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException;
80
ICompilationUnit becomeWorkingCopy(IProblemRequestor problemRequestor, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException;
81
81
82
/**
82
/**
83
 * Returns the smallest element within this class file that 
84
 * includes the given source position (a method, field, etc.), or
85
 * <code>null</code> if there is no element other than the class file
86
 * itself at the given position, or if the given position is not
87
 * within the source range of this class file.
88
 *
89
 * @param position a source position inside the class file
90
 * @return the innermost Java element enclosing a given source position or <code>null</code>
91
 *  if none (excluding the class file).
92
 *
93
 * @exception JavaModelException if this element does not exist or if an
94
 *      exception occurs while accessing its corresponding resource
95
 */
96
IJavaElement getElementAt(int position) throws JavaModelException;
97
/**
98
 * Returns the type contained in this class file.
83
 * Returns the type contained in this class file.
99
 *
84
 *
100
 * @return the type contained in this class file
85
 * @return the type contained in this class file
Lines 105-133 Link Here
105
IType getType() throws JavaModelException;
90
IType getType() throws JavaModelException;
106
/**
91
/**
107
 * Returns a working copy on the source associated with this class file using the given 
92
 * Returns a working copy on the source associated with this class file using the given 
108
 * owner to create the buffer, or <code>null</code> if there is no source associated
109
 * with the class file.
110
 * <p>
111
 * The buffer will be automatically initialized with the source of the class file
112
 * upon creation.
113
 * <p>
114
 * The only valid operations on this working copy are <code>getBuffer()</code> or <code>getPrimary()</code>.
115
 *
116
 * @param owner the owner that creates a buffer that is used to get the content of the working copy
117
 *                 or <code>null</code> if the primary owner should be used
118
 * @param monitor a progress monitor used to report progress while opening this compilation unit
119
 *                 or <code>null</code> if no progress should be reported 
120
 * @return a  a working copy on the source associated with this class file
121
 * @exception JavaModelException if the source of this class file can
122
 *   not be determined. Reasons include:
123
 * <ul>
124
 * <li> This class file does not exist (ELEMENT_DOES_NOT_EXIST)</li>
125
 * </ul>
126
 * @since 3.0
127
 */
128
ICompilationUnit getWorkingCopy(WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException;
129
/**
130
 * Returns a working copy on the source associated with this class file using the given 
131
 * factory to create the buffer, or <code>null</code> if there is no source associated
93
 * factory to create the buffer, or <code>null</code> if there is no source associated
132
 * with the class file.
94
 * with the class file.
133
 * <p>
95
 * <p>
Lines 147-153 Link Here
147
 * <li> This class file does not exist (ELEMENT_DOES_NOT_EXIST)</li>
109
 * <li> This class file does not exist (ELEMENT_DOES_NOT_EXIST)</li>
148
 * </ul>
110
 * </ul>
149
 * @since 2.0
111
 * @since 2.0
150
 * @deprecated Use {@link #getWorkingCopy(WorkingCopyOwner, IProgressMonitor)} instead
112
 * @deprecated Use {@link ISourceUnit#getWorkingCopy(WorkingCopyOwner, IProgressMonitor)} instead
151
 */
113
 */
152
IJavaElement getWorkingCopy(IProgressMonitor monitor, IBufferFactory factory) throws JavaModelException;
114
IJavaElement getWorkingCopy(IProgressMonitor monitor, IBufferFactory factory) throws JavaModelException;
153
/**
115
/**
(-)model/org/eclipse/jdt/core/ISourceUnit.java (+94 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2006 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 org.eclipse.jdt.core;
12
13
import org.eclipse.core.runtime.IProgressMonitor;
14
15
16
/**
17
 * Represents an entire Java source unit (either an <code>ICompilationUnit</code>
18
 * or an <code>IClassFile</code>).
19
 * 
20
 * <p>
21
 * This interface is not intended to be implemented by clients.
22
 * </p>
23
 *
24
 * @see ICompilationUnit
25
 * @see IClassFile
26
 * @since 3.3
27
 */
28
public interface ISourceUnit extends IJavaElement, IParent, IOpenable, ISourceReference, ICodeAssist {
29
30
/**
31
 * Finds the primary type of this Java source unit (that is, the type with the same name as the
32
 * compilation unit, or the type of a class file), or <code>null</code> if no such a type exists.
33
 * 
34
 * @return the found primary type of this Java source unit, or <code>null</code> if no such a type exists
35
 */
36
IType findPrimaryType();
37
38
/**
39
 * Returns the smallest element within this Java source unit that 
40
 * includes the given source position (that is, a method, field, etc.), or
41
 * <code>null</code> if there is no element other than the Java source unit
42
 * itself at the given position, or if the given position is not
43
 * within the source range of the source of this Java source unit.
44
 *
45
 * @param position a source position inside the Java source unit
46
 * @return the innermost Java element enclosing a given source position or <code>null</code>
47
 *	if none (excluding the Java source unit).
48
 * @throws JavaModelException if the Java source unit does not exist or if an
49
 *	exception occurs while accessing its corresponding resource
50
 */
51
IJavaElement getElementAt(int position) throws JavaModelException;
52
	
53
/**
54
 * Returns a shared working copy on this compilation unit or class file using the given working copy owner to create
55
 * the buffer. If this is already a working copy of the given owner, the element itself is returned.
56
 * This API can only answer an already existing working copy if it is based on the same
57
 * original Java source unit AND was using the same working copy owner (that is, as defined by {@link Object#equals}).	 
58
 * <p>
59
 * The life time of a shared working copy is as follows:
60
 * <ul>
61
 * <li>The first call to {@link #getWorkingCopy(WorkingCopyOwner, IProgressMonitor)} 
62
 * 	creates a new working copy for this element</li>
63
 * <li>Subsequent calls increment an internal counter.</li>
64
 * <li>A call to {@link ICompilationUnit#discardWorkingCopy()} decrements the internal counter.</li>
65
 * <li>When this counter is 0, the working copy is discarded.
66
 * </ul>
67
 * So users of this method must discard exactly once the working copy.
68
 * <p>
69
 * Note that the working copy owner will be used for the life time of the shared working copy, that is if the 
70
 * working copy is closed then reopened, this owner will be used.
71
 * The buffer will be automatically initialized with the original's Java source unit content upon creation.
72
 * <p>
73
 * When the shared working copy instance is created, an ADDED IJavaElementDelta is reported on this
74
 * working copy.
75
 * </p><p>
76
 * Since 2.1, a working copy can be created on a not-yet existing compilation
77
 * unit. In particular, such a working copy can then be committed in order to create
78
 * the corresponding compilation unit.
79
 * </p><p>
80
 * Note that possible problems of this working copy are not reported using this method.
81
 * </p>
82
 * 
83
 * @param owner the working copy owner that creates a buffer that is used to get the content 
84
 * 				of the working copy
85
 * @param monitor a progress monitor used to report progress while opening this compilation unit
86
 *                 or <code>null</code> if no progress should be reported 
87
 * @throws JavaModelException if the contents of this element can
88
 *   	not be determined. 
89
 * @return a new working copy of this Java source unit using the given owner to create
90
 *		the buffer, or this Java source unit if it is already a working copy
91
 */
92
ICompilationUnit getWorkingCopy(WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException;
93
94
}

Return to bug 125504