Skip to content

Commit f8ba27b

Browse files
author
Rob Stryker
committed
Fix CompletionTests_1_5.test0008 by adding provisional API
Signed-off-by: Rob Stryker <stryker@redhat.com>
1 parent ee3792b commit f8ba27b

File tree

5 files changed

+141
-14
lines changed

5 files changed

+141
-14
lines changed

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

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@
4949
import org.eclipse.jdt.core.dom.IMethodBinding;
5050
import org.eclipse.jdt.core.dom.IModuleBinding;
5151
import org.eclipse.jdt.core.dom.IPackageBinding;
52+
import org.eclipse.jdt.core.dom.ISignatureProvider;
5253
import org.eclipse.jdt.core.dom.ITypeBinding;
5354
import org.eclipse.jdt.core.dom.IVariableBinding;
5455
import org.eclipse.jdt.core.dom.JavacBindingResolver;
56+
import org.eclipse.jdt.core.dom.JavacBindingResolver.BindingKeyException;
5557
import org.eclipse.jdt.core.dom.MethodDeclaration;
5658
import org.eclipse.jdt.core.dom.Modifier;
5759
import org.eclipse.jdt.core.dom.RecordDeclaration;
5860
import org.eclipse.jdt.core.dom.TypeDeclaration;
59-
import org.eclipse.jdt.core.dom.JavacBindingResolver.BindingKeyException;
6061
import org.eclipse.jdt.internal.compiler.codegen.ConstantPool;
6162
import org.eclipse.jdt.internal.core.BinaryType;
6263
import org.eclipse.jdt.internal.core.JavaElement;
@@ -67,12 +68,9 @@
6768
import com.sun.tools.javac.code.Attribute;
6869
import com.sun.tools.javac.code.Flags;
6970
import com.sun.tools.javac.code.Kinds;
70-
import com.sun.tools.javac.code.Symbol;
71-
import com.sun.tools.javac.code.Type;
72-
import com.sun.tools.javac.code.TypeTag;
73-
import com.sun.tools.javac.code.Types;
7471
import com.sun.tools.javac.code.Kinds.Kind;
7572
import com.sun.tools.javac.code.Kinds.KindSelector;
73+
import com.sun.tools.javac.code.Symbol;
7674
import com.sun.tools.javac.code.Symbol.ClassSymbol;
7775
import com.sun.tools.javac.code.Symbol.CompletionFailure;
7876
import com.sun.tools.javac.code.Symbol.MethodSymbol;
@@ -81,6 +79,7 @@
8179
import com.sun.tools.javac.code.Symbol.TypeSymbol;
8280
import com.sun.tools.javac.code.Symbol.TypeVariableSymbol;
8381
import com.sun.tools.javac.code.Symbol.VarSymbol;
82+
import com.sun.tools.javac.code.Type;
8483
import com.sun.tools.javac.code.Type.ArrayType;
8584
import com.sun.tools.javac.code.Type.ClassType;
8685
import com.sun.tools.javac.code.Type.ErrorType;
@@ -90,11 +89,13 @@
9089
import com.sun.tools.javac.code.Type.MethodType;
9190
import com.sun.tools.javac.code.Type.TypeVar;
9291
import com.sun.tools.javac.code.Type.WildcardType;
92+
import com.sun.tools.javac.code.TypeTag;
93+
import com.sun.tools.javac.code.Types;
9394
import com.sun.tools.javac.code.Types.FunctionDescriptorLookupError;
9495
import com.sun.tools.javac.util.Name;
9596
import com.sun.tools.javac.util.Names;
9697

97-
public abstract class JavacTypeBinding implements ITypeBinding {
98+
public abstract class JavacTypeBinding implements ITypeBinding, ISignatureProvider {
9899

99100
private static final ITypeBinding[] NO_TYPE_ARGUMENTS = new ITypeBinding[0];
100101

@@ -313,6 +314,10 @@ private static String removeTrailingSemicolon(String key) {
313314
return key.endsWith(";") ? key.substring(0, key.length() - 1) : key;
314315
}
315316

317+
public String getSignature() {
318+
return getKey(true, true);
319+
}
320+
316321
private String getKey(Type t) {
317322
return getKey(t, this.typeSymbol.flatName());
318323
}
@@ -321,13 +326,21 @@ public String getKey(boolean includeTypeParameters) {
321326
return getKey(this.type, this.typeSymbol.flatName(), includeTypeParameters);
322327
}
323328

329+
public String getKey(boolean includeTypeParameters, boolean ignoreFilenames) {
330+
return getKey(this.type, this.typeSymbol.flatName(), includeTypeParameters, ignoreFilenames);
331+
}
332+
324333
public String getKey(Type t, Name n) {
325334
return getKey(type, n, true);
326335
}
336+
327337
public String getKey(Type t, Name n, boolean includeTypeParameters) {
338+
return getKey(t, n, includeTypeParameters, false);
339+
}
340+
public String getKey(Type t, Name n, boolean includeTypeParameters, boolean signatureMode) {
328341
try {
329342
StringBuilder builder = new StringBuilder();
330-
getKey(builder, t, n, false, includeTypeParameters, this.resolver);
343+
getKey(builder, t, n, false, includeTypeParameters, signatureMode, this.resolver);
331344
return builder.toString();
332345
} catch(BindingKeyException bke) {
333346
return null;
@@ -344,6 +357,10 @@ static void getKey(StringBuilder builder, Type typeToBuild, boolean isLeaf, bool
344357
}
345358

346359
static void getKey(StringBuilder builder, Type typeToBuild, Name n, boolean isLeaf, boolean includeParameters, JavacBindingResolver resolver) throws BindingKeyException {
360+
getKey(builder, typeToBuild, n, isLeaf, includeParameters, false, resolver);
361+
}
362+
363+
static void getKey(StringBuilder builder, Type typeToBuild, Name n, boolean isLeaf, boolean includeParameters, boolean signatureMode, JavacBindingResolver resolver) throws BindingKeyException {
347364
if (typeToBuild instanceof Type.JCNoType) {
348365
return;
349366
}
@@ -390,7 +407,11 @@ static void getKey(StringBuilder builder, Type typeToBuild, Name n, boolean isLe
390407
* but the test suite expects test0502.A$182,
391408
* where 182 is the location in the source of the symbol.
392409
*/
393-
builder.append(n.toString().replace('.', '/'));
410+
if( signatureMode ) {
411+
builder.append(n.toString());
412+
} else {
413+
builder.append(n.toString().replace('.', '/'));
414+
}
394415
// This is a hack and will likely need to be enhanced
395416
if (typeToBuild.tsym instanceof ClassSymbol classSymbol && !(classSymbol.type instanceof ErrorType) && classSymbol.owner instanceof PackageSymbol) {
396417
JavaFileObject sourcefile = classSymbol.sourcefile;
@@ -402,7 +423,7 @@ static void getKey(StringBuilder builder, Type typeToBuild, Name n, boolean isLe
402423
} catch (IllegalArgumentException e) {
403424
// probably: uri is not a valid path
404425
}
405-
if (fileName != null && !fileName.startsWith(classSymbol.getSimpleName().toString())) {
426+
if (fileName != null && !signatureMode && !fileName.startsWith(classSymbol.getSimpleName().toString())) {
406427
// There are multiple top-level types in this file,
407428
// inject 'FileName~' before the type name to show that this type came from `FileName.java`
408429
// (eg. Lorg/eclipse/jdt/FileName~MyTopLevelType;)

org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/DOMCompletionContext.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,7 @@ public char[][] getExpectedTypesSignatures() {
200200
return null;
201201
}
202202
var res = this.expectedTypes.getExpectedTypes().stream() //
203-
.map(type -> type.getKey()) //
204-
.map(name -> name.replace('/', '.'))
205-
.map(String::toCharArray) //
203+
.map(type -> DOMCompletionEngineBuilder.getSignature(type)) //
206204
.toArray(char[][]::new);
207205
return res.length == 0 ? null : res;
208206
}

org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/DOMCompletionEngineBuilder.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package org.eclipse.jdt.internal.codeassist;
1515

1616
import org.eclipse.jdt.core.dom.IMethodBinding;
17+
import org.eclipse.jdt.core.dom.ISignatureProvider;
1718
import org.eclipse.jdt.core.dom.ITypeBinding;
1819
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
1920
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
@@ -188,8 +189,15 @@ static char[] getSignature(IMethodBinding methodBinding) {
188189
}
189190
}
190191

192+
static String getSignatureAsString(ITypeBinding typeBinding) {
193+
if( typeBinding instanceof ISignatureProvider isp) {
194+
return isp.getSignature();
195+
}
196+
return typeBinding.getKey().replace('/', '.');
197+
}
198+
191199
static char[] getSignature(ITypeBinding typeBinding) {
192-
return typeBinding.getKey().replace('/', '.').toCharArray();
200+
return getSignatureAsString(typeBinding).toCharArray();
193201
}
194202

195203
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 IBM Corporation and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*******************************************************************************/
11+
12+
package org.eclipse.jdt.core.dom;
13+
14+
/**
15+
* A signature provider is an object, usually a binding of some type,
16+
* that is capable of providing a signature, such as a method signature
17+
* or a type signature.
18+
*
19+
* This class is Provisional API for use by JDT/UI or JDT/Debug, which may possibly be removed in a future version.
20+
*
21+
* @since 3.42
22+
* @noimplement This interface is not intended to be implemented by clients.
23+
*
24+
*/
25+
public interface ISignatureProvider {
26+
/**
27+
* Returns the signature for this object.
28+
* <p>
29+
* Within a single cluster of bindings (produced by the same call to an
30+
* {@code ASTParser#create*(*)} method)), each binding has a distinct signature.
31+
* The signatures are generated in a manner that is predictable and as
32+
* stable as possible. This last property makes these signatures useful for
33+
* comparing bindings between different clusters of bindings (for example,
34+
* the bindings between the "before" and "after" ASTs of the same
35+
* compilation unit).
36+
* </p>
37+
* <p>
38+
* The exact details of how the keys are generated is unspecified.
39+
* However, it is a function of the following information:
40+
* <ul>
41+
* <li>packages - the name of the package (for an unnamed package,
42+
* some internal id)</li>
43+
* <li>classes or interfaces - the VM name of the type and the key
44+
* of its package</li>
45+
* <li>array types - the key of the component type and number of
46+
* dimensions</li>
47+
* <li>primitive types - the name of the primitive type</li>
48+
* <li>fields - the name of the field and the key of its declaring
49+
* type</li>
50+
* <li>methods - the name of the method, the key of its declaring
51+
* type, and the keys of the parameter types</li>
52+
* <li>constructors - the key of its declaring class, and the
53+
* keys of the parameter types</li>
54+
* <li>local variables - the name of the local variable, the index of the
55+
* declaring block relative to its parent, the key of its method</li>
56+
* <li>local types - the name of the type, the index of the declaring
57+
* block relative to its parent, the key of its method</li>
58+
* <li>anonymous types - the occurrence count of the anonymous
59+
* type relative to its declaring type, the key of its declaring type</li>
60+
* <li>enum types - treated like classes</li>
61+
* <li>annotation types - treated like interfaces</li>
62+
* <li>type variables - the name of the type variable and
63+
* the key of the generic type or generic method that declares that
64+
* type variable</li>
65+
* <li>wildcard types - the key of the optional wildcard type bound</li>
66+
* <li>capture type bindings - the key of the wildcard captured</li>
67+
* <li>generic type instances - the key of the generic type and the keys
68+
* of the type arguments used to instantiate it, and whether the
69+
* instance is explicit (a parameterized type reference) or
70+
* implicit (a raw type reference)</li>
71+
* <li>generic method instances - the key of the generic method and the keys
72+
* of the type arguments used to instantiate it, and whether the
73+
* instance is explicit (a parameterized method reference) or
74+
* implicit (a raw method reference)</li>
75+
* <li>members of generic type instances - the key of the generic type
76+
* instance and the key of the corresponding member in the generic
77+
* type</li>
78+
* <li>annotations - the key of the annotated element and the key of
79+
* the annotation type</li>
80+
* </ul>
81+
* <p>
82+
* The key for a type binding does <em>not</em> contain {@link ITypeBinding#getTypeAnnotations() type annotations},
83+
* so type bindings with different type annotations may have the same key (iff they denote the same un-annotated type).
84+
* By construction, this also applies to method bindings if their declaring types contain type annotations.
85+
* </p>
86+
* <p>Note that the key for member value pair bindings is
87+
* not yet implemented. This method returns <code>null</code> for that kind of bindings.<br>
88+
* Recovered bindings have a unique key.
89+
* </p>
90+
*
91+
* @return the key for this binding
92+
*/
93+
public String getSignature();
94+
}

org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/TypeBinding.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.eclipse.jdt.core.IJavaElement;
2222
import org.eclipse.jdt.core.JavaCore;
2323
import org.eclipse.jdt.core.compiler.CharOperation;
24+
import org.eclipse.jdt.internal.codeassist.impl.Engine;
2425
import org.eclipse.jdt.internal.compiler.ast.StringLiteral;
2526
import org.eclipse.jdt.internal.compiler.ast.Wildcard;
2627
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
@@ -35,7 +36,7 @@
3536
/**
3637
* Internal implementation of type bindings.
3738
*/
38-
class TypeBinding implements ITypeBinding {
39+
class TypeBinding implements ITypeBinding, ISignatureProvider {
3940
private static final StringLiteral EXPRESSION = new org.eclipse.jdt.internal.compiler.ast.StringLiteral(0,0);
4041

4142
protected static final IMethodBinding[] NO_METHOD_BINDINGS = new IMethodBinding[0];
@@ -571,6 +572,11 @@ private JavaElement getUnresolvedJavaElement(org.eclipse.jdt.internal.compiler.l
571572
return null;
572573
}
573574

575+
@Override
576+
public String getSignature() {
577+
return new String(Engine.getSignature(this.binding));
578+
}
579+
574580
@Override
575581
public String getKey() {
576582
if (this.key == null) {

0 commit comments

Comments
 (0)