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:
anna
2010-03-05 14:07:36 +03:00
parent 9e086ac5f3
commit b728796708
6 changed files with 45 additions and 6 deletions
@@ -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/>
@@ -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(); }
}