Skip to content

Commit 09ebc21

Browse files
mickaelistriadatho7561
authored andcommitted
Fix resolving JavaElement for binary types/methods
* Special rules for when using a mock name for Javac (this could probably be improved by getting ASTParser to pass through the actual class name and resusing it) * Use resolved types signatures from binding to retrieve method reference * Better signature for parameterized/generic types
1 parent df87c33 commit 09ebc21

File tree

6 files changed

+59
-14
lines changed

6 files changed

+59
-14
lines changed

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
*/
113113
public class JavacCompilationUnitResolver implements ICompilationUnitResolver {
114114

115+
public static final String MOCK_NAME_FOR_CLASSES = "whatever_InvalidNameWE_HOP3_n00ne_will_Ever_use_in_real_file.java";
116+
115117
private final class ForwardDiagnosticsAsDOMProblems implements DiagnosticListener<JavaFileObject> {
116118
public final Map<JavaFileObject, CompilationUnit> filesToUnits;
117119
private final JavacProblemConverter problemConverter;
@@ -711,7 +713,7 @@ public Void visitClass(ClassTree node, Void p) {
711713
sourceUnitPath = Path.of(lastSegment);
712714
}
713715
if( sourceUnitPath == null )
714-
sourceUnitPath = Path.of(new File("whatever.java").toURI());
716+
sourceUnitPath = Path.of(new File(MOCK_NAME_FOR_CLASSES).toURI());
715717
} else {
716718
sourceUnitPath = Path.of(unitFile.toURI());
717719
}

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/codeassist/SignatureUtils.java renamed to org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/SignatureUtils.java

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
*
99
* SPDX-License-Identifier: EPL-2.0
1010
*******************************************************************************/
11-
package org.eclipse.jdt.internal.codeassist;
11+
package org.eclipse.jdt.internal;
1212

1313
import java.util.ArrayList;
1414
import java.util.List;
15+
import java.util.stream.Collectors;
16+
import java.util.stream.Stream;
1517

1618
import org.eclipse.core.runtime.ILog;
1719
import org.eclipse.core.runtime.IProgressMonitor;
@@ -95,23 +97,54 @@ public static String createSignature(IType type) {
9597
}
9698

9799
/**
98-
* Returns the signature of the given type binding.
100+
* Returns the signature of the given type binding as a character array.
99101
*
100102
* @param typeBinding the type binding to get the signature of
101-
* @return the signature of the given type binding
103+
* @return the signature of the given type binding as a character array
102104
*/
103-
public static String getSignature(ITypeBinding typeBinding) {
104-
return SignatureUtils.getSignatureForTypeKey(typeBinding.getKey());
105+
public static char[] getSignatureChar(ITypeBinding typeBinding) {
106+
return SignatureUtils.getSignature(typeBinding).toCharArray();
105107
}
106108

107109
/**
108-
* Returns the signature of the given type binding as a character array.
110+
* Returns the signature of the given type binding.
109111
*
110112
* @param typeBinding the type binding to get the signature of
111-
* @return the signature of the given type binding as a character array
113+
* @return the signature of the given type binding
112114
*/
113-
public static char[] getSignatureChar(ITypeBinding typeBinding) {
114-
return SignatureUtils.getSignatureForTypeKey(typeBinding.getKey()).toCharArray();
115+
public static String getSignature(ITypeBinding typeBinding) {
116+
if (typeBinding.isArray()) {
117+
return Signature.createArraySignature(getSignature(typeBinding.getComponentType()), 1);
118+
}
119+
if (typeBinding.isWildcardType()) {
120+
return Signature.createTypeParameterSignature(typeBinding.getName(), typeBinding.getBound() != null ? new String[] { getSignature(typeBinding.getBound()) } : null);
121+
}
122+
ITypeBinding[] typeBounds = typeBinding.getTypeBounds();
123+
if (typeBinding.isTypeVariable() || typeBinding.isWildcardType()) {
124+
return Signature.C_TYPE_VARIABLE + typeBinding.getName() + Signature.C_NAME_END;
125+
}
126+
if (typeBinding.isIntersectionType()) {
127+
return Signature.createIntersectionTypeSignature(Stream.of(typeBounds).map(SignatureUtils::getSignature).toArray(String[]::new));
128+
}
129+
if (typeBinding.isParameterizedType()) {
130+
StringBuilder res = new StringBuilder(Signature.createTypeSignature(typeBinding.getErasure().getQualifiedName(), true));
131+
res.deleteCharAt(res.length() - 1);
132+
return res.toString()
133+
+ Signature.C_GENERIC_START
134+
+ Stream.of(typeBinding.getTypeArguments()).map(SignatureUtils::getSignature).collect(Collectors.joining())
135+
+ Signature.C_GENERIC_END
136+
+ Signature.C_NAME_END;
137+
}
138+
if (typeBinding.isGenericType()) {
139+
StringBuilder res = new StringBuilder(Signature.createTypeSignature(typeBinding.getErasure().getQualifiedName(), true));
140+
res.deleteCharAt(res.length() - 1);
141+
return res.toString()
142+
+ Signature.C_GENERIC_START
143+
+ Stream.of(typeBinding.getTypeParameters()).map(SignatureUtils::getSignature).collect(Collectors.joining())
144+
+ Signature.C_GENERIC_END
145+
+ Signature.C_NAME_END;
146+
}
147+
return SignatureUtils.getSignatureForTypeKey(typeBinding.getKey());
115148
}
116149

117150
/**

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/codeassist/DOMCompletionEngine.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
import org.eclipse.jdt.core.search.SearchPattern;
160160
import org.eclipse.jdt.core.search.TypeNameMatch;
161161
import org.eclipse.jdt.core.search.TypeNameMatchRequestor;
162+
import org.eclipse.jdt.internal.SignatureUtils;
162163
import org.eclipse.jdt.internal.codeassist.impl.AssistOptions;
163164
import org.eclipse.jdt.internal.codeassist.impl.Keywords;
164165
import org.eclipse.jdt.internal.codeassist.impl.RestrictedIdentifiers;

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/codeassist/DOMCompletionUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.eclipse.jdt.core.dom.FieldAccess;
3232
import org.eclipse.jdt.core.dom.ITypeBinding;
3333
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
34+
import org.eclipse.jdt.internal.SignatureUtils;
3435
import org.eclipse.jdt.core.dom.QualifiedName;
3536
import org.eclipse.jdt.core.dom.SuperMethodReference;
3637
import org.eclipse.jdt.core.dom.TypeMethodReference;

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacMethodBinding.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Objects;
1919
import java.util.Set;
2020
import java.util.stream.Collectors;
21+
import java.util.stream.Stream;
2122

2223
import org.eclipse.jdt.core.IMethod;
2324
import org.eclipse.jdt.core.IType;
@@ -36,6 +37,7 @@
3637
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
3738
import org.eclipse.jdt.core.dom.TypeParameter;
3839
import org.eclipse.jdt.core.dom.JavacBindingResolver.BindingKeyException;
40+
import org.eclipse.jdt.internal.SignatureUtils;
3941
import org.eclipse.jdt.internal.core.BinaryMethod;
4042
import org.eclipse.jdt.internal.core.JavaElement;
4143
import org.eclipse.jdt.internal.core.Member;
@@ -280,6 +282,10 @@ private IMethod getJavaElementForMethodDeclaration(IType currentType, MethodDecl
280282
typeParamsList.add(typeParams.get(i).getName().toString());
281283
}
282284

285+
IMethod result = currentType.getMethod(getName(), Stream.of(getParameterTypes()).map(SignatureUtils::getSignature).toArray(String[]::new));
286+
if (result.exists()) {
287+
return result;
288+
}
283289
List<SingleVariableDeclaration> p = methodDeclaration.parameters();
284290
String[] params = p.stream() //
285291
.map(param -> {
@@ -289,10 +295,11 @@ private IMethod getJavaElementForMethodDeclaration(IType currentType, MethodDecl
289295
}
290296
return sig;
291297
}).toArray(String[]::new);
292-
IMethod result = currentType.getMethod(getName(), params);
293-
if (currentType.isBinary() || result.exists()) {
298+
result = currentType.getMethod(getName(), params);
299+
if (result.exists()) {
294300
return result;
295301
}
302+
296303
IMethod[] methods = null;
297304
try {
298305
methods = currentType.getMethods();

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/dom/JavacTypeBinding.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.eclipse.jdt.core.dom.ITypeBinding;
5656
import org.eclipse.jdt.core.dom.IVariableBinding;
5757
import org.eclipse.jdt.core.dom.JavacBindingResolver;
58+
import org.eclipse.jdt.core.dom.JavacCompilationUnitResolver;
5859
import org.eclipse.jdt.core.dom.MethodDeclaration;
5960
import org.eclipse.jdt.core.dom.Modifier;
6061
import org.eclipse.jdt.core.dom.RecordDeclaration;
@@ -255,7 +256,7 @@ private IJavaElement computeJavaElement() {
255256
}
256257
}
257258
ITypeRoot typeRoot = null;
258-
if (jfo != null) {
259+
if (jfo != null && !jfo.getName().endsWith(JavacCompilationUnitResolver.MOCK_NAME_FOR_CLASSES)) {
259260
var jfoFile = new File(jfo.getName());
260261
var jfoPath = new Path(jfo.getName());
261262
Stream<IFile> fileStream = jfoFile.isFile() ?
@@ -577,7 +578,7 @@ static void getKey(StringBuilder builder, Type typeToBuild, String n, boolean is
577578
} catch (IllegalArgumentException e) {
578579
// probably: uri is not a valid path
579580
}
580-
if (fileName != null && !fileName.startsWith(classSymbol.getSimpleName().toString())) {
581+
if (fileName != null && !fileName.startsWith(classSymbol.getSimpleName().toString()) && !fileName.endsWith(JavacCompilationUnitResolver.MOCK_NAME_FOR_CLASSES)) {
581582
// There are multiple top-level types in this file,
582583
// inject 'FileName~' before the type name to show that this type came from `FileName.java`
583584
// (eg. Lorg/eclipse/jdt/FileName~MyTopLevelType;)

0 commit comments

Comments
 (0)