diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/QuickFixTest22.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/QuickFixTest22.java index 53c297c033b..e98aff6d33a 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/QuickFixTest22.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/QuickFixTest22.java @@ -410,6 +410,86 @@ private static Foo 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 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"); diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java index 81eb4932225..06fd3edf8f0 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java @@ -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;