Skip to content

Commit

Permalink
Used Resolved* JavaElement in Javac*Bindings.getJavaElement()
Browse files Browse the repository at this point in the history
Those allow to store a more accurate key.
  • Loading branch information
mickaelistria committed Dec 12, 2024
1 parent dc4db81 commit 47178be
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,24 @@
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.JavacBindingResolver;
import org.eclipse.jdt.core.dom.JavacBindingResolver.BindingKeyException;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.TypeParameter;
import org.eclipse.jdt.core.dom.JavacBindingResolver.BindingKeyException;
import org.eclipse.jdt.internal.core.Member;
import org.eclipse.jdt.internal.core.ResolvedSourceMethod;
import org.eclipse.jdt.internal.core.SourceMethod;
import org.eclipse.jdt.internal.core.util.Util;

import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.TypeSymbol;
import com.sun.tools.javac.code.Symbol.VarSymbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Type.ForAll;
import com.sun.tools.javac.code.Type.JCNoType;
import com.sun.tools.javac.code.Type.MethodType;
Expand Down Expand Up @@ -276,7 +278,7 @@ private IJavaElement getJavaElementForMethodDeclaration(IType currentType, Metho
}).toArray(String[]::new);
IMethod result = currentType.getMethod(getName(), params);
if (currentType.isBinary() || result.exists()) {
return result;
return resolved(result);
}
IMethod[] methods = null;
try {
Expand All @@ -288,9 +290,16 @@ private IJavaElement getJavaElementForMethodDeclaration(IType currentType, Metho
IMethod[] candidates = Member.findMethods(result, methods);
if (candidates == null || candidates.length == 0)
return null;
return candidates[0];
return resolved(candidates[0]);
}


private IMethod resolved(IMethod from) {
if (from instanceof SourceMethod sourceMethod && !(from instanceof ResolvedSourceMethod)) {
return new ResolvedSourceMethod(sourceMethod.getParent(), sourceMethod.getElementName(), sourceMethod.getParameterTypes(), getKey(), sourceMethod.getOccurrenceCount());
}
return from;
}

private IJavaElement getJavaElementForAnnotationTypeMemberDeclaration(IType currentType, AnnotationTypeMemberDeclaration annotationTypeMemberDeclaration) {
IMethod result = currentType.getMethod(getName(), new String[0]);
if (currentType.isBinary() || result.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,25 @@
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.JavacBindingResolver;
import org.eclipse.jdt.core.dom.JavacBindingResolver.BindingKeyException;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.RecordDeclaration;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.JavacBindingResolver.BindingKeyException;
import org.eclipse.jdt.internal.compiler.codegen.ConstantPool;
import org.eclipse.jdt.internal.core.JavaElement;
import org.eclipse.jdt.internal.core.ResolvedSourceType;
import org.eclipse.jdt.internal.core.SourceType;

import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.code.Kinds.Kind;
import com.sun.tools.javac.code.Kinds.KindSelector;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.CompletionFailure;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
Expand All @@ -74,7 +79,6 @@
import com.sun.tools.javac.code.Symbol.TypeSymbol;
import com.sun.tools.javac.code.Symbol.TypeVariableSymbol;
import com.sun.tools.javac.code.Symbol.VarSymbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Type.ArrayType;
import com.sun.tools.javac.code.Type.ClassType;
import com.sun.tools.javac.code.Type.ErrorType;
Expand All @@ -84,8 +88,6 @@
import com.sun.tools.javac.code.Type.MethodType;
import com.sun.tools.javac.code.Type.TypeVar;
import com.sun.tools.javac.code.Type.WildcardType;
import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.code.Types.FunctionDescriptorLookupError;
import com.sun.tools.javac.util.Name;

Expand Down Expand Up @@ -198,17 +200,17 @@ public IJavaElement getJavaElement() {
if (isAnonymous()) {
if (getDeclaringMethod() != null && getDeclaringMethod().getJavaElement() instanceof IMethod method) {
// TODO find proper occurenceCount (eg checking the source range)
return method.getType("", 1);
return resolved(method.getType("", 1));
} else if( getDeclaringMember() instanceof IBinding gdm && gdm != null && gdm.getJavaElement() instanceof IField field) {
return field.getType("", 1);
return resolved(field.getType("", 1));
} else if (getDeclaringClass() != null && getDeclaringClass().getJavaElement() instanceof IType type) {
return type.getType("", 1);
return resolved(type.getType("", 1));
}
}
if( this.typeSymbol.owner instanceof MethodSymbol) {
if (getDeclaringMethod() != null && getDeclaringMethod().getJavaElement() instanceof IMethod method) {
// TODO find proper occurenceCount (eg checking the source range)
return method.getType(this.typeSymbol.name.toString(), 1);
return resolved(method.getType(this.typeSymbol.name.toString(), 1));
}
}

Expand Down Expand Up @@ -247,22 +249,29 @@ public IJavaElement getJavaElement() {
done |= (candidate == null);
}
if(candidate != null && candidate.exists()) {
return candidate;
return resolved(candidate);
}
}
try {
IType ret = this.resolver.javaProject.findType(cleanedUpName(this.type), this.resolver.getWorkingCopyOwner(), new NullProgressMonitor());
if (ret != null) {
return ret;
return resolved(ret);
}
} catch (JavaModelException ex) {
ILog.get().error(ex.getMessage(), ex);
}
return candidate;
return resolved(candidate);
}
return null;
}

private IType resolved(IType type) {
if (type instanceof SourceType && !(type instanceof ResolvedSourceType)) {
return new ResolvedSourceType((JavaElement)type.getParent(), type.getElementName(), getKey(), type.getOccurrenceCount());
}
return type;
}

private static String cleanedUpName(Type type) {
if (type instanceof ClassType classType && classType.getEnclosingType() instanceof ClassType enclosing) {
return cleanedUpName(enclosing) + "$" + type.tsym.getSimpleName().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Objects;

import org.eclipse.core.runtime.ILog;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
Expand All @@ -27,28 +28,30 @@
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.JavacBindingResolver;
import org.eclipse.jdt.core.dom.JavacBindingResolver.BindingKeyException;
import org.eclipse.jdt.core.dom.LambdaExpression;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import org.eclipse.jdt.core.dom.JavacBindingResolver.BindingKeyException;
import org.eclipse.jdt.internal.core.DOMToModelPopulator;
import org.eclipse.jdt.internal.core.JavaElement;
import org.eclipse.jdt.internal.core.LambdaMethod;
import org.eclipse.jdt.internal.core.LocalVariable;
import org.eclipse.jdt.internal.core.ResolvedSourceField;
import org.eclipse.jdt.internal.core.SourceField;
import org.eclipse.jdt.internal.core.util.Util;

import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.TypeSymbol;
import com.sun.tools.javac.code.Symbol.VarSymbol;
import com.sun.tools.javac.code.Type;

public abstract class JavacVariableBinding implements IVariableBinding {

Expand Down Expand Up @@ -115,7 +118,7 @@ public IJavaElement getJavaElement() {
if (this.variableSymbol.owner instanceof TypeSymbol parentType // field
&& parentType.type != null
&& this.resolver.bindings.getTypeBinding(parentType.type).getJavaElement() instanceof IType type) {
return type.getField(this.variableSymbol.name.toString());
return resolved(type.getField(this.variableSymbol.name.toString()));
}
IMethodBinding methodBinding = getDeclaringMethod();
if (methodBinding != null && methodBinding.getJavaElement() instanceof IMethod method) {
Expand Down Expand Up @@ -153,6 +156,13 @@ public IJavaElement getJavaElement() {
return null;
}

private IField resolved(IField field) {
if (field instanceof SourceField sourceField && !(sourceField instanceof ResolvedSourceField)) {
return new ResolvedSourceField(sourceField.getParent(), sourceField.getElementName(), getKey(), sourceField.getOccurrenceCount());
}
return field;
}

@Override
public String getKey() {
try {
Expand Down

0 comments on commit 47178be

Please sign in to comment.