introduce constant: don't traverse inside class or lambda to check unhandled throwables; check for calls to instance methods though

This commit is contained in:
Anna.Kozlova
2016-07-20 18:23:02 +02:00
parent d4c8dc223d
commit 5170b5d3ea
4 changed files with 56 additions and 0 deletions
@@ -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<PsiClassType> 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;
@@ -0,0 +1,17 @@
import java.io.IOException;
class Test {
void bar() throws IOException {}
I get() {
return <selection>new I() {
@Override
public void foo() throws IOException {
bar();
}
}</selection>;
}
}
interface I {
void foo() throws IOException;
}
@@ -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;
}
@@ -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);