mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
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:
+14
@@ -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;
|
||||
|
||||
+17
@@ -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;
|
||||
}
|
||||
+19
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user