mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-108548 Incorrect "Cannot reference nonstatic symbol from static context" should work in static methods with the inspection disabled
This commit is contained in:
+13
-10
@@ -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;
|
||||
|
||||
+76
-2
@@ -788,8 +788,8 @@ class A {
|
||||
class B {}
|
||||
}
|
||||
|
||||
A.B foo = new <error descr="Cannot reference non-static symbol 'A.B' from static context">A.B</error>()
|
||||
''')
|
||||
A.B foo = new A.<warning descr="Cannot reference non-static symbol 'A.B' from static context">B</warning>()
|
||||
''', GrUnresolvedAccessInspection)
|
||||
}
|
||||
|
||||
void testDuplicatedVar0() {
|
||||
@@ -1450,4 +1450,78 @@ def foo3(final i) {
|
||||
''')
|
||||
}
|
||||
|
||||
void testNonStaticInnerClass1() {
|
||||
testHighlighting('''\
|
||||
class MyController {
|
||||
static def list() {
|
||||
def myInnerClass = new MyCommand.<error descr="Cannot reference non-static symbol 'MyCommand.MyInnerClass' from static context">MyInnerClass</error>()
|
||||
print myInnerClass
|
||||
}
|
||||
}
|
||||
|
||||
class MyCommand {
|
||||
class MyInnerClass {
|
||||
}
|
||||
}
|
||||
''', GrUnresolvedAccessInspection)
|
||||
}
|
||||
|
||||
void testNonStaticInnerClass2() {
|
||||
testHighlighting('''\
|
||||
class MyController {
|
||||
def list() {
|
||||
def myInnerClass = new MyCommand.<warning descr="Cannot reference non-static symbol 'MyCommand.MyInnerClass' from static context">MyInnerClass</warning>()
|
||||
print myInnerClass
|
||||
}
|
||||
}
|
||||
|
||||
class MyCommand {
|
||||
class MyInnerClass {
|
||||
}
|
||||
}
|
||||
''', GrUnresolvedAccessInspection)
|
||||
}
|
||||
|
||||
void testNonStaticInnerClass3() {
|
||||
myFixture.configureByText('_.groovy', '''\
|
||||
class MyController {
|
||||
static def list() {
|
||||
def myInnerClass = new MyCommand.<error descr="Cannot reference non-static symbol 'MyCommand.MyInnerClass' from static context">MyInnerClass</error>()
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user