From 8733c7580a6127eb11ec4e51ef70165d9060c7d7 Mon Sep 17 00:00:00 2001 From: Daniil Ovchinnikov Date: Wed, 25 Jul 2018 14:33:58 +0300 Subject: [PATCH] [groovy] check element class and name before resolving code reference in GrCodeReferenceElementImpl#isReferenceTo --- .../types/GrCodeReferenceElementImpl.java | 42 +++++++++++++++---- .../lang/LiteralConstructorUsagesTest.groovy | 30 +++---------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/types/GrCodeReferenceElementImpl.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/types/GrCodeReferenceElementImpl.java index 8b5cf86dcafa..1153cc80a6e3 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/types/GrCodeReferenceElementImpl.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/types/GrCodeReferenceElementImpl.java @@ -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 it.getElement()) + .anyMatch(it -> manager.areElementsEquivalent(it, element)); } @Override diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/LiteralConstructorUsagesTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/LiteralConstructorUsagesTest.groovy index b56effac42ba..a2d975674ce1 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/LiteralConstructorUsagesTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/LiteralConstructorUsagesTest.groovy @@ -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() } }