Allow to find usages of Java class via references to its constructor #KT-4521 Fixed

This commit is contained in:
Alexey Sedunov
2014-02-07 12:25:13 +04:00
parent 8b6bd8a184
commit d549257bfc
18 changed files with 138 additions and 17 deletions
@@ -1,18 +1,18 @@
/*
* Copyright 2010-2014 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 2010-2014 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.
*/
package org.jetbrains.jet.plugin.references
@@ -38,6 +38,7 @@ import com.intellij.util.containers.ContainerUtil
import org.jetbrains.jet.asJava.*
import org.jetbrains.jet.lang.psi.JetElement
import org.jetbrains.jet.utils.keysToMap
import com.intellij.psi.PsiMethod
public trait JetReference : PsiPolyVariantReference {
public fun resolveToDescriptors(): Collection<DeclarationDescriptor>
@@ -68,7 +69,13 @@ abstract class AbstractJetReference<T : JetElement>(element: T)
override fun bindToElement(element: PsiElement): PsiElement = throw IncorrectOperationException()
protected fun areElementsEquivalent(el1: PsiElement, el2: PsiElement): Boolean = el1.namedUnwrappedElement == el2.namedUnwrappedElement
protected fun checkElementMatch(referenceTarget: PsiElement, elementToMatch: PsiElement): Boolean {
val unwrappedTarget = referenceTarget.namedUnwrappedElement
val unwrappedElementToMatch = elementToMatch.namedUnwrappedElement
return (unwrappedTarget == unwrappedElementToMatch) ||
(referenceTarget is PsiMethod && referenceTarget.isConstructor() && referenceTarget.getContainingClass() == elementToMatch)
}
[suppress("CAST_NEVER_SUCCEEDS")]
override fun getVariants(): Array<Any> = PsiReference.EMPTY_ARRAY as Array<Any>
@@ -147,7 +154,7 @@ public abstract class JetSimpleReference<T : JetReferenceExpression>(expression:
if (resolvedElement == null || element == null) {
return false
}
return areElementsEquivalent(element, resolvedElement)
return checkElementMatch(resolvedElement, element)
}
}
@@ -156,6 +163,6 @@ public abstract class JetMultiReference<T : JetElement>(expression: T) : Abstrac
if (element == null) {
return false
}
return multiResolve(false).map { it.getElement() }.filterNotNull().any { areElementsEquivalent(it, element) }
return multiResolve(false).map { it.getElement() }.filterNotNull().any { checkElementMatch(it, element) }
}
}
@@ -4,6 +4,10 @@ public class <caret>A {
public String bar = "bar";
public static String BAR = "BAR";
public A() {
}
public void foo() {
}
@@ -1,5 +1,7 @@
// PSI_ELEMENT: com.intellij.psi.PsiClass
// OPTIONS: derivedClasses
public class <caret>A {
public A() {
}
}
@@ -1,5 +1,7 @@
// PSI_ELEMENT: com.intellij.psi.PsiClass
// OPTIONS: derivedClasses
public class <caret>A {
public A() {
}
}
@@ -1,5 +1,7 @@
// PSI_ELEMENT: com.intellij.psi.PsiClass
// OPTIONS: derivedClasses
public class <caret>A {
public A() {
}
}
@@ -1,5 +1,7 @@
// PSI_ELEMENT: com.intellij.psi.PsiClass
// OPTIONS: derivedClasses
public class <caret>A {
public A() {
}
}
@@ -1,5 +1,7 @@
// PSI_ELEMENT: com.intellij.psi.PsiClass
// OPTIONS: derivedClasses
public class <caret>A {
public A() {
}
}
@@ -1,5 +1,7 @@
// PSI_ELEMENT: com.intellij.psi.PsiClass
// OPTIONS: derivedClasses
public class <caret>A {
public A() {
}
}
@@ -1,5 +1,7 @@
// PSI_ELEMENT: com.intellij.psi.PsiClass
// OPTIONS: derivedClasses
public class <caret>A {
public A() {
}
}
@@ -4,6 +4,10 @@ public class <caret>A {
public String bar = "bar";
public static String BAR = "BAR";
public A() {
}
public void foo() {
}
@@ -4,6 +4,10 @@ public class <caret>A {
public String bar = "bar";
public static String BAR = "BAR";
public A() {
}
public void foo() {
}
@@ -1,2 +1,7 @@
Function call (20: 18) super<A>.foo()
Function call (7: 11) A.foos()
New instance creation (2: 20) var next: A? = A()
New instance creation (32: 18) fun X.bar(a: A = A()) {
Supertype (1: 39) public class X(bar: String? = A.BAR): A() {
Supertype (23: 19) class object: A() {
Supertype (28: 11) object O: A() {
@@ -0,0 +1,14 @@
// PSI_ELEMENT: com.intellij.psi.PsiClass
// OPTIONS: usages
public class <caret>A {
public String bar = "bar";
public static String BAR = "BAR";
public void foo() {
}
public static void foos() {
}
}
@@ -0,0 +1,38 @@
public class X(bar: String? = A.BAR): A() {
var next: A? = A()
val myBar: String? = A.BAR
{
A.BAR = ""
A.foos()
}
fun foo(a: A) {
val aa: A = a
aa.bar = ""
}
fun getNext(): A? {
return next
}
public override fun foo() {
super<A>.foo()
}
class object: A() {
}
}
object O: A() {
}
fun X.bar(a: A = A()) {
}
fun Any.toA(): A? {
return if (this is A) this as A else null
}
@@ -0,0 +1,18 @@
Class/object property type (2: 15) var next: A? = A()
Function return types (15: 20) fun getNext(): A? {
Function return types (36: 16) fun Any.toA(): A? {
Local variable declaration (11: 17) val aa: A = a
Nested class/object (1: 31) public class X(bar: String? = A.BAR): A() {
Nested class/object (3: 26) val myBar: String? = A.BAR
Nested class/object (6: 9) A.BAR = ""
Nested class/object (7: 9) A.foos()
New instance creation (2: 20) var next: A? = A()
New instance creation (32: 18) fun X.bar(a: A = A()) {
Parameter type (10: 16) fun foo(a: A) {
Parameter type (32: 14) fun X.bar(a: A = A()) {
Super type qualifier (20: 15) super<A>.foo()
Supertype (1: 39) public class X(bar: String? = A.BAR): A() {
Supertype (23: 19) class object: A() {
Supertype (28: 11) object O: A() {
Target type of 'is' operation (37: 24) return if (this is A) this as A else null
Usage in cast target type (37: 35) return if (this is A) this as A else null
@@ -4,6 +4,10 @@ public class Outer {
public class <caret>A {
public String bar = "bar";
public A() {
}
public void foo() {
}
@@ -5,6 +5,10 @@ public class Outer {
public String bar = "bar";
public static String BAR = "BAR";
public A() {
}
public void foo() {
}
@@ -631,6 +631,11 @@ public class JetFindUsagesTestGenerated extends AbstractJetFindUsagesTest {
doTest("idea/testData/findUsages/java/findJavaClassUsages/JKClassMethodsUsages.0.java");
}
@TestMetadata("JKClassWithImplicitConstructorAllUsages.0.java")
public void testJKClassWithImplicitConstructorAllUsages() throws Exception {
doTest("idea/testData/findUsages/java/findJavaClassUsages/JKClassWithImplicitConstructorAllUsages.0.java");
}
@TestMetadata("JKDerivedInterfaces.0.java")
public void testJKDerivedInterfaces() throws Exception {
doTest("idea/testData/findUsages/java/findJavaClassUsages/JKDerivedInterfaces.0.java");