Skip to content

Commit

Permalink
Fix some bugs requiring == for multiple resolutions on a node (eclips…
Browse files Browse the repository at this point in the history
…e-jdt#621)

* Fix some bugs requiring == for multiple resolutions on a node

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

* Cleanup

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

---------

Signed-off-by: Rob Stryker <[email protected]>
Co-authored-by: Rob Stryker <[email protected]>
  • Loading branch information
2 people authored and mickaelistria committed Feb 10, 2025
1 parent fd79fbd commit 641c914
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -131,6 +132,13 @@ public JavacModuleBinding getModuleBinding(ModuleSymbol moduleSymbol) {
moduleBindings.putIfAbsent(newInstance.getKey(), newInstance);
return moduleBindings.get(newInstance.getKey());
}
public JavacModuleBinding getModuleBinding(JCModuleDecl moduleDecl) {
JavacModuleBinding newInstance = new JavacModuleBinding(moduleDecl, JavacBindingResolver.this) { };
// Overwrite existing
moduleBindings.put(newInstance.getKey(), newInstance);
return moduleBindings.get(newInstance.getKey());
}

//
private Map<String, JavacPackageBinding> packageBindings = new HashMap<>();
public JavacPackageBinding getPackageBinding(PackageSymbol packageSymbol) {
Expand Down Expand Up @@ -233,6 +241,7 @@ public IBinding getBinding(String key) {
}
public final Bindings bindings = new Bindings();
private WorkingCopyOwner owner;
private HashMap<ASTNode, IBinding> resolvedBindingsCache = new HashMap<>();

public JavacBindingResolver(IJavaProject javaProject, JavacTask javacTask, Context context, JavacConverter converter, WorkingCopyOwner owner) {
this.javac = javacTask;
Expand Down Expand Up @@ -587,8 +596,20 @@ IMethodBinding resolveMethod(SuperMethodInvocation method) {
return null;
}

IBinding resolveCached(ASTNode node, Function<ASTNode, IBinding> l) {
IBinding ret = resolvedBindingsCache.get(node);
if( ret == null ) {
ret = l.apply(node);
if( ret != null )
resolvedBindingsCache.put(node, ret);
}
return ret;
}
@Override
IBinding resolveName(Name name) {
return resolveCached(name, (n) -> resolveNameImpl((Name)n));
}
private IBinding resolveNameImpl(Name name) {
resolve();
JCTree tree = this.converter.domToJavac.get(name);
if( tree != null ) {
Expand Down Expand Up @@ -661,6 +682,9 @@ IBinding resolveNameToJavac(Name name, JCTree tree) {
if (tree instanceof JCTypeParameter variableDecl && variableDecl.type != null && variableDecl.type.tsym != null) {
return this.bindings.getBinding(variableDecl.type.tsym, variableDecl.type);
}
if (tree instanceof JCModuleDecl variableDecl && variableDecl.sym != null && variableDecl.sym.type instanceof ModuleType mtt) {
return this.bindings.getModuleBinding(variableDecl);
}
return null;
}

Expand Down Expand Up @@ -771,6 +795,10 @@ public ITypeBinding resolveExpressionType(Expression expr) {

@Override
IMethodBinding resolveConstructor(ClassInstanceCreation expression) {
return (IMethodBinding)resolveCached(expression, (n) -> resolveConstructorImpl((ClassInstanceCreation)n));
}

private IMethodBinding resolveConstructorImpl(ClassInstanceCreation expression) {
resolve();
return this.converter.domToJavac.get(expression) instanceof JCNewClass jcExpr
&& jcExpr.constructor != null
Expand All @@ -781,6 +809,10 @@ IMethodBinding resolveConstructor(ClassInstanceCreation expression) {

@Override
IMethodBinding resolveConstructor(ConstructorInvocation invocation) {
return (IMethodBinding)resolveCached(invocation, (n) -> resolveConstructorImpl((ConstructorInvocation)n));
}

private IMethodBinding resolveConstructorImpl(ConstructorInvocation invocation) {
resolve();
JCTree javacElement = this.converter.domToJavac.get(invocation);
if (javacElement instanceof JCMethodInvocation javacMethodInvocation) {
Expand Down Expand Up @@ -861,6 +893,10 @@ private java.util.List<TypeSymbol> getTypeArguments(final MethodInvocation metho
}

IModuleBinding resolveModule(ModuleDeclaration module) {
return (IModuleBinding)resolveCached(module, (n) -> resolveModuleImpl((ModuleDeclaration)n));
}

private IBinding resolveModuleImpl(ModuleDeclaration module) {
resolve();
JCTree javacElement = this.converter.domToJavac.get(module);
if( javacElement instanceof JCModuleDecl jcmd) {
Expand Down Expand Up @@ -909,6 +945,10 @@ public Object getValueFromAttribute(Attribute attribute) {

@Override
IBinding resolveImport(ImportDeclaration importDeclaration) {
return resolveCached(importDeclaration, (n) -> resolveImportImpl((ImportDeclaration)n));
}

private IBinding resolveImportImpl(ImportDeclaration importDeclaration) {
var javac = this.converter.domToJavac.get(importDeclaration.getName());
if (javac instanceof JCFieldAccess fieldAccess) {
if (fieldAccess.sym != null) {
Expand Down Expand Up @@ -966,6 +1006,10 @@ public ITypeBinding resolveWellKnownType(String typeName) {

@Override
IAnnotationBinding resolveAnnotation(Annotation annotation) {
return (IAnnotationBinding)resolveCached(annotation, (n) -> resolveAnnotationImpl((Annotation)n));
}

IAnnotationBinding resolveAnnotationImpl(Annotation annotation) {
resolve();
IBinding recipient = null;
if (annotation.getParent() instanceof AnnotatableType annotatable) {
Expand All @@ -982,6 +1026,10 @@ IAnnotationBinding resolveAnnotation(Annotation annotation) {

@Override
IBinding resolveReference(MethodRef ref) {
return resolveCached(ref, (n) -> resolveReferenceImpl((MethodRef)n));
}

private IBinding resolveReferenceImpl(MethodRef ref) {
resolve();
DocTreePath path = this.converter.findDocTreePath(ref);
if (path != null && JavacTrees.instance(this.context).getElement(path) instanceof Symbol symbol) {
Expand All @@ -992,6 +1040,10 @@ IBinding resolveReference(MethodRef ref) {

@Override
IBinding resolveReference(MemberRef ref) {
return resolveCached(ref, (n) -> resolveReferenceImpl((MemberRef)n));
}

private IBinding resolveReferenceImpl(MemberRef ref) {
resolve();
DocTreePath path = this.converter.findDocTreePath(ref);
if (path != null && JavacTrees.instance(this.context).getElement(path) instanceof Symbol symbol) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ private PackageDeclaration convert(JCPackageDecl javac) {
private ModuleDeclaration convert(JCModuleDecl javac) {
ModuleDeclaration res = this.ast.newModuleDeclaration();
res.setName(toName(javac.getName()));
this.domToJavac.put(res.getName(), javac);
boolean isOpen = javac.getModuleType() == ModuleKind.OPEN;
res.setOpen(isOpen);
if (javac.getDirectives() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
*******************************************************************************/
package org.eclipse.jdt.internal.javac.dom;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.lang.model.element.ModuleElement.DirectiveKind;

Expand All @@ -34,12 +37,18 @@
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
import com.sun.tools.javac.code.Type.ClassType;
import com.sun.tools.javac.code.Type.ModuleType;
import com.sun.tools.javac.tree.JCTree.JCDirective;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCIdent;
import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
import com.sun.tools.javac.tree.JCTree.JCRequires;
public abstract class JavacModuleBinding implements IModuleBinding {

private static final ITypeBinding[] NO_TYPE_ARGUMENTS = new ITypeBinding[0];
final JavacBindingResolver resolver;
public final ModuleSymbol moduleSymbol;
private final ModuleType moduleType;
private JCModuleDecl moduleDecl;

public JavacModuleBinding(final ModuleType moduleType, final JavacBindingResolver resolver) {
this((ModuleSymbol) moduleType.tsym, moduleType, resolver);
Expand All @@ -49,6 +58,11 @@ public JavacModuleBinding(final ModuleSymbol moduleSymbol, final JavacBindingRes
this(moduleSymbol, (ModuleType)moduleSymbol.type, resolver);
}

public JavacModuleBinding(final JCModuleDecl decl, final JavacBindingResolver resolver) {
this(decl.sym, (ModuleType)decl.sym.type, resolver);
this.moduleDecl = decl;
}

public JavacModuleBinding(final ModuleSymbol moduleSymbol, final ModuleType moduleType, JavacBindingResolver resolver) {
this.moduleType = moduleType;
this.moduleSymbol = moduleSymbol;
Expand Down Expand Up @@ -112,15 +126,29 @@ public boolean isOpen() {

@Override
public IModuleBinding[] getRequiredModules() {
RequiresDirective[] arr = this.moduleSymbol.getDirectives().stream() //
.filter(x -> x.getKind() == DirectiveKind.REQUIRES) //
.map(x -> (RequiresDirective)x) //
.toArray(RequiresDirective[]::new);
IModuleBinding[] arr2 = new IModuleBinding[arr.length];
for( int i = 0; i < arr.length; i++ ) {
arr2[i] = this.resolver.bindings.getModuleBinding((ModuleType)arr[i].module.type);
ArrayList<ModuleSymbol> mods = new ArrayList<>();
this.moduleSymbol.getDirectives().stream()
.filter(x -> x.getKind() == DirectiveKind.REQUIRES)
.map(x -> ((RequiresDirective)x).module)
.forEachOrdered(mods::add);
if( this.moduleDecl != null ) {
List<JCDirective> directives = this.moduleDecl.getDirectives();
for( JCDirective jcd : directives ) {
if( jcd instanceof JCRequires jcr) {
JCExpression jce = jcr.moduleName;
if( jce instanceof JCIdent jcid && jcid.sym instanceof ModuleSymbol mss) {
if( !mods.contains(mss)) {
mods.add(mss);
}
}
}
}
}
return arr2;
IModuleBinding[] ret = new IModuleBinding[mods.size()];
for( int i = 0; i < mods.size(); i++ ) {
ret[i] = this.resolver.bindings.getModuleBinding(mods.get(i));
}
return ret;
}

@Override
Expand Down

0 comments on commit 641c914

Please sign in to comment.