mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-06 11:50:54 +07:00
inline method: ensure required braces are not removed (IDEA-201741)
This commit is contained in:
@@ -1497,22 +1497,26 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
|
||||
PsiStatement[] statements = codeBlock.getStatements();
|
||||
if (statements.length == 1) {
|
||||
final PsiElement codeBlockParent = codeBlock.getParent();
|
||||
PsiStatement statement = statements[0];
|
||||
if (codeBlockParent instanceof PsiLambdaExpression) {
|
||||
if (statements[0] instanceof PsiReturnStatement) {
|
||||
final PsiExpression returnValue = ((PsiReturnStatement)statements[0]).getReturnValue();
|
||||
if (statement instanceof PsiReturnStatement) {
|
||||
final PsiExpression returnValue = ((PsiReturnStatement)statement).getReturnValue();
|
||||
if (returnValue != null) {
|
||||
codeBlock.replace(returnValue);
|
||||
}
|
||||
} else if (statements[0] instanceof PsiExpressionStatement){
|
||||
codeBlock.replace(((PsiExpressionStatement)statements[0]).getExpression());
|
||||
} else if (statement instanceof PsiExpressionStatement){
|
||||
codeBlock.replace(((PsiExpressionStatement)statement).getExpression());
|
||||
}
|
||||
}
|
||||
else if (codeBlockParent instanceof PsiBlockStatement) {
|
||||
if (!(ifStatementWithAppendableElseBranch(statement) &&
|
||||
codeBlockParent.getParent() instanceof PsiIfStatement &&
|
||||
((PsiIfStatement)codeBlockParent.getParent()).getElseBranch() != null)) {
|
||||
codeBlockParent.replace(statement);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (codeBlockParent instanceof PsiBlockStatement) {
|
||||
codeBlockParent.replace(statements[0]);
|
||||
} else {
|
||||
codeBlock.replace(statements[0]);
|
||||
}
|
||||
codeBlock.replace(statement);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1532,6 +1536,14 @@ public class InlineMethodProcessor extends BaseRefactoringProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean ifStatementWithAppendableElseBranch(PsiStatement statement) {
|
||||
if (statement instanceof PsiIfStatement) {
|
||||
PsiStatement elseBranch = ((PsiIfStatement)statement).getElseBranch();
|
||||
return elseBranch == null || elseBranch instanceof PsiIfStatement;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private PsiExpression getSimpleFieldInitializer(PsiField field, PsiClassInitializer initializer) {
|
||||
final PsiStatement[] statements = initializer.getBody().getStatements();
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
class Temp {
|
||||
public static void main(String... args) {
|
||||
if (args.length > 0)
|
||||
check<caret>Args(args);
|
||||
else
|
||||
System.out.println("help");
|
||||
}
|
||||
|
||||
private static void checkArgs(String[] args) {
|
||||
if (args[0].equals("foo"))
|
||||
System.out.println("bar");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
class Temp {
|
||||
public static void main(String... args) {
|
||||
if (args.length > 0) {
|
||||
if (args[0].equals("foo"))
|
||||
System.out.println("bar");
|
||||
}
|
||||
else
|
||||
System.out.println("help");
|
||||
}
|
||||
|
||||
private static void checkArgs(String[] args) {
|
||||
if (args[0].equals("foo"))
|
||||
System.out.println("bar");
|
||||
}
|
||||
}
|
||||
@@ -312,6 +312,10 @@ public class InlineMethodTest extends LightRefactoringTestCase {
|
||||
doTestInlineThisOnly();
|
||||
}
|
||||
|
||||
public void testIfElseIfWithSingleStatement() {
|
||||
doTestInlineThisOnly();
|
||||
}
|
||||
|
||||
public void testUnresolvedArgPassedToSameNameParameter() {
|
||||
doTestInlineThisOnly();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user