mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java-highlighting] Provide fixes for unexpected primitive type pattern
IDEA-279711 GitOrigin-RevId: 58d634c764bf9ab92d0821d3e456ab4e267b2eb9
This commit is contained in:
committed by
intellij-monorepo-bot
parent
d44db1b1c2
commit
cc9c5fe107
@@ -529,6 +529,8 @@ public abstract class QuickFixFactory {
|
||||
public abstract @NotNull IntentionAction createReceiverParameterTypeFix(@NotNull PsiReceiverParameter receiverParameter,
|
||||
@NotNull PsiType enclosingClassType);
|
||||
|
||||
public abstract @NotNull IntentionAction createVariableTypeFix(@NotNull PsiVariable variable, @NotNull PsiType type);
|
||||
|
||||
public abstract @NotNull IntentionAction createConvertInterfaceToClassFix(@NotNull PsiClass aClass);
|
||||
|
||||
@Nullable
|
||||
|
||||
+22
-2
@@ -443,10 +443,16 @@ public class SwitchBlockHighlightingModel {
|
||||
return null;
|
||||
}
|
||||
else if (label instanceof PsiPattern) {
|
||||
PsiType patternType = JavaPsiPatternUtil.getPatternType((PsiPattern)label);
|
||||
PsiPattern pattern = (PsiPattern)label;
|
||||
PsiType patternType = JavaPsiPatternUtil.getPatternType(pattern);
|
||||
if (!(patternType instanceof PsiClassType) && !(patternType instanceof PsiArrayType)) {
|
||||
String expectedTypes = JavaErrorBundle.message("switch.class.or.array.type.expected");
|
||||
return createError(label, JavaErrorBundle.message("unexpected.type", expectedTypes, JavaHighlightUtil.formatType(patternType)));
|
||||
String message = JavaErrorBundle.message("unexpected.type", expectedTypes, JavaHighlightUtil.formatType(patternType));
|
||||
HighlightInfo info = createError(label, message);
|
||||
if (patternType instanceof PsiPrimitiveType) {
|
||||
registerVariableTypeFixes(info, pattern, (PsiPrimitiveType)patternType);
|
||||
}
|
||||
return info;
|
||||
}
|
||||
if (!TypeConversionUtil.areTypesConvertible(mySelectorType, patternType)) {
|
||||
return HighlightUtil.createIncompatibleTypeHighlightInfo(mySelectorType, patternType, label.getTextRange(), 0);
|
||||
@@ -740,6 +746,20 @@ public class SwitchBlockHighlightingModel {
|
||||
QuickFixAction.registerQuickFixAction(info, getFixFactory().createDeleteDefaultFix(myFile, info));
|
||||
}
|
||||
|
||||
private static void registerVariableTypeFixes(@Nullable HighlightInfo info,
|
||||
@NotNull PsiPattern pattern,
|
||||
@NotNull PsiPrimitiveType primitiveType) {
|
||||
PsiType arrayType = PsiTypesUtil.createArrayType(primitiveType, 1);
|
||||
PsiPatternVariable patternVariable = JavaPsiPatternUtil.getPatternVariable(pattern);
|
||||
if (patternVariable == null) return;
|
||||
PsiClassType boxedType = primitiveType.getBoxedType(patternVariable);
|
||||
IntentionAction changeToArrayTypeFix = getFixFactory().createVariableTypeFix(patternVariable, arrayType);
|
||||
QuickFixAction.registerQuickFixAction(info, changeToArrayTypeFix);
|
||||
if (boxedType == null) return;
|
||||
IntentionAction changeToBoxTypeFix = getFixFactory().createVariableTypeFix(patternVariable, boxedType);
|
||||
QuickFixAction.registerQuickFixAction(info, changeToBoxTypeFix);
|
||||
}
|
||||
|
||||
private void checkSealedClassCompleteness(@NotNull PsiClass selectorClass,
|
||||
@NotNull List<PsiCaseLabelElement> elements,
|
||||
@NotNull List<HighlightInfo> results) {
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ public class VariableTypeFix extends LocalQuickFixAndIntentionActionOnPsiElement
|
||||
private final PsiType myReturnType;
|
||||
protected final String myName;
|
||||
|
||||
protected VariableTypeFix(@NotNull PsiVariable variable, @NotNull PsiType toReturn) {
|
||||
public VariableTypeFix(@NotNull PsiVariable variable, @NotNull PsiType toReturn) {
|
||||
super(variable);
|
||||
myReturnType = GenericsUtil.getVariableTypeByExpressionType(toReturn);
|
||||
myName = variable.getName();
|
||||
|
||||
+5
@@ -1052,6 +1052,11 @@ public final class QuickFixFactoryImpl extends QuickFixFactory {
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull IntentionAction createVariableTypeFix(@NotNull PsiVariable variable, @NotNull PsiType type) {
|
||||
return new VariableTypeFix(variable, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull IntentionAction createConvertInterfaceToClassFix(@NotNull PsiClass aClass) {
|
||||
return new ConvertInterfaceToClassFix(aClass);
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Change variable 'i' type to 'Integer'" "true"
|
||||
class Test {
|
||||
void foo(Object o) {
|
||||
switch (o) {
|
||||
case Integer i -> System.out.println("int");
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Change variable 'i' type to 'int[]'" "true"
|
||||
class Test {
|
||||
void foo(Object o) {
|
||||
switch (o) {
|
||||
case int[] i -> System.out.println("int");
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Change variable 'i' type to 'Integer'" "true"
|
||||
class Test {
|
||||
void foo(Object o) {
|
||||
switch (o) {
|
||||
case int i<caret> -> System.out.println("int");
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Change variable 'i' type to 'int[]'" "true"
|
||||
class Test {
|
||||
void foo(Object o) {
|
||||
switch (o) {
|
||||
case int i<caret> -> System.out.println("int");
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -2,11 +2,19 @@
|
||||
package com.intellij.java.codeInsight.daemon.quickFix;
|
||||
|
||||
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
|
||||
import com.intellij.testFramework.LightProjectDescriptor;
|
||||
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class VariableTypeTest extends LightQuickFixParameterizedTestCase {
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/codeInsight/daemonCodeAnalyzer/quickFix/variableType";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull LightProjectDescriptor getProjectDescriptor() {
|
||||
return LightJavaCodeInsightFixtureTestCase.JAVA_17;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user