Skip to content

Commit cae21d0

Browse files
authored
Fix add permitted types quickfix (#1846)
- fix LocalCorrectionsSubProcessor.addPermittedTypes() method to not pass empty importName to the search pattern - add new test to QuickFixTest22 - fixes #1845
1 parent d0563dc commit cae21d0

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/QuickFixTest22.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,86 @@ private static Foo<String> getFoo() {
410410
assertEqualString(preview1, expected1);
411411
}
412412

413+
@Test
414+
public void testAddPermittedTypesToSwitchStatement2() throws Exception {
415+
fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin");
416+
fJProject1.setRawClasspath(projectsetup.getDefaultClasspath(), null);
417+
JavaProjectHelper.set22CompilerOptions(fJProject1);
418+
419+
fSourceFolder= JavaProjectHelper.addSourceContainer(fJProject1, "src");
420+
IPackageFragment pack1= fSourceFolder.createPackageFragment("test", false, null);
421+
422+
String shape= """
423+
package test;
424+
public sealed class Shape permits Circle, Square {
425+
}
426+
""";
427+
pack1.createCompilationUnit("Shape.java", shape, false, null);
428+
429+
String circle= """
430+
package test;
431+
public final class Circle extends Shape {
432+
}
433+
""";
434+
pack1.createCompilationUnit("Circle.java", circle, false, null);
435+
436+
String square= """
437+
package test;
438+
public final class Square extends Shape {
439+
}
440+
""";
441+
pack1.createCompilationUnit("Square.java", square, false, null);
442+
443+
String test= """
444+
package test;
445+
446+
public class TestSwitch {
447+
448+
public static void main(String[] args) {
449+
Shape shape = getShape();
450+
switch (shape) {
451+
}
452+
}
453+
454+
private static Shape getShape() {
455+
return new Circle();
456+
}
457+
}
458+
""";
459+
460+
ICompilationUnit cu= pack1.createCompilationUnit("TestSwitch.java", test, false, null);
461+
462+
CompilationUnit astRoot= getASTRoot(cu);
463+
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot, 1);
464+
assertCorrectLabels(proposals);
465+
466+
CUCorrectionProposal proposal1= (CUCorrectionProposal) proposals.get(0);
467+
String preview1= getPreviewContent(proposal1);
468+
469+
String expected1= """
470+
package test;
471+
472+
public class TestSwitch {
473+
474+
public static void main(String[] args) {
475+
Shape shape = getShape();
476+
switch (shape) {
477+
case Circle c -> {}
478+
case Square s -> {}
479+
case null -> {}
480+
default -> {}
481+
}
482+
}
483+
484+
private static Shape getShape() {
485+
return new Circle();
486+
}
487+
}
488+
""";
489+
490+
assertEqualString(preview1, expected1);
491+
}
492+
413493
@Test
414494
public void testAddPermittedTypesToSwitchExpression() throws Exception {
415495
fJProject1= JavaProjectHelper.createJavaProject("TestProject1", "bin");

org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,9 +2461,13 @@ public void acceptSearchMatch(SearchMatch match) throws CoreException {
24612461
String[][] resolvedName= sealedType.resolveType(permittedTypeName);
24622462
for (int i= 0; i < resolvedName.length; ++i) {
24632463
String[] inner= resolvedName[i];
2464-
if (!inner[0].isEmpty() && !inner[0].equals(pkgName)) {
2465-
needImport= true;
2464+
if (!inner[0].isEmpty()) {
24662465
importName= inner[0] + "." + inner[1]; //$NON-NLS-1$
2466+
if (!inner[0].equals(pkgName)) {
2467+
needImport= true;
2468+
}
2469+
} else {
2470+
importName= inner[1];
24672471
}
24682472
if (permittedTypeName.startsWith(sealedType.getTypeQualifiedName('.'))) {
24692473
needImport= false;

0 commit comments

Comments
 (0)