mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
inline: unwrap try-with-resources if no resources left after inline (IDEA-109541)
This commit is contained in:
+22
@@ -18,6 +18,7 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.PropertyUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import gnu.trove.THashSet;
|
||||
@@ -249,6 +250,7 @@ public class RemoveUnusedVariableUtil {
|
||||
if (element instanceof PsiField) {
|
||||
((PsiField)element).normalizeDeclaration();
|
||||
}
|
||||
if (processResourceVariable(element)) break;
|
||||
element.delete();
|
||||
}
|
||||
return !sideEffectsFound;
|
||||
@@ -258,4 +260,24 @@ public class RemoveUnusedVariableUtil {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean processResourceVariable(PsiElement element) {
|
||||
final PsiTryStatement tryStatement = PsiTreeUtil.getParentOfType(element, PsiTryStatement.class);
|
||||
if (element instanceof PsiResourceVariable && tryStatement != null) {
|
||||
final PsiResourceList resourceList = tryStatement.getResourceList();
|
||||
if (resourceList != null &&
|
||||
resourceList.getResourceVariables().contains(element) &&
|
||||
resourceList.getResourceVariablesCount() == 1) {
|
||||
|
||||
final PsiCodeBlock tryBlock = tryStatement.getTryBlock();
|
||||
if (tryBlock != null) {
|
||||
tryBlock.deleteChildRange(tryBlock.getFirstChild(), tryBlock.getFirstBodyElement());
|
||||
tryBlock.deleteChildRange(tryBlock.getLastBodyElement(), tryBlock.getLastChild());
|
||||
tryStatement.replace(tryBlock);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,10 +42,8 @@ import com.intellij.refactoring.RefactoringBundle;
|
||||
import com.intellij.refactoring.util.CommonRefactoringUtil;
|
||||
import com.intellij.refactoring.util.InlineUtil;
|
||||
import com.intellij.refactoring.util.RefactoringMessageDialog;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.Processor;
|
||||
import com.intellij.util.Query;
|
||||
import com.intellij.util.*;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -234,10 +232,11 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
|
||||
final Runnable runnable = new Runnable() {
|
||||
public void run() {
|
||||
try{
|
||||
PsiExpression[] exprs = new PsiExpression[refsToInline.length];
|
||||
SmartPsiElementPointer<PsiExpression>[] exprs = new SmartPsiElementPointer[refsToInline.length];
|
||||
final SmartPointerManager pointerManager = SmartPointerManager.getInstance(project);
|
||||
for(int idx = 0; idx < refsToInline.length; idx++){
|
||||
PsiJavaCodeReferenceElement refElement = (PsiJavaCodeReferenceElement)refsToInline[idx];
|
||||
exprs[idx] = InlineUtil.inlineVariable(local, defToInline, refElement);
|
||||
exprs[idx] = pointerManager.createSmartPsiElementPointer(InlineUtil.inlineVariable(local, defToInline, refElement));
|
||||
}
|
||||
|
||||
if (!isInliningVariableInitializer(defToInline)) {
|
||||
@@ -251,12 +250,17 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
|
||||
}
|
||||
|
||||
if (editor != null && !ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
highlightManager.addOccurrenceHighlights(editor, exprs, attributes, true, null);
|
||||
highlightManager.addOccurrenceHighlights(editor, ContainerUtil.convert(exprs, new PsiExpression[refsToInline.length], new Function<SmartPsiElementPointer<PsiExpression>, PsiExpression>() {
|
||||
@Override
|
||||
public PsiExpression fun(SmartPsiElementPointer<PsiExpression> pointer) {
|
||||
return pointer.getElement();
|
||||
}
|
||||
}), attributes, true, null);
|
||||
WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
|
||||
}
|
||||
|
||||
for (final PsiExpression expr : exprs) {
|
||||
InlineUtil.tryToInlineArrayCreationForVarargs(expr);
|
||||
for (final SmartPsiElementPointer<PsiExpression> expr : exprs) {
|
||||
InlineUtil.tryToInlineArrayCreationForVarargs(expr.getElement());
|
||||
}
|
||||
}
|
||||
catch (IncorrectOperationException e){
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
class Test {
|
||||
void m() throws Exception {
|
||||
try (AutoCloseable inl<caret>ineMe = null) {
|
||||
try (AutoCloseable r2 = inlineMe) {
|
||||
System.out.println(inlineMe + ", " + r2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class Test {
|
||||
void m() throws Exception {
|
||||
try (AutoCloseable r2 = null) {
|
||||
System.out.println(null + ", " + r2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -209,6 +209,10 @@ public class InlineLocalTest extends LightCodeInsightTestCase {
|
||||
doTest(true);
|
||||
}
|
||||
|
||||
public void testResourceVariable() throws Exception {
|
||||
doTest(false);
|
||||
}
|
||||
|
||||
public void testEnclosingThisExpression() throws Exception {
|
||||
doTest(true);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user