IDEA-90736 (preserve resource declarations from deleted try statement)

This commit is contained in:
Roman Shevchenko
2012-09-04 21:06:52 +04:00
parent f73177c04e
commit f4e5519502
11 changed files with 146 additions and 36 deletions
@@ -28,12 +28,10 @@ import com.siyeh.ig.InspectionGadgetsFix;
import org.jetbrains.annotations.NotNull;
public class EmptyFinallyBlockInspection extends BaseInspection {
@Override
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message(
"empty.finally.block.display.name");
return InspectionGadgetsBundle.message("empty.finally.block.display.name");
}
@Override
@@ -44,8 +42,7 @@ public class EmptyFinallyBlockInspection extends BaseInspection {
@Override
@NotNull
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"empty.finally.block.problem.descriptor");
return InspectionGadgetsBundle.message("empty.finally.block.problem.descriptor");
}
@Override
@@ -60,19 +57,15 @@ public class EmptyFinallyBlockInspection extends BaseInspection {
}
private static class RemoveTryFinallyBlockFix extends InspectionGadgetsFix {
@NotNull
public String getName() {
return InspectionGadgetsBundle.message(
"remove.try.finally.block.quickfix");
return InspectionGadgetsBundle.message("remove.try.finally.block.quickfix");
}
@Override
protected void doFix(Project project, ProblemDescriptor descriptor)
throws IncorrectOperationException {
protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
final PsiElement element = descriptor.getPsiElement();
final PsiTryStatement tryStatement =
PsiTreeUtil.getParentOfType(element, PsiTryStatement.class);
final PsiTryStatement tryStatement = PsiTreeUtil.getParentOfType(element, PsiTryStatement.class);
if (tryStatement == null) {
return;
}
@@ -81,21 +74,33 @@ public class EmptyFinallyBlockInspection extends BaseInspection {
return;
}
final PsiElement parent = tryStatement.getParent();
if (parent == null) {
return;
}
final PsiResourceList resources = tryStatement.getResourceList();
if (resources != null) {
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
for (PsiResourceVariable resource : resources.getResourceVariables()) {
final PsiStatement statement = factory.createStatementFromText(resource.getText() + ";", parent);
parent.addBefore(statement, tryStatement);
}
}
final PsiElement first = tryBlock.getFirstBodyElement();
final PsiElement last = tryBlock.getLastBodyElement();
if (first != null && last != null) {
parent.addRangeAfter(first, last, tryStatement);
}
tryStatement.delete();
}
}
private static class RemoveFinallyBlockFix extends InspectionGadgetsFix {
@NotNull
public String getName() {
return InspectionGadgetsBundle.message(
"remove.finally.block.quickfix");
return InspectionGadgetsBundle.message("remove.finally.block.quickfix");
}
@Override
@@ -0,0 +1,23 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class DeleteEmptyFinally {
void m() {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,24 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class DeleteEmptyFinally {
void m() {
try {
} catch (Exception e) {
e.printStackTrace();
} <caret>finally {
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -0,0 +1,23 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
abstract class DeleteTryWithEmptyFinally {
abstract OutputStream open();
void m() throws IOException {
OutputStream stream = open();
stream.write(0xCAFEBABE);
}
}
@@ -0,0 +1,25 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
abstract class DeleteTryWithEmptyFinally {
abstract OutputStream open();
void m() throws IOException {
try (OutputStream stream = open()) {
stream.write(0xCAFEBABE);
} <caret>finally {
}
}
}
@@ -1,17 +1,17 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.siyeh.ig;
@@ -23,7 +23,7 @@ import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
/**
* @author anna
* Date: 16-Jun-2009
* @since 16-Jun-2009
*/
@SuppressWarnings({"JUnitTestCaseWithNonTrivialConstructors"})
public abstract class IGQuickFixesTestCase extends JavaCodeInsightFixtureTestCase {
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,14 +15,16 @@
*/
package com.siyeh.ig.fixes;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.IGQuickFixesTestCase;
import com.siyeh.ig.errorhandling.CaughtExceptionImmediatelyRethrownInspection;
import com.siyeh.ig.errorhandling.EmptyFinallyBlockInspection;
public class DeleteCatchSectionFixTest extends IGQuickFixesTestCase {
public class DeleteTrySectionsFixTest extends IGQuickFixesTestCase {
@Override
protected void setUp() throws Exception {
super.setUp();
myFixture.enableInspections(new CaughtExceptionImmediatelyRethrownInspection());
myFixture.enableInspections(new CaughtExceptionImmediatelyRethrownInspection(), new EmptyFinallyBlockInspection());
}
@Override
@@ -30,11 +32,19 @@ public class DeleteCatchSectionFixTest extends IGQuickFixesTestCase {
return "tryCatch";
}
public void testDeleteTryStatement() throws Exception {
doTest("deleteTryStatement", "Remove 'try catch' statement");
public void testDeleteTryStatement() {
doTest(InspectionGadgetsBundle.message("remove.try.catch.quickfix"));
}
public void testDeleteEmptyTryStatement() throws Exception {
doTest("deleteEmptyTryStatement", "Remove 'try catch' statement");
public void testDeleteEmptyTryStatement() {
doTest(InspectionGadgetsBundle.message("remove.try.catch.quickfix"));
}
public void testDeleteEmptyFinally() {
doTest(InspectionGadgetsBundle.message("remove.finally.block.quickfix"));
}
public void testDeleteTryWithEmptyFinally() {
doTest(InspectionGadgetsBundle.message("remove.try.finally.block.quickfix"));
}
}