IDEA-117191 (Inspection "Inner class may be static" not working if inner class is return type)

This commit is contained in:
Bas Leijdekkers
2013-11-29 13:10:20 +01:00
parent 2ea48a5832
commit c613f66907
4 changed files with 39 additions and 16 deletions
@@ -54,7 +54,8 @@ public class InnerClassMayBeStaticInspection extends BaseInspection {
}
private static class InnerClassMayBeStaticFix extends InspectionGadgetsFix {
@Override
@Override
@NotNull
public String getFamilyName() {
return getName();
@@ -136,18 +136,13 @@ public class InnerClassReferenceVisitor extends JavaRecursiveElementVisitor {
else if (element instanceof PsiLocalVariable || element instanceof PsiParameter) {
final PsiElement containingMethod = PsiTreeUtil.getParentOfType(reference, PsiMethod.class);
final PsiElement referencedMethod = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
if (containingMethod != null && referencedMethod != null &&
!containingMethod.equals(referencedMethod)) {
if (containingMethod != null && referencedMethod != null && !containingMethod.equals(referencedMethod)) {
referencesStaticallyAccessible = false;
}
}
else if ((element instanceof PsiClass)) {
else if (element instanceof PsiClass) {
final PsiClass aClass = (PsiClass)element;
final PsiElement scope = aClass.getScope();
if (!(scope instanceof PsiClass)) {
return;
}
referencesStaticallyAccessible &= aClass.hasModifierProperty(PsiModifier.STATIC);
referencesStaticallyAccessible &= isClassStaticallyAccessible(aClass);
}
}
}
@@ -44,16 +44,29 @@ class D {
void foo() {
new Object() {
class Y {}
}
};
}
}
class StaticInnerClass {
private int foo;
int bar;
public class Baz extends StaticInnerClass {
Baz() {
foo = -1;
}
}
class C extends StaticInnerClass {{
bar = 1;
}}
}
class SomeBeanUnitTest {
private class BeanCreator {
public BeanCreator withQuery() {
return null;
}
}
}
@@ -1,9 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>InnerClassMayBeStaticInspection.java</file>
<line>6</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Inner class may be 'static'</problem_class>
<description>Inner class &lt;code&gt;Nested&lt;/code&gt; may be 'static' #loc</description>
</problem>
<problem>
<file>InnerClassMayBeStaticInspection.java</file>
<line>6</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Inner class may be 'static'</problem_class>
<description>Inner class &lt;code&gt;Nested&lt;/code&gt; may be 'static' #loc</description>
</problem>
<problem>
<file>InnerClassMayBeStaticInspection.java</file>
<line>60</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Inner class may be 'static'</problem_class>
<description>Inner class &lt;code&gt;C&lt;/code&gt; may be 'static' #loc</description>
</problem>
<problem>
<file>InnerClassMayBeStaticInspection.java</file>
<line>66</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Inner class may be 'static'</problem_class>
<description>Inner class &lt;code&gt;BeanCreator&lt;/code&gt; may be 'static' #loc</description>
</problem>
</problems>