[java-refactoring] IJPL-157516 T: InlineUtil.inlineVariable

GitOrigin-RevId: 89f3781736ca0f67437538878c26174286657973
This commit is contained in:
Tagir Valeev
2024-06-28 17:23:07 +00:00
committed by intellij-monorepo-bot
parent be04e5725f
commit e35d04820c
4 changed files with 35 additions and 0 deletions
@@ -744,16 +744,21 @@ public final class InlineUtil implements CommonJavaInlineUtil {
boolean isAccessedForWriting = false;
boolean usedAsResource = false;
for (PsiReferenceExpression refElement : refs) {
if (PsiUtil.isAccessedForWriting(refElement)) {
isAccessedForWriting = true;
}
if (refElement.getParent() instanceof PsiResourceExpression) {
usedAsResource = true;
}
}
boolean shouldBeFinal = variable.hasModifierProperty(PsiModifier.FINAL) && strictlyFinal;
Project project = variable.getProject();
boolean canInline = refs.size() == 1 && !isAccessedForWriting && isFirstUse(variable, refs.get(0)) ||
canInlineParameterOrThisVariable(project, initializer, shouldBeFinal, strictlyFinal, refs.size(), isAccessedForWriting);
canInline &= !usedAsResource;
if (canInline) {
if (shouldBeFinal) {
declareUsedLocalsFinal(initializer, true);
@@ -0,0 +1,15 @@
class X {
void use() {
AutoCloseable r = () -> {};
<caret>test(r);
}
void test(AutoCloseable ref) {
try(ref) {
System.out.println(ref);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@@ -0,0 +1,13 @@
class X {
void use() {
AutoCloseable r = () -> {};
AutoCloseable ref = r;
try(ref) {
System.out.println(ref);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@@ -49,6 +49,8 @@ public class InlineMethodTest extends LightRefactoringTestCase {
}
public void testSideEffect() { doTest(); }
public void testParamAsAutocloseableRef() { doTest(); }
public void testInlineWithTry() { doTest(); }
public void testEmptyMethod() { doTest(); }