Skip to content

Commit

Permalink
Revert "Ensure calls to ASTParser.createBindings use the correct unit…
Browse files Browse the repository at this point in the history
… resolver"

This reverts commit 3730b06.
  • Loading branch information
Rob Stryker committed Dec 10, 2024
1 parent 0615a26 commit f89f3fe
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 101 deletions.
97 changes: 7 additions & 90 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 @@ -27,7 +27,6 @@
import org.eclipse.jdt.core.*;
import org.eclipse.jdt.core.compiler.CategorizedProblem;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.dom.CompilationUnitResolver.IntArrayList;
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 All @@ -39,13 +38,16 @@
import org.eclipse.jdt.internal.compiler.parser.RecoveryScanner;
import org.eclipse.jdt.internal.compiler.parser.RecoveryScannerData;
import org.eclipse.jdt.internal.compiler.parser.Scanner;
import org.eclipse.jdt.internal.compiler.util.HashtableOfObjectToInt;
import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
import org.eclipse.jdt.internal.core.*;
import org.eclipse.jdt.internal.core.BasicCompilationUnit;
import org.eclipse.jdt.internal.core.BinaryType;
import org.eclipse.jdt.internal.core.ClassFileWorkingCopy;
import org.eclipse.jdt.internal.core.DefaultWorkingCopyOwner;
import org.eclipse.jdt.internal.core.JavaModelManager;
import org.eclipse.jdt.internal.core.PackageFragment;
import org.eclipse.jdt.internal.core.dom.ICompilationUnitResolver;
import org.eclipse.jdt.internal.core.dom.util.DOMASTUtil;
import org.eclipse.jdt.internal.core.util.CodeSnippetParsingUtil;
import org.eclipse.jdt.internal.core.util.DOMFinder;
import org.eclipse.jdt.internal.core.util.RecordedParsingInformation;
import org.eclipse.jdt.internal.core.util.Util;

Expand Down Expand Up @@ -1165,98 +1167,13 @@ public IBinding[] createBindings(IJavaElement[] elements, IProgressMonitor monit
if ((this.bits & CompilationUnitResolver.IGNORE_METHOD_BODIES) != 0) {
flags |= ICompilationUnit.IGNORE_METHOD_BODIES;
}

return resolve(elements, this.apiLevel, this.compilerOptions, this.project, this.workingCopyOwner, flags, this.unitResolver, monitor);
return CompilationUnitResolver.resolve(elements, this.apiLevel, this.compilerOptions, this.project, this.workingCopyOwner, flags, monitor);
} finally {
// reset to defaults to allow reuse (and avoid leaking)
initializeDefaults();
}
}


static IBinding[] resolve(
final IJavaElement[] elements,
int apiLevel,
Map compilerOptions,
IJavaProject javaProject,
WorkingCopyOwner owner,
int flags,
ICompilationUnitResolver unitResolver,
IProgressMonitor monitor) {

final int length = elements.length;
final HashMap sourceElementPositions = new HashMap(); // a map from ICompilationUnit to int[] (positions in elements)
int cuNumber = 0;
final HashtableOfObjectToInt binaryElementPositions = new HashtableOfObjectToInt(); // a map from String (binding key) to int (position in elements)
for (int i = 0; i < length; i++) {
IJavaElement element = elements[i];
if (!(element instanceof SourceRefElement))
throw new IllegalStateException(element + " is not part of a compilation unit or class file"); //$NON-NLS-1$
Object cu = element.getAncestor(IJavaElement.COMPILATION_UNIT);
if (cu != null) {
// source member
IntArrayList intList = (IntArrayList) sourceElementPositions.get(cu);
if (intList == null) {
sourceElementPositions.put(cu, intList = new IntArrayList());
cuNumber++;
}
intList.add(i);
} else {
// binary member or method argument
try {
String key;
if (element instanceof BinaryMember)
key = ((BinaryMember) element).getKey(true/*open to get resolved info*/);
else if (element instanceof LocalVariable)
key = ((LocalVariable) element).getKey(true/*open to get resolved info*/);
else if (element instanceof org.eclipse.jdt.internal.core.TypeParameter)
key = ((org.eclipse.jdt.internal.core.TypeParameter) element).getKey(true/*open to get resolved info*/);
else if (element instanceof BinaryModule)
key = ((BinaryModule) element).getKey(true);
else
throw new IllegalArgumentException(element + " has an unexpected type"); //$NON-NLS-1$
binaryElementPositions.put(key, i);
} catch (JavaModelException e) {
throw new IllegalArgumentException(element + " does not exist", e); //$NON-NLS-1$
}
}
}
ICompilationUnit[] cus = new ICompilationUnit[cuNumber];
sourceElementPositions.keySet().toArray(cus);

int bindingKeyNumber = binaryElementPositions.size();
String[] bindingKeys = new String[bindingKeyNumber];
binaryElementPositions.keysToArray(bindingKeys);

class Requestor extends ASTRequestor {
IBinding[] bindings = new IBinding[length];
@Override
public void acceptAST(ICompilationUnit source, CompilationUnit ast) {
// TODO (jerome) optimize to visit the AST only once
IntArrayList intList = (IntArrayList) sourceElementPositions.get(source);
for (int i = 0; i < intList.length; i++) {
final int index = intList.list[i];
SourceRefElement element = (SourceRefElement) elements[index];
DOMFinder finder = new DOMFinder(ast, element, true/*resolve binding*/);
try {
finder.search();
} catch (JavaModelException e) {
throw new IllegalArgumentException(element + " does not exist", e); //$NON-NLS-1$
}
this.bindings[index] = finder.foundBinding;
}
}
@Override
public void acceptBinding(String bindingKey, IBinding binding) {
int index = binaryElementPositions.get(bindingKey);
this.bindings[index] = binding;
}
}
Requestor requestor = new Requestor();
unitResolver.resolve(cus, bindingKeys, requestor, apiLevel, compilerOptions, javaProject, owner, flags, monitor);
return requestor.bindings;
}

private ASTNode internalCreateAST(IProgressMonitor monitor) {
return JavaModelManager.cacheZipFiles(() -> internalCreateASTCached(monitor));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -59,18 +60,14 @@
import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
import org.eclipse.jdt.internal.compiler.util.HashtableOfObject;
import org.eclipse.jdt.internal.compiler.util.HashtableOfObjectToInt;
import org.eclipse.jdt.internal.compiler.util.Messages;
import org.eclipse.jdt.internal.compiler.util.Util;
import org.eclipse.jdt.internal.core.CancelableNameEnvironment;
import org.eclipse.jdt.internal.core.CancelableProblemFactory;
import org.eclipse.jdt.internal.core.ClasspathEntry;
import org.eclipse.jdt.internal.core.INameEnvironmentWithProgress;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jdt.internal.core.NameLookup;
import org.eclipse.jdt.internal.core.SourceTypeElementInfo;
import org.eclipse.jdt.internal.core.*;
import org.eclipse.jdt.internal.core.dom.ICompilationUnitResolver;
import org.eclipse.jdt.internal.core.util.BindingKeyResolver;
import org.eclipse.jdt.internal.core.util.CommentRecorderParser;
import org.eclipse.jdt.internal.core.util.DOMFinder;

@SuppressWarnings({ "rawtypes", "unchecked" })
class CompilationUnitResolver extends Compiler {
Expand Down Expand Up @@ -869,7 +866,6 @@ public static CompilationUnitDeclaration resolve(
}
}
}

public static IBinding[] resolve(
final IJavaElement[] elements,
int apiLevel,
Expand All @@ -878,11 +874,79 @@ public static IBinding[] resolve(
WorkingCopyOwner owner,
int flags,
IProgressMonitor monitor) {
// Should not be called anymore? Candidate for deprecation
return ASTParser.resolve(elements, apiLevel, compilerOptions, javaProject, owner, flags, getInstance(), monitor);
}

final int length = elements.length;
final HashMap sourceElementPositions = new HashMap(); // a map from ICompilationUnit to int[] (positions in elements)
int cuNumber = 0;
final HashtableOfObjectToInt binaryElementPositions = new HashtableOfObjectToInt(); // a map from String (binding key) to int (position in elements)
for (int i = 0; i < length; i++) {
IJavaElement element = elements[i];
if (!(element instanceof SourceRefElement))
throw new IllegalStateException(element + " is not part of a compilation unit or class file"); //$NON-NLS-1$
Object cu = element.getAncestor(IJavaElement.COMPILATION_UNIT);
if (cu != null) {
// source member
IntArrayList intList = (IntArrayList) sourceElementPositions.get(cu);
if (intList == null) {
sourceElementPositions.put(cu, intList = new IntArrayList());
cuNumber++;
}
intList.add(i);
} else {
// binary member or method argument
try {
String key;
if (element instanceof BinaryMember)
key = ((BinaryMember) element).getKey(true/*open to get resolved info*/);
else if (element instanceof LocalVariable)
key = ((LocalVariable) element).getKey(true/*open to get resolved info*/);
else if (element instanceof org.eclipse.jdt.internal.core.TypeParameter)
key = ((org.eclipse.jdt.internal.core.TypeParameter) element).getKey(true/*open to get resolved info*/);
else if (element instanceof BinaryModule)
key = ((BinaryModule) element).getKey(true);
else
throw new IllegalArgumentException(element + " has an unexpected type"); //$NON-NLS-1$
binaryElementPositions.put(key, i);
} catch (JavaModelException e) {
throw new IllegalArgumentException(element + " does not exist", e); //$NON-NLS-1$
}
}
}
ICompilationUnit[] cus = new ICompilationUnit[cuNumber];
sourceElementPositions.keySet().toArray(cus);

int bindingKeyNumber = binaryElementPositions.size();
String[] bindingKeys = new String[bindingKeyNumber];
binaryElementPositions.keysToArray(bindingKeys);

class Requestor extends ASTRequestor {
IBinding[] bindings = new IBinding[length];
@Override
public void acceptAST(ICompilationUnit source, CompilationUnit ast) {
// TODO (jerome) optimize to visit the AST only once
IntArrayList intList = (IntArrayList) sourceElementPositions.get(source);
for (int i = 0; i < intList.length; i++) {
final int index = intList.list[i];
SourceRefElement element = (SourceRefElement) elements[index];
DOMFinder finder = new DOMFinder(ast, element, true/*resolve binding*/);
try {
finder.search();
} catch (JavaModelException e) {
throw new IllegalArgumentException(element + " does not exist", e); //$NON-NLS-1$
}
this.bindings[index] = finder.foundBinding;
}
}
@Override
public void acceptBinding(String bindingKey, IBinding binding) {
int index = binaryElementPositions.get(bindingKey);
this.bindings[index] = binding;
}
}
Requestor requestor = new Requestor();
resolve(cus, bindingKeys, requestor, apiLevel, compilerOptions, javaProject, owner, flags, monitor);
return requestor.bindings;
}
/*
* When unit result is about to be accepted, removed back pointers
* to unresolved bindings
Expand Down

0 comments on commit f89f3fe

Please sign in to comment.