From 97409d3851ab99a4b34c099cc1fd297910253df2 Mon Sep 17 00:00:00 2001 From: Khemraj Rathore Date: Mon, 30 Dec 2024 17:24:26 +0530 Subject: [PATCH] fix - inheritsFrom in kotlin, when generics are used --- .../io/joern/kotlin2cpg/ast/AstCreator.scala | 2 +- .../kotlin2cpg/querying/TypeDeclTests.scala | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/joern-cli/frontends/kotlin2cpg/src/main/scala/io/joern/kotlin2cpg/ast/AstCreator.scala b/joern-cli/frontends/kotlin2cpg/src/main/scala/io/joern/kotlin2cpg/ast/AstCreator.scala index ba8dd372ea56..5b7a4d253268 100644 --- a/joern-cli/frontends/kotlin2cpg/src/main/scala/io/joern/kotlin2cpg/ast/AstCreator.scala +++ b/joern-cli/frontends/kotlin2cpg/src/main/scala/io/joern/kotlin2cpg/ast/AstCreator.scala @@ -538,7 +538,7 @@ class AstCreator(fileWithMeta: KtFileWithMeta, bindingContext: BindingContext, g protected def fullNameByImportPath(typeRef: KtTypeReference, file: KtFile): Option[String] = { if (typeRef == null) { None } else { - val typeRefText = typeRef.getText.stripSuffix("?") + val typeRefText = typeRef.getText.split("<").headOption.getOrElse(typeRef.getText).stripSuffix("?") file.getImportList.getImports.asScala.collectFirst { case directive if directive.getImportedName != null && directive.getImportedName.toString == typeRefText => directive.getImportPath.getPathStr diff --git a/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/querying/TypeDeclTests.scala b/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/querying/TypeDeclTests.scala index 3ee7dc51b82d..6f2c483ad456 100644 --- a/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/querying/TypeDeclTests.scala +++ b/joern-cli/frontends/kotlin2cpg/src/test/scala/io/joern/kotlin2cpg/querying/TypeDeclTests.scala @@ -451,4 +451,21 @@ class TypeDeclTests extends KotlinCode2CpgFixture(withOssDataflow = false) { firstCallOfSecondaryCtor.methodFullName shouldBe "mypkg.QClass.:void()" } } + + "CPG for inheritance having generics" should { + val cpg = code(""" + |import org.apache.flink.streaming.api.functions.sink.RichSinkFunction + | + |class ApiSink(private val apiUrl: String) : RichSinkFunction() { + | private var httpClient: CloseableHttpClient? = null + | + |} + |""".stripMargin) + + "resolve base types" in { + cpg.typeDecl.name("ApiSink").inheritsFromTypeFullName.l shouldBe List( + "org.apache.flink.streaming.api.functions.sink.RichSinkFunction" + ) + } + } }