mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
extract method: fix complete normally custom check (IDEA-161592)
This commit is contained in:
+16
-7
@@ -47,7 +47,8 @@ import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.wm.WindowManager;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.*;
|
||||
import com.intellij.psi.controlFlow.ControlFlowUtil;
|
||||
import com.intellij.psi.controlFlow.*;
|
||||
import com.intellij.psi.controlFlow.ControlFlow;
|
||||
import com.intellij.psi.impl.source.codeStyle.JavaCodeStyleManagerImpl;
|
||||
import com.intellij.psi.scope.processor.VariablesProcessor;
|
||||
import com.intellij.psi.scope.util.PsiScopesUtil;
|
||||
@@ -1092,13 +1093,21 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
}
|
||||
|
||||
private boolean hasNormalExit() {
|
||||
boolean hasNormalExit = false;
|
||||
PsiElement lastElement = myElements[myElements.length - 1];
|
||||
if (!(lastElement instanceof PsiReturnStatement || lastElement instanceof PsiBreakStatement ||
|
||||
lastElement instanceof PsiContinueStatement)) {
|
||||
hasNormalExit = true;
|
||||
try {
|
||||
PsiCodeBlock block = JavaPsiFacade.getElementFactory(myProject).createCodeBlock();
|
||||
block.addRange(myElements[0], myElements[myElements.length - 1]);
|
||||
ControlFlow flow = ControlFlowFactory.getInstance(myProject).getControlFlow(block, new LocalsControlFlowPolicy(block), false, false);
|
||||
return ControlFlowUtil.canCompleteNormally(flow, 0, flow.getSize());
|
||||
}
|
||||
catch (AnalysisCanceledException e) {
|
||||
//check incomplete code as simple as possible
|
||||
PsiElement lastElement = myElements[myElements.length - 1];
|
||||
if (!(lastElement instanceof PsiReturnStatement || lastElement instanceof PsiBreakStatement ||
|
||||
lastElement instanceof PsiContinueStatement)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return hasNormalExit;
|
||||
}
|
||||
|
||||
protected boolean isNeedToChangeCallContext() {
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
class Test {
|
||||
|
||||
private static void f(boolean a, boolean b) {
|
||||
if (a) {
|
||||
<selection>if (b) {
|
||||
System.out.println("");
|
||||
return;
|
||||
} else {
|
||||
System.out.println("");
|
||||
return;
|
||||
}</selection>
|
||||
} else {
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
class Test {
|
||||
|
||||
private static void f(boolean a, boolean b) {
|
||||
if (a) {
|
||||
newMethod(b);
|
||||
return;
|
||||
} else {
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
|
||||
private static void newMethod(boolean b) {
|
||||
if (b) {
|
||||
System.out.println("");
|
||||
return;
|
||||
} else {
|
||||
System.out.println("");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
class Test {
|
||||
|
||||
private static void f(boolean a, boolean b) {
|
||||
if (a) {
|
||||
<selection>try {
|
||||
System.out.println();
|
||||
return;
|
||||
}
|
||||
catch (Exception e) {
|
||||
return;
|
||||
}</selection>
|
||||
} else {
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
class Test {
|
||||
|
||||
private static void f(boolean a, boolean b) {
|
||||
if (a) {
|
||||
newMethod();
|
||||
return;
|
||||
} else {
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
|
||||
private static void newMethod() {
|
||||
try {
|
||||
System.out.println();
|
||||
return;
|
||||
}
|
||||
catch (Exception e) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -868,6 +868,14 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testNormalExitIf() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testNormalExitTry() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testMethodAnnotations() throws Exception {
|
||||
doTest();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user