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 |
setJavaFile(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 |
setJavaFile(source); |
495 |
// clear the others |
480 |
} |
|
|
481 |
|
482 |
/* (non-Javadoc) |
483 |
* Sets the source code to be parsed. |
484 |
*/ |
485 |
void setJavaFile(IJavaFile source) { |
486 |
this.javaFileSource = 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.javaFileSource == 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 782-826
Link Here
|
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 sourceUnit = null; |
785 |
IJavaElement element = null; |
774 |
IJavaFile javaFile = null; |
786 |
if (this.compilationUnitSource != null) { |
775 |
WorkingCopyOwner wcOwner = this.workingCopyOwner; |
787 |
/* |
776 |
if (this.javaFileSource != null) { |
788 |
* this.compilationUnitSource is an instance of org.eclipse.jdt.internal.core.CompilationUnit that implements |
777 |
switch (this.javaFileSource.getElementType()) { |
789 |
* both org.eclipse.jdt.core.ICompilationUnit and org.eclipse.jdt.internal.compiler.env.ICompilationUnit |
778 |
case IJavaElement.COMPILATION_UNIT: |
790 |
*/ |
779 |
/* |
791 |
sourceUnit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) this.compilationUnitSource; |
780 |
* this.compilationUnitSource is an instance of org.eclipse.jdt.internal.core.CompilationUnit that implements |
792 |
/* |
781 |
* both org.eclipse.jdt.core.ICompilationUnit and org.eclipse.jdt.internal.compiler.env.ICompilationUnit |
793 |
* use a BasicCompilation that caches the source instead of using the compilationUnitSource directly |
782 |
*/ |
794 |
* (if it is a working copy, the source can change between the parse and the AST convertion) |
783 |
sourceUnit = (org.eclipse.jdt.internal.compiler.env.ICompilationUnit) this.javaFileSource; |
795 |
* (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=75632) |
784 |
/* |
796 |
*/ |
785 |
* use a BasicCompilation that caches the source instead of using the compilationUnitSource directly |
797 |
sourceUnit = new BasicCompilationUnit(sourceUnit.getContents(), sourceUnit.getPackageName(), new String(sourceUnit.getFileName()), this.project); |
786 |
* (if it is a working copy, the source can change between the parse and the AST convertion) |
798 |
element = this.compilationUnitSource; |
787 |
* (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=75632) |
799 |
} else if (this.classFileSource != null) { |
788 |
*/ |
800 |
try { |
789 |
sourceUnit = new BasicCompilationUnit(sourceUnit.getContents(), sourceUnit.getPackageName(), new String(sourceUnit.getFileName()), this.project); |
801 |
String sourceString = this.classFileSource.getSource(); |
790 |
javaFile = this.javaFileSource; |
802 |
if (sourceString == null) { |
791 |
wcOwner = ((ICompilationUnit) this.javaFileSource).getOwner(); |
|
|
792 |
break; |
793 |
case IJavaElement.CLASS_FILE: |
794 |
try { |
795 |
String sourceString = this.javaFileSource.getSource(); |
796 |
if (sourceString == null) { |
797 |
throw new IllegalStateException(); |
798 |
} |
799 |
PackageFragment packageFragment = (PackageFragment) this.javaFileSource.getParent(); |
800 |
BinaryType type = (BinaryType) this.javaFileSource.findPrimaryType(); |
801 |
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 |
803 |
char[] fileName = binaryType.getFileName(); |
804 |
int firstDollar = CharOperation.indexOf('$', fileName); |
805 |
if (firstDollar != -1) { |
806 |
char[] suffix = SuffixConstants.SUFFIX_class; |
807 |
int suffixLength = suffix.length; |
808 |
char[] newFileName = new char[firstDollar + suffixLength]; |
809 |
System.arraycopy(fileName, 0, newFileName, 0, firstDollar); |
810 |
System.arraycopy(suffix, 0, newFileName, firstDollar, suffixLength); |
811 |
fileName = newFileName; |
812 |
} |
813 |
sourceUnit = new BasicCompilationUnit(sourceString.toCharArray(), Util.toCharArrays(packageFragment.names), new String(fileName), this.project); |
814 |
javaFile = this.javaFileSource; |
815 |
} catch(JavaModelException e) { |
816 |
// an error occured accessing the java element |
817 |
throw new IllegalStateException(); |
818 |
} |
819 |
break; |
820 |
default: |
803 |
throw new IllegalStateException(); |
821 |
throw new IllegalStateException(); |
804 |
} |
|
|
805 |
PackageFragment packageFragment = (PackageFragment) this.classFileSource.getParent(); |
806 |
BinaryType type = (BinaryType) this.classFileSource.getType(); |
807 |
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 |
809 |
char[] fileName = binaryType.getFileName(); |
810 |
int firstDollar = CharOperation.indexOf('$', fileName); |
811 |
if (firstDollar != -1) { |
812 |
char[] suffix = SuffixConstants.SUFFIX_class; |
813 |
int suffixLength = suffix.length; |
814 |
char[] newFileName = new char[firstDollar + suffixLength]; |
815 |
System.arraycopy(fileName, 0, newFileName, 0, firstDollar); |
816 |
System.arraycopy(suffix, 0, newFileName, firstDollar, suffixLength); |
817 |
fileName = newFileName; |
818 |
} |
819 |
sourceUnit = new BasicCompilationUnit(sourceString.toCharArray(), Util.toCharArrays(packageFragment.names), new String(fileName), this.project); |
820 |
element = this.classFileSource; |
821 |
} catch(JavaModelException e) { |
822 |
// an error occured accessing the java element |
823 |
throw new IllegalStateException(); |
824 |
} |
822 |
} |
825 |
} else if (this.rawSource != null) { |
823 |
} else if (this.rawSource != null) { |
826 |
needToResolveBindings = this.resolveBindings && this.unitName != null && this.project != null && this.compilerOptions != null; |
824 |
needToResolveBindings = this.resolveBindings && this.unitName != null && this.project != null && this.compilerOptions != null; |
Lines 865-874
Link Here
|
865 |
this.apiLevel, |
863 |
this.apiLevel, |
866 |
this.compilerOptions, |
864 |
this.compilerOptions, |
867 |
needToResolveBindings, |
865 |
needToResolveBindings, |
868 |
this.compilationUnitSource == null ? this.workingCopyOwner : this.compilationUnitSource.getOwner(), |
866 |
wcOwner, |
869 |
needToResolveBindings ? new DefaultBindingResolver.BindingTables() : null, |
867 |
needToResolveBindings ? new DefaultBindingResolver.BindingTables() : null, |
870 |
monitor); |
868 |
monitor); |
871 |
result.setJavaElement(element); |
869 |
result.setJavaFile(javaFile); |
872 |
return result; |
870 |
return result; |
873 |
} finally { |
871 |
} finally { |
874 |
if (compilationUnitDeclaration != null && this.resolveBindings) { |
872 |
if (compilationUnitDeclaration != null && this.resolveBindings) { |