IDEA-390428 [java]: allow creating switch statement from any class type

when patterns in switch are available

GitOrigin-RevId: 1a3458be17a41b492fbae86e186c8792c46a2b12
This commit is contained in:
Bas Leijdekkers
2026-06-12 20:33:30 +00:00
committed by intellij-monorepo-bot
parent 868d5a0e26
commit b46f3009ce
5 changed files with 49 additions and 77 deletions
@@ -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<PsiEx
}
private static boolean isValidTypeForSwitch(@Nullable PsiType type, PsiElement context) {
if (type instanceof PsiClassType) {
PsiClass resolvedClass = ((PsiClassType)type).resolve();
if (resolvedClass == null) {
return false;
}
if (type instanceof PsiClassType classType) {
if (PsiUtil.isAvailable(JavaFeature.PATTERNS_IN_SWITCH, context)) return true;
PsiClass resolvedClass = classType.resolve();
if (resolvedClass == null) return false;
return (PsiUtil.isAvailable(JavaFeature.ENUMS, context) &&
(resolvedClass.isEnum() || isSuitablePrimitiveType(PsiPrimitiveType.getUnboxedType(type)))) ||
(PsiUtil.isAvailable(JavaFeature.STRING_SWITCH, context) &&
@@ -85,10 +84,11 @@ public final class CreateSwitchIntention extends PsiUpdateModCommandAction<PsiEx
}
private static boolean isSuitablePrimitiveType(@Nullable PsiType type) {
if (type == null) {
return false;
}
return type.equals(PsiTypes.intType()) || type.equals(PsiTypes.byteType()) || type.equals(PsiTypes.shortType()) || type.equals(PsiTypes.charType());
if (type == null) return false;
return type.equals(PsiTypes.intType())
|| type.equals(PsiTypes.byteType())
|| type.equals(PsiTypes.shortType())
|| type.equals(PsiTypes.charType());
}
@Override
@@ -0,0 +1,8 @@
class X {
public static double getPerimeter(Shape shape) throws IllegalArgumentException {
shape<caret>
}
}
interface Shape { }
record Rectangle(double length, double width) implements Shape { }
record Circle(double radius) implements Shape { }
@@ -0,0 +1,8 @@
class X {
public static double getPerimeter(Shape shape) throws IllegalArgumentException {
shape<caret>
}
}
interface Shape { }
record Rectangle(double length, double width) implements Shape { }
record Circle(double radius) implements Shape { }
@@ -0,0 +1,9 @@
class X {
public static double getPerimeter(Shape shape) throws IllegalArgumentException {
switch (shape) {<caret>
}
}
}
interface Shape { }
record Rectangle(double length, double width) implements Shape { }
record Circle(double radius) implements Shape { }
@@ -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);