Skip to content

Commit

Permalink
Fix add permitted types quickfix (#1846)
Browse files Browse the repository at this point in the history
- fix LocalCorrectionsSubProcessor.addPermittedTypes() method to
  not pass empty importName to the search pattern
- add new test to QuickFixTest22
- fixes #1845
  • Loading branch information
jjohnstn authored Dec 9, 2024
1 parent d0563dc commit cae21d0
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
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

0 comments on commit cae21d0

Please sign in to comment.