mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
field can be local extended: suggest read only fields used in e.g. constructor once as can be local; process inner class constructors as methods (IDEA-52670)
This commit is contained in:
+10
-6
@@ -153,17 +153,21 @@ public class FieldCanBeLocalInspection extends BaseLocalInspectionTool {
|
||||
}
|
||||
}
|
||||
}
|
||||
final Collection<PsiVariable> writtenVariables = ControlFlowUtil.getWrittenVariables(controlFlow, 0, controlFlow.getSize(), false);
|
||||
final List<PsiReferenceExpression> readBeforeWrites = ControlFlowUtil.getReadBeforeWrite(controlFlow);
|
||||
for (final PsiReferenceExpression readBeforeWrite : readBeforeWrites) {
|
||||
final PsiElement resolved = readBeforeWrite.resolve();
|
||||
if (resolved instanceof PsiField) {
|
||||
final PsiField field = (PsiField)resolved;
|
||||
PsiElement parent = body.getParent();
|
||||
if (!(parent instanceof PsiMethod) ||
|
||||
!((PsiMethod)parent).isConstructor() ||
|
||||
field.getInitializer() == null ||
|
||||
field.hasModifierProperty(PsiModifier.STATIC)) {
|
||||
candidates.remove(field);
|
||||
if (writtenVariables.contains(field)){
|
||||
PsiElement parent = body.getParent();
|
||||
if (!(parent instanceof PsiMethod) ||
|
||||
!((PsiMethod)parent).isConstructor() ||
|
||||
field.getInitializer() == null ||
|
||||
field.hasModifierProperty(PsiModifier.STATIC) ||
|
||||
!PsiTreeUtil.isAncestor(((PsiMethod)parent).getContainingClass(), field, true)) {
|
||||
candidates.remove(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems/>
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
public class Outer {
|
||||
private int value = 0;
|
||||
|
||||
public class Inner {
|
||||
private final int myValue;
|
||||
|
||||
public Inner() {
|
||||
myValue = value++;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<problems>
|
||||
<problem>
|
||||
<file>Test.java</file>
|
||||
<line>2</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Field can be local</problem_class>
|
||||
<description>Field can be converted to a local variable</description>
|
||||
</problem>
|
||||
</problems>
|
||||
@@ -0,0 +1,11 @@
|
||||
public class Outer {
|
||||
private int value = 0;
|
||||
|
||||
public class Inner {
|
||||
private final int myValue;
|
||||
|
||||
public Inner() {
|
||||
myValue = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,4 +24,6 @@ public class FieldCanBeLocalTest extends InspectionTestCase {
|
||||
public void testConstructor () throws Exception { doTest(); }
|
||||
public void testStaticFinal() throws Exception { doTest(); }
|
||||
public void testStaticAccess() throws Exception { doTest(); }
|
||||
public void testInnerClassConstructor() throws Exception { doTest(); }
|
||||
public void testLocalVar2InnerClass() throws Exception { doTest(); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user