[groovy] check element class and name before resolving code reference in GrCodeReferenceElementImpl#isReferenceTo

This commit is contained in:
Daniil Ovchinnikov
2018-07-25 14:34:34 +03:00
parent 89f472d1fc
commit 8733c7580a
2 changed files with 39 additions and 33 deletions
@@ -1,5 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. 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.psi.impl.types;
import com.intellij.lang.ASTNode;
@@ -15,7 +14,6 @@ import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.types.CodeReferenceKind;
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement;
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList;
@@ -163,14 +161,40 @@ public class GrCodeReferenceElementImpl extends GrReferenceElementImpl<GrCodeRef
@Override
public boolean isReferenceTo(PsiElement element) {
final PsiManager manager = getManager();
if (element instanceof PsiNamedElement && getParent() instanceof GrImportStatement) {
final GroovyResolveResult[] results = multiResolve(false);
for (GroovyResolveResult result : results) {
if (manager.areElementsEquivalent(result.getElement(), element)) return true;
}
switch (getKind()) {
case PACKAGE_REFERENCE:
return referencesPackage(element);
case REFERENCE:
return referencesPackage(element) || element instanceof PsiClass && resolvesTo(element);
case IMPORT_REFERENCE:
return element instanceof PsiMember &&
element instanceof PsiNamedElement &&
checkName((PsiNamedElement)element) &&
multiResolvesTo(element);
default:
throw new IllegalStateException();
}
return manager.areElementsEquivalent(element, resolve());
}
private boolean referencesPackage(@NotNull PsiElement element) {
return element instanceof PsiPackage && checkName((PsiNamedElement)element) && resolvesTo(element);
}
private boolean checkName(@NotNull PsiNamedElement namedElement) {
final String name = namedElement.getName();
final String referenceName = getReferenceName();
return referenceName != null && referenceName.equals(name);
}
private boolean resolvesTo(@NotNull PsiElement element) {
return getManager().areElementsEquivalent(element, resolve());
}
private boolean multiResolvesTo(@NotNull PsiElement element) {
final PsiManager manager = getManager();
return resolve(false).stream()
.map(it -> it.getElement())
.anyMatch(it -> manager.areElementsEquivalent(it, element));
}
@Override
@@ -1,18 +1,4 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2018 JetBrains s.r.o. 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
import com.intellij.psi.search.searches.MethodReferencesSearch
@@ -25,19 +11,13 @@ import org.jetbrains.plugins.groovy.LightGroovyTestCase
class LiteralConstructorUsagesTest extends LightGroovyTestCase {
void testList_AsCast() throws Exception {
def foo = myFixture.addClass("""class Foo {
Foo() {}
}
}""")
def foo = myFixture.addClass('class Foo { Foo() {} }')
myFixture.addFileToProject "a.groovy", "def x = [] as Foo"
assertOneElement(ReferencesSearch.search(foo.constructors[0]).findAll())
}
void testMap_AsCast() throws Exception {
def foo = myFixture.addClass("""class Foo {
Foo() {}
}
}""")
def foo = myFixture.addClass('class Foo { Foo() {} }')
myFixture.addFileToProject "a.groovy", "def x = [:] as Foo"
assertOneElement(ReferencesSearch.search(foo.constructors[0]).findAll())
}
@@ -53,6 +33,8 @@ import groovy.transform.Immutable
Money d = [amount: 100, currency:'USA']
"""
assertEquals 1, MethodReferencesSearch.search(myFixture.findClass("Money").constructors[0]).findAll().size()
def constructors = myFixture.findClass("Money").constructors
assertEquals 0, MethodReferencesSearch.search(constructors[0]).size()
assertEquals 1, MethodReferencesSearch.search(constructors[1]).size()
}
}