mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
InvertIfConditionAction: nullability warnings fixed
This commit is contained in:
+16
-14
@@ -32,6 +32,7 @@ import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author mike
|
||||
@@ -87,11 +88,11 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
|
||||
|
||||
ControlFlow controlFlow = buildControlFlow(block);
|
||||
|
||||
PsiExpression condition = (PsiExpression) ifStatement.getCondition().copy();
|
||||
PsiExpression condition = (PsiExpression) Objects.requireNonNull(ifStatement.getCondition()).copy();
|
||||
|
||||
ifStatement = setupBranches(ifStatement, controlFlow);
|
||||
if (condition != null) {
|
||||
ifStatement.getCondition().replace(CodeInsightServicesUtil.invertCondition(condition));
|
||||
Objects.requireNonNull(ifStatement.getCondition()).replace(CodeInsightServicesUtil.invertCondition(condition));
|
||||
}
|
||||
|
||||
formatIf(ifStatement);
|
||||
@@ -101,9 +102,9 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
|
||||
final Project project = ifStatement.getProject();
|
||||
PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
|
||||
|
||||
PsiElement thenBranch = ifStatement.getThenBranch().copy();
|
||||
PsiElement thenBranch = Objects.requireNonNull(ifStatement.getThenBranch()).copy();
|
||||
PsiElement elseBranch = ifStatement.getElseBranch() != null ? ifStatement.getElseBranch().copy() : null;
|
||||
PsiElement condition = ifStatement.getCondition().copy();
|
||||
PsiElement condition = Objects.requireNonNull(ifStatement.getCondition()).copy();
|
||||
|
||||
final CodeStyleManager codeStyle = CodeStyleManager.getInstance(project);
|
||||
|
||||
@@ -118,22 +119,22 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
|
||||
ifStatement = (PsiIfStatement)codeStyle.reformat(ifStatement);
|
||||
|
||||
if (!(thenBranch instanceof PsiBlockStatement)) {
|
||||
PsiBlockStatement codeBlock1 = (PsiBlockStatement)ifStatement.getThenBranch().replace(codeBlock);
|
||||
PsiBlockStatement codeBlock1 = (PsiBlockStatement)Objects.requireNonNull(ifStatement.getThenBranch()).replace(codeBlock);
|
||||
codeBlock1 = (PsiBlockStatement)codeStyle.reformat(codeBlock1);
|
||||
codeBlock1.getCodeBlock().add(thenBranch);
|
||||
}
|
||||
else {
|
||||
ifStatement.getThenBranch().replace(thenBranch);
|
||||
Objects.requireNonNull(ifStatement.getThenBranch()).replace(thenBranch);
|
||||
}
|
||||
|
||||
if (elseBranch != null) {
|
||||
if (!(elseBranch instanceof PsiBlockStatement)) {
|
||||
PsiBlockStatement codeBlock1 = (PsiBlockStatement)ifStatement.getElseBranch().replace(codeBlock);
|
||||
PsiBlockStatement codeBlock1 = (PsiBlockStatement)Objects.requireNonNull(ifStatement.getElseBranch()).replace(codeBlock);
|
||||
codeBlock1 = (PsiBlockStatement)codeStyle.reformat(codeBlock1);
|
||||
codeBlock1.getCodeBlock().add(elseBranch);
|
||||
}
|
||||
else {
|
||||
elseBranch = ifStatement.getElseBranch().replace(elseBranch);
|
||||
elseBranch = Objects.requireNonNull(ifStatement.getElseBranch()).replace(elseBranch);
|
||||
|
||||
if (emptyBlock(((PsiBlockStatement)elseBranch).getCodeBlock())) {
|
||||
ifStatement.getElseBranch().delete();
|
||||
@@ -141,7 +142,7 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
|
||||
}
|
||||
}
|
||||
|
||||
ifStatement.getCondition().replace(condition);
|
||||
Objects.requireNonNull(ifStatement.getCondition()).replace(condition);
|
||||
}
|
||||
|
||||
private static boolean emptyBlock (PsiCodeBlock block) {
|
||||
@@ -174,7 +175,7 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
|
||||
PsiElementFactory factory = JavaPsiFacade.getInstance(ifStatement.getProject()).getElementFactory();
|
||||
Project project = ifStatement.getProject();
|
||||
|
||||
PsiStatement thenBranch = ifStatement.getThenBranch();
|
||||
PsiStatement thenBranch = Objects.requireNonNull(ifStatement.getThenBranch());
|
||||
PsiStatement elseBranch = ifStatement.getElseBranch();
|
||||
|
||||
if (elseBranch != null) {
|
||||
@@ -215,7 +216,7 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
|
||||
ifStatement = addAfterWithinCodeBlock(ifStatement, thenBranch);
|
||||
}
|
||||
}
|
||||
ifStatement.getThenBranch().replace(statement);
|
||||
Objects.requireNonNull(ifStatement.getThenBranch()).replace(statement);
|
||||
return ifStatement;
|
||||
}
|
||||
PsiElement element = flow.getElement(endOffset);
|
||||
@@ -227,14 +228,14 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
|
||||
PsiStatement statement = factory.createStatementFromText("continue;", ifStatement);
|
||||
statement = (PsiStatement)codeStyle.reformat(statement);
|
||||
ifStatement = addAfterWithinCodeBlock(ifStatement, thenBranch);
|
||||
ifStatement.getThenBranch().replace(statement);
|
||||
Objects.requireNonNull(ifStatement.getThenBranch()).replace(statement);
|
||||
return ifStatement;
|
||||
}
|
||||
|
||||
if (element instanceof PsiReturnStatement) {
|
||||
PsiReturnStatement returnStatement = (PsiReturnStatement) element;
|
||||
ifStatement = addAfterWithinCodeBlock(ifStatement, thenBranch);
|
||||
ifStatement.getThenBranch().replace(returnStatement.copy());
|
||||
Objects.requireNonNull(ifStatement.getThenBranch()).replace(returnStatement.copy());
|
||||
|
||||
ControlFlow flow2 = buildControlFlow(findCodeBlock(ifStatement));
|
||||
if (!ControlFlowUtil.isInstructionReachable(flow2, flow2.getStartOffset(returnStatement), 0)) returnStatement.delete();
|
||||
@@ -283,7 +284,7 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
|
||||
setElseBranch(ifStatement, thenBranch, flow);
|
||||
PsiStatement statement = factory.createStatementFromText("{}", ifStatement);
|
||||
statement = (PsiStatement) codeStyle.reformat(statement);
|
||||
statement = (PsiStatement) ifStatement.getThenBranch().replace(statement);
|
||||
statement = (PsiStatement) Objects.requireNonNull(ifStatement.getThenBranch()).replace(statement);
|
||||
codeStyle.reformat(statement);
|
||||
return ifStatement;
|
||||
}
|
||||
@@ -327,6 +328,7 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction {
|
||||
PsiIfStatement ifStatement = (PsiIfStatement)factory.createStatementFromText("if (true) {}", statement);
|
||||
ifStatement = (PsiIfStatement)codeStyle.reformat(ifStatement);
|
||||
PsiStatement thenBranch = ifStatement.getThenBranch();
|
||||
assert thenBranch instanceof PsiBlockStatement;
|
||||
((PsiBlockStatement)thenBranch).getCodeBlock().add(statement);
|
||||
PsiCodeBlock stmt = ((PsiBlockStatement)statement.replace(thenBranch)).getCodeBlock();
|
||||
return stmt.getStatements()[0];
|
||||
|
||||
Reference in New Issue
Block a user