diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/untypedUnresolvedAccess/GrUnresolvedAccessInspection.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/untypedUnresolvedAccess/GrUnresolvedAccessInspection.java index 6d14ce05328c..b3b0c99318fb 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/untypedUnresolvedAccess/GrUnresolvedAccessInspection.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/codeInspection/untypedUnresolvedAccess/GrUnresolvedAccessInspection.java @@ -138,7 +138,7 @@ public class GrUnresolvedAccessInspection extends GroovySuppressableInspectionTo return profile.isToolEnabled(unusedDefKey, file); } - private static GrUnresolvedAccessInspection getInstance(PsiFile file, Project project) { + public static GrUnresolvedAccessInspection getInstance(PsiFile file, Project project) { final InspectionProfile profile = InspectionProjectProfileManager.getInstance(project).getInspectionProfile(); return (GrUnresolvedAccessInspection)profile.getUnwrappedTool(SHORT_NAME, file); } @@ -194,8 +194,11 @@ public class GrUnresolvedAccessInspection extends GroovySuppressableInspectionTo if (!inStaticContext && GroovySuppressableInspectionTool.isElementToolSuppressedIn(refElement, SHORT_NAME)) return null; - GrUnresolvedAccessInspection inspection = getInstance(refElement.getContainingFile(), refElement.getProject()); - if (!inspection.myHighlightInnerClasses) return null; + if (!inStaticContext) { + GrUnresolvedAccessInspection inspection = getInstance(refElement.getContainingFile(), refElement.getProject()); + if (inspection == null) return null; + if (!inspection.myHighlightInnerClasses) return null; + } GrNewExpression newExpression = (GrNewExpression)refElement.getParent(); if (resolved instanceof PsiClass) { @@ -221,7 +224,7 @@ public class GrUnresolvedAccessInspection extends GroovySuppressableInspectionTo PsiElement refNameElement = ref.getReferenceNameElement(); if (refNameElement == null) return null; - boolean cannotBeDynamic = PsiUtil.isCompileStatic(ref) || isPropertyAccessInStaticMethod(ref); + boolean inStaticContext = PsiUtil.isCompileStatic(ref) || isPropertyAccessInStaticMethod(ref); GroovyResolveResult resolveResult = getBestResolveResult(ref); if (resolveResult.getElement() != null) { @@ -229,16 +232,16 @@ public class GrUnresolvedAccessInspection extends GroovySuppressableInspectionTo if (isStaticOk(resolveResult)) return null; String message = GroovyBundle.message("cannot.reference.non.static", ref.getReferenceName()); - return createAnnotationForRef(ref, cannotBeDynamic, message); + return createAnnotationForRef(ref, inStaticContext, message); } if (ResolveUtil.isKeyOfMap(ref) || isClassReference(ref)) { return null; } - if (!cannotBeDynamic) { - if (!isInspectionEnabled(ref.getContainingFile(), ref.getProject())) return null; + if (!inStaticContext) { GrUnresolvedAccessInspection inspection = getInstance(ref.getContainingFile(), ref.getProject()); + if (inspection == null) return null; if (!inspection.myHighlightIfGroovyObjectOverridden && areGroovyObjectMethodsOverridden(ref)) return null; if (!inspection.myHighlightIfMissingMethodsDeclared && areMissingMethodsDeclared(ref)) return null; @@ -246,8 +249,8 @@ public class GrUnresolvedAccessInspection extends GroovySuppressableInspectionTo if (GroovySuppressableInspectionTool.isElementToolSuppressedIn(ref, SHORT_NAME)) return null; } - if (cannotBeDynamic || shouldHighlightAsUnresolved(ref)) { - HighlightInfo info = createAnnotationForRef(ref, cannotBeDynamic, GroovyBundle.message("cannot.resolve", ref.getReferenceName())); + if (inStaticContext || shouldHighlightAsUnresolved(ref)) { + HighlightInfo info = createAnnotationForRef(ref, inStaticContext, GroovyBundle.message("cannot.resolve", ref.getReferenceName())); if (info == null) return null; HighlightDisplayKey displayKey = HighlightDisplayKey.find(SHORT_NAME); @@ -259,7 +262,7 @@ public class GrUnresolvedAccessInspection extends GroovySuppressableInspectionTo registerAddImportFixes(ref, info, displayKey); } - registerReferenceFixes(ref, info, cannotBeDynamic, displayKey); + registerReferenceFixes(ref, info, inStaticContext, displayKey); UnresolvedReferenceQuickFixProvider.registerReferenceFixes(ref, new QuickFixActionRegistrarAdapter(info, displayKey)); OrderEntryFix.registerFixes(new QuickFixActionRegistrarAdapter(info, displayKey), ref); return info; diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GroovyHighlightingTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GroovyHighlightingTest.groovy index 7dc1e6fd9186..f8d9730d4ffb 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GroovyHighlightingTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/highlighting/GroovyHighlightingTest.groovy @@ -788,8 +788,8 @@ class A { class B {} } -A.B foo = new A.B() -''') +A.B foo = new A.B() +''', GrUnresolvedAccessInspection) } void testDuplicatedVar0() { @@ -1450,4 +1450,78 @@ def foo3(final i) { ''') } + void testNonStaticInnerClass1() { + testHighlighting('''\ +class MyController { + static def list() { + def myInnerClass = new MyCommand.MyInnerClass() + print myInnerClass + } +} + +class MyCommand { + class MyInnerClass { + } +} +''', GrUnresolvedAccessInspection) + } + + void testNonStaticInnerClass2() { + testHighlighting('''\ +class MyController { + def list() { + def myInnerClass = new MyCommand.MyInnerClass() + print myInnerClass + } +} + +class MyCommand { + class MyInnerClass { + } +} +''', GrUnresolvedAccessInspection) + } + + void testNonStaticInnerClass3() { + myFixture.configureByText('_.groovy', '''\ +class MyController { + static def list() { + def myInnerClass = new MyCommand.MyInnerClass() + print myInnerClass + } +} + +class MyCommand { + class MyInnerClass { + } +} +''') + + myFixture.enableInspections(GrUnresolvedAccessInspection) + + GrUnresolvedAccessInspection.getInstance(myFixture.file, myFixture.project).myHighlightInnerClasses = false + myFixture.testHighlighting(true, false, true) + } + + void testNonStaticInnerClass4() { + myFixture.configureByText('_.groovy', '''\ +class MyController { + def list() { + def myInnerClass = new MyCommand.MyInnerClass() + print myInnerClass + } +} + +class MyCommand { + class MyInnerClass { + } +} +''') + + myFixture.enableInspections(GrUnresolvedAccessInspection) + + GrUnresolvedAccessInspection.getInstance(myFixture.file, myFixture.project).myHighlightInnerClasses = false + myFixture.testHighlighting(true, false, true) + } + } \ No newline at end of file