Skip to content

Commit 8aa0ce8

Browse files
committed
Fix generating signatures from bindings
1 parent 1ac7097 commit 8aa0ce8

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/SignatureUtils.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static String getSignature(ITypeBinding typeBinding) {
126126
return Signature.C_EXTENDS + Stream.of(upper).map(SignatureUtils::getSignature).collect(Collectors.joining());
127127
}
128128
var lower = typeBinding.getWildcard();
129-
if (lower != null) {
129+
if (lower != null && lower != typeBinding) {
130130
return Signature.C_SUPER + SignatureUtils.getSignature(lower);
131131
}
132132
// TODO if typeBinding.getBounds(): C_EXTENDS, C_SUPER
@@ -144,7 +144,10 @@ public static String getSignature(ITypeBinding typeBinding) {
144144
res.deleteCharAt(res.length() - 1);
145145
return res.toString()
146146
+ Signature.C_GENERIC_START
147-
+ Stream.of(typeBinding.getTypeArguments()).map(SignatureUtils::getSignature).collect(Collectors.joining())
147+
+ Stream.of(typeBinding.getTypeArguments())
148+
.map(SignatureUtils::getSignature)
149+
.map(sig -> "+Ljava.lang.Object;".equals(sig) ? "*" : sig)
150+
.collect(Collectors.joining())
148151
+ Signature.C_GENERIC_END
149152
+ Signature.C_NAME_END;
150153
}
@@ -177,7 +180,11 @@ public static String getSignatureForTypeKey(String key) {
177180
}
178181

179182
public static String getSignature(IMethodBinding methodBinding) {
180-
return getSignatureForMethodKey(methodBinding.getKey());
183+
return Signature.C_PARAM_START
184+
+ Stream.of(methodBinding.getParameterTypes()).map(SignatureUtils::getSignature).collect(Collectors.joining())
185+
+ Signature.C_PARAM_END
186+
+ SignatureUtils.getSignature(methodBinding.getReturnType());
187+
// exceptions? type params?
181188
}
182189

183190
/**
@@ -187,7 +194,7 @@ public static String getSignature(IMethodBinding methodBinding) {
187194
* @return the signature of the given method binding as a character array
188195
*/
189196
public static char[] getSignatureChar(IMethodBinding methodBinding) {
190-
return getSignatureForMethodKey(methodBinding.getKey()).toCharArray();
197+
return getSignature(methodBinding).toCharArray();
191198
}
192199

193200
public static char[] getSignatureChar(IMethod method) {
@@ -203,7 +210,7 @@ public static char[] getSignatureChar(IMethod method) {
203210
public static String getSignatureForMethodKey(String key) {
204211
String fullKey = key
205212
.replace('/', '.')
206-
.replace("<+Ljava.lang.Object;>", "<*>")
213+
.replace("+Ljava.lang.Object;", "*")
207214
.replace("<>;", ";");
208215
String removeName = fullKey.substring(fullKey.indexOf('('));
209216
int firstException = removeName.indexOf('|');

org.eclipse.jdt.core.tests.javac/src/org/eclipse/jdt/core/tests/javac/RegressionTests.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import static org.junit.Assert.assertArrayEquals;
1414
import static org.junit.Assert.assertEquals;
1515
import static org.junit.Assert.assertTrue;
16-
import static org.junit.Assert.assertNotNull;
1716

1817
import java.io.File;
1918
import java.io.IOException;
@@ -227,18 +226,26 @@ public void testParameterCountWithCapture() throws Exception {
227226
var modelMethod = (IMethod)domMethod.getJavaElement();
228227
String signature = modelMethod.getSignature();
229228
assertEquals(1, Signature.getParameterCount(signature));
230-
CompletionProposal[] mapProposal = new CompletionProposal[] { null };
229+
Set<CompletionProposal> invalidProposals = new HashSet<>();
231230
unit.codeComplete(index + 1, new org.eclipse.jdt.core.CompletionRequestor() {
232231
@Override
233232
public void accept(CompletionProposal proposal) {
234-
if (proposal.getCompletion() != null && Set.of("map" /* DOM first */, "map()" /* Legacy */).contains(new String(proposal.getCompletion()))) {
235-
mapProposal[0] = proposal;
233+
if (proposal.getKind() != CompletionProposal.METHOD_REF) {
234+
return;
235+
}
236+
try {
237+
Signature.toCharArray(proposal.getDeclarationSignature());
238+
} catch (Exception ex) {
239+
invalidProposals.add(proposal);
240+
}
241+
try {
242+
Signature.toCharArray(proposal.getSignature());
243+
} catch (Exception ex) {
244+
invalidProposals.add(proposal);
236245
}
237246
}
238247
});
239-
assertNotNull(mapProposal[0]);
240-
// ensure next line doesn't cause exception
241-
Signature.toCharArray(mapProposal[0].getDeclarationSignature());
248+
assertEquals(Set.of(), invalidProposals);
242249
}
243250
}
244251

0 commit comments

Comments
 (0)