mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
make intention work on nested try statement without resource lists
This commit is contained in:
+29
-14
@@ -37,10 +37,13 @@ public class MergeNestedTryStatementsIntention extends Intention {
|
||||
@Override
|
||||
protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
|
||||
final PsiTryStatement tryStatement1 = (PsiTryStatement)element.getParent();
|
||||
final StringBuilder newTryStatement = new StringBuilder("try (");
|
||||
final StringBuilder newTryStatement = new StringBuilder("try ");
|
||||
final PsiResourceList list1 = tryStatement1.getResourceList();
|
||||
boolean semicolon = false;
|
||||
boolean resourceList = false;
|
||||
if (list1 != null) {
|
||||
resourceList = true;
|
||||
newTryStatement.append('(');
|
||||
final List<PsiResourceVariable> variables1 = list1.getResourceVariables();
|
||||
for (PsiResourceVariable variable : variables1) {
|
||||
if (semicolon) {
|
||||
@@ -57,29 +60,41 @@ public class MergeNestedTryStatementsIntention extends Intention {
|
||||
return;
|
||||
}
|
||||
final PsiStatement[] statements = tryBlock1.getStatements();
|
||||
final PsiTryStatement tryStatement2 = (PsiTryStatement)statements[0];
|
||||
final PsiResourceList list2 = tryStatement2.getResourceList();
|
||||
if (list2 == null) {
|
||||
if (statements.length != 1) {
|
||||
return;
|
||||
}
|
||||
final List<PsiResourceVariable> variables2 = list2.getResourceVariables();
|
||||
for (PsiResourceVariable variable : variables2) {
|
||||
if (semicolon) {
|
||||
newTryStatement.append(';');
|
||||
final PsiTryStatement tryStatement2 = (PsiTryStatement)statements[0];
|
||||
final PsiResourceList list2 = tryStatement2.getResourceList();
|
||||
if (list2 != null) {
|
||||
if (!resourceList) {
|
||||
newTryStatement.append('(');
|
||||
}
|
||||
else {
|
||||
semicolon = true;
|
||||
resourceList = true;
|
||||
final List<PsiResourceVariable> variables2 = list2.getResourceVariables();
|
||||
for (PsiResourceVariable variable : variables2) {
|
||||
if (semicolon) {
|
||||
newTryStatement.append(';');
|
||||
}
|
||||
else {
|
||||
semicolon = true;
|
||||
}
|
||||
newTryStatement.append(variable.getText());
|
||||
}
|
||||
newTryStatement.append(variable.getText());
|
||||
}
|
||||
newTryStatement.append(")");
|
||||
if (resourceList) {
|
||||
newTryStatement.append(")");
|
||||
}
|
||||
final PsiCodeBlock tryBlock2 = tryStatement2.getTryBlock();
|
||||
if (tryBlock2 == null) {
|
||||
return;
|
||||
}
|
||||
newTryStatement.append(tryBlock2.getText());
|
||||
final PsiCatchSection[] catchSections = tryStatement1.getCatchSections();
|
||||
for (PsiCatchSection section : catchSections) {
|
||||
final PsiCatchSection[] catchSections2 = tryStatement2.getCatchSections();
|
||||
for (PsiCatchSection section : catchSections2) {
|
||||
newTryStatement.append(section.getText());
|
||||
}
|
||||
final PsiCatchSection[] catchSections1 = tryStatement1.getCatchSections();
|
||||
for (PsiCatchSection section : catchSections1) {
|
||||
newTryStatement.append(section.getText());
|
||||
}
|
||||
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(element.getProject());
|
||||
|
||||
+3
-13
@@ -35,7 +35,7 @@ class NestedTryStatementsPredicate implements PsiElementPredicate {
|
||||
return false;
|
||||
}
|
||||
final PsiElement parent = element.getParent();
|
||||
if (!isSimpleTry(parent, false)) {
|
||||
if (!isSimpleTry(parent)) {
|
||||
return false;
|
||||
}
|
||||
final PsiTryStatement tryStatement = (PsiTryStatement)parent;
|
||||
@@ -48,24 +48,14 @@ class NestedTryStatementsPredicate implements PsiElementPredicate {
|
||||
return false;
|
||||
}
|
||||
final PsiStatement statement = statements[0];
|
||||
return isSimpleTry(statement, true);
|
||||
return isSimpleTry(statement);
|
||||
}
|
||||
|
||||
private static boolean isSimpleTry(PsiElement element, boolean withResources) {
|
||||
private static boolean isSimpleTry(PsiElement element) {
|
||||
if (!(element instanceof PsiTryStatement)) {
|
||||
return false;
|
||||
}
|
||||
final PsiTryStatement tryStatement = (PsiTryStatement)element;
|
||||
if (withResources) {
|
||||
final PsiResourceList resourceList = tryStatement.getResourceList();
|
||||
if (resourceList == null) {
|
||||
return false;
|
||||
}
|
||||
final PsiCatchSection[] sections = tryStatement.getCatchSections();
|
||||
if (sections.length != 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
final PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock();
|
||||
if (finallyBlock != null) {
|
||||
return false;
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.siyeh.ipp.exceptions.mergeTry;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
class OldStyle {
|
||||
void foo(File file1) {
|
||||
<caret>try {
|
||||
try {
|
||||
FileInputStream in = new FileInputStream(file1);
|
||||
} catch (FileNotFoundException e) {
|
||||
// log
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// log
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.siyeh.ipp.exceptions.mergeTry;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
class OldStyle {
|
||||
void foo(File file1) {
|
||||
try {
|
||||
FileInputStream in = new FileInputStream(file1);
|
||||
} catch (FileNotFoundException e) {
|
||||
// log
|
||||
} catch (Exception e) {
|
||||
// log
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -10,6 +10,7 @@ public class MergeNestedTryStatementsIntentionTest extends IPPTestCase {
|
||||
|
||||
public void testSimple() { doTest(); }
|
||||
public void testWithoutAndWithResources() { doTest(); }
|
||||
public void testOldStyle() { doTest(); }
|
||||
|
||||
@Override
|
||||
protected String getIntentionName() {
|
||||
|
||||
Reference in New Issue
Block a user