From b46f3009ce70e9ab6e11225b6e2cf096c28ea88e Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 11 Jun 2026 17:57:01 +0200 Subject: [PATCH] IDEA-390428 [java]: allow creating switch statement from any class type when patterns in switch are available GitOrigin-RevId: 1a3458be17a41b492fbae86e186c8792c46a2b12 --- .../intention/impl/CreateSwitchIntention.java | 20 ++--- .../createSwitch/noPatternMatching.java | 8 ++ .../createSwitch/patternMatching.java | 8 ++ .../createSwitch/patternMatching_after.java | 9 +++ .../intention/CreateSwitchTest.java | 81 ++++--------------- 5 files changed, 49 insertions(+), 77 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/createSwitch/noPatternMatching.java create mode 100644 java/java-tests/testData/codeInsight/createSwitch/patternMatching.java create mode 100644 java/java-tests/testData/codeInsight/createSwitch/patternMatching_after.java diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/CreateSwitchIntention.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/CreateSwitchIntention.java index 355bc1388580..5b4ce4328e52 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/CreateSwitchIntention.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/CreateSwitchIntention.java @@ -1,4 +1,4 @@ -// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInsight.intention.impl; import com.intellij.codeInsight.intention.PriorityAction; @@ -71,11 +71,10 @@ public final class CreateSwitchIntention extends PsiUpdateModCommandAction + } +} +interface Shape { } +record Rectangle(double length, double width) implements Shape { } +record Circle(double radius) implements Shape { } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/createSwitch/patternMatching.java b/java/java-tests/testData/codeInsight/createSwitch/patternMatching.java new file mode 100644 index 000000000000..060e6600dfa8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/createSwitch/patternMatching.java @@ -0,0 +1,8 @@ +class X { + public static double getPerimeter(Shape shape) throws IllegalArgumentException { + shape + } +} +interface Shape { } +record Rectangle(double length, double width) implements Shape { } +record Circle(double radius) implements Shape { } \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/createSwitch/patternMatching_after.java b/java/java-tests/testData/codeInsight/createSwitch/patternMatching_after.java new file mode 100644 index 000000000000..9f92c95b4a8c --- /dev/null +++ b/java/java-tests/testData/codeInsight/createSwitch/patternMatching_after.java @@ -0,0 +1,9 @@ +class X { + public static double getPerimeter(Shape shape) throws IllegalArgumentException { + switch (shape) { + } + } +} +interface Shape { } +record Rectangle(double length, double width) implements Shape { } +record Circle(double radius) implements Shape { } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/CreateSwitchTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/CreateSwitchTest.java index 759a09ae50aa..1df6c4039e89 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/CreateSwitchTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/CreateSwitchTest.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.java.codeInsight.intention; import com.intellij.JavaTestUtil; @@ -31,59 +17,20 @@ public class CreateSwitchTest extends JavaCodeInsightFixtureTestCase { return JavaTestUtil.getJavaTestDataPath() + "/codeInsight/createSwitch/"; } - public void testEnum() { - doTest(); - } - - public void testEnum2() { - doTest(); - } - - public void testString() { - withJava7(this::doTest); - } - - public void testPrimitive() { - doTest(); - } - - public void testBoxedType() { - doTest(); - } - - public void testNotAvailable() { - doTestNotAvailable(); - } - - public void testNotAvailable2() { - doTestNotAvailable(); - } - - public void testNotAvailableInForUpdate() { - doTestNotAvailable(); - } - - public void testNotAvailableInAssignment() { - doTestNotAvailable(); - } - - public void testNotAvailableOnRedCode() { - withJava7(this::doTestNotAvailable); - } - - public void testNotFailingOnBadEscapes() { withJava7(this::doTestNotAvailable); } - + public void testEnum() { doTest(); } + public void testEnum2() { doTest(); } + public void testString() { IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_1_7, this::doTest); } + public void testPrimitive() { doTest(); } + public void testBoxedType() { doTest(); } + public void testNotAvailable() { doTestNotAvailable(); } + public void testNotAvailable2() { doTestNotAvailable(); } + public void testNotAvailableInForUpdate() { doTestNotAvailable(); } + public void testNotAvailableInAssignment() { doTestNotAvailable(); } + public void testNotAvailableOnRedCode() { IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_1_7, this::doTestNotAvailable); } + public void testNotFailingOnBadEscapes() { IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_1_7, this::doTestNotAvailable); } public void testNotAvailableOnLiteral() { doTestNotAvailable(); } - - private void withJava7(Runnable runnable) { - final LanguageLevel oldLanguageLevel = IdeaTestUtil.setProjectLanguageLevel(getProject(), LanguageLevel.JDK_1_7); - try { - runnable.run(); - } - finally { - IdeaTestUtil.setProjectLanguageLevel(getProject(), oldLanguageLevel); - } - } + public void testPatternMatching() { IdeaTestUtil.withLevel(getModule(), LanguageLevel.JDK_21, this::doTest); } + public void testNoPatternMatching() { doTestNotAvailable(); } private void doTest() { final String name = getTestName(true);