[java] FL-24196 Java: Inline local variable from its declaration throws NPE

GitOrigin-RevId: 8d7e4e03293e944e36fb3fd7dcd9b461c8ac2639
This commit is contained in:
Anton Lobov
2024-01-18 14:06:32 +00:00
committed by intellij-monorepo-bot
parent f338c4fd53
commit 1bf43101d9
4 changed files with 27 additions and 6 deletions
@@ -101,7 +101,7 @@ public final class InlineLocalHandler extends JavaInlineActionHandler {
}
final PsiReferenceExpression refExpr = PsiTreeUtil.getParentOfType(element, PsiReferenceExpression.class);
InlineMode mode;
if (JavaRefactoringSettings.getInstance().INLINE_LOCAL_THIS) mode = InlineMode.INLINE_ONE;
if (refExpr != null && JavaRefactoringSettings.getInstance().INLINE_LOCAL_THIS) mode = InlineMode.INLINE_ONE;
else mode = InlineMode.CHECK_CONFLICTS;
return doInline(context, (PsiVariable)Objects.requireNonNull(context.element()), refExpr, mode);
@@ -109,11 +109,11 @@ public final class InlineLocalHandler extends JavaInlineActionHandler {
private static ModCommand doInline(@NotNull ActionContext context,
@NotNull PsiVariable var,
PsiReferenceExpression refExpr,
@Nullable PsiReferenceExpression refExpr,
@NotNull InlineMode mode) {
PsiElement block = PsiUtil.getVariableCodeBlock(var, null);
List<PsiReferenceExpression> allRefs =
mode == InlineMode.INLINE_ONE || block == null ? List.of(refExpr) :
refExpr != null && (mode == InlineMode.INLINE_ONE || block == null) ? List.of(refExpr) :
VariableAccessUtils.getVariableReferences(var, block);
if (allRefs.isEmpty()) {
return ModCommand.error(RefactoringBundle.message("variable.is.never.used", var.getName()));
@@ -204,7 +204,7 @@ public final class InlineLocalHandler extends JavaInlineActionHandler {
List<PsiElement> refsToInlineList = new ArrayList<>();
boolean simpleInlining = false;
if (mode == InlineMode.INLINE_ONE) {
if (mode == InlineMode.INLINE_ONE && refExpr != null) {
refsToInlineList.add(refExpr);
} else {
if (defToInline == local.getInitializer()) {
@@ -0,0 +1,6 @@
class X {
void foo() {
int <caret>x = 2;
System.out.println(x + x);
}
}
@@ -0,0 +1,5 @@
class X {
void foo() {
System.out.println(2 + 2);
}
}
@@ -3,14 +3,13 @@ package com.intellij.java.refactoring.inline;
import com.intellij.JavaTestUtil;
import com.intellij.codeInsight.TargetElementUtil;
import com.intellij.java.refactoring.JavaRefactoringBundle;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiLocalVariable;
import com.intellij.refactoring.RefactoringBundle;
import com.intellij.refactoring.JavaRefactoringSettings;
import com.intellij.refactoring.inline.InlineLocalHandler;
import com.intellij.testFramework.IdeaTestUtil;
import com.intellij.testFramework.LightJavaCodeInsightTestCase;
@@ -371,6 +370,17 @@ public class InlineLocalTest extends LightJavaCodeInsightTestCase {
"Variable is used as resource reference");
}
public void testLocalVariableInThisOnlyMode() {
boolean initialSetting = JavaRefactoringSettings.getInstance().INLINE_LOCAL_THIS;
try {
JavaRefactoringSettings.getInstance().INLINE_LOCAL_THIS = true;
doTest();
}
finally {
JavaRefactoringSettings.getInstance().INLINE_LOCAL_THIS = initialSetting;
}
}
private void doTest() {
doTest(LanguageLevel.JDK_1_7);
}