Skip to content

Commit

Permalink
Suboptimal/No code completion suggestions for switching on sealed
Browse files Browse the repository at this point in the history
interfaces

* Fixes #3300
  • Loading branch information
srikanth-sankaran committed Nov 13, 2024
1 parent 9398905 commit dbb0329
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,40 @@ void method() {}
"length[METHOD_REF]{length(), Ljava.lang.String;, ()I, length, null, "+(DEFAULT_RELEVANCE + R_NON_STATIC)+"}",
requestor.getResults());
}

// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3300
// Suboptimal/No code completion suggestions for switching on sealed interfaces
public void testIssue3300() throws JavaModelException {
this.workingCopies = new ICompilationUnit[1];
this.workingCopies[0] = getWorkingCopy("/Completion23/src/Foo.java",
"""
public sealed interface Foo {
record FooImpl_a(String x) implements Foo {
}
record FooImpl_b(String y, String z) implements Foo {
}
private static Foo getFoo() {
return new Foo.FooImpl_b("a", "b");
}
public static void main(String[] args) {
Foo foo = getFoo();
switch (foo) {
case Foo // offers no completion here.
}
}
}
""");
CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, true, true, false);
String str = this.workingCopies[0].getSource();
String completeBehind = "case Foo";
int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner);

assertResults("Foo.FooImpl_a[TYPE_REF]{FooImpl_a, , LFoo$FooImpl_a;, null, null, null, null, [300, 303], 72}\n" +
"Foo.FooImpl_b[TYPE_REF]{FooImpl_b, , LFoo$FooImpl_b;, null, null, null, null, [300, 303], 72}\n" +
"Foo[TYPE_REF]{Foo, , LFoo;, null, null, null, null, [300, 303], 86}", requestor.getResults());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3795,26 +3795,26 @@ private void completionOnSingleNameReference(ASTNode astNode, ASTNode astNodePar
this.completionToken = singleNameReference.token;
SwitchStatement switchStatement = astNodeParent instanceof SwitchStatement ? (SwitchStatement) astNodeParent : null;
boolean isSwitchEnumOrType = false;
if((switchStatement != null
if ((switchStatement != null
&& switchStatement.expression.resolvedType != null)) {
TypeBinding resolvedType = switchStatement.expression.resolvedType;
isSwitchEnumOrType = resolvedType.isEnum();
if(!isSwitchEnumOrType) {
if( this.compilerOptions.complianceLevel >= ClassFileConstants.JDK17)
if (!isSwitchEnumOrType) {
if (this.compilerOptions.complianceLevel >= ClassFileConstants.JDK21)
isSwitchEnumOrType = resolvedType.isClass() || resolvedType.isInterface() || resolvedType.isRecord();
}

}
if (isSwitchEnumOrType) {
TypeBinding resolvedType = switchStatement.expression.resolvedType;
if(resolvedType.isEnum()) {
if (resolvedType.isEnum()) {
if (!this.requestor.isIgnored(CompletionProposal.FIELD_REF)) {
this.assistNodeIsEnum = true;
findEnumConstantsFromSwithStatement(this.completionToken, switchStatement);
}
}
else {
// if switch with class/interface/record - J17 onwards
} else {
// if switch with class/interface/record - J21 onwards
findTypesAndPackages(this.completionToken, scope, true, false, new ObjectVector());
char[][] keywords = new char[2][];
int count = 0;
if (switchStatement.defaultCase == null) {
Expand Down

0 comments on commit dbb0329

Please sign in to comment.