Skip to content

Commit

Permalink
fix - inheritsFrom in kotlin, when generics are used
Browse files Browse the repository at this point in the history
  • Loading branch information
khemrajrathore committed Dec 30, 2024
1 parent b300d46 commit 97409d3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,21 @@ class TypeDeclTests extends KotlinCode2CpgFixture(withOssDataflow = false) {
firstCallOfSecondaryCtor.methodFullName shouldBe "mypkg.QClass.<init>:void()"
}
}

"CPG for inheritance having generics" should {
val cpg = code("""
|import org.apache.flink.streaming.api.functions.sink.RichSinkFunction
|
|class ApiSink<T>(private val apiUrl: String) : RichSinkFunction<T>() {
| 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"
)
}
}
}

0 comments on commit 97409d3

Please sign in to comment.