mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
disable inline variable used in resource list (IDEA-171153)
This commit is contained in:
@@ -55,6 +55,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@@ -249,6 +250,11 @@ public class InlineLocalHandler extends JavaInlineActionHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Arrays.stream(refsToInline).anyMatch(ref -> ref.getParent() instanceof PsiResourceExpression)) {
|
||||
CommonRefactoringUtil.showErrorHint(project, editor, RefactoringBundle.getCannotRefactorMessage("Variable is used as resource reference"), REFACTORING_NAME, HelpID.INLINE_VARIABLE);
|
||||
return;
|
||||
}
|
||||
|
||||
final Runnable runnable = () -> {
|
||||
final String refactoringId = "refactoring.inline.local.variable";
|
||||
try{
|
||||
|
||||
@@ -53,11 +53,15 @@ public class InlineUtil {
|
||||
PsiJavaCodeReferenceElement ref,
|
||||
PsiExpression thisAccessExpr)
|
||||
throws IncorrectOperationException {
|
||||
final PsiElement parent = ref.getParent();
|
||||
if (parent instanceof PsiResourceExpression) {
|
||||
LOG.error("Unable to inline resource reference");
|
||||
return (PsiExpression)ref;
|
||||
}
|
||||
PsiManager manager = initializer.getManager();
|
||||
|
||||
PsiClass thisClass = RefactoringChangeUtil.getThisClass(initializer);
|
||||
PsiClass refParent = RefactoringChangeUtil.getThisClass(ref);
|
||||
final PsiElement parent = ref.getParent();
|
||||
final PsiType varType = variable.getType();
|
||||
initializer = RefactoringUtil.convertInitializerToNormalExpression(initializer, varType);
|
||||
if (initializer instanceof PsiPolyadicExpression) {
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
class Test {
|
||||
void m() {
|
||||
String <caret>s = "";
|
||||
try (s) {
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -311,10 +311,14 @@ public class InlineLocalTest extends LightCodeInsightTestCase {
|
||||
fail("Conflict was not detected");
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
assertEquals(e.getMessage(), conflictMessage);
|
||||
assertEquals(conflictMessage, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testVariableInsideResourceList() {
|
||||
doTest(false, "Cannot perform refactoring.\n" +
|
||||
"Variable is used as resource reference");
|
||||
}
|
||||
|
||||
private void doTest(final boolean inlineDef) {
|
||||
setLanguageLevel(LanguageLevel.JDK_1_7);
|
||||
|
||||
Reference in New Issue
Block a user