Skip to content

Commit

Permalink
Refactor ASTRequestor to reduce the access it has to the compiler
Browse files Browse the repository at this point in the history
This reduces the access that `ASTRequestor` has to the `CompilationUnitResolver`
to just the method it needs, `createBinding`.

This also makes it possible to use an alternate additional binding
resolver instead of hardcoding the one implemented by `CompilationUnitResolver`.

Signed-off-by: David Thompson <davthomp@redhat.com>
  • Loading branch information
datho7561 committed Jun 25, 2024
1 parent 7e0c89e commit 451b85f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.jdt.core.dom;

import java.util.function.Function;

import org.eclipse.jdt.core.ICompilationUnit;

/**
Expand All @@ -36,12 +38,14 @@
public abstract class ASTRequestor {

/**
* The compilation unit resolver used to resolve bindings, or
* <code>null</code> if none. Note that this field is non-null
* The function used to resolve additional bindings,
* or <code>null</code> if none.
* The function accepts the binding key and returns the corresponding <code>IBinding</code>.
* Note that this field is non-null
* only within the dynamic scope of a call to
* <code>ASTParser.createASTs</code>.
*/
CompilationUnitResolver compilationUnitResolver = null;
Function<String, IBinding> additionalBindingResolver = null;

/**
* Creates a new instance.
Expand Down Expand Up @@ -111,8 +115,8 @@ public final IBinding[] createBindings(String[] bindingKeys) {
IBinding[] result = new IBinding[length];
for (int i = 0; i < length; i++) {
result[i] = null;
if (this.compilationUnitResolver != null) {
result[i] = this.compilationUnitResolver.createBinding(bindingKeys[i]);
if (this.additionalBindingResolver != null) {
result[i] = this.additionalBindingResolver.apply(bindingKeys[i]);
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ private void resolve(
int flags) {

// temporarily connect ourselves to the ASTResolver - must disconnect when done
astRequestor.compilationUnitResolver = this;
astRequestor.additionalBindingResolver = this::createBinding;
this.bindingTables = new DefaultBindingResolver.BindingTables();
CompilationUnitDeclaration unit = null;
try {
Expand Down Expand Up @@ -1067,7 +1067,7 @@ private void resolve(
throw e; // rethrow
} finally {
// disconnect ourselves from ast requestor
astRequestor.compilationUnitResolver = null;
astRequestor.additionalBindingResolver = null;
}
}

Expand Down

0 comments on commit 451b85f

Please sign in to comment.