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);