Skip to content

Commit

Permalink
kotlin2cpg: create nodes for annotation params (#2800)
Browse files Browse the repository at this point in the history
  • Loading branch information
ursachec committed Jun 2, 2023
1 parent cfafc11 commit aa4ece3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,20 @@ trait KtPsiToAst {
.lineNumber(line(entry))
.columnNumber(column(entry))
.fullName(typeFullName)
annotationAst(node, List())

val children =
entry.getValueArguments.asScala.flatMap { varg =>
varg.getArgumentExpression match {
case ste: KtStringTemplateExpression if ste.getEntries.size == 1 =>
val node = NewAnnotationLiteral().code(ste.getText)
Some(Ast(node))
case ce: KtConstantExpression =>
val node = NewAnnotationLiteral().code(ce.getText)
Some(Ast(node))
case _ => None
}
}.toList
annotationAst(node, children)
}

def astsForMethod(ktFn: KtNamedFunction, needsThisParameter: Boolean = false, withVirtualModifier: Boolean = false)(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.joern.kotlin2cpg.querying

import io.joern.kotlin2cpg.testfixtures.KotlinCode2CpgFixture
import io.shiftleft.codepropertygraph.generated.nodes.Annotation
import io.shiftleft.codepropertygraph.generated.nodes.{Annotation, AnnotationLiteral}
import io.shiftleft.semanticcpg.language._

class AnnotationsTests extends KotlinCode2CpgFixture(withOssDataflow = false) {
Expand Down Expand Up @@ -74,6 +74,9 @@ class AnnotationsTests extends KotlinCode2CpgFixture(withOssDataflow = false) {
annotation.code shouldBe "@RequestParam(\"username\")"
annotation.name shouldBe "RequestParam"
annotation.fullName shouldBe "mypkg.RequestParam"

val List(annotationLiteral) = annotation.astChildren.collectAll[AnnotationLiteral].l
annotationLiteral.code shouldBe "\"username\""
}
}
}

0 comments on commit aa4ece3

Please sign in to comment.