Skip to content

Commit

Permalink
javasrc2cpg: Line Numbers for Annotations (#2302)
Browse files Browse the repository at this point in the history
- Added line number info for annotation nodes
  • Loading branch information
DavidBakerEffendi authored Feb 24, 2023
1 parent b225f40 commit eb3a843
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,7 @@ class AstCreator(filename: String, javaParserAst: CompilationUnit, global: Globa
.code(annotationExpr.toString)
.name(annotationExpr.getName.getIdentifier)
.fullName(expressionReturnTypeFullName(annotationExpr).getOrElse(fallbackType))
.lineNumber(line(annotationExpr))
}

private def astForAnnotationExpr(annotationExpr: AnnotationExpr): Ast = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
annotationNode.code shouldBe "@NormalAnnotation(value = \"classAnnotation\")"
annotationNode.name shouldBe "NormalAnnotation"
annotationNode.fullName shouldBe "some.NormalAnnotation"
annotationNode.lineNumber shouldBe Some(5)
}

"test annotation node parameter assignment child" in {
Expand Down Expand Up @@ -61,6 +62,7 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
annotationNode.code shouldBe "@SingleAnnotation(\"classAnnotation\")"
annotationNode.name shouldBe "SingleAnnotation"
annotationNode.fullName shouldBe "some.SingleAnnotation"
annotationNode.lineNumber shouldBe Some(5)
}

"test annotation node parameter assignment child" in {
Expand Down Expand Up @@ -99,6 +101,7 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
annotationNode.code shouldBe "@MarkerAnnotation()"
annotationNode.name shouldBe "MarkerAnnotation"
annotationNode.fullName shouldBe "some.MarkerAnnotation"
annotationNode.lineNumber shouldBe Some(5)
}

"test annotation node parameter assignment child" in {
Expand All @@ -119,11 +122,11 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
|""".stripMargin)

"test annotation node properties" in {
import scala.jdk.CollectionConverters._
val annotationNode = cpg.method.nameExact(io.joern.x2cpg.Defines.ConstructorMethodName).annotation.head
annotationNode.code shouldBe "@MarkerAnnotation()"
annotationNode.name shouldBe "MarkerAnnotation"
annotationNode.fullName shouldBe "some.MarkerAnnotation"
annotationNode.lineNumber shouldBe Some(5)
}

"test annotation node parameter assignment child" in {
Expand All @@ -146,6 +149,7 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
annotationNode.code shouldBe "@MarkerAnnotation"
annotationNode.name shouldBe "MarkerAnnotation"
annotationNode.fullName shouldBe "some.MarkerAnnotation"
annotationNode.lineNumber shouldBe Some(5)
}
}

Expand All @@ -162,6 +166,7 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
annotationNode.code shouldBe "@MarkerAnnotation"
annotationNode.name shouldBe "MarkerAnnotation"
annotationNode.fullName shouldBe "some.MarkerAnnotation"
annotationNode.lineNumber shouldBe Some(4)
}
}

Expand All @@ -181,6 +186,7 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
annotationNode.code shouldBe "@NormalAnnotation(value = 2)"
annotationNode.name shouldBe "NormalAnnotation"
annotationNode.fullName shouldBe "some.NormalAnnotation"
annotationNode.lineNumber shouldBe Some(5)
}

"test annotation node parameter value" in {
Expand All @@ -207,6 +213,7 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
annotationNode.code shouldBe "@NormalAnnotation(value = { \"aaa\", \"bbb\" })"
annotationNode.name shouldBe "NormalAnnotation"
annotationNode.fullName shouldBe "some.NormalAnnotation"
annotationNode.lineNumber shouldBe Some(5)
}

"test annotation node parameter assignment child" in {
Expand Down Expand Up @@ -255,6 +262,7 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
annotationNode.code shouldBe "@NormalAnnotation(value = @OtherAnnotation)"
annotationNode.name shouldBe "NormalAnnotation"
annotationNode.fullName shouldBe "some.NormalAnnotation"
annotationNode.lineNumber shouldBe Some(6)
}

"test annotation node parameter value" in {
Expand Down Expand Up @@ -286,10 +294,12 @@ class AnnotationTests extends JavaSrcCode2CpgFixture {
wildcard1.name shouldBe "Wildcard1"
wildcard1.fullName shouldBe "b.Wildcard1"
wildcard1.code shouldBe "@Wildcard1"
wildcard1.lineNumber shouldBe Some(6)

wildcard2.name shouldBe "Wildcard2"
wildcard2.fullName shouldBe "b.Wildcard2"
wildcard2.code shouldBe "@Wildcard2"
wildcard2.lineNumber shouldBe Some(7)

case result => fail(s"Expected 3 annotations for Foo but got $result")
}
Expand Down

0 comments on commit eb3a843

Please sign in to comment.