From b406d85ca3241839f6c36393d1dba708c0bb938f Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Sun, 9 Feb 2020 23:21:01 +0200 Subject: [PATCH] Analyze Data Flow to Here: Java super-methods supported --- .../jetbrains/kotlin/idea/slicer/Slicer.kt | 41 ++++++++++--------- idea/testData/slicer/inflow/overrideFun.java | 3 ++ idea/testData/slicer/inflow/overrideFun.kt | 6 ++- .../slicer/inflow/overrideFun.leafGroups.txt | 5 +++ .../slicer/inflow/overrideFun.results.txt | 1 + 5 files changed, 36 insertions(+), 20 deletions(-) create mode 100644 idea/testData/slicer/inflow/overrideFun.java diff --git a/idea/src/org/jetbrains/kotlin/idea/slicer/Slicer.kt b/idea/src/org/jetbrains/kotlin/idea/slicer/Slicer.kt index 38a4ddc8161..b69503b9391 100644 --- a/idea/src/org/jetbrains/kotlin/idea/slicer/Slicer.kt +++ b/idea/src/org/jetbrains/kotlin/idea/slicer/Slicer.kt @@ -80,25 +80,6 @@ private fun KtDeclaration.processHierarchyUpward(scope: AnalysisScope, processor .forEach(processor) } -private fun KtFunction.processCalls(scope: SearchScope, exactFunctionOnly: Boolean, processor: (UsageInfo) -> Unit) { - val options = KotlinFunctionFindUsagesOptions(project).apply { - isSearchForTextOccurrences = false - isSkipImportStatements = true - searchScope = scope.intersectWith(useScope) - } - if (exactFunctionOnly) { - processAllExactUsages(options, processor) - } else { - val descriptor = unsafeResolveToDescriptor() as? CallableMemberDescriptor ?: return - for (superDescriptor in descriptor.getDeepestSuperDeclarations()) { - when (val declaration = superDescriptor.originalSource.getPsi()) { - is KtDeclaration -> declaration.processAllUsages(options, processor) - else -> {} //TODO - } - } - } -} - private enum class AccessKind { READ_ONLY, WRITE_ONLY, WRITE_WITH_OPTIONAL_READ, READ_OR_WRITE } @@ -150,6 +131,28 @@ abstract class Slicer( } abstract fun processChildren() + + protected fun KtFunction.processCalls(scope: SearchScope, exactFunctionOnly: Boolean, usageProcessor: (UsageInfo) -> Unit) { + val options = KotlinFunctionFindUsagesOptions(project).apply { + isSearchForTextOccurrences = false + isSkipImportStatements = true + searchScope = scope.intersectWith(useScope) + } + if (exactFunctionOnly) { + processAllExactUsages(options, usageProcessor) + } else { + val descriptor = unsafeResolveToDescriptor() as? CallableMemberDescriptor ?: return + for (superDescriptor in descriptor.getDeepestSuperDeclarations()) { + val declaration = superDescriptor.originalSource.getPsi() ?: continue + when (declaration) { + is KtDeclaration -> declaration.processAllUsages(options, usageProcessor) + // todo: work around the bug in JavaSliceProvider.transform() + is PsiMethod -> processor.process(JavaSliceUsage.createRootUsage(declaration, parentUsage.params)) + else -> declaration.passToProcessor() + } + } + } + } } class InflowSlicer( diff --git a/idea/testData/slicer/inflow/overrideFun.java b/idea/testData/slicer/inflow/overrideFun.java new file mode 100644 index 00000000000..33ab0721ed1 --- /dev/null +++ b/idea/testData/slicer/inflow/overrideFun.java @@ -0,0 +1,3 @@ +interface JavaInterface { + void foo(int p); +} \ No newline at end of file diff --git a/idea/testData/slicer/inflow/overrideFun.kt b/idea/testData/slicer/inflow/overrideFun.kt index 69091246e22..34c14f9e154 100644 --- a/idea/testData/slicer/inflow/overrideFun.kt +++ b/idea/testData/slicer/inflow/overrideFun.kt @@ -4,7 +4,7 @@ interface I { fun foo(p: Int) } -class C : I { +class C : I, JavaInterface { override fun foo(p: Int) { println(p) } @@ -17,3 +17,7 @@ class C : I { fun f(i: I) { i.foo(2) } + +fun g(i: JavaInterface) { + i.foo(3) +} diff --git a/idea/testData/slicer/inflow/overrideFun.leafGroups.txt b/idea/testData/slicer/inflow/overrideFun.leafGroups.txt index 84ceab6cb70..d7057d81bbe 100644 --- a/idea/testData/slicer/inflow/overrideFun.leafGroups.txt +++ b/idea/testData/slicer/inflow/overrideFun.leafGroups.txt @@ -1,3 +1,8 @@ +2 void foo(int p); +9 println(p) +8 override fun foo(p: Int) { +2 void foo(int p); + 13 foo(1) 9 println(p) 8 override fun foo(p: Int) { diff --git a/idea/testData/slicer/inflow/overrideFun.results.txt b/idea/testData/slicer/inflow/overrideFun.results.txt index 11a785941c4..7b01acd65eb 100644 --- a/idea/testData/slicer/inflow/overrideFun.results.txt +++ b/idea/testData/slicer/inflow/overrideFun.results.txt @@ -1,4 +1,5 @@ 9 println(p) 8 override fun foo(p: Int) { +2 void foo(int p); 13 foo(1) 18 i.foo(2)