ExtractMethodProcessor: fix NPE extracting method from empty parenthesis: EA-214509

GitOrigin-RevId: 5fc2d19d08efca879d6e8174d0a8500eb7803dc5
This commit is contained in:
Roman.Ivanov
2019-11-07 02:40:58 +00:00
committed by intellij-monorepo-bot
parent 4c321d86bc
commit b36a94855a
3 changed files with 22 additions and 2 deletions
@@ -143,8 +143,13 @@ public class ExtractMethodProcessor implements MatchProvider {
myProject = project;
myEditor = editor;
if (elements.length != 1 || !(elements[0] instanceof PsiBlockStatement)) {
myElements = elements.length == 1 && elements[0] instanceof PsiParenthesizedExpression
? new PsiElement[] {PsiUtil.skipParenthesizedExprDown((PsiExpression)elements[0])} : elements;
if (elements.length == 1 && elements[0] instanceof PsiParenthesizedExpression) {
PsiExpression expression = PsiUtil.skipParenthesizedExprDown((PsiExpression)elements[0]);
myElements = expression != null ? new PsiElement[]{expression} : elements;
}
else {
myElements = elements;
}
myEnclosingBlockStatement = null;
}
else {
@@ -0,0 +1,6 @@
class C {
void foo() {
int x = <selection>()</selection>;
}
}
@@ -1170,6 +1170,15 @@ public class ExtractMethodTest extends LightJavaCodeInsightTestCase {
doTest();
}
public void testEmptyParenthesis() throws Exception {
try {
doTest();
fail("Should not work for empty parenthesis");
}
catch (PrepareFailedException ignore) {
}
}
public void testQualifyWhenConflictingNamePresent() throws Exception {
final CommonCodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject()).getCommonSettings(JavaLanguage.INSTANCE);
settings.ELSE_ON_NEW_LINE = true;