mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IfStatementWithIdenticalBranches: explicit type for array assignment
IDEA-190203
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
// "Extract common part with variables from if " "true"
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Main {
|
||||
public static void main(String... args) {
|
||||
int i = 1;
|
||||
String[] array;
|
||||
if (i % 2 == 0) {
|
||||
array = new String[]{};
|
||||
} else {
|
||||
array = new String[]{"not empty"};
|
||||
}
|
||||
main(array);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// "Extract common part with variables from if " "true"
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Main {
|
||||
public static void main(String... args) {
|
||||
int i = 1;
|
||||
if<caret> (i % 2 == 0) {
|
||||
String[] array = {};
|
||||
main(array);
|
||||
} else {
|
||||
String[] array = {"not empty"};
|
||||
main(array);
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
-5
@@ -36,7 +36,7 @@ import java.util.stream.Collectors;
|
||||
import static com.intellij.util.ObjectUtils.tryCast;
|
||||
|
||||
// Not really with identical branches, but also common parts
|
||||
public class IfStatementWithIdenticalBranchesInspection extends BaseJavaBatchLocalInspectionTool {
|
||||
public class IfStatementWithIdenticalBranchesInspection extends AbstractBaseJavaLocalInspectionTool {
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
|
||||
@@ -276,6 +276,7 @@ public class IfStatementWithIdenticalBranchesInspection extends BaseJavaBatchLoc
|
||||
PsiVariable thenVariable = extractVariable(thenStatement);
|
||||
PsiLocalVariable elseVariable = extractVariable(elseStatement);
|
||||
if(thenVariable == null || elseVariable == null) return false;
|
||||
String typeText = thenVariable.getType().getCanonicalText();
|
||||
String thenVariableTypeText = thenVariable.getType().getCanonicalText();
|
||||
PsiModifierList thenModifierList = thenVariable.getModifierList();
|
||||
String modifiers;
|
||||
@@ -298,8 +299,8 @@ public class IfStatementWithIdenticalBranchesInspection extends BaseJavaBatchLoc
|
||||
PsiStatement varDeclarationStmt = factory.createStatementFromText(variableDeclaration, parent);
|
||||
parent.addBefore(varDeclarationStmt, ifStatement);
|
||||
|
||||
replaceWithDeclarationIfNeeded(ifStatement, factory, thenStatement, thenInitializer, varName);
|
||||
replaceWithDeclarationIfNeeded(ifStatement, factory, elseStatement, elseInitializer, varName);
|
||||
replaceWithDeclarationIfNeeded(ifStatement, factory, thenStatement, thenInitializer, varName, typeText);
|
||||
replaceWithDeclarationIfNeeded(ifStatement, factory, elseStatement, elseInitializer, varName, typeText);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -348,9 +349,16 @@ public class IfStatementWithIdenticalBranchesInspection extends BaseJavaBatchLoc
|
||||
PsiElementFactory factory,
|
||||
PsiStatement statement,
|
||||
PsiExpression initializer,
|
||||
String varName) {
|
||||
String varName,
|
||||
String type) {
|
||||
if (initializer != null) {
|
||||
PsiStatement assignment = factory.createStatementFromText(varName + "=" + initializer.getText() + ";", ifStatement);
|
||||
final String initializerText;
|
||||
if (initializer instanceof PsiArrayInitializerExpression) {
|
||||
initializerText = "new " + type + initializer.getText();
|
||||
} else {
|
||||
initializerText = initializer.getText();
|
||||
}
|
||||
PsiStatement assignment = factory.createStatementFromText(varName + "=" + initializerText + ";", ifStatement);
|
||||
statement.replace(assignment);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user