[java-intentions] CreateSwitchIntention: enable on Character, Byte, Short, and Integer

GitOrigin-RevId: 185df516fd2aba29b73b9183a58073eec4d3d118
This commit is contained in:
Andrey.Cherkasov
2022-07-20 05:53:08 +00:00
committed by intellij-monorepo-bot
parent e89a3d743c
commit fb05bfd331
4 changed files with 34 additions and 5 deletions
@@ -60,18 +60,22 @@ public class CreateSwitchIntention extends BaseElementAtCaretIntentionAction imp
}
private static boolean isValidTypeForSwitch(@Nullable PsiType type, PsiElement context) {
if (type == null) {
return false;
}
if (type instanceof PsiClassType) {
PsiClass resolvedClass = ((PsiClassType)type).resolve();
if (resolvedClass == null) {
return false;
}
return (PsiUtil.isLanguageLevel5OrHigher(context) && resolvedClass.isEnum())
return (PsiUtil.isLanguageLevel5OrHigher(context) &&
(resolvedClass.isEnum() || isSuitablePrimitiveType(PsiPrimitiveType.getUnboxedType(type))))
|| (PsiUtil.isLanguageLevel7OrHigher(context) && CommonClassNames.JAVA_LANG_STRING.equals(resolvedClass.getQualifiedName()));
}
return isSuitablePrimitiveType(type);
}
private static boolean isSuitablePrimitiveType(@Nullable PsiType type) {
if (type == null) {
return false;
}
return type.equals(PsiType.INT) || type.equals(PsiType.BYTE) || type.equals(PsiType.SHORT) || type.equals(PsiType.CHAR);
}
@@ -0,0 +1,10 @@
class X {
void m() {
Byte foo = 10;
fo<caret>o
}
}
@@ -0,0 +1,11 @@
class X {
void m() {
Byte foo = 10;
switch (foo) {<caret>
}
}
}
@@ -47,6 +47,10 @@ public class CreateSwitchTest extends JavaCodeInsightFixtureTestCase {
doTest();
}
public void testBoxedType() {
doTest();
}
public void testNotAvailable() {
doTestNotAvailable();
}