From 5170b5d3eab60053a741b8ee4b0a11cc9e44ebac Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Wed, 20 Jul 2016 17:47:05 +0200 Subject: [PATCH] introduce constant: don't traverse inside class or lambda to check unhandled throwables; check for calls to instance methods though --- .../IntroduceConstantHandler.java | 14 ++++++++++++++ .../AnonymousClassWithThrownClause.java | 17 +++++++++++++++++ .../AnonymousClassWithThrownClause_after.java | 19 +++++++++++++++++++ .../refactoring/IntroduceConstantTest.java | 6 ++++++ 4 files changed, 56 insertions(+) create mode 100644 java/java-tests/testData/refactoring/introduceConstant/AnonymousClassWithThrownClause.java create mode 100644 java/java-tests/testData/refactoring/introduceConstant/AnonymousClassWithThrownClause_after.java diff --git a/java/java-impl/src/com/intellij/refactoring/introduceField/IntroduceConstantHandler.java b/java/java-impl/src/com/intellij/refactoring/introduceField/IntroduceConstantHandler.java index aa2f8bd60c04..40472c6bc746 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceField/IntroduceConstantHandler.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceField/IntroduceConstantHandler.java @@ -230,6 +230,7 @@ public class IntroduceConstantHandler extends BaseExpressionToFieldHandler { private static class IsStaticFinalInitializerExpression extends ClassMemberReferencesVisitor { private PsiElement myElementReference; private final PsiExpression myInitializer; + private boolean myCheckThrowables = true; public IsStaticFinalInitializerExpression(PsiClass aClass, PsiExpression initializer) { super(aClass); @@ -251,12 +252,25 @@ public class IntroduceConstantHandler extends BaseExpressionToFieldHandler { @Override public void visitCallExpression(PsiCallExpression callExpression) { super.visitCallExpression(callExpression); + if (!myCheckThrowables) return; final List checkedExceptions = ExceptionUtil.getThrownCheckedExceptions(new PsiElement[]{callExpression}); if (!checkedExceptions.isEmpty()) { myElementReference = callExpression; } } + @Override + public void visitClass(PsiClass aClass) { + myCheckThrowables = false; + super.visitClass(aClass); + } + + @Override + public void visitLambdaExpression(PsiLambdaExpression expression) { + myCheckThrowables = false; + super.visitLambdaExpression(expression); + } + protected void visitClassMemberReferenceElement(PsiMember classMember, PsiJavaCodeReferenceElement classMemberReference) { if (!classMember.hasModifierProperty(PsiModifier.STATIC)) { myElementReference = classMemberReference; diff --git a/java/java-tests/testData/refactoring/introduceConstant/AnonymousClassWithThrownClause.java b/java/java-tests/testData/refactoring/introduceConstant/AnonymousClassWithThrownClause.java new file mode 100644 index 000000000000..c97d1d83217b --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceConstant/AnonymousClassWithThrownClause.java @@ -0,0 +1,17 @@ +import java.io.IOException; +class Test { + void bar() throws IOException {} + + I get() { + return new I() { + @Override + public void foo() throws IOException { + bar(); + } + }; + } +} + +interface I { + void foo() throws IOException; +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/introduceConstant/AnonymousClassWithThrownClause_after.java b/java/java-tests/testData/refactoring/introduceConstant/AnonymousClassWithThrownClause_after.java new file mode 100644 index 000000000000..4f348e7fb8cc --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceConstant/AnonymousClassWithThrownClause_after.java @@ -0,0 +1,19 @@ +import java.io.IOException; +class Test { + public static final I xxx = new I() { + @Override + public void foo() throws IOException { + bar(); + } + }; + + void bar() throws IOException {} + + I get() { + return xxx; + } +} + +interface I { + void foo() throws IOException; +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceConstantTest.java b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceConstantTest.java index b92d69600d9f..c2edd708904d 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/IntroduceConstantTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/IntroduceConstantTest.java @@ -52,6 +52,12 @@ public class IntroduceConstantTest extends LightCodeInsightTestCase { doTest(true); } + public void testAnonymousClassWithThrownClause() throws Exception { + configureByFile(BASE_PATH + getTestName(false) + ".java"); + new MockIntroduceConstantHandler(null).invoke(getProject(), getEditor(), getFile(), null); + checkResultByFile(BASE_PATH + getTestName(false) + "_after.java"); + } + public void testAnnotationDescription() throws Exception { configureByFile(BASE_PATH + getTestName(false) + ".java"); new MockIntroduceConstantHandler(null).invoke(getProject(), getEditor(), getFile(), null);