Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix add permitted types quickfix #1846

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,86 @@ private static Foo<String> getFoo() {
assertEqualString(preview1, expected1);
}

@Test
public void testAddPermittedTypesToSwitchStatement2() throws Exception {
fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin");
fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null);
JavaProjectHelper.set22CompilerOptions(fJProject1);

fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");
IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);

String shape= """
package test;
public sealed class Shape permits Circle, Square {
}
""";
pack1.createCompilationUnit("Shape.java", shape, false, null);

String circle= """
package test;
public final class Circle extends Shape {
}
""";
pack1.createCompilationUnit("Circle.java", circle, false, null);

String square= """
package test;
public final class Square extends Shape {
}
""";
pack1.createCompilationUnit("Square.java", square, false, null);

String test= """
package test;

public class TestSwitch {

public static void main(String[] args) {
Shape shape = getShape();
switch (shape) {
}
}

private static Shape getShape() {
return new Circle();
}
}
""";

ICompilationUnit cu= pack1.createCompilationUnit("TestSwitch.java", test, false, null);

CompilationUnit astRoot= getASTRoot(cu);
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 1);
assertCorrectLabels(proposals);

CUCorrectionProposal proposal1= (CUCorrectionProposal) proposals.get(0);
String preview1= getPreviewContent(proposal1);

String expected1= """
package test;

public class TestSwitch {

public static void main(String[] args) {
Shape shape = getShape();
switch (shape) {
case Circle c -> {}
case Square s -> {}
case null -> {}
default -> {}
}
}

private static Shape getShape() {
return new Circle();
}
}
""";

assertEqualString(preview1, expected1);
}

@Test
public void testAddPermittedTypesToSwitchExpression() throws Exception {
fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2461,9 +2461,13 @@ public void acceptSearchMatch(SearchMatch match) throws CoreException {
String[][] resolvedName= sealedType.resolveType(permittedTypeName);
for (int i= 0; i < resolvedName.length; ++i) {
String[] inner= resolvedName[i];
if (!inner[0].isEmpty() && !inner[0].equals(pkgName)) {
needImport= true;
if (!inner[0].isEmpty()) {
importName= inner[0] + "." + inner[1]; //$NON-NLS-1$
if (!inner[0].equals(pkgName)) {
needImport= true;
}
} else {
importName= inner[1];
}
if (permittedTypeName.startsWith(sealedType.getTypeQualifiedName('.'))) {
needImport= false;
Expand Down
Loading