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.IJavaFile; |
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 IJavaFile javaFileSource = 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.javaFileSource = 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.javaFileSource = 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 |
setSource((IJavaFile) 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 |
setSource((IJavaFile) source); |
495 |
// clear the others |
480 |
} |
|
|
481 |
|
482 |
/** |
483 |
* Sets the source code to be parsed. |
484 |
* <p>This method automatically sets the project (and compiler |
485 |
* options) based on the given compilation unit of class file, in a manner |
486 |
* equivalent to <code>setProject(source.getJavaProject())</code>.</p> |
487 |
* <p>If source is a class file has without source attachment, the creation of the |
488 |
* ast will fail with an IllegalStateException.</p> |
489 |
* |
490 |
* @param source the Java model compilation unit or class file whose corresponding source code |
491 |
* is to be parsed, or <code>null</code> if none |
492 |
* @since 3.3 |
493 |
*/ |
494 |
public void setSource(IJavaFile source) { |
495 |
this.javaFileSource = source; |
496 |
// clear the other |
496 |
this.rawSource = null; |
497 |
this.rawSource = null; |
497 |
this.compilationUnitSource = null; |
|
|
498 |
if (source != null) { |
498 |
if (source != null) { |
499 |
this.project = source.getJavaProject(); |
499 |
this.project = source.getJavaProject(); |
500 |
Map options = this.project.getOptions(true); |
500 |
Map options = this.project.getOptions(true); |
Lines 623-630
Link Here
|
623 |
if (monitor != null) monitor.beginTask("", 1); //$NON-NLS-1$ |
623 |
if (monitor != null) monitor.beginTask("", 1); //$NON-NLS-1$ |
624 |
try { |
624 |
try { |
625 |
if ((this.rawSource == null) |
625 |
if ((this.rawSource == null) |
626 |
&& (this.compilationUnitSource == null) |
626 |
&& (this.javaFileSource == null)) { |
627 |
&& (this.classFileSource == null)) { |
|
|
628 |
throw new IllegalStateException("source not specified"); //$NON-NLS-1$ |
627 |
throw new IllegalStateException("source not specified"); //$NON-NLS-1$ |
629 |
} |
628 |
} |
630 |
result = internalCreateAST(monitor); |
629 |
result = internalCreateAST(monitor); |
Lines 782-803
Link Here
|
782 |
try { |
781 |
try { |
783 |
NodeSearcher searcher = null; |
782 |
NodeSearcher searcher = null; |
784 |
org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = null; |
783 |
org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = null; |
785 |
IJavaElement element = null; |
784 |
IJavaFile javaFile = null; |
786 |
if (this.compilationUnitSource != null) { |
785 |
if (this.javaFileSource instanceof ICompilationUnit) { |
787 |
sourceUnit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) this.compilationUnitSource; |
786 |
sourceUnit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) this.javaFileSource; |
788 |
// use a BasicCompilation that caches the source instead of using the compilationUnitSource directly |
787 |
// use a BasicCompilation that caches the source instead of using the javaFileSource directly |
789 |
// (if it is a working copy, the source can change between the parse and the AST convertion) |
788 |
// (if it is a working copy, the source can change between the parse and the AST convertion) |
790 |
// (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=75632) |
789 |
// (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=75632) |
791 |
sourceUnit = new BasicCompilationUnit(sourceUnit.getContents(), sourceUnit.getPackageName(), new String(sourceUnit.getFileName()), this.project); |
790 |
sourceUnit = new BasicCompilationUnit(sourceUnit.getContents(), sourceUnit.getPackageName(), new String(sourceUnit.getFileName()), this.project); |
792 |
element = this.compilationUnitSource; |
791 |
javaFile = this.javaFileSource; |
793 |
} else if (this.classFileSource != null) { |
792 |
} else if (this.javaFileSource instanceof IClassFile) { |
794 |
try { |
793 |
try { |
795 |
String sourceString = this.classFileSource.getSource(); |
794 |
String sourceString = this.javaFileSource.getSource(); |
796 |
if (sourceString == null) { |
795 |
if (sourceString == null) { |
797 |
throw new IllegalStateException(); |
796 |
throw new IllegalStateException(); |
798 |
} |
797 |
} |
799 |
PackageFragment packageFragment = (PackageFragment) this.classFileSource.getParent(); |
798 |
PackageFragment packageFragment = (PackageFragment) this.javaFileSource.getParent(); |
800 |
BinaryType type = (BinaryType) this.classFileSource.getType(); |
799 |
BinaryType type = (BinaryType) this.javaFileSource.findPrimaryType(); |
801 |
IBinaryType binaryType = (IBinaryType) type.getElementInfo(); |
800 |
IBinaryType binaryType = (IBinaryType) type.getElementInfo(); |
802 |
// file name is used to recreate the Java element, so it has to be the toplevel .class file name |
801 |
// file name is used to recreate the Java element, so it has to be the toplevel .class file name |
803 |
char[] fileName = binaryType.getFileName(); |
802 |
char[] fileName = binaryType.getFileName(); |
Lines 811-817
Link Here
|
811 |
fileName = newFileName; |
810 |
fileName = newFileName; |
812 |
} |
811 |
} |
813 |
sourceUnit = new BasicCompilationUnit(sourceString.toCharArray(), Util.toCharArrays(packageFragment.names), new String(fileName), this.project); |
812 |
sourceUnit = new BasicCompilationUnit(sourceString.toCharArray(), Util.toCharArrays(packageFragment.names), new String(fileName), this.project); |
814 |
element = this.classFileSource; |
813 |
javaFile = this.javaFileSource; |
815 |
} catch(JavaModelException e) { |
814 |
} catch(JavaModelException e) { |
816 |
// an error occured accessing the java element |
815 |
// an error occured accessing the java element |
817 |
throw new IllegalStateException(); |
816 |
throw new IllegalStateException(); |
Lines 859-868
Link Here
|
859 |
this.apiLevel, |
858 |
this.apiLevel, |
860 |
this.compilerOptions, |
859 |
this.compilerOptions, |
861 |
needToResolveBindings, |
860 |
needToResolveBindings, |
862 |
this.compilationUnitSource == null ? this.workingCopyOwner : this.compilationUnitSource.getOwner(), |
861 |
! (this.javaFileSource instanceof ICompilationUnit) ? this.workingCopyOwner : ((ICompilationUnit) this.javaFileSource).getOwner(), |
863 |
needToResolveBindings ? new DefaultBindingResolver.BindingTables() : null, |
862 |
needToResolveBindings ? new DefaultBindingResolver.BindingTables() : null, |
864 |
monitor); |
863 |
monitor); |
865 |
result.setJavaElement(element); |
864 |
result.setJavaElement(javaFile); |
866 |
return result; |
865 |
return result; |
867 |
} finally { |
866 |
} finally { |
868 |
if (compilationUnitDeclaration != null && this.resolveBindings) { |
867 |
if (compilationUnitDeclaration != null && this.resolveBindings) { |