Skip to content

Commit

Permalink
Add compilation unit resolver extension point and open a minimal set …
Browse files Browse the repository at this point in the history
…of API to be useful

Signed-off-by: Rob Stryker <[email protected]>

Cleanup ASTParser for legibility and to use the new interface

Signed-off-by: Rob Stryker <[email protected]>

Add facade to CompilationUnitResolver

Signed-off-by: Rob Stryker <[email protected]>

exsd doc change

Signed-off-by: Rob Stryker <[email protected]>

Partial revert and cleanup

Signed-off-by: Rob Stryker <[email protected]>
  • Loading branch information
Rob Stryker committed Jun 11, 2024
1 parent e04ddc9 commit 2c5e65b
Show file tree
Hide file tree
Showing 10 changed files with 426 additions and 131 deletions.
78 changes: 21 additions & 57 deletions org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.eclipse.jdt.core.WorkingCopyOwner;
import org.eclipse.jdt.core.compiler.CategorizedProblem;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
import org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall;
import org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath;
Expand Down Expand Up @@ -221,6 +220,8 @@ public static ASTParser newParser(int level) {
*/
private int bits;

private final ICompilationUnitResolver unitResolver;

/**
* Creates a new AST parser for the given API level.
* <p>
Expand All @@ -233,6 +234,7 @@ public static ASTParser newParser(int level) {
ASTParser(int level) {
DOMASTUtil.checkASTLevel(level);
this.apiLevel = level;
this.unitResolver = CompilationUnitResolverDiscovery.getInstance();
initializeDefaults();
}

Expand All @@ -245,10 +247,13 @@ private List<Classpath> getClasspath() throws IllegalStateException {
}
if (this.sourcepaths != null) {
for (int i = 0, max = this.sourcepaths.length; i < max; i++) {
String encoding = this.sourcepathsEncodings == null ? null : this.sourcepathsEncodings[i];
main.processPathEntries(
Main.DEFAULT_SIZE_CLASSPATH,
allClasspaths, this.sourcepaths[i], encoding, true, false);
String sourcePath = this.sourcepaths[i];
if (sourcePath != null) {
String encoding = this.sourcepathsEncodings == null ? null : this.sourcepathsEncodings[i];
main.processPathEntries(
Main.DEFAULT_SIZE_CLASSPATH,
allClasspaths, sourcePath, encoding, true, false);
}
}
}
if (this.classpaths != null) {
Expand Down Expand Up @@ -957,9 +962,9 @@ public void createASTs(ICompilationUnit[] compilationUnits, String[] bindingKeys
if ((this.bits & CompilationUnitResolver.BINDING_RECOVERY) != 0) {
flags |= ICompilationUnit.ENABLE_BINDINGS_RECOVERY;
}
CompilationUnitResolver.resolve(compilationUnits, bindingKeys, requestor, this.apiLevel, this.compilerOptions, this.project, this.workingCopyOwner, flags, monitor);
this.unitResolver.resolve(compilationUnits, bindingKeys, requestor, this.apiLevel, this.compilerOptions, this.project, this.workingCopyOwner, flags, monitor);
} else {
CompilationUnitResolver.parse(compilationUnits, requestor, this.apiLevel, this.compilerOptions, flags, monitor);
this.unitResolver.parse(compilationUnits, requestor, this.apiLevel, this.compilerOptions, flags, monitor);
}
} finally {
// reset to defaults to allow reuse (and avoid leaking)
Expand Down Expand Up @@ -1052,9 +1057,9 @@ public void createASTs(String[] sourceFilePaths, String[] encodings, String[] bi
if ((this.bits & CompilationUnitResolver.BINDING_RECOVERY) != 0) {
flags |= ICompilationUnit.ENABLE_BINDINGS_RECOVERY;
}
CompilationUnitResolver.resolve(sourceFilePaths, encodings, bindingKeys, requestor, this.apiLevel, this.compilerOptions, getClasspath(), flags, monitor);
this.unitResolver.resolve(sourceFilePaths, encodings, bindingKeys, requestor, this.apiLevel, this.compilerOptions, getClasspath(), flags, monitor);
} else {
CompilationUnitResolver.parse(sourceFilePaths, encodings, requestor, this.apiLevel, this.compilerOptions, flags, monitor);
this.unitResolver.parse(sourceFilePaths, encodings, requestor, this.apiLevel, this.compilerOptions, flags, monitor);
}
} finally {
// reset to defaults to allow reuse (and avoid leaking)
Expand Down Expand Up @@ -1159,7 +1164,6 @@ private ASTNode internalCreateASTCached(IProgressMonitor monitor) {
}
break;
case K_COMPILATION_UNIT :
CompilationUnitDeclaration compilationUnitDeclaration = null;
try {
NodeSearcher searcher = null;
org.eclipse.jdt.internal.compiler.env.ICompilationUnit sourceUnit = null;
Expand Down Expand Up @@ -1238,59 +1242,19 @@ private ASTNode internalCreateASTCached(IProgressMonitor monitor) {
if (searcher == null && ((this.bits & CompilationUnitResolver.IGNORE_METHOD_BODIES) != 0)) {
flags |= ICompilationUnit.IGNORE_METHOD_BODIES;
}

if (needToResolveBindings) {
if ((this.bits & CompilationUnitResolver.BINDING_RECOVERY) != 0) {
flags |= ICompilationUnit.ENABLE_BINDINGS_RECOVERY;
}
try {
// parse and resolve
compilationUnitDeclaration =
CompilationUnitResolver.resolve(
sourceUnit,
this.project,
getClasspath(),
searcher,
this.compilerOptions,
this.workingCopyOwner,
flags,
monitor);
} catch (JavaModelException e) {
flags &= ~ICompilationUnit.ENABLE_BINDINGS_RECOVERY;
compilationUnitDeclaration = CompilationUnitResolver.parse(
sourceUnit,
searcher,
this.compilerOptions,
flags);
needToResolveBindings = false;
}
} else {
compilationUnitDeclaration = CompilationUnitResolver.parse(
sourceUnit,
searcher,
this.compilerOptions,
flags,
this.project);
needToResolveBindings = false;
}
CompilationUnit result = CompilationUnitResolver.convert(
compilationUnitDeclaration,
sourceUnit.getContents(),
this.apiLevel,
this.compilerOptions,
needToResolveBindings,
wcOwner,
needToResolveBindings ? new DefaultBindingResolver.BindingTables() : null,
flags,
monitor,
this.project != null,
this.project);
result.setTypeRoot(this.typeRoot);
return result;

CompilationUnit result2 = this.unitResolver.toCompilationUnit(sourceUnit, needToResolveBindings, this.project, getClasspath(), searcher, this.apiLevel, this.compilerOptions, this.workingCopyOwner, wcOwner, flags, monitor);
result2.setTypeRoot(this.typeRoot);
return result2;
} finally {
if (compilationUnitDeclaration != null
&& ((this.bits & CompilationUnitResolver.RESOLVE_BINDING) != 0)) {
compilationUnitDeclaration.cleanUp();
}
// unitResolver should already handle this.
// Leaving this finally in place to avoid changing indentation
}
}
throw new IllegalStateException();
Expand Down
Loading

0 comments on commit 2c5e65b

Please sign in to comment.