diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/ResolvePropertyTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/ResolvePropertyTest.java index c48d9b10966b..f07a1184e498 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/ResolvePropertyTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/resolve/ResolvePropertyTest.java @@ -1,1486 +1,1531 @@ -// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. -package org.jetbrains.plugins.groovy.lang.resolve +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.jetbrains.plugins.groovy.lang.resolve; -import com.intellij.psi.* -import com.intellij.psi.util.PropertyUtil -import org.jetbrains.plugins.groovy.GroovyFileType -import org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement -import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult -import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField -import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable -import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression -import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter -import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod -import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant -import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod -import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod -import org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrBindingVariable -import org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrTraitMethod -import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil -import org.jetbrains.plugins.groovy.util.TestUtils +import com.intellij.psi.*; +import com.intellij.psi.util.PropertyUtil; +import com.intellij.testFramework.UsefulTestCase; +import junit.framework.TestCase; +import org.jetbrains.plugins.groovy.GroovyFileType; +import org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement; +import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod; +import org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrBindingVariable; +import org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrTraitMethod; +import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil; +import org.jetbrains.plugins.groovy.util.TestUtils; +import org.jetbrains.plugins.groovy.util.ThrowingTransformation; -import static org.jetbrains.plugins.groovy.util.ThrowingTransformation.disableTransformations - -class ResolvePropertyTest extends GroovyResolveTestCase { - final String basePath = TestUtils.testDataPath + "resolve/property/" - - void testParameter1() throws Exception { - disableTransformations testRootDisposable - resolve "A.groovy", GrParameter +public class ResolvePropertyTest extends GroovyResolveTestCase { + public void testParameter1() { + ThrowingTransformation.disableTransformations(getTestRootDisposable()); + resolve("A.groovy", GrParameter.class); } - void testClosureParameter1() throws Exception { - disableTransformations testRootDisposable - resolve "A.groovy", GrParameter + public void testClosureParameter1() { + ThrowingTransformation.disableTransformations(getTestRootDisposable()); + resolve("A.groovy", GrParameter.class); } - void testClosureOwner() throws Exception { - PsiReference ref = configureByFile("closureOwner/A.groovy") - PsiElement resolved = ref.resolve() - assertInstanceOf(resolved, PsiMethod) + public void testClosureOwner() { + PsiReference ref = configureByFile("closureOwner/A.groovy"); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, PsiMethod.class); } - void testLocal1() throws Exception { - disableTransformations testRootDisposable - doTest("local1/A.groovy") + public void testLocal1() throws Exception { + ThrowingTransformation.disableTransformations(getTestRootDisposable()); + doTest("local1/A.groovy"); } - void testField1() throws Exception { - disableTransformations testRootDisposable - doTest("field1/A.groovy") + public void testField1() throws Exception { + ThrowingTransformation.disableTransformations(getTestRootDisposable()); + doTest("field1/A.groovy"); } - void testField2() throws Exception { - doTest("field2/A.groovy") + public void testField2() throws Exception { + doTest("field2/A.groovy"); } - void testArrayLength() throws Exception { - doTest("arrayLength/A.groovy") + public void testArrayLength() throws Exception { + doTest("arrayLength/A.groovy"); } - void testFromGetter() throws Exception { - PsiReference ref = configureByFile("fromGetter/A.groovy") - assertTrue(ref.resolve() instanceof GrAccessorMethod) + public void testFromGetter() { + PsiReference ref = configureByFile("fromGetter/A.groovy"); + TestCase.assertTrue(ref.resolve() instanceof GrAccessorMethod); } - void testFromGetter2() throws Exception { - PsiReference ref = configureByFile("fromGetter2/A.groovy") - assertTrue(ref.resolve() instanceof GrAccessorMethod) + public void testFromGetter2() { + PsiReference ref = configureByFile("fromGetter2/A.groovy"); + TestCase.assertTrue(ref.resolve() instanceof GrAccessorMethod); } - void testFromSetter2() throws Exception { - PsiReference ref = configureByFile("fromSetter2/A.groovy") - assertTrue(ref.resolve() instanceof GrAccessorMethod) + public void testFromSetter2() { + PsiReference ref = configureByFile("fromSetter2/A.groovy"); + TestCase.assertTrue(ref.resolve() instanceof GrAccessorMethod); } - void testFromSetter() throws Exception { - PsiReference ref = configureByFile("fromSetter/A.groovy") - assertTrue(ref.resolve() instanceof GrAccessorMethod) + public void testFromSetter() { + PsiReference ref = configureByFile("fromSetter/A.groovy"); + TestCase.assertTrue(ref.resolve() instanceof GrAccessorMethod); } - void testCatchParameter() throws Exception { - disableTransformations testRootDisposable - resolve "CatchParameter.groovy", GrParameter + public void testCatchParameter() { + ThrowingTransformation.disableTransformations(getTestRootDisposable()); + resolve("CatchParameter.groovy", GrParameter.class); } - void testCaseClause() throws Exception { - disableTransformations testRootDisposable - doTest("caseClause/CaseClause.groovy") + public void testCaseClause() throws Exception { + ThrowingTransformation.disableTransformations(getTestRootDisposable()); + doTest("caseClause/CaseClause.groovy"); } - void testGrvy104() throws Exception { - disableTransformations testRootDisposable - doTest("grvy104/Test.groovy") + public void testGrvy104() throws Exception { + ThrowingTransformation.disableTransformations(getTestRootDisposable()); + doTest("grvy104/Test.groovy"); } - void testGrvy270() throws Exception { - PsiReference ref = configureByFile("grvy270/Test.groovy") - assertNull(ref.resolve()) + public void testGrvy270() { + PsiReference ref = configureByFile("grvy270/Test.groovy"); + TestCase.assertNull(ref.resolve()); } - void testGrvy1483() throws Exception { - disableTransformations testRootDisposable - resolve "Test.groovy", GrVariable + public void testGrvy1483() { + ThrowingTransformation.disableTransformations(getTestRootDisposable()); + resolve("Test.groovy", GrVariable.class); } - void testField3() throws Exception { - GrReferenceElement ref = (GrReferenceElement)configureByFile("field3/A.groovy").element - GroovyResolveResult resolveResult = ref.advancedResolve() - assertTrue(resolveResult.element instanceof GrField) - assertFalse(resolveResult.validResult) + public void testField3() { + GrReferenceElement ref = (GrReferenceElement)configureByFile("field3/A.groovy").getElement(); + GroovyResolveResult resolveResult = ref.advancedResolve(); + TestCase.assertTrue(resolveResult.getElement() instanceof GrField); + TestCase.assertFalse(resolveResult.isValidResult()); } - void testToGetter() throws Exception { - GrReferenceElement ref = (GrReferenceElement)configureByFile("toGetter/A.groovy").element - PsiElement resolved = ref.resolve() - assertTrue(resolved instanceof GrMethod) - assertTrue(PropertyUtil.isSimplePropertyGetter((PsiMethod)resolved)) + public void testToGetter() { + GrReferenceElement ref = (GrReferenceElement)configureByFile("toGetter/A.groovy").getElement(); + PsiElement resolved = ref.resolve(); + TestCase.assertTrue(resolved instanceof GrMethod); + TestCase.assertTrue(PropertyUtil.isSimplePropertyGetter((PsiMethod)resolved)); } - void testToSetter() throws Exception { - GrReferenceElement ref = (GrReferenceElement)configureByFile("toSetter/A.groovy").element - PsiElement resolved = ref.resolve() - assertTrue(resolved instanceof GrMethod) - assertTrue(PropertyUtil.isSimplePropertySetter((PsiMethod)resolved)) + public void testToSetter() { + GrReferenceElement ref = (GrReferenceElement)configureByFile("toSetter/A.groovy").getElement(); + PsiElement resolved = ref.resolve(); + TestCase.assertTrue(resolved instanceof GrMethod); + TestCase.assertTrue(PropertyUtil.isSimplePropertySetter((PsiMethod)resolved)); } - void testUndefinedVar1() throws Exception { - PsiReference ref = configureByFile("undefinedVar1/A.groovy") - PsiElement resolved = ref.resolve() - assertInstanceOf(resolved, GrBindingVariable) + public void testUndefinedVar1() { + PsiReference ref = configureByFile("undefinedVar1/A.groovy"); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, GrBindingVariable.class); } - void testRecursive1() throws Exception { - PsiReference ref = configureByFile("recursive1/A.groovy") - PsiElement resolved = ref.resolve() - assertTrue(resolved instanceof GrField) + public void testRecursive1() { + PsiReference ref = configureByFile("recursive1/A.groovy"); + PsiElement resolved = ref.resolve(); + TestCase.assertTrue(resolved instanceof GrField); } - void testRecursive2() throws Exception { - PsiReference ref = configureByFile("recursive2/A.groovy") - PsiElement resolved = ref.resolve() - assertTrue(resolved instanceof GrMethod) - assertEquals(CommonClassNames.JAVA_LANG_OBJECT, ((GrMethod)resolved).returnType.canonicalText) + public void testRecursive2() { + PsiReference ref = configureByFile("recursive2/A.groovy"); + PsiElement resolved = ref.resolve(); + TestCase.assertTrue(resolved instanceof GrMethod); + TestCase.assertEquals(CommonClassNames.JAVA_LANG_OBJECT, ((GrMethod)resolved).getReturnType().getCanonicalText()); } - void testUndefinedVar2() throws Exception { - doUndefinedVarTest("undefinedVar2/A.groovy") + public void testUndefinedVar2() throws Exception { + doUndefinedVarTest("undefinedVar2/A.groovy"); } - void testUndefinedVar3() { - resolveByText(''' -(aa, b) = [1, 4] -c = aa -''', GrBindingVariable) + public void testUndefinedVar3() { + resolveByText(""" + + (aa, b) = [1, 4] + c = aa + \s\ + """, GrBindingVariable.class); } - void testDefinedVar1() throws Exception { - disableTransformations testRootDisposable - resolve "A.groovy", GrVariable + public void testDefinedVar1() { + ThrowingTransformation.disableTransformations(getTestRootDisposable()); + resolve("A.groovy", GrVariable.class); } - void testOperatorOverload() throws Exception { - doTest("operatorOverload/A.groovy") + public void testOperatorOverload() throws Exception { + doTest("operatorOverload/A.groovy"); } - void testEnumConstant() throws Exception { - PsiReference ref = configureByFile("enumConstant/A.groovy") - assertTrue(ref.resolve() instanceof GrEnumConstant) + public void testEnumConstant() throws Exception { + PsiReference ref = configureByFile("enumConstant/A.groovy"); + TestCase.assertTrue(ref.resolve() instanceof GrEnumConstant); } - void testStackOverflow() throws Exception { - doTest("stackOverflow/A.groovy") + public void testStackOverflow() throws Exception { + doTest("stackOverflow/A.groovy"); } - void testFromDifferentCaseClause() throws Exception { - PsiReference ref = configureByFile("fromDifferentCaseClause/A.groovy") - assertNull(ref.resolve()) + public void testFromDifferentCaseClause() { + PsiReference ref = configureByFile("fromDifferentCaseClause/A.groovy"); + TestCase.assertNull(ref.resolve()); } - void testNotSettingProperty() throws Exception { - PsiReference ref = configureByFile("notSettingProperty/A.groovy") - assertNull(ref.resolve()) + public void testNotSettingProperty() { + PsiReference ref = configureByFile("notSettingProperty/A.groovy"); + TestCase.assertNull(ref.resolve()); } - void testGrvy633() throws Exception { - PsiReference ref = configureByFile("grvy633/A.groovy") - assertNull(ref.resolve()) + public void testGrvy633() { + PsiReference ref = configureByFile("grvy633/A.groovy"); + TestCase.assertNull(ref.resolve()); } - void testGrvy575() throws Exception { - disableTransformations testRootDisposable - doTest("grvy575/A.groovy") + public void testGrvy575() throws Exception { + ThrowingTransformation.disableTransformations(getTestRootDisposable()); + doTest("grvy575/A.groovy"); } - void testGrvy747() throws Exception { - PsiReference ref = configureByFile("grvy747/A.groovy") - assertTrue(ref.resolve() instanceof GrField) + public void testGrvy747() { + PsiReference ref = configureByFile("grvy747/A.groovy"); + TestCase.assertTrue(ref.resolve() instanceof GrField); } - void testClosureCall() throws Exception { - disableTransformations testRootDisposable - PsiReference ref = configureByFile("closureCall/ClosureCall.groovy") - assertTrue(ref.resolve() instanceof GrVariable) + public void testClosureCall() { + ThrowingTransformation.disableTransformations(getTestRootDisposable()); + PsiReference ref = configureByFile("closureCall/ClosureCall.groovy"); + TestCase.assertTrue(ref.resolve() instanceof GrVariable); } - void testUnderscoredField() throws Exception { - PsiReference ref = configureByFile("underscoredField/UnderscoredField.groovy") - final GrField field = assertInstanceOf(ref.resolve(), GrField.class) - assertFalse(ref.isReferenceTo(field.getters[0])) - assertTrue(ref.isReferenceTo(field)) + public void testUnderscoredField() { + PsiReference ref = configureByFile("underscoredField/UnderscoredField.groovy"); + final GrField field = UsefulTestCase.assertInstanceOf(ref.resolve(), GrField.class); + TestCase.assertFalse(ref.isReferenceTo(field.getGetters()[0])); + TestCase.assertTrue(ref.isReferenceTo(field)); } - void testPropertyWithoutField1() throws Exception { - PsiReference ref = configureByFile("propertyWithoutField1/PropertyWithoutField1.groovy") - assertInstanceOf(ref.resolve(), GrMethod.class) + public void testPropertyWithoutField1() { + PsiReference ref = configureByFile("propertyWithoutField1/PropertyWithoutField1.groovy"); + UsefulTestCase.assertInstanceOf(ref.resolve(), GrMethod.class); } - void testPropertyWithoutField2() throws Exception { - PsiReference ref = configureByFile("propertyWithoutField2/PropertyWithoutField2.groovy") - assertInstanceOf(ref.resolve(), GrMethod.class) + public void testPropertyWithoutField2() { + PsiReference ref = configureByFile("propertyWithoutField2/PropertyWithoutField2.groovy"); + UsefulTestCase.assertInstanceOf(ref.resolve(), GrMethod.class); } - void testFieldAssignedInTheSameMethod() throws Exception { - PsiReference ref = configureByFile("fieldAssignedInTheSameMethod/FieldAssignedInTheSameMethod.groovy") - assertInstanceOf(ref.resolve(), GrField.class) + public void testFieldAssignedInTheSameMethod() { + PsiReference ref = configureByFile("fieldAssignedInTheSameMethod/FieldAssignedInTheSameMethod.groovy"); + UsefulTestCase.assertInstanceOf(ref.resolve(), GrField.class); } - void testPrivateFieldAssignment() throws Throwable { + public void testPrivateFieldAssignment() { myFixture.configureByText(GroovyFileType.GROOVY_FILE_TYPE, """ - class Aaaaa { - final def aaa + + class Aaaaa { + final def aaa + \s + def foo() { + aaa = 2 + } + }\ + """); + PsiReference reference = myFixture.getFile().findReferenceAt(myFixture.getEditor().getCaretModel().getOffset()); + UsefulTestCase.assertInstanceOf(reference.resolve(), GrField.class); + } - def foo() { - aaa = 2 + public void testOverriddenGetter() { + myFixture.configureByText("a.groovy", """ + interface Foo { + def getFoo() + } + interface Bar extends Foo { + def getFoo() + } + \s + Bar b + b.foo"""); + PsiReference reference = myFixture.getFile().findReferenceAt(myFixture.getEditor().getCaretModel().getOffset()); + TestCase.assertEquals("Bar", UsefulTestCase.assertInstanceOf(reference.resolve(), GrMethod.class).getContainingClass().getName()); + } + + public void testIDEADEV40403() { + myFixture.configureByFile("IDEADEV40403/A.groovy"); + PsiReference reference = findReference(); + PsiElement resolved = reference.resolve(); + PsiClass clazz = UsefulTestCase.assertInstanceOf(resolved, PsiMethod.class).getContainingClass(); + TestCase.assertEquals("Script", clazz.getName()); + } + + public void testBooleanGetterPropertyAccess() { + myFixture.configureByText("a.groovy", "print([].empty)"); + PsiReference ref = findReference(); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, PsiMethod.class); + } + + public PsiReference findReference() { return myFixture.getFile().findReferenceAt(myFixture.getEditor().getCaretModel().getOffset()); } + + public void testTriplePropertyUsages() { + myFixture.configureByText("a.groovy", """ + + class Foo { + def bar + def zoo = bar + } + \s\ + """); + PsiReference ref = findReference(); + GrField target = UsefulTestCase.assertInstanceOf(ref.resolve(), GrField.class); + TestCase.assertTrue(ref.isReferenceTo(target)); + TestCase.assertFalse(ref.isReferenceTo(target.getGetters()[0])); + TestCase.assertFalse(ref.isReferenceTo(target.getSetter())); + } + + public void testAliasedStaticImport() { + myFixture.addClass(""" + class Main { + static def foo=4 + """); + + myFixture.configureByText("a.groovy", """ + import static Main.foo as bar + print bar + } + """); + PsiReference ref = findReference(); + PsiField target = UsefulTestCase.assertInstanceOf(ref.resolve(), PsiField.class); + TestCase.assertEquals("foo", target.getName()); + } + + private void doTest(String fileName) { + PsiReference ref = configureByFile(fileName); + PsiElement resolved = ref.resolve(); + TestCase.assertTrue(resolved instanceof GrVariable); + } + + private void doUndefinedVarTest(String fileName) { + PsiReference ref = configureByFile(fileName); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, GrBindingVariable.class); + } + + public void testBooleanProperty() { + myFixture.configureByText("Abc.groovy", """ + class A{ + boolean getFoo(){return true} + boolean isFoo(){return false} + } + print new A().foo + """); + PsiReference ref = findReference(); + PsiElement resolved = ref.resolve(); + TestCase.assertNotNull(resolved); + assertEquals("getFoo", ((PsiMethod)resolved).getName()); + } + + public void testExplicitBooleanProperty() { + myFixture.configureByText("Abc.groovy", """ + class A{ + boolean foo + } + print new A().foo + """); + PsiReference ref = findReference(); + PsiElement resolved = ref.resolve(); + assertEquals("getFoo", ((PsiMethod)resolved).getName()); + } + + public void testStaticFieldAndNonStaticGetter() { + myFixture.configureByText("Abc.groovy", "print Float.NaN"); + PsiReference ref = findReference(); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, PsiField.class); + } + + public void testPropertyAndFieldDeclarationInsideClass() { + myFixture.configureByText("a.groovy", """ + class Foo { + def foo + public def foo + def bar() { + print foo + } + } + """); + PsiReference ref = findReference(); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, GrField.class); + TestCase.assertTrue(((GrField)resolved).getModifierList().hasExplicitVisibilityModifiers()); + } + + public void testPropertyAndFieldDeclarationOutsideClass() { + myFixture.configureByText("a.groovy", """ + class Foo { + def foo + public def foo + + def bar() { + print foo + } + } + print new Foo().foo + """); + PsiReference ref = findReference(); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, GrAccessorMethod.class); + } + + public void testPropertyAndFieldDeclarationWithSuperClass1() { + myFixture.configureByText("a.groovy", """ + class Bar{ + def foo + } + class Foo extends Bar { + public def foo + + def bar() { + print foo + } + } + print new Foo().foo + """); + PsiReference ref = findReference(); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, GrAccessorMethod.class); + } + + public void testPropertyAndFieldDeclarationWithSuperClass2() { + myFixture.configureByText("a.groovy", """ + class Bar{ + def foo + } + class Foo extends Bar { + public def foo + + def bar() { + print foo + } + } + print new Foo().foo + """); + PsiReference ref = findReference(); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, GrField.class); + TestCase.assertTrue(((GrField)resolved).getModifierList().hasExplicitVisibilityModifiers()); + } + + public void testPropertyAndFieldDeclarationWithSuperClass3() { + myFixture.configureByText("a.groovy", """ + class Bar{ + public def foo + } + class Foo extends Bar { + def foo + + def bar() { + print foo + } + } + print new Foo().foo + """); + PsiReference ref = findReference(); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, GrAccessorMethod.class); + } + + public void testPropertyAndFieldDeclarationWithSuperClass4() { + myFixture.configureByText("a.groovy", """ + class Bar{ + public def foo + } + class Foo extends Bar { + def foo + + def bar() { + print foo + } + } + print new Foo().foo + """); + PsiReference ref = findReference(); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, GrField.class); + TestCase.assertFalse(((GrField)resolved).getModifierList().hasExplicitVisibilityModifiers()); + } + + public void testReadAccessToStaticallyImportedProperty() { + + myFixture.addFileToProject("a.groovy", """ + class Foo { + static def bar + } + """); + myFixture.configureByText("b.groovy", """ + import static Foo.bar + print bar + """); + PsiReference ref = findReference(); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, GrAccessorMethod.class); + } + + public void testWriteAccessToStaticallyImportedProperty() { + myFixture.addFileToProject("a.groovy", """ + class Foo { + static def bar + } + """); + myFixture.configureByText("b.groovy", """ + import static Foo.bar + bar = 2 + """); + PsiReference ref = findReference(); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, GrAccessorMethod.class); + } + + public void testGetterToStaticallyImportedProperty() { + myFixture.addFileToProject("a.groovy", """ + class Foo { + static def bar + } + """); + myFixture.configureByText("b.groovy", """ + import static Foo.bar + setBar(2) + """); + PsiReference ref = findReference(); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, GrAccessorMethod.class); + } + + public void testPropertyInCallExpression() { + myFixture.configureByText("a.groovy", """ + class Foo { + def foo = { + return {int i -> print i} + } + + def foo(String s){ + print s + } + } + new Foo().foo(2)\ + """); + PsiReference ref = findReference(); + PsiElement resolved = ref.resolve(); + + UsefulTestCase.assertInstanceOf(resolved, GrAccessorMethod.class); + } + + public void test_property_vs_field_in_call_from_outside() { + GrMethod method = resolveByText(""" + class C { + def foo = { 42 } + def getFoo() { return { 43 } } + } + new C().foo(2) + """, GrMethod.class); + assertFalse(method instanceof GrAccessorMethod); + } + + public void testPropertyImportedOnDemand() { + myFixture.addFileToProject("foo/A.groovy", "package foo; class Foo {static def foo}"); + myFixture.configureByText("B.groovy", """ + package foo + import static Foo.* + print foo + """); + + PsiReference ref = findReference(); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, GrAccessorMethod.class); + } + + public void testSetterToAliasedImportedProperty() { + myFixture.addFileToProject("a.groovy", """ + class Foo { + static def bar + } + """); + myFixture.configureByText("b.groovy", """ + import static Foo.bar as foo + setFoo(2) + """); + PsiReference ref = findReference(); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, GrAccessorMethod.class); + } + + public void testPhysicalSetterToStaticallyImportedProperty() { + myFixture.addFileToProject("a.groovy", """ + class Foo { + static def bar + static def setBar(def bar){} + } + """); + myFixture.configureByText("b.groovy", """ + import static Foo.bar as foo + setFoo(2) + """); + PsiReference ref = findReference(); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, GrMethod.class); + } + + public void testPropertyUseInCategory() { + PsiReference ref = configureByFile("propertyUseInCategory/a.groovy"); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, PsiMethod.class); + } + + public void testCollectionItemFields() { + PsiReference ref = configureByFile("collectionItemFields/A.groovy"); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, PsiField.class); + } + + public void testFieldAccessInStaticContext() { + PsiReference ref = configureByFile("fieldAccessInStaticContext/A.groovy"); + GroovyResolveResult resolveResult = ((GrReferenceExpression)ref).advancedResolve(); + TestCase.assertFalse(resolveResult.isStaticsOK()); + } + + public void testFieldAccessInClosureVsStaticContext() { + PsiReference ref = configureByFile("fieldAccessInClosureVsStaticContext/A.groovy"); + GroovyResolveResult resolveResult = ((GrReferenceExpression)ref).advancedResolve(); + TestCase.assertTrue(resolveResult.isStaticsOK()); + } + + public void testUpperCaseFieldAndGetter() { + TestCase.assertTrue(resolve("A.groovy") instanceof GrField); + } + + public void testUpperCaseFieldWithoutGetter() { + UsefulTestCase.assertInstanceOf(resolve("A.groovy"), GrField.class); + } + + public void testGetterWithUpperCaseFieldReference() { + TestCase.assertNull(resolve("A.groovy")); + } + + public void testCommandExpressions() { + UsefulTestCase.assertInstanceOf(resolve("A.groovy"), GrAccessorMethod.class); + } + + public void testStaticFieldOfInterface() { + final GroovyResolveResult result = advancedResolve("A.groovy"); + TestCase.assertTrue(result.isStaticsOK()); + } + + public void testNonStaticFieldInStaticContext() { + final GroovyResolveResult result = advancedResolve("A.groovy"); + TestCase.assertFalse(result.isStaticsOK()); + } + + public void testPropertyInExprStatement() { + PsiElement result = resolve("A.groovy"); + UsefulTestCase.assertInstanceOf(result, GrAccessorMethod.class); + } + + public void testPreferAlias() { + myFixture.addFileToProject("a/B.groovy", "package a; class B {public static def f1; public static def f2}"); + TestCase.assertEquals("f2", ((GrField)resolve("A.groovy")).getName()); + } + + public void testF1property() { + UsefulTestCase.assertInstanceOf(resolve("A.groovy"), GrAccessorMethod.class); + } + + public void testAnonymousClassFieldAndLocalVar() { + ThrowingTransformation.disableTransformations(getTestRootDisposable()); + final PsiElement resolved = resolve("A.groovy"); + UsefulTestCase.assertInstanceOf(resolved, PsiVariable.class); + TestCase.assertTrue(PsiUtil.isLocalVariable(resolved)); + } + + public void _testResolveForVarOutsideOfFor() { + final PsiElement resolved = resolve("A.groovy"); + UsefulTestCase.assertInstanceOf(resolved, GrParameter.class); + } + + public void testDontResolveForVarOutsideOfFor() { + TestCase.assertNull(resolve("A.groovy")); + } + + public void testOperatorOverloading() { + UsefulTestCase.assertInstanceOf(resolve("A.groovy"), GrAccessorMethod.class); + } + + public void testResolveClosureOverloader() { + UsefulTestCase.assertInstanceOf(resolve("A.groovy"), GrAccessorMethod.class); + } + + public void testJavaLoggingTransform() { + myFixture.addClass("package groovy.util.logging; public @interface Log { String value() default \"\"; }"); + PsiReference ref = configureByText("@groovy.util.logging.Log class Foo { { log.inf } }"); + assertEquals("java.util.logging.Logger", + UsefulTestCase.assertInstanceOf(ref.resolve(), PsiVariable.class).getType().getCanonicalText()); + } + + public void testNonLoggingField() { + myFixture.addClass("package groovy.util.logging; public @interface Log { String value() default \"\"; }"); + assertNull(configureByText("@groovy.util.logging.Log class Foo { { alog.inf } }").resolve()); + } + + public void testJavaLoggingTransformCustomName() { + myFixture.addClass("package groovy.util.logging; public @interface Log { String value() default \"\"; }"); + PsiReference ref = configureByText("@groovy.util.logging.Log('myLog') class Foo { { myLog.inf } }"); + assertEquals("java.util.logging.Logger", + UsefulTestCase.assertInstanceOf(ref.resolve(), PsiVariable.class).getType().getCanonicalText()); + } + + public void testCommonsLoggingTransform() { + myFixture.addClass("package groovy.util.logging; public @interface Commons { String value() default \"\"; }"); + PsiReference ref = configureByText("@groovy.util.logging.Commons('myLog') class Foo { { myLog.inf } }"); + assertEquals("org.apache.commons.logging.Log", + UsefulTestCase.assertInstanceOf(ref.resolve(), PsiVariable.class).getType().getCanonicalText()); + } + + public void testFieldTransform() { + addGroovyTransformField(); + PsiReference ref = configureByText(""" + @groovy.transform.Field def aaa = 2 + def foo() { println aaa } + \s"""); + assertTrue(ref.resolve() instanceof GrVariable); + } + + public void testScriptFieldVariableOutside() { + myFixture.addFileToProject("Foo.groovy", "if (true) { @groovy.transform.Field def a = 2 }"); + + PsiReference ref = configureByText("println new Foo().a"); + assertTrue(ref.resolve() instanceof GrVariable); + } + + public void testScriptVariableFromScriptMethod() { + PsiReference ref = configureByText(""" + def aaa = 2 + def foo() { println aaa } + """); + assertNull(ref.resolve()); + } + + public void testConfigObject() { + PsiReference ref = configureByText(""" + def config = new ConfigObject() + print config.configFile + """); + assertNotNull(ref.resolve()); + } + + public void testResolveInsideWith0() { + PsiElement resolved = resolve("a.groovy", GrAccessorMethod.class); + assertEquals("A", ((PsiMember)resolved).getContainingClass().getName()); + } + + public void testResolveInsideWith1() { + PsiElement resolved = resolve("a.groovy", GrAccessorMethod.class); + assertEquals("B", ((PsiMember)resolved).getContainingClass().getName()); + } + + public void testLocalVarVsFieldInWithClosure() { + // TODO disableTransformations getTestRootDisposable() + PsiReference ref = configureByText(""" + class Test { + def var + } + int var = 4 + new Test().with() { + print var + } + """); + TestCase.assertFalse(ref.resolve() instanceof GrField); + TestCase.assertTrue(ref.resolve() instanceof GrVariable); + } + + public void testCapitalizedProperty1() { + PsiReference ref = configureByText(""" + class A { + def Prop + } + + new A().Prop + """); + TestCase.assertNotNull(ref.resolve()); + } + + public void testCapitalizedProperty2() { + PsiReference ref = configureByText(""" + class A { + def Prop + } + + new A().prop + """); + TestCase.assertNotNull(ref.resolve()); + } + + public void testCapitalizedProperty3() { + PsiReference ref = configureByText(""" + class A { + def prop + } + + new A().Prop + """); + TestCase.assertNull(ref.resolve()); + } + + public void testCapitalizedProperty4() { + PsiReference ref = configureByText(""" + class A { + def prop + } + + new A().prop + """); + TestCase.assertNotNull(ref.resolve()); + } + + public void testGetChars() { + PsiReference ref = configureByText(""" + 'abc'.chars + """); + PsiElement resolved = ref.resolve(); + UsefulTestCase.assertInstanceOf(resolved, GrGdkMethod.class); + PsiMethod method = ((GrGdkMethod)resolved).getStaticMethod(); + + TestCase.assertEquals("java.lang.CharSequence", method.getParameterList().getParameters()[0].getType().getCanonicalText()); + } + + public void testLocalVarNotAvailableInClass() { + PsiReference ref = configureByText(""" + def aa = 5 + + class Inner { + def foo() { + print aa + } + } + """); + TestCase.assertNull(ref.resolve()); + } + + public void testLocalVarNotAvailableInMethod() { + PsiReference ref = configureByText(""" + def aa = 5 + + def foo() { + print aa + } + """); + TestCase.assertNull(ref.resolve()); + } + + public void testScriptFieldNotAvailableInClass() { + addGroovyTransformField(); + PsiReference ref = configureByText(""" + import groovy.transform.Field + + @Field + def aa = 5 + + class X { + def foo() { + print aa + } + } + """); + TestCase.assertNull(ref.resolve()); + } + + public void testInitializerOfScriptField() { + addGroovyTransformField(); + PsiReference ref = configureByText(""" + import groovy.transform.Field + + def xx = 5 + @Field + def aa = 5 + xx + """); + TestCase.assertNull(ref.resolve()); + } + + public void testInitializerOfScriptField2() { + addGroovyTransformField(); + PsiReference ref = configureByText(""" + import groovy.transform.Field + + @Field + def xx = 5 + + @Field + def aa = 5 + xx + """); + UsefulTestCase.assertInstanceOf(ref.resolve(), GrVariable.class); + } + + public void testAliasedImportedPropertyWithGetterInAlias() { + myFixture.addFileToProject("Foo.groovy", """ + class Foo { + static def prop = 2 + } + """); + + PsiReference ref = configureByText(""" + import static Foo.getProp as getOther + + print other + """); + + UsefulTestCase.assertInstanceOf(ref.resolve(), GrAccessorMethod.class); + } + + public void testPrefLocalVarToScriptName() { + myFixture.addFileToProject("foo.groovy", "print 2"); + + PsiReference ref = configureByText(""" + class Bar { + public float FOO = 1.23f + public float ZZZ = 5.78f + public float pi = 3.14f + public float xxx = 6f + + void doSmth() {} + } + + class User { + public static void main(String[] args) { + Bar foo = new Bar() + foo.bar() + println foo.FOO * foo.ZZZ * foo.pi * foo.xxx + foo.doSmth() + } + } + """); + + UsefulTestCase.assertInstanceOf(ref.resolve(), GrField.class); + } + + public void testResolveEnumConstantInsideItsInitializer() { + PsiReference ref = configureByText(""" + enum MyEnum { + CONST { + void get() { + CONST + } + } + } + """); + TestCase.assertNotNull(ref); + } + + public void testSpreadWithIterator() { + final PsiReference ref = configureByText(""" + class Person { String name } + class Twin { + Person one, two + def iterator() { + return [one, two].iterator() + } + } + + def tw = new Twin(one: new Person(name:'Tom'), + two: new Person(name:'Tim')) + assert tw*.name == ['Tom', 'Tim'] + """); + + final PsiMember resolved = UsefulTestCase.assertInstanceOf(ref.resolve(), PsiMember.class); + TestCase.assertNotNull(resolved.getContainingClass()); + TestCase.assertEquals("Person", resolved.getContainingClass().getName()); + } + + public void testAutoSpread() { + PsiReference ref = configureByText(""" + class A { + String getString() {return "a";} + } + + println ([[new A()]].string) + """); + + final PsiElement resolved = ref.resolve(); + + TestCase.assertNotNull(resolved); + } + + public void testImportedProperty() { + myFixture.addFileToProject("pack/Const.groovy", """ + package pack + + class Const { + static final int Field1 = 2 + } + """); + resolveByText(""" + import static pack.Const.getField1 + + print Field1 + """, null); + } + + public void testImportedProperty2() { + myFixture.addFileToProject("pack/Const.groovy", """ + package pack + + class Const { + static final int Field1 = 2 + } + """); + + resolveByText(""" + import static pack.Const.getField1 + + print field1 + """, GrAccessorMethod.class); + } + + public void testImportedProperty3() { + myFixture.addFileToProject("pack/Const.groovy", """ + package pack + + class Const { + static final int Field1 = 2 + } + """); + + resolveByText(""" + import static pack.Const.getField1 as getBar + + print Bar + """, null); + } + + public void testImportedProperty4() { + myFixture.addFileToProject("pack/Const.groovy", """ + package pack + + class Const { + static final int Field1 = 2 + } + """); + + resolveByText(""" + import static pack.Const.getField1 as getBar + + print bar + """, GrAccessorMethod.class); + } + + public void testImportedProperty5() { + myFixture.addFileToProject("pack/Const.groovy", """ + package pack + + class Const { + static final int field1 = 2 + } + """); + + resolveByText(""" + import static pack.Const.getField1 + + print Field1 + """, null); + } + + public void testLocalVarVsClassFieldInAnonymous() { + ThrowingTransformation.disableTransformations(getTestRootDisposable()); + GrVariable resolved = resolveByText(""" + class A { + public foo + } + + def foo = 4 + + new A() { + def foo() { + print foo + } + } + """, GrVariable.class); + assertFalse(resolved instanceof PsiField); + } + + public void testInterfaceDoesNotResolveWithExpressionQualifier() { + PsiReference ref = configureByText(""" + class Foo { + interface Inner { + } + + public Inner = 5 + } + + new Foo().Inner + """); + + UsefulTestCase.assertInstanceOf(ref.resolve(), PsiField.class); + } + + public void testInterfaceDoesNotResolveWithExpressionQualifier2() { + PsiReference ref = configureByText(""" + class Foo { + interface Inner { + } + + public Inner = 5 + } + + def foo = new Foo() + print foo.Inner + """); + + UsefulTestCase.assertInstanceOf(ref.resolve(), PsiField.class); + } + + public void testResolveBinding1() { + resolveByText(""" + abc = 4 + + print abc + """, GrBindingVariable.class); + } + + public void ignoreTestResolveBinding2() { + resolveByText(""" + print abc + + abc = 4 + """, GrBindingVariable.class); + } + + public void testResolveBinding3() { + resolveByText(""" + abc = 4 + + print abc + """, GrBindingVariable.class); + } + + public void testResolveBinding4() { + resolveByText(""" + print abc + + abc = 4 + """, GrBindingVariable.class); + } + + public void testResolveBinding5() { + resolveByText(""" + def foo() { + abc = 4 + } + + def bar() { + print abc + } + """, null); + } + + public void testResolveBinding6() { + resolveByText(""" + def foo() { + print abc + } + + def bar() { + abc = 4 + } + """, null); + } + + public void testResolveBinding7() { + resolveByText(""" + def foo() { + abc = 4 + } + + def bar() { + print abc + } + """, null); + } + + public void testResolveBinding8() { + resolveByText(""" + def foo() { + print abc + } + + def bar() { + abc = 4 + } + """, null); + } + + public void testBinding9() { + resolveByText(""" + aa = 5 + print aa + aa = 6 + print aa + """, GrBindingVariable.class); + } + + public void testBinding10() { + resolveByText(""" + aa = 5 + print aa + aa = 6 + print aa + """, GrBindingVariable.class); + } + + public void testBinding11() { + resolveByText(""" + aa = 5 + print aa + aa = 6 + print aa + """, GrBindingVariable.class); + } + + public void testBinding12() { + resolveByText(""" + aa = 5 + print aa + aa = 6 + print aa + """, GrBindingVariable.class); + } + + public void testBinding13() { + resolveByText(""" + aaa = 1 + + def foo() { + aaa + } + """, GrBindingVariable.class); + } + + public void testBinding14() { + resolveByText(""" + def foo() { + aaa + } + + aaa = 1 + """, null); + } + + public void testVarVsPackage1() { + myFixture.addClass("package p; public class A {}"); + resolveByText(""" + def p = [A:5] + + print p.A + """, PsiPackage.class); + } + + public void testVarVsPackage2() { + myFixture.addClass("package p; public class A {}"); + ThrowingTransformation.disableTransformations(getTestRootDisposable()); + + resolveByText(""" + def p = [A:5] + + print p + """, GrVariable.class); + } + + public void testVarVsPackage3() { + myFixture.addClass("package p; public class A {}"); + ThrowingTransformation.disableTransformations(getTestRootDisposable()); + + resolveByText(""" + def p = [A:{2}] + + print p.A() + """, GrVariable.class); + } + + public void testVarVsPackage4() { + myFixture.addClass("package p; public class A {public static int foo(){return 2;}}"); + + resolveByText(""" + def p = [A:[foo:{-2}]] + + print p.A.foo() + """, PsiPackage.class); + } + + public void testVarVsClass1() { + myFixture.addClass("package p; public class A {public static int foo() {return 1;}}"); + ThrowingTransformation.disableTransformations(getTestRootDisposable()); + + resolveByText(""" + import p.A + def A = [a:{-1}] + print A + """, GrVariable.class); + } + + public void testVarVsClass2() { + myFixture.addClass("package p; public class A {public static int foo() {return 1;}}"); + ThrowingTransformation.disableTransformations(getTestRootDisposable()); + + resolveByText(""" + import p.A + def A = [a:{-1}] + print A.a() + """, GrVariable.class); + } + + public void testTraitPublicField1() { + resolveByText(""" + trait T { + public int field = 4 + } + + class C implements T { + + void foo() { + print T__field + } + } + """, GrField.class); + } + + public void testTraitPublicField2() { + resolveByText(""" + trait T { + public int field = 4 + + void foo() { + print field + } + } + """, GrField.class); + } + + public void testTraitPublicField3() { + resolveByText(""" + trait T { + public int field = 4 + + void foo() { + print T__field + } + } + """, GrField.class); + } + + public void testTraitPublicField4() { + resolveByText(""" + trait T { + public int field = 4 + } + + class C implements T {} + + new C().T__field + """, GrField.class); + } + + public void testTraitProperty1() { + resolveByText(""" + trait T { + int prop = 4 + } + + class C extends T {} + + new C().prop + """, GrTraitMethod.class); + } + + public void testTraitProperty2() { + resolveByText(""" + trait T { + int prop = 4 + } + + class C extends T { + def bar() { + print prop + } + } + """, GrTraitMethod.class); + } + + public void testTraitProperty3() { + resolveByText(""" + trait T { + int prop = 4 + + void foo() { + print prop + } + } + """, GrField.class); + } + + public void testTraitPropertyFromAsOperator1() { + resolveByText(""" + trait A { + def foo = 5 + } + class B { + def bar() {} + } + + def v = new B() as A + print v.foo + """, GrAccessorMethod.class); + } + + public void testTraitPropertyFromAsOperator2() { + resolveByText(""" + trait A { + public foo = 5 + } + class B { + def bar() {} + } + + def v = new B() as A + print v.A__foo + """, GrField.class); + } + + public void testTraitField1() { + resolveByText(""" + trait T { + public foo = 4 + } + + class X implements T{ + def bar() { + print foo } - }""") - def reference = myFixture.file.findReferenceAt(myFixture.editor.caretModel.offset) - assertInstanceOf(reference.resolve(), GrField.class) + } + """, null); } - void testOverriddenGetter() throws Throwable { - myFixture.configureByText("a.groovy", """interface Foo { - def getFoo() - } - interface Bar extends Foo { - def getFoo() - } - - Bar b - b.foo""") - def reference = myFixture.file.findReferenceAt(myFixture.editor.caretModel.offset) - assertEquals("Bar", assertInstanceOf(reference.resolve(), GrMethod.class).containingClass.name) + public void testTraitField2() { + resolveByText(""" + trait T { + public foo + + def bar() { + print foo + } + } + """, PsiField.class); } - void testIDEADEV40403() { - myFixture.configureByFile("IDEADEV40403/A.groovy") - def reference = findReference() - def resolved = reference.resolve() - def clazz = assertInstanceOf(resolved, PsiMethod).containingClass - assertEquals "Script", clazz.name + public void testSetterOverloading() { + PsiMethod method = resolveByText(""" + class A { + void setFoo(File f) {} + + void setFoo(String s) {} + } + + new A().foo = "" + """, GrMethod.class); + assertEquals("java.lang.String", method.getParameterList().getParameters()[0].getType().getCanonicalText()); } - void testBooleanGetterPropertyAccess() { - myFixture.configureByText("a.groovy", "print([].empty)") - def ref = findReference() - def resolved = ref.resolve() - assertInstanceOf resolved, PsiMethod + public void testOnDemandStaticImport() { + myFixture.addClass(""" + package somepackage; + public class Holder { + public static Object getStuff() { return new Object(); } + } + """); + + PsiMethod method = resolveByText(""" + import static somepackage.Holder.* + class Foo { + static field = stuff + } + """, PsiMethod.class); + assertEquals("getStuff", method.getName()); } - def findReference() { myFixture.file.findReferenceAt(myFixture.editor.caretModel.offset) } + public void testPreferLocalOverMapKey() { + ThrowingTransformation.disableTransformations(getTestRootDisposable()); - void testTriplePropertyUsages() throws Exception { - myFixture.configureByText "a.groovy", """ -class Foo { - def bar - def zoo = bar -} -""" - def ref = findReference() - GrField target = assertInstanceOf(ref.resolve(), GrField) - assertTrue ref.isReferenceTo(target) - assertFalse ref.isReferenceTo(target.getters[0]) - assertFalse ref.isReferenceTo(target.setter) + resolveByText("def abc = 42; [:].with { abc }", GrVariable.class); } - void testAliasedStaticImport() throws Exception { - myFixture.addClass """ class Main { - static def foo=4 -""" - - myFixture.configureByText "a.groovy", """ -import static Main.foo as bar -print bar -} -""" - def ref = findReference() - def target = assertInstanceOf(ref.resolve(), PsiField) - assertEquals target.name, "foo" + public void testClassPropertyVsInstanceProperty() { + PsiMethod property = resolveByText(""" + class A { + int getName() { 42 } + } + + println A.name + """, PsiMethod.class); + assertEquals("java.lang.Class", property.getContainingClass().getQualifiedName()); } - private void doTest(String fileName) throws Exception { - PsiReference ref = configureByFile(fileName) - PsiElement resolved = ref.resolve() - assertTrue(resolved instanceof GrVariable) + public void testDontResolveToFieldInMethodCall() { + resolveByText(""" + class Fff { + List testThis = [""] + def usage() { testThis(1) } + def testThis(a) {} + } + """, GrMethod.class); } - private void doUndefinedVarTest(String fileName) throws Exception { - PsiReference ref = configureByFile(fileName) - PsiElement resolved = ref.resolve() - assertInstanceOf(resolved, GrBindingVariable) + public void testStaticFieldViaClassInstance() { + resolveByText(""" + class A { public static someStaticField = 42 } + def a = A // class instance + a.someStaticField + """, GrField.class); } - void testBooleanProperty() throws Exception { - myFixture.configureByText("Abc.groovy", """class A{ - boolean getFoo(){return true} - boolean isFoo(){return false} - } - print new A().foo""") - def ref = findReference() - def resolved = ref.resolve() - assertNotNull resolved - assert ((PsiMethod)resolved).name == "getFoo" + public void testGPathInArray() { + resolveByText(""" + class A { + int foo + A(int x) { foo = x } + } + + A[] ai = [new A(1), new A(2)].toArray() + + println(ai.foo) + """, GrAccessorMethod.class); } - void testExplicitBooleanProperty() throws Exception { - myFixture.configureByText("Abc.groovy", """class A{ - boolean foo - } - print new A().foo""") - def ref = findReference() - def resolved = ref.resolve() - assert ((PsiMethod)resolved).name == "getFoo" + public void testGPathInArray2() { + resolveByText(""" + class A { + int length + A(int x) { length = x } + } + + A[] ai = [new A(1), new A(2)].toArray() + + println(ai.length) + """, GrField.class); } - void testStaticFieldAndNonStaticGetter() { - myFixture.configureByText("Abc.groovy", "print Float.NaN") - def ref = findReference() - def resolved = ref.resolve() - assertInstanceOf resolved, PsiField.class - } - - void testPropertyAndFieldDeclarationInsideClass() { - myFixture.configureByText("a.groovy", """class Foo { - def foo - public def foo - - def bar() { - print foo - } -}""") - def ref = findReference() - def resolved = ref.resolve() - assertInstanceOf resolved, GrField - assertTrue((resolved as GrField).modifierList.hasExplicitVisibilityModifiers()) - } - - void testPropertyAndFieldDeclarationOutsideClass() { - myFixture.configureByText("a.groovy", """class Foo { - def foo - public def foo - - def bar() { - print foo - } -} -print new Foo().foo""") - def ref = findReference() - def resolved = ref.resolve() - assertInstanceOf resolved, GrAccessorMethod.class - } - - void testPropertyAndFieldDeclarationWithSuperClass1() { - myFixture.configureByText("a.groovy", """ -class Bar{ - def foo -} -class Foo extends Bar { - public def foo - - def bar() { - print foo - } -} -print new Foo().foo""") - def ref = findReference() - def resolved = ref.resolve() - assertInstanceOf resolved, GrAccessorMethod.class - } - - void testPropertyAndFieldDeclarationWithSuperClass2() { - myFixture.configureByText("a.groovy", """ -class Bar{ - def foo -} -class Foo extends Bar { - public def foo - - def bar() { - print foo - } -} -print new Foo().foo""") - def ref = findReference() - def resolved = ref.resolve() - assertInstanceOf resolved, GrField - assertTrue((resolved as GrField).modifierList.hasExplicitVisibilityModifiers()) - } - - void testPropertyAndFieldDeclarationWithSuperClass3() { - myFixture.configureByText("a.groovy", """ -class Bar{ - public def foo -} -class Foo extends Bar { - def foo - - def bar() { - print foo - } -} -print new Foo().foo""") - def ref = findReference() - def resolved = ref.resolve() - assertInstanceOf resolved, GrAccessorMethod.class - } - - void testPropertyAndFieldDeclarationWithSuperClass4() { - myFixture.configureByText("a.groovy", """ -class Bar{ - public def foo -} -class Foo extends Bar { - def foo - - def bar() { - print foo - } -} -print new Foo().foo""") - def ref = findReference() - def resolved = ref.resolve() - assertInstanceOf resolved, GrField - assertTrue(!(resolved as GrField).modifierList.hasExplicitVisibilityModifiers()) - } - - void testReadAccessToStaticallyImportedProperty() { - - myFixture.addFileToProject("a.groovy", """class Foo { - static def bar -}""") - myFixture.configureByText("b.groovy", """import static Foo.bar -print bar -""") - def ref = findReference() - def resolved = ref.resolve() - assertInstanceOf resolved, GrAccessorMethod.class - } - - void testWriteAccessToStaticallyImportedProperty() { - myFixture.addFileToProject("a.groovy", """class Foo { - static def bar -}""") - myFixture.configureByText("b.groovy", """import static Foo.bar -bar = 2 -""") - def ref = findReference() - def resolved = ref.resolve() - assertInstanceOf resolved, GrAccessorMethod.class - } - - void testGetterToStaticallyImportedProperty() { - myFixture.addFileToProject("a.groovy", """class Foo { - static def bar -}""") - myFixture.configureByText("b.groovy", """import static Foo.bar -setBar(2) -""") - def ref = findReference() - def resolved = ref.resolve() - assertInstanceOf resolved, GrAccessorMethod - } - - void testPropertyInCallExpression() { - myFixture.configureByText("a.groovy", """ -class Foo { - def foo = { - return {int i -> print i} - } - - def foo(String s){ - print s - } -} -new Foo().foo(2)""" - ) - def ref = findReference() - def resolved = ref.resolve() - - assertInstanceOf resolved, GrAccessorMethod - } - - void 'test property vs field in call from outside'() { - def method = resolveByText '''\ -class C { - def foo = { 42 } - def getFoo() { return { 43 } } -} -new C().foo(2) -''', GrMethod - assert !(method instanceof GrAccessorMethod) - } - - void testPropertyImportedOnDemand() { - myFixture.addFileToProject("foo/A.groovy", 'package foo; class Foo {static def foo}') - myFixture.configureByText("B.groovy", """package foo -import static Foo.* -print foo""") - - def ref = findReference() - def resolved = ref.resolve() - assertInstanceOf(resolved, GrAccessorMethod) - } - - void testSetterToAliasedImportedProperty() { - myFixture.addFileToProject("a.groovy", """class Foo { - static def bar -}""") - myFixture.configureByText("b.groovy", """import static Foo.bar as foo -setFoo(2) -""") - def ref = findReference() - def resolved = ref.resolve() - assertInstanceOf resolved, GrAccessorMethod - } - - void testPhysicalSetterToStaticallyImportedProperty() { - myFixture.addFileToProject("a.groovy", """class Foo { - static def bar - static def setBar(def bar){} -}""") - myFixture.configureByText("b.groovy", """import static Foo.bar as foo -setFoo(2) -""") - def ref = findReference() - def resolved = ref.resolve() - assertInstanceOf resolved, GrMethod - } - - void testPropertyUseInCategory() { - PsiReference ref = configureByFile("propertyUseInCategory/a.groovy") - PsiElement resolved = ref.resolve() - assertInstanceOf resolved, PsiMethod - } - - void testCollectionItemFields() { - PsiReference ref = configureByFile("collectionItemFields/A.groovy") - PsiElement resolved = ref.resolve() - assertInstanceOf resolved, PsiField - } - - void testFieldAccessInStaticContext() { - def ref = configureByFile("fieldAccessInStaticContext/A.groovy") - def resolveResult = (ref as GrReferenceExpression).advancedResolve() - assertFalse resolveResult.staticsOK - } - - void testFieldAccessInClosureVsStaticContext() { - def ref = configureByFile("fieldAccessInClosureVsStaticContext/A.groovy") - def resolveResult = (ref as GrReferenceExpression).advancedResolve() - assertTrue resolveResult.staticsOK - } - - void testUpperCaseFieldAndGetter() { - assertTrue resolve("A.groovy") instanceof GrField - } - - void testUpperCaseFieldWithoutGetter() { - assertInstanceOf(resolve("A.groovy"), GrField) - } - - void testGetterWithUpperCaseFieldReference() { - assertNull resolve("A.groovy") - } - - void testCommandExpressions() { - assertInstanceOf resolve("A.groovy"), GrAccessorMethod - } - - void testStaticFieldOfInterface() { - final GroovyResolveResult result = advancedResolve("A.groovy") - assertTrue result.staticsOK - } - - void testNonStaticFieldInStaticContext() { - final GroovyResolveResult result = advancedResolve("A.groovy") - assertFalse result.staticsOK - } - - void testPropertyInExprStatement() { - def result = resolve("A.groovy") - assertInstanceOf result, GrAccessorMethod - } - - void testPreferAlias() { - myFixture.addFileToProject "a/B.groovy", "package a; class B {public static def f1; public static def f2}" - assertEquals 'f2', ((GrField)resolve("A.groovy")).name - } - - void testF1property() { - assertInstanceOf resolve("A.groovy"), GrAccessorMethod - } - - void testAnonymousClassFieldAndLocalVar() { - disableTransformations testRootDisposable - final PsiElement resolved = resolve("A.groovy") - assertInstanceOf resolved, PsiVariable - assertTrue PsiUtil.isLocalVariable(resolved) - } - - void _testResolveForVarOutsideOfFor() { - final PsiElement resolved = resolve("A.groovy") - assertInstanceOf resolved, GrParameter - } - - void testDontResolveForVarOutsideOfFor() { - assertNull resolve("A.groovy") - } - - void testOperatorOverloading() { - assertInstanceOf resolve("A.groovy"), GrAccessorMethod - } - - void testResolveClosureOverloader() { - assertInstanceOf resolve("A.groovy"), GrAccessorMethod - } - - void testJavaLoggingTransform() { - myFixture.addClass('package groovy.util.logging; public @interface Log { String value() default ""; }') - def ref = configureByText("@groovy.util.logging.Log class Foo { { log.inf } }") - assert assertInstanceOf(ref.resolve(), PsiVariable).type.canonicalText == 'java.util.logging.Logger' - } - - void testNonLoggingField() { - myFixture.addClass('package groovy.util.logging; public @interface Log { String value() default ""; }') - assert !configureByText("@groovy.util.logging.Log class Foo { { alog.inf } }").resolve() - } - - void testJavaLoggingTransformCustomName() { - myFixture.addClass('package groovy.util.logging; public @interface Log { String value() default ""; }') - def ref = configureByText("@groovy.util.logging.Log('myLog') class Foo { { myLog.inf } }") - assert assertInstanceOf(ref.resolve(), PsiVariable).type.canonicalText == 'java.util.logging.Logger' - } - - void testCommonsLoggingTransform() { - myFixture.addClass('package groovy.util.logging; public @interface Commons { String value() default ""; }') - def ref = configureByText("@groovy.util.logging.Commons('myLog') class Foo { { myLog.inf } }") - assert assertInstanceOf(ref.resolve(), PsiVariable).type.canonicalText == 'org.apache.commons.logging.Log' - } - - void testFieldTransform() { - addGroovyTransformField() - def ref = configureByText("""@groovy.transform.Field def aaa = 2 -def foo() { println aaa } -""") - assert ref.resolve() instanceof GrVariable - } - - void testScriptFieldVariableOutside() { - myFixture.addFileToProject("Foo.groovy", "if (true) { @groovy.transform.Field def a = 2 }") - - def ref = configureByText("println new Foo().a") - assert ref.resolve() instanceof GrVariable - } - - void testScriptVariableFromScriptMethod() { - def ref = configureByText("""def aaa = 2 -def foo() { println aaa } -""") - assert !ref.resolve() - } - - void testConfigObject() { - def ref = configureByText(''' -def config = new ConfigObject() -print config.configFile''') - assert ref.resolve() - } - - void testResolveInsideWith0() { - def resolved = resolve('a.groovy', GrAccessorMethod) - assertEquals(resolved.containingClass.name, 'A') - } - - void testResolveInsideWith1() { - def resolved = resolve('a.groovy', GrAccessorMethod) - assertEquals(resolved.containingClass.name, 'B') - } - - void testLocalVarVsFieldInWithClosure() { -// TODO disableTransformations testRootDisposable - def ref = configureByText('''\ -class Test { - def var -} - -int var = 4 -new Test().with() { - print var -} -''') - assertFalse ref.resolve() instanceof GrField - assertTrue ref.resolve() instanceof GrVariable - } - - void testCapitalizedProperty1() { - def ref = configureByText('''\ -class A { - def Prop -} - -new A().Prop''') - assertNotNull ref.resolve() - } - - void testCapitalizedProperty2() { - def ref = configureByText('''\ -class A { - def Prop -} - -new A().prop''') - assertNotNull ref.resolve() - } - - void testCapitalizedProperty3() { - def ref = configureByText('''\ -class A { - def prop -} - -new A().Prop''') - assertNull ref.resolve() - } - - void testCapitalizedProperty4() { - def ref = configureByText('''\ -class A { - def prop -} - -new A().prop''') - assertNotNull ref.resolve() - } - - void testGetChars() { - def ref = configureByText('''\ -'abc'.chars -''') - def resolved = ref.resolve() - assertInstanceOf(resolved, GrGdkMethod) - def method = resolved.staticMethod as PsiMethod - - assertEquals(method.parameterList.parameters[0].type.canonicalText, 'java.lang.CharSequence') - } - - void testLocalVarNotAvailableInClass() { - def ref = configureByText('''\ -def aa = 5 - -class Inner { - def foo() { - print aa - } -}''') - assertNull(ref.resolve()) - } - - void testLocalVarNotAvailableInMethod() { - def ref = configureByText('''\ -def aa = 5 - -def foo() { - print aa -}''') - assertNull(ref.resolve()) - } - - void testScriptFieldNotAvailableInClass() { - addGroovyTransformField() - def ref = configureByText('''\ -import groovy.transform.Field - -@Field -def aa = 5 - -class X { - def foo() { - print aa - } -}''') - assertNull(ref.resolve()) - } - - void testInitializerOfScriptField() { - addGroovyTransformField() - def ref = configureByText('''\ -import groovy.transform.Field - -def xx = 5 -@Field -def aa = 5 + xx -''') - assertNull(ref.resolve()) - } - - void testInitializerOfScriptField2() { - addGroovyTransformField() - def ref = configureByText('''\ -import groovy.transform.Field - -@Field -def xx = 5 - -@Field -def aa = 5 + xx -''') - assertInstanceOf(ref.resolve(), GrVariable) - } - - void testAliasedImportedPropertyWithGetterInAlias() { - myFixture.addFileToProject('Foo.groovy', '''\ -class Foo { - static def prop = 2 -} -''') - - def ref = configureByText('''\ -import static Foo.getProp as getOther - -print other -''') - - assertInstanceOf(ref.resolve(), GrAccessorMethod) - } - - void testPrefLocalVarToScriptName() { - myFixture.addFileToProject('foo.groovy', 'print 2') - - def ref = configureByText('''\ -class Bar { - public float FOO = 1.23f - public float ZZZ = 5.78f - public float pi = 3.14f - public float xxx = 6f - - void doSmth() {} -} - -class User { - public static void main(String[] args) { - Bar foo = new Bar() - foo.bar() - println foo.FOO * foo.ZZZ * foo.pi * foo.xxx - foo.doSmth() - } -} -''') - - assertInstanceOf(ref.resolve(), GrField) - } - - void testResolveEnumConstantInsideItsInitializer() { - def ref = configureByText('''\ -enum MyEnum { - CONST { - void get() { - CONST - } - } - -} -''') - assertNotNull(ref) - } - - void testSpreadWithIterator() { - final PsiReference ref = configureByText('''\ -class Person { String name } -class Twin { - Person one, two - def iterator() { - return [one, two].iterator() - } -} - -def tw = new Twin(one: new Person(name:'Tom'), - two: new Person(name:'Tim')) -assert tw*.name == ['Tom', 'Tim'] -''') - - final resolved = assertInstanceOf(ref.resolve(), PsiMember) - assertNotNull(resolved.containingClass) - assertEquals('Person', resolved.containingClass.name) - } - - void testAutoSpread() { - def ref = configureByText('''\ -class A { - String getString() {return "a";} -} - -println ([[new A()]].string) -''') - - final resolved = ref.resolve() - - assertNotNull(resolved) - } - - void testImportedProperty() { - myFixture.addFileToProject('pack/Const.groovy', '''\ -package pack - -class Const { - static final int Field1 = 2 -} -''') - resolveByText '''\ -import static pack.Const.getField1 - -print Field1 -''', null - } - - void testImportedProperty2() { - myFixture.addFileToProject('pack/Const.groovy', '''\ -package pack - -class Const { - static final int Field1 = 2 -} -''') - - resolveByText '''\ -import static pack.Const.getField1 - -print field1 -''', GrAccessorMethod - } - - void testImportedProperty3() { - myFixture.addFileToProject('pack/Const.groovy', '''\ -package pack - -class Const { - static final int Field1 = 2 -} -''') - - resolveByText '''\ -import static pack.Const.getField1 as getBar - -print Bar -''', null - } - - void testImportedProperty4() { - myFixture.addFileToProject('pack/Const.groovy', '''\ -package pack - -class Const { - static final int Field1 = 2 -} -''') - - resolveByText '''\ -import static pack.Const.getField1 as getBar - -print bar -''', GrAccessorMethod - } - - void testImportedProperty5() { - myFixture.addFileToProject('pack/Const.groovy', '''\ -package pack - -class Const { - static final int field1 = 2 -} -''') - - resolveByText '''\ -import static pack.Const.getField1 - -print Field1 -''', null - } - - void testLocalVarVsClassFieldInAnonymous() { - disableTransformations testRootDisposable - def resolved = resolveByText '''\ - class A { - public foo - } - - def foo = 4 - - new A() { - def foo() { - print foo - } - } -''', GrVariable - assert !(resolved instanceof PsiField) - } - - void testInterfaceDoesNotResolveWithExpressionQualifier() { - def ref = configureByText('''\ -class Foo { - interface Inner { - } - - public Inner = 5 -} - -new Foo().Inner -''') - - assertInstanceOf(ref.resolve(), PsiField) - } - - void testInterfaceDoesNotResolveWithExpressionQualifier2() { - def ref = configureByText('''\ -class Foo { - interface Inner { - } - - public Inner = 5 -} - -def foo = new Foo() -print foo.Inner -''') - - assertInstanceOf(ref.resolve(), PsiField) - } - - void testResolveBinding1() { - resolveByText('''\ -abc = 4 - -print abc -''', GrBindingVariable) - } - - void ignoreTestResolveBinding2() { - resolveByText('''\ -print abc - -abc = 4 -''', GrBindingVariable) - } - - void testResolveBinding3() { - resolveByText('''\ -abc = 4 - -print abc -''', GrBindingVariable) - } - - void testResolveBinding4() { - resolveByText('''\ -print abc - -abc = 4 -''', GrBindingVariable) - } - - - void testResolveBinding5() { - resolveByText '''\ -def foo() { - abc = 4 -} - -def bar() { - print abc -} -''', null - } - - void testResolveBinding6() { - resolveByText'''\ -def foo() { - print abc -} - -def bar() { - abc = 4 -} -''', null - } - - void testResolveBinding7() { - resolveByText '''\ -def foo() { - abc = 4 -} - -def bar() { - print abc -} -''', null - } - - void testResolveBinding8() { - resolveByText '''\ -def foo() { - print abc -} - -def bar() { - abc = 4 -} -''', null - } - - void testBinding9() { - resolveByText('''\ -aa = 5 -print aa -aa = 6 -print aa -''', GrBindingVariable) - } - - void testBinding10() { - resolveByText('''\ -aa = 5 -print aa -aa = 6 -print aa -''', GrBindingVariable) - } - - void testBinding11() { - resolveByText('''\ -aa = 5 -print aa -aa = 6 -print aa -''', GrBindingVariable) - } - - void testBinding12() { - resolveByText('''\ -aa = 5 -print aa -aa = 6 -print aa -''', GrBindingVariable) - } - - void testBinding13() { - resolveByText '''\ -aaa = 1 - -def foo() { - aaa -} -''', GrBindingVariable - } - - void testBinding14() { - resolveByText '''\ -def foo() { - aaa -} - -aaa = 1 -''', null - } - - void testVarVsPackage1() { - myFixture.addClass('''package p; public class A {}''') - - resolveByText('''\ - def p = [A:5] - - print p.A -''', PsiPackage) - } - - void testVarVsPackage2() { - myFixture.addClass('''package p; public class A {}''') - disableTransformations testRootDisposable - - resolveByText '''\ - def p = [A:5] - - print p -''', GrVariable - } - - void testVarVsPackage3() { - myFixture.addClass('''package p; public class A {}''') - disableTransformations testRootDisposable - - resolveByText '''\ - def p = [A:{2}] - - print p.A() -''', GrVariable - } - - void testVarVsPackage4() { - myFixture.addClass('''package p; public class A {public static int foo(){return 2;}}''') - - resolveByText('''\ - def p = [A:[foo:{-2}]] - - print p.A.foo() -''', PsiPackage) - } - - void testVarVsClass1() { - myFixture.addClass('package p; public class A {public static int foo() {return 1;}}') - disableTransformations testRootDisposable - - resolveByText '''\ -import p.A - -def A = [a:{-1}] - -print A -''', GrVariable - } - - void testVarVsClass2() { - myFixture.addClass('package p; public class A {public static int foo() {return 1;}}') - disableTransformations testRootDisposable - - resolveByText '''\ -import p.A - -def A = [a:{-1}] - -print A.a() -''', GrVariable - } - - void testTraitPublicField1() { - resolveByText(''' -trait T { - public int field = 4 -} - -class C implements T { - - void foo() { - print T__field - } -} -''', GrField) - } - - void testTraitPublicField2() { - resolveByText(''' -trait T { - public int field = 4 - - void foo() { - print field - } -} -''', GrField) - } - - void testTraitPublicField3() { - resolveByText(''' -trait T { - public int field = 4 - - void foo() { - print T__field - } -} -''', GrField) - } - - void testTraitPublicField4() { - resolveByText(''' -trait T { - public int field = 4 -} - -class C implements T {} - -new C().T__field -''', GrField) - } - - void testTraitProperty1() { - resolveByText(''' -trait T { - int prop = 4 -} - -class C extends T {} - -new C().prop -''', GrTraitMethod) - } - - void testTraitProperty2() { - resolveByText(''' -trait T { - int prop = 4 -} - -class C extends T { - def bar() { - print prop - } -} -''', GrTraitMethod) - } - - void testTraitProperty3() { - resolveByText(''' -trait T { - int prop = 4 - - void foo() { - print prop - } -} -''', GrField) - } - - void testTraitPropertyFromAsOperator1() { - resolveByText(''' -trait A { - def foo = 5 -} -class B { - def bar() {} -} - -def v = new B() as A -print v.foo -''', GrAccessorMethod) - } - - void testTraitPropertyFromAsOperator2() { - resolveByText(''' -trait A { - public foo = 5 -} -class B { - def bar() {} -} - -def v = new B() as A -print v.A__foo -''', GrField) - } - - void testTraitField1() { - resolveByText(''' - trait T { - public foo = 4 - } - - class X implements T{ - def bar() { - print foo - } - } -''', null) - } - - void testTraitField2() { - resolveByText(''' - trait T { - public foo - - def bar() { - print foo - } - } -''', PsiField) - } - - void 'test setter overloading'() { - def method = resolveByText('''\ -class A { - void setFoo(File f) {} - - void setFoo(String s) {} -} - -new A().foo = "" -''', GrMethod) - assert method.parameterList.parameters.first().type.canonicalText == 'java.lang.String' - } - - void 'test on demand static import'() { - fixture.with { - addClass ''' -package somepackage; -public class Holder { - public static Object getStuff() { return new Object(); } -} -''' - def method = resolveByText('''\ -import static somepackage.Holder.* -class Foo { - static field = stuff -} -''', PsiMethod) - assert method.name == 'getStuff' - } - } - - void 'test prefer local over map key'() { - disableTransformations testRootDisposable - resolveByText 'def abc = 42; [:].with { abc }', GrVariable - } - - void 'test Class property vs instance property'() { - def property = resolveByText '''\ -class A { - int getName() { 42 } -} - -println A.name -''', PsiMethod - assert property.containingClass.qualifiedName == 'java.lang.Class' - } - - void "test don't resolve to field in method call"() { - resolveByText '''\ -class Fff { - List testThis = [""] - def usage() { testThis(1) } - def testThis(a) {} -} -''', GrMethod - } - - void 'test static field via class instance'() { - resolveByText '''\ -class A { public static someStaticField = 42 } -def a = A // class instance -a.someStaticField -''', GrField - } - - void 'test gpath in array'() { - resolveByText """ -class A { - int foo - A(int x) { foo = x } -} - -A[] ai = [new A(1), new A(2)].toArray() - -println(ai.foo) -""", GrAccessorMethod - } - - void 'test gpath in array 2'() { - resolveByText """ -class A { - int length - A(int x) { length = x } -} - -A[] ai = [new A(1), new A(2)].toArray() - -println(ai.length) -""", GrField + @Override + public final String getBasePath() { + return TestUtils.getTestDataPath() + "resolve/property/"; } }