mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IfCanBeSwitchInspection: make it aware of switch expression, move to impl module
This commit is contained in:
+11
-7
@@ -31,7 +31,7 @@ public class EnhancedSwitchMigrationInspection extends AbstractBaseJavaLocalInsp
|
||||
return new JavaElementVisitor() {
|
||||
@Override
|
||||
public void visitSwitchStatement(PsiSwitchStatement statement) {
|
||||
SwitchReplacer replacer = getSwitchReplacer(statement);
|
||||
SwitchReplacer replacer = findSwitchReplacer(statement);
|
||||
if (replacer == null) return;
|
||||
PsiElement switchKeyword = statement.getFirstChild();
|
||||
holder.registerProblem(switchKeyword, InspectionsBundle.message(
|
||||
@@ -109,8 +109,12 @@ public class EnhancedSwitchMigrationInspection extends AbstractBaseJavaLocalInsp
|
||||
return tryCast(declaredElements[0], PsiLocalVariable.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Before using this method make sure you are using correct version of Java.
|
||||
*
|
||||
*/
|
||||
@Nullable
|
||||
private static SwitchReplacer getSwitchReplacer(PsiSwitchStatement switchStatement) {
|
||||
public static SwitchReplacer findSwitchReplacer(PsiSwitchStatement switchStatement) {
|
||||
PsiExpression expression = switchStatement.getExpression();
|
||||
if (expression == null) return null;
|
||||
PsiCodeBlock body = switchStatement.getBody();
|
||||
@@ -215,7 +219,7 @@ public class EnhancedSwitchMigrationInspection extends AbstractBaseJavaLocalInsp
|
||||
}
|
||||
}
|
||||
|
||||
private interface SwitchReplacer {
|
||||
public interface SwitchReplacer {
|
||||
void replace(@NotNull PsiStatement switchStatement);
|
||||
|
||||
ReplacementType getType();
|
||||
@@ -252,7 +256,7 @@ public class EnhancedSwitchMigrationInspection extends AbstractBaseJavaLocalInsp
|
||||
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
|
||||
PsiSwitchStatement statement = PsiTreeUtil.getParentOfType(descriptor.getStartElement(), PsiSwitchStatement.class);
|
||||
if (statement == null) return;
|
||||
SwitchReplacer replacer = getSwitchReplacer(statement);
|
||||
SwitchReplacer replacer = findSwitchReplacer(statement);
|
||||
if (replacer == null) return;
|
||||
replacer.replace(statement);
|
||||
}
|
||||
@@ -309,7 +313,7 @@ public class EnhancedSwitchMigrationInspection extends AbstractBaseJavaLocalInsp
|
||||
*/
|
||||
|
||||
@Nullable
|
||||
public static SwitchReplacer inspectReturningSwitch(@NotNull PsiStatement statement,
|
||||
private static SwitchReplacer inspectReturningSwitch(@NotNull PsiStatement statement,
|
||||
@NotNull PsiExpression expressionBeingSwitched,
|
||||
@NotNull List<? extends OldSwitchStatementBranch> branches,
|
||||
boolean isExhaustive) {
|
||||
@@ -405,7 +409,7 @@ public class EnhancedSwitchMigrationInspection extends AbstractBaseJavaLocalInsp
|
||||
* }
|
||||
*/
|
||||
@Nullable
|
||||
public static SwitchReplacer inspectVariableAssigningSwitch(@NotNull PsiStatement statement,
|
||||
private static SwitchReplacer inspectVariableAssigningSwitch(@NotNull PsiStatement statement,
|
||||
@NotNull PsiExpression expressionBeingSwitched,
|
||||
@NotNull List<? extends OldSwitchStatementBranch> branches,
|
||||
boolean isExhaustive) {
|
||||
@@ -476,7 +480,7 @@ public class EnhancedSwitchMigrationInspection extends AbstractBaseJavaLocalInsp
|
||||
* Suggest replacement with enhanced switch statement
|
||||
*/
|
||||
@Nullable
|
||||
public static SwitchReplacer inspectReplacementWithStatement(@NotNull PsiStatement statement,
|
||||
private static SwitchReplacer inspectReplacementWithStatement(@NotNull PsiStatement statement,
|
||||
@NotNull PsiExpression expressionBeingSwitched,
|
||||
@NotNull List<? extends OldSwitchStatementBranch> branches,
|
||||
boolean isExhaustive) {
|
||||
|
||||
+12
-2
@@ -17,14 +17,17 @@ package com.siyeh.ig.migration;
|
||||
|
||||
import com.intellij.codeInsight.Nullability;
|
||||
import com.intellij.codeInspection.CommonQuickFixBundle;
|
||||
import com.intellij.codeInspection.EnhancedSwitchMigrationInspection;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.codeInspection.ProblemHighlightType;
|
||||
import com.intellij.codeInspection.dataFlow.NullabilityUtil;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.WriteExternalException;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.ui.DocumentAdapter;
|
||||
import com.intellij.util.ui.CheckBox;
|
||||
@@ -236,6 +239,7 @@ public class IfCanBeSwitchInspection extends BaseInspection {
|
||||
switchStatementText.append('}');
|
||||
final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(ifStatement.getProject());
|
||||
final PsiElementFactory factory = psiFacade.getElementFactory();
|
||||
PsiSwitchStatement replacement;
|
||||
if (breaksNeedRelabeled) {
|
||||
final StringBuilder out = new StringBuilder();
|
||||
if (!(breakTarget instanceof PsiLabeledStatement)) {
|
||||
@@ -244,11 +248,17 @@ public class IfCanBeSwitchInspection extends BaseInspection {
|
||||
termReplace(breakTarget, statementToReplace, switchStatementText, out);
|
||||
final String newStatementText = out.toString();
|
||||
final PsiStatement newStatement = factory.createStatementFromText(newStatementText, ifStatement);
|
||||
breakTarget.replace(newStatement);
|
||||
replacement = (PsiSwitchStatement)breakTarget.replace(newStatement);
|
||||
}
|
||||
else {
|
||||
final PsiStatement newStatement = factory.createStatementFromText(switchStatementText.toString(), ifStatement);
|
||||
statementToReplace.replace(newStatement);
|
||||
replacement = (PsiSwitchStatement)statementToReplace.replace(newStatement);
|
||||
}
|
||||
if (PsiUtil.getLanguageLevel(replacement).isAtLeast(LanguageLevel.JDK_12_PREVIEW)) {
|
||||
EnhancedSwitchMigrationInspection.SwitchReplacer replacer = EnhancedSwitchMigrationInspection.findSwitchReplacer(replacement);
|
||||
if (replacer != null) {
|
||||
replacer.replace(replacement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import java.util.Objects;
|
||||
|
||||
class ObjectsEquals {
|
||||
int objectsEquals(String param) {
|
||||
re<caret>turn switch (param) {
|
||||
case "a" -> 1;
|
||||
case "b" -> 2;
|
||||
default -> 3;
|
||||
};
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
import java.util.Objects;
|
||||
|
||||
class ObjectsEquals {
|
||||
int objectsEquals(String param) {
|
||||
if<caret> (Objects.equals(param, "a")) {
|
||||
return 1;
|
||||
} else if (Objects.equals(param, "b")) {
|
||||
return 2;
|
||||
} else {
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.siyeh.ig.fixes.migration;
|
||||
|
||||
import com.intellij.codeInspection.CommonQuickFixBundle;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.PsiKeyword;
|
||||
import com.intellij.testFramework.IdeaTestUtil;
|
||||
import com.intellij.testFramework.builders.JavaModuleFixtureBuilder;
|
||||
import com.siyeh.ig.IGQuickFixesTestCase;
|
||||
import com.siyeh.ig.migration.IfCanBeSwitchInspection;
|
||||
|
||||
public class IfCanBeSwitchSwitchExpressionFixTest extends IGQuickFixesTestCase {
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
final IfCanBeSwitchInspection inspection = new IfCanBeSwitchInspection();
|
||||
inspection.minimumBranches = 2;
|
||||
inspection.suggestIntSwitches = true;
|
||||
myFixture.enableInspections(inspection);
|
||||
myRelativePath = "migration/if_can_be_switch";
|
||||
myDefaultHint = CommonQuickFixBundle.message("fix.replace.x.with.y", PsiKeyword.IF, PsiKeyword.SWITCH);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tuneFixture(JavaModuleFixtureBuilder builder) throws Exception {
|
||||
super.tuneFixture(builder);
|
||||
builder.setLanguageLevel(LanguageLevel.JDK_12_PREVIEW);
|
||||
builder.addJdk(IdeaTestUtil.getMockJdk18Path().getPath());
|
||||
}
|
||||
|
||||
public void testJava12() { doTest();}
|
||||
}
|
||||
Reference in New Issue
Block a user