Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3428 - Ensure calls to ASTParser.createBindings use the correc… #3429

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 90 additions & 7 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,6 +27,7 @@
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 @@ -38,16 +39,13 @@
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.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.*;
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 @@ -1166,13 +1164,98 @@
if ((this.bits & CompilationUnitResolver.IGNORE_METHOD_BODIES) != 0) {
flags |= ICompilationUnit.IGNORE_METHOD_BODIES;
}
return CompilationUnitResolver.resolve(elements, this.apiLevel, this.compilerOptions, this.project, this.workingCopyOwner, flags, monitor);

return resolve(elements, this.apiLevel, this.compilerOptions, this.project, this.workingCopyOwner, flags, this.unitResolver, 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());

Check warning on line 1199 in org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java

View check run for this annotation

Jenkins - Eclipse JDT / Compiler and API Tools

Unchecked Raw

NORMAL: Type safety: The method put(Object, Object) belongs to the raw type HashMap. References to generic type HashMap should be parameterized
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);

Check warning on line 1255 in org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/ASTParser.java

View check run for this annotation

Jenkins - Eclipse JDT / Compiler and API Tools

Unchecked Raw

NORMAL: Type safety: The expression of type Map needs unchecked conversion to conform to Map
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,7 +19,6 @@
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 @@ -60,14 +59,18 @@
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.*;
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.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 @@ -866,6 +869,7 @@ public static CompilationUnitDeclaration resolve(
}
}
}

public static IBinding[] resolve(
final IJavaElement[] elements,
int apiLevel,
Expand All @@ -874,79 +878,11 @@ 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
Loading